From 1d89f46e85645aab57f6ef7692dc7380bd29bb6e Mon Sep 17 00:00:00 2001 From: Bill Maxwell Date: Thu, 14 Mar 2024 16:10:50 -0700 Subject: [PATCH] add python example Shows off json-query tool by using the python gptscript module. Signed-off-by: Bill Maxwell --- docs/README-USECASES.md | 4 ++ examples/json-notebook/README.md | 24 ++++++++++ examples/json-notebook/json-query.ipynb | 59 +++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 examples/json-notebook/README.md create mode 100644 examples/json-notebook/json-query.ipynb diff --git a/docs/README-USECASES.md b/docs/README-USECASES.md index 2ca7e158..81d312f3 100644 --- a/docs/README-USECASES.md +++ b/docs/README-USECASES.md @@ -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) diff --git a/examples/json-notebook/README.md b/examples/json-notebook/README.md new file mode 100644 index 00000000..3897cde3 --- /dev/null +++ b/examples/json-notebook/README.md @@ -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. diff --git a/examples/json-notebook/json-query.ipynb b/examples/json-notebook/json-query.ipynb new file mode 100644 index 00000000..2de271b3 --- /dev/null +++ b/examples/json-notebook/json-query.ipynb @@ -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 +}