mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-12 18:47:21 +00:00
Added missed provider pages. Added descriptions and links. Fixed inconsistency in text formatting. Co-authored-by: Erick Friis <erick@langchain.dev>
250 lines
10 KiB
Plaintext
250 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "85fb2c03-ab88-4c8c-97e3-a7f2954555ab",
|
|
"metadata": {},
|
|
"source": [
|
|
"# JSON Toolkit\n",
|
|
"\n",
|
|
"This notebook showcases an agent interacting with large `JSON/dict` objects. \n",
|
|
"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",
|
|
"\n",
|
|
"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",
|
|
"\n",
|
|
"We will use the JSON agent to answer some questions about the API spec."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "dc11cb15",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install -qU langchain-community"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "893f90fd-f8f6-470a-a76d-1f200ba02e2f",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Initialization"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "ff988466-c389-4ec6-b6ac-14364a537fd5",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-10-02T17:52:40.107644Z",
|
|
"iopub.status.busy": "2024-10-02T17:52:40.107485Z",
|
|
"iopub.status.idle": "2024-10-02T17:52:40.110169Z",
|
|
"shell.execute_reply": "2024-10-02T17:52:40.109841Z",
|
|
"shell.execute_reply.started": "2024-10-02T17:52:40.107633Z"
|
|
},
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import yaml\n",
|
|
"from langchain_community.agent_toolkits import JsonToolkit, create_json_agent\n",
|
|
"from langchain_community.tools.json.tool import JsonSpec\n",
|
|
"from langchain_openai import OpenAI"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "9ecd1ba0-3937-4359-a41e-68605f0596a1",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-10-02T17:59:54.134295Z",
|
|
"iopub.status.busy": "2024-10-02T17:59:54.134138Z",
|
|
"iopub.status.idle": "2024-10-02T17:59:54.137250Z",
|
|
"shell.execute_reply": "2024-10-02T17:59:54.136636Z",
|
|
"shell.execute_reply.started": "2024-10-02T17:59:54.134283Z"
|
|
},
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"with open(\"openai_openapi.yml\") as f:\n",
|
|
" data = yaml.load(f, Loader=yaml.FullLoader)\n",
|
|
"json_spec = JsonSpec(dict_={}, max_value_length=4000)\n",
|
|
"json_toolkit = JsonToolkit(spec=json_spec)\n",
|
|
"\n",
|
|
"json_agent_executor = create_json_agent(\n",
|
|
" llm=OpenAI(temperature=0), toolkit=json_toolkit, verbose=True\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "910eccbc-9d42-49b6-a4ca-1fbc418fcee7",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Individual tools\n",
|
|
"\n",
|
|
"Let's see what individual tools are inside the Jira toolkit."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "b16a3ee5-ca16-452e-993f-c27228b945ac",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-10-02T18:00:30.527665Z",
|
|
"iopub.status.busy": "2024-10-02T18:00:30.527053Z",
|
|
"iopub.status.idle": "2024-10-02T18:00:30.538483Z",
|
|
"shell.execute_reply": "2024-10-02T18:00:30.537672Z",
|
|
"shell.execute_reply.started": "2024-10-02T18:00:30.527626Z"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[('json_spec_list_keys',\n",
|
|
" '\\n Can be used to list all keys at a given path. \\n Before calling this you should be SURE that the path to this exists.\\n The input is a text representation of the path to the dict in Python syntax (e.g. data[\"key1\"][0][\"key2\"]).\\n '),\n",
|
|
" ('json_spec_get_value',\n",
|
|
" '\\n Can be used to see value in string format at a given path.\\n Before calling this you should be SURE that the path to this exists.\\n The input is a text representation of the path to the dict in Python syntax (e.g. data[\"key1\"][0][\"key2\"]).\\n ')]"
|
|
]
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"[(el.name, el.description) for el in json_toolkit.get_tools()]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "05cfcb24-4389-4b8f-ad9e-466e3fca8db0",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Example: getting the required POST parameters for a request"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "faf13702-50f0-4d1b-b91f-48c750ccfd98",
|
|
"metadata": {
|
|
"tags": []
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
|
|
"\u001b[32;1m\u001b[1;3mAction: json_spec_list_keys\n",
|
|
"Action Input: data\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3m['openapi', 'info', 'servers', 'tags', 'paths', 'components', 'x-oaiMeta']\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I should look at the paths key to see what endpoints exist\n",
|
|
"Action: json_spec_list_keys\n",
|
|
"Action Input: data[\"paths\"]\u001b[0m\n",
|
|
"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",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I should look at the /completions endpoint to see what parameters are required\n",
|
|
"Action: json_spec_list_keys\n",
|
|
"Action Input: data[\"paths\"][\"/completions\"]\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3m['post']\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I should look at the post key to see what parameters are required\n",
|
|
"Action: json_spec_list_keys\n",
|
|
"Action Input: data[\"paths\"][\"/completions\"][\"post\"]\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3m['operationId', 'tags', 'summary', 'requestBody', 'responses', 'x-oaiMeta']\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I should look at the requestBody key to see what parameters are required\n",
|
|
"Action: json_spec_list_keys\n",
|
|
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"]\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3m['required', 'content']\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I should look at the required key to see what parameters are required\n",
|
|
"Action: json_spec_get_value\n",
|
|
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"][\"required\"]\u001b[0m\n",
|
|
"Observation: \u001b[33;1m\u001b[1;3mTrue\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I should look at the content key to see what parameters are required\n",
|
|
"Action: json_spec_list_keys\n",
|
|
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"][\"content\"]\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3m['application/json']\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I should look at the application/json key to see what parameters are required\n",
|
|
"Action: json_spec_list_keys\n",
|
|
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"]\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3m['schema']\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I should look at the schema key to see what parameters are required\n",
|
|
"Action: json_spec_list_keys\n",
|
|
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"][\"schema\"]\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3m['$ref']\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I should look at the $ref key to see what parameters are required\n",
|
|
"Action: json_spec_get_value\n",
|
|
"Action Input: data[\"paths\"][\"/completions\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"][\"schema\"][\"$ref\"]\u001b[0m\n",
|
|
"Observation: \u001b[33;1m\u001b[1;3m#/components/schemas/CreateCompletionRequest\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I should look at the CreateCompletionRequest schema to see what parameters are required\n",
|
|
"Action: json_spec_list_keys\n",
|
|
"Action Input: data[\"components\"][\"schemas\"][\"CreateCompletionRequest\"]\u001b[0m\n",
|
|
"Observation: \u001b[36;1m\u001b[1;3m['type', 'properties', 'required']\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I should look at the required key to see what parameters are required\n",
|
|
"Action: json_spec_get_value\n",
|
|
"Action Input: data[\"components\"][\"schemas\"][\"CreateCompletionRequest\"][\"required\"]\u001b[0m\n",
|
|
"Observation: \u001b[33;1m\u001b[1;3m['model']\u001b[0m\n",
|
|
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer\n",
|
|
"Final Answer: The required parameters in the request body to the /completions endpoint are 'model'.\u001b[0m\n",
|
|
"\n",
|
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"\"The required parameters in the request body to the /completions endpoint are 'model'.\""
|
|
]
|
|
},
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"json_agent_executor.run(\n",
|
|
" \"What are the required parameters in the request body to the /completions endpoint?\"\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ba9c9d30",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"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.10.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|