Skip to content

add python example #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/README-USECASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ Here is a GPTScript that performs sentiment analysis on the input text: [Social

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)

### JSON Files

Here is a GPTScript that uses the Python module to query JSON data in a jupyter notebook: [json-notebook](../examples/json-notebook)

### Understanding Code

Here is a GPTScript that summarizes the code stored under the current directory: [`describe-code.gpt`](../examples/describe-code.gpt)
Expand Down
24 changes: 24 additions & 0 deletions examples/json-notebook/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# JSON Query tool and Jupyter Notebook

This is a simple example of how to use the JSON Query tool in Jupyter Notebook, along with the gptscript python module.

## Prerequisites

You will need to set the OPENAI_API_KEY environment variable with your OpenAI key in the shell you start the notebook from.

## Running the notebook

Open the notebook, it will install both gptscript when possible and download a dataset and query it using the json-query tool.

## Example

```bash
# Create a Python venv
python3 -m venv venv
. ./venv/bin/activate
pip install jupyter ipykernel
python -m ipykernel install --user --name=venv
jupyter notebook
```

Once the notebook is open, make sure you are using the venv kernel and run the cells.
59 changes: 59 additions & 0 deletions examples/json-notebook/json-query.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "98d5cd09-dfe4-4bbe-b1ba-344a535fa763",
"metadata": {},
"outputs": [],
"source": [
"pip install gptscript"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "144cb0f4-7936-4701-9246-e948c09f94c5",
"metadata": {},
"outputs": [],
"source": [
"from gptscript.command import stream_exec\n",
"from gptscript.tool import Tool\n",
"\n",
"t = Tool(tools=[\"github.com/gptscript-ai/json-query\",\"sys.download\",\"sys.read\"],\n",
" instructions=\"\"\"\n",
"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",
"Wait until the file is downloaded before proceeding.\n",
"Using the downloaded file, determine which agencies have AI initiatives and list a summary of each.\n",
"\"\"\",\n",
" )\n",
"\n",
"results, err, wait = stream_exec(t)\n",
"for line in err:\n",
" print(line)\n",
"print(results.read())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "venv"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}