Skip to content

Commit 7e7d470

Browse files
Merge pull request #162 from cloudnautique/main
add python example
2 parents 9e7751f + 1d89f46 commit 7e7d470

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

docs/README-USECASES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ Here is a GPTScript that performs sentiment analysis on the input text: [Social
8282

8383
Here is a GPTScript that reads the content of a CSV file and make query using natural language: [csv-reader.gpt](https://github.com/gptscript-ai/csv-reader/blob/main/examples/csv-reader.gpt)
8484

85+
### JSON Files
86+
87+
Here is a GPTScript that uses the Python module to query JSON data in a jupyter notebook: [json-notebook](../examples/json-notebook)
88+
8589
### Understanding Code
8690

8791
Here is a GPTScript that summarizes the code stored under the current directory: [`describe-code.gpt`](../examples/describe-code.gpt)

examples/json-notebook/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# JSON Query tool and Jupyter Notebook
2+
3+
This is a simple example of how to use the JSON Query tool in Jupyter Notebook, along with the gptscript python module.
4+
5+
## Prerequisites
6+
7+
You will need to set the OPENAI_API_KEY environment variable with your OpenAI key in the shell you start the notebook from.
8+
9+
## Running the notebook
10+
11+
Open the notebook, it will install both gptscript when possible and download a dataset and query it using the json-query tool.
12+
13+
## Example
14+
15+
```bash
16+
# Create a Python venv
17+
python3 -m venv venv
18+
. ./venv/bin/activate
19+
pip install jupyter ipykernel
20+
python -m ipykernel install --user --name=venv
21+
jupyter notebook
22+
```
23+
24+
Once the notebook is open, make sure you are using the venv kernel and run the cells.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "98d5cd09-dfe4-4bbe-b1ba-344a535fa763",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"pip install gptscript"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "144cb0f4-7936-4701-9246-e948c09f94c5",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"from gptscript.command import stream_exec\n",
21+
"from gptscript.tool import Tool\n",
22+
"\n",
23+
"t = Tool(tools=[\"github.com/gptscript-ai/json-query\",\"sys.download\",\"sys.read\"],\n",
24+
" instructions=\"\"\"\n",
25+
"download the contents from this url https://data.transportation.gov/api/views/anj8-k6f5/rows.json?accessType=DOWNLOAD and save to file ai-usecases.json\n",
26+
"Wait until the file is downloaded before proceeding.\n",
27+
"Using the downloaded file, determine which agencies have AI initiatives and list a summary of each.\n",
28+
"\"\"\",\n",
29+
" )\n",
30+
"\n",
31+
"results, err, wait = stream_exec(t)\n",
32+
"for line in err:\n",
33+
" print(line)\n",
34+
"print(results.read())"
35+
]
36+
}
37+
],
38+
"metadata": {
39+
"kernelspec": {
40+
"display_name": "venv",
41+
"language": "python",
42+
"name": "venv"
43+
},
44+
"language_info": {
45+
"codemirror_mode": {
46+
"name": "ipython",
47+
"version": 3
48+
},
49+
"file_extension": ".py",
50+
"mimetype": "text/x-python",
51+
"name": "python",
52+
"nbconvert_exporter": "python",
53+
"pygments_lexer": "ipython3",
54+
"version": "3.12.2"
55+
}
56+
},
57+
"nbformat": 4,
58+
"nbformat_minor": 5
59+
}

0 commit comments

Comments
 (0)