Skip to content

Commit 82baecc

Browse files
agola11hwchase17
andauthored
Add a SQL agent for interacting with SQL Databases and JSON Agent for interacting with large JSON blobs (langchain-ai#1150)
This PR adds * `ZeroShotAgent.as_sql_agent`, which returns an agent for interacting with a sql database. This builds off of `SQLDatabaseChain`. The main advantages are 1) answering general questions about the db, 2) access to a tool for double checking queries, and 3) recovering from errors * `ZeroShotAgent.as_json_agent` which returns an agent for interacting with json blobs. * Several examples in notebooks --------- Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
1 parent 35f1e8f commit 82baecc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+5994
-120
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "85fb2c03-ab88-4c8c-97e3-a7f2954555ab",
6+
"metadata": {},
7+
"source": [
8+
"# JSON Agent\n",
9+
"\n",
10+
"This notebook showcases an agent designed to interact with large JSON/dict objects. This is useful when you want to answer questions about a JSON blob that's too large to fit in the context window of an LLM. The agent is able to iteratively explore the blob to find what it needs to answer the user's question.\n",
11+
"\n",
12+
"In the below example, we are using the OpenAPI spec for the OpenAI API, which you can find [here](https://github.com/openai/openai-openapi/blob/master/openapi.yaml).\n",
13+
"\n",
14+
"We will use the JSON agent to answer some questions about the API spec."
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"id": "893f90fd-f8f6-470a-a76d-1f200ba02e2f",
20+
"metadata": {},
21+
"source": [
22+
"## Initialization"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 1,
28+
"id": "ff988466-c389-4ec6-b6ac-14364a537fd5",
29+
"metadata": {
30+
"tags": []
31+
},
32+
"outputs": [],
33+
"source": [
34+
"import os\n",
35+
"import yaml\n",
36+
"\n",
37+
"from langchain.agents import (\n",
38+
" create_json_agent,\n",
39+
" AgentExecutor\n",
40+
")\n",
41+
"from langchain.agents.agent_toolkits import JsonToolkit\n",
42+
"from langchain.chains import LLMChain\n",
43+
"from langchain.llms.openai import OpenAI\n",
44+
"from langchain.requests import RequestsWrapper\n",
45+
"from langchain.tools.json.tool import JsonSpec"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 2,
51+
"id": "9ecd1ba0-3937-4359-a41e-68605f0596a1",
52+
"metadata": {
53+
"tags": []
54+
},
55+
"outputs": [],
56+
"source": [
57+
"with open(\"openai_openapi.yml\") as f:\n",
58+
" data = yaml.load(f, Loader=yaml.FullLoader)\n",
59+
"json_spec = JsonSpec(dict_=data, max_value_length=4000)\n",
60+
"json_toolkit = JsonToolkit(spec=json_spec)\n",
61+
"\n",
62+
"json_agent_executor = create_json_agent(\n",
63+
" llm=OpenAI(temperature=0),\n",
64+
" toolkit=json_toolkit,\n",
65+
" verbose=True\n",
66+
")"
67+
]
68+
},
69+
{
70+
"cell_type": "markdown",
71+
"id": "05cfcb24-4389-4b8f-ad9e-466e3fca8db0",
72+
"metadata": {},
73+
"source": [
74+
"## Example: getting the required POST parameters for a request"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 5,
80+
"id": "faf13702-50f0-4d1b-b91f-48c750ccfd98",
81+
"metadata": {
82+
"tags": []
83+
},
84+
"outputs": [
85+
{
86+
"name": "stdout",
87+
"output_type": "stream",
88+
"text": [
89+
"\n",
90+
"\n",
91+
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
92+
"\u001b[32;1m\u001b[1;3mAction: json_spec_list_keys\n",
93+
"Action Input: data\u001b[0m\n",
94+
"Observation: \u001b[36;1m\u001b[1;3m['openapi', 'info', 'servers', 'tags', 'paths', 'components', 'x-oaiMeta']\u001b[0m\n",
95+
"Thought:\u001b[32;1m\u001b[1;3m I should look at the paths key to see what endpoints exist\n",
96+
"Action: json_spec_list_keys\n",
97+
"Action Input: data[\"paths\"]\u001b[0m\n",
98+
"Observation: \u001b[36;1m\u001b[1;3m['/engines', '/engines/{engine_id}', '/completions', '/edits', '/images/generations', '/images/edits', '/images/variations', '/embeddings', '/engines/{engine_id}/search', '/files', '/files/{file_id}', '/files/{file_id}/content', '/answers', '/classifications', '/fine-tunes', '/fine-tunes/{fine_tune_id}', '/fine-tunes/{fine_tune_id}/cancel', '/fine-tunes/{fine_tune_id}/events', '/models', '/models/{model}', '/moderations']\u001b[0m\n",
99+
"Thought:\u001b[32;1m\u001b[1;3m I should look at the /completions endpoint to see what parameters are required\n",
100+
"Action: json_spec_list_keys\n",
101+
"Action Input: data[\"paths\"][\"/completions\"]\u001b[0m\n",
102+
"Observation: \u001b[36;1m\u001b[1;3m['post']\u001b[0m\n",
103+
"Thought:\u001b[32;1m\u001b[1;3m I should look at the post key to see what parameters are required\n",
104+
"Action: json_spec_list_keys\n",
105+
"Action Input: data[\"paths\"][\"/completions\"][\"post\"]\u001b[0m\n",
106+
"Observation: \u001b[36;1m\u001b[1;3m['operationId', 'tags', 'summary', 'requestBody', 'responses', 'x-oaiMeta']\u001b[0m\n",
107+
"Thought:\u001b[32;1m\u001b[1;3m I should look at the requestBody key to see what parameters are required\n",
108+
"Action: json_spec_list_keys\n",
109+
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"]\u001b[0m\n",
110+
"Observation: \u001b[36;1m\u001b[1;3m['required', 'content']\u001b[0m\n",
111+
"Thought:\u001b[32;1m\u001b[1;3m I should look at the required key to see what parameters are required\n",
112+
"Action: json_spec_get_value\n",
113+
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"][\"required\"]\u001b[0m\n",
114+
"Observation: \u001b[33;1m\u001b[1;3mTrue\u001b[0m\n",
115+
"Thought:\u001b[32;1m\u001b[1;3m I should look at the content key to see what parameters are required\n",
116+
"Action: json_spec_list_keys\n",
117+
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"][\"content\"]\u001b[0m\n",
118+
"Observation: \u001b[36;1m\u001b[1;3m['application/json']\u001b[0m\n",
119+
"Thought:\u001b[32;1m\u001b[1;3m I should look at the application/json key to see what parameters are required\n",
120+
"Action: json_spec_list_keys\n",
121+
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"]\u001b[0m\n",
122+
"Observation: \u001b[36;1m\u001b[1;3m['schema']\u001b[0m\n",
123+
"Thought:\u001b[32;1m\u001b[1;3m I should look at the schema key to see what parameters are required\n",
124+
"Action: json_spec_list_keys\n",
125+
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"][\"schema\"]\u001b[0m\n",
126+
"Observation: \u001b[36;1m\u001b[1;3m['$ref']\u001b[0m\n",
127+
"Thought:\u001b[32;1m\u001b[1;3m I should look at the $ref key to see what parameters are required\n",
128+
"Action: json_spec_get_value\n",
129+
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"][\"schema\"][\"$ref\"]\u001b[0m\n",
130+
"Observation: \u001b[33;1m\u001b[1;3m#/components/schemas/CreateCompletionRequest\u001b[0m\n",
131+
"Thought:\u001b[32;1m\u001b[1;3m I should look at the CreateCompletionRequest schema to see what parameters are required\n",
132+
"Action: json_spec_list_keys\n",
133+
"Action Input: data[\"components\"][\"schemas\"][\"CreateCompletionRequest\"]\u001b[0m\n",
134+
"Observation: \u001b[36;1m\u001b[1;3m['type', 'properties', 'required']\u001b[0m\n",
135+
"Thought:\u001b[32;1m\u001b[1;3m I should look at the required key to see what parameters are required\n",
136+
"Action: json_spec_get_value\n",
137+
"Action Input: data[\"components\"][\"schemas\"][\"CreateCompletionRequest\"][\"required\"]\u001b[0m\n",
138+
"Observation: \u001b[33;1m\u001b[1;3m['model']\u001b[0m\n",
139+
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
140+
"Final Answer: The required parameters in the request body to the /completions endpoint are 'model'.\u001b[0m\n",
141+
"\n",
142+
"\u001b[1m> Finished chain.\u001b[0m\n"
143+
]
144+
},
145+
{
146+
"data": {
147+
"text/plain": [
148+
"\"The required parameters in the request body to the /completions endpoint are 'model'.\""
149+
]
150+
},
151+
"execution_count": 5,
152+
"metadata": {},
153+
"output_type": "execute_result"
154+
}
155+
],
156+
"source": [
157+
"json_agent_executor.run(\"What are the required parameters in the request body to the /completions endpoint?\")"
158+
]
159+
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": null,
163+
"id": "ba9c9d30",
164+
"metadata": {},
165+
"outputs": [],
166+
"source": []
167+
}
168+
],
169+
"metadata": {
170+
"kernelspec": {
171+
"display_name": "Python 3 (ipykernel)",
172+
"language": "python",
173+
"name": "python3"
174+
},
175+
"language_info": {
176+
"codemirror_mode": {
177+
"name": "ipython",
178+
"version": 3
179+
},
180+
"file_extension": ".py",
181+
"mimetype": "text/x-python",
182+
"name": "python",
183+
"nbconvert_exporter": "python",
184+
"pygments_lexer": "ipython3",
185+
"version": "3.10.9"
186+
}
187+
},
188+
"nbformat": 4,
189+
"nbformat_minor": 5
190+
}

0 commit comments

Comments
 (0)