RESTful APIs (Application Programming Interfaces) are an integral part of modern web services, and yet as the popularity of large language models (LLMs) increases, we have not seen enough APIs being made accessible to users at the scale that LLMs can enable.
Imagine verbally telling your computer, “Get me weather data for Seattle” and have it magically retrieve the correct and latest information from a trusted API. With LangChain, a Requests Toolkit, and a ReAct agent, talking to your API with natural language is easier than ever.
This blog post will walk you through the process of setting up and utilizing the Requests Toolkit with LangChain in Python. The key steps of the process include acquiring OpenAPI specifications for your selected API, selecting tools, and creating and invoking a LangGraph-based ReAct agent.
Pre-Requisites
To get started you’ll need to install LangChain and LangGraph. While installing LangChain you will also end up installing the Requests Toolkit which comes bundled with the community-developed set of LangChain toolkits.
Before you can use LangChain to interact with an API, you need to obtain the OpenAPI specification for your API.
This spec provides details about the available endpoints, request methods, and data formats. Most modern APIs use OpenAPI (formerly Swagger) specifications, which are often available in JSON or YAML format. For this example, we will just be using the JSON Placeholder API.
It is recommended you familiarize yourself a little with the API yourself by sending a few sample queries to the API using Postman or otherwise.
Explore all about LangChain and its use cases
Setup Tools
To get started we’ll first import the relevant LangChain classes.
Then you can select the HTTP tools from the requests Toolkit. These tools include RequestsGetTool, RequestsPostTool, RequestsPatchTool, and so on. One for each of the 5 HTTP requests that you can make to a RESTful API.
Since some of these requests can lead to dangerous irreversible changes, like the deletion of critical data, we have had to actively pass the allow_dangerous_requests parameter to enable these. The requests wrapper parameters include any authentication headers or otherwise that the API may require.
You can find more details about necessary headers in your API documentation. For the JSON Placeholder API, we’re good to go without any authentication headers.
Just to stay safe we’ll also only choose to use the POST and GET tools, which we can select by simply choosing the first 2 elements of the tools list.
Import API Specifications
Next up, we’ll get the file for our API specifications and import them into the JsonSpec format from the Langchain community.
While the JSON Placeholder API spec is small, certain API specs can be massive, and you may benefit from adjusting the max_value_length in your code accordingly. Find the JSON Placeholder spec here.
Setup ReAct Agent
A ReAct agent in LangChain is a specialized tool that combines reasoning and action. It uses a combination of a large language model’s ability to “reason” through natural language with the capability to execute actions based on that reasoning. And when it gets the results of its actions it can react to them (pun intended) and choose the next appropriate action.
Learn more about AI agent workflows in this LangGraph tutorial
We’ll get started with a simple ReAct agent pre-provided within LangGraph.
The create_react_agent prebuilt function generates a LangGraph agent which prompted by the user query starts interactions with the AI agent and keeps on looping between tools as long as every AI agent call generates a tool request (i.e. requires a tool to be used).
Typically, the AI agent will end the process with the responses from tools (API requests in our case) containing the response to the user’s query.
Invoking your ReAct Agent
Once your ReAct agent is set up, you can invoke it to perform API requests. This is a simple step.
events is a Python generator object which you can invoke step by step in a for-loop, as it executes the next step in its process, every time the loop completes one iteration.
Read more about the top 6 Python libraries for data science
Ideally, this should give out an output similar to this:
Human Message
Fetch the titles of the top 10 posts.
AI Message
Tool Calls: requests_get (call_ym8FFptxrPgASvyqWBrnbIUZ) Call ID: call_ym8FFptxrPgASvyqWBrnbIUZ Args: url: https://jsonplaceholder.typicode.com/posts
Tool Message
Name: requests_get [ … request response … ]
AI Message
Here are the titles of the top 10 posts:
- **sunt aut facere repellat provident occaecati excepturi optio reprehenderit**
- **qui est esse**
- **ea molestias quasi exercitationem repellat qui ipsa sit aut**
- **eum et est occaecati**
- **nesciunt quas odio**
- **dolorem eum magni eos aperiam quia**
- **magnam facilis autem**
- **dolorem dolore est ipsam**
- **nesciunt iure omnis dolorem tempora et accusantium**
- **optio molestias id quia eum**
Navigate through the working of agents in LangChain
You can also receive the response more simply to be passed onto another API or interface by storing the final result from the LLM call into a single variable this way:
Conclusion
Using LangChain’s Requests toolkit to execute API requests with natural language opens up new possibilities for interacting with data. By understanding your API spec, carefully selecting tools, and leveraging a ReAct agent, you can streamline how you interact with APIs, making data access and manipulation more intuitive and efficient.
I have managed to test this functionality with a variety of other APIs and approaches. While other approaches like OpenAPI toolkit, Gorilla, RestGPT, and API chains exist, the Requests Toolkit leveraging a LangGraph-based ReAct agent seems to be the most effective, and reliable way to integrate natural language processing with API interactions.
In my usage, it has worked for various APIs including but not limited to APIs from Slack, ClinicalTrials.gov, TMDB, and OpenAI. Feel free to initiate discussions below and share your experiences with other APIs.
Written by: Zain Ahmed Usmani