{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "85fb2c03-ab88-4c8c-97e3-a7f2954555ab",
   "metadata": {},
   "source": [
    "# JSON Agent\n",
    "\n",
    "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",
    "\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": "markdown",
   "id": "893f90fd-f8f6-470a-a76d-1f200ba02e2f",
   "metadata": {},
   "source": [
    "## Initialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ff988466-c389-4ec6-b6ac-14364a537fd5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import yaml\n",
    "\n",
    "from langchain.agents import (\n",
    "    create_json_agent,\n",
    "    AgentExecutor\n",
    ")\n",
    "from langchain.agents.agent_toolkits import JsonToolkit\n",
    "from langchain.chains import LLMChain\n",
    "from langchain.llms.openai import OpenAI\n",
    "from langchain.requests import RequestsWrapper\n",
    "from langchain.tools.json.tool import JsonSpec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9ecd1ba0-3937-4359-a41e-68605f0596a1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "with open(\"openai_openapi.yml\") as f:\n",
    "    data = yaml.load(f, Loader=yaml.FullLoader)\n",
    "json_spec = JsonSpec(dict_=data, max_value_length=4000)\n",
    "json_toolkit = JsonToolkit(spec=json_spec)\n",
    "\n",
    "json_agent_executor = create_json_agent(\n",
    "    llm=OpenAI(temperature=0),\n",
    "    toolkit=json_toolkit,\n",
    "    verbose=True\n",
    ")"
   ]
  },
  {
   "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(\"What are the required parameters in the request body to the /completions endpoint?\")"
   ]
  },
  {
   "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
