.ipynb
.pdf
OpenAPI Chain
 Contents 
Load the spec
Select the Operation
Construct the chain
Return raw response
Example POST message
OpenAPI Chain#
This notebook shows an example of using an OpenAPI chain to call an endpoint in natural language, and get back a response in natural language.
from langchain.tools import OpenAPISpec, APIOperation
from langchain.chains import OpenAPIEndpointChain
from langchain.requests import Requests
from langchain.llms import OpenAI
Load the spec#
Load a wrapper of the spec (so we can work with it more easily). You can load from a url or from a local file.
spec = OpenAPISpec.from_url("https://www.klarna.com/us/shopping/public/openai/v0/api-docs/")
Attempting to load an OpenAPI 3.0.1 spec.  This may result in degraded performance. Convert your OpenAPI spec to 3.1.* spec for better support.
# Alternative loading from file
# spec = OpenAPISpec.from_file("openai_openapi.yaml")
Select the Operation#
In order to provide a focused on modular chain, we create a chain specifically only for one of the endpoints. Here we get an API operation from a specified endpoint and method.
operation = APIOperation.from_openapi_spec(spec, '/public/openai/v0/products', "get")
Construct the chain#
We can now construct a chain to interact with it. In order to construct such a chain, we will pass in:
The operation endpoint
A requests wrapper (can be used to handle authentication, etc)
The LLM to use to interact with it
llm = OpenAI() # Load a Language Model
chain = OpenAPIEndpointChain.from_api_operation(
    operation, 
    llm, 
    requests=Requests(), 
    verbose=True,
rtdocs_stable/api.python.langchain.com/en/stable/modules/chains/examples/openapi.html