.ipynb
.pdf
OpenAPI Agent
 Contents 
Initialization
Example: agent capable of analyzing OpenAPI spec and making requests
OpenAPI Agent#
This notebook showcases an agent designed to interact with an OpenAPI spec and make a correct API request based on the information it has gathered from the spec.
In the below example, we are using the OpenAPI spec for the OpenAI API, which you can find here.
Initialization#
import os
import yaml
from langchain.agents import create_openapi_agent
from langchain.agents.agent_toolkits import OpenAPIToolkit
from langchain.llms.openai import OpenAI
from langchain.requests import RequestsWrapper
from langchain.tools.json.tool import JsonSpec
with open("openai_openapi.yml") as f:
    data = yaml.load(f, Loader=yaml.FullLoader)
json_spec=JsonSpec(dict_=data, max_value_length=4000)
headers = {
    "Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}"
}
requests_wrapper=RequestsWrapper(headers=headers)
openapi_toolkit = OpenAPIToolkit.from_llm(OpenAI(temperature=0), json_spec, requests_wrapper, verbose=True)
openapi_agent_executor = create_openapi_agent(
    llm=OpenAI(temperature=0),
    toolkit=openapi_toolkit,
    verbose=True
)
Example: agent capable of analyzing OpenAPI spec and making requests#
openapi_agent_executor.run("Make a post request to openai /completions. The prompt should be 'tell me a joke.'")
> Entering new AgentExecutor chain...
Action: json_explorer
Action Input: What is the base url for the API?
> Entering new AgentExecutor chain...
Action: json_spec_list_keys
Action Input: data