Agent API Calls
Motivation
Agents can play a crucial role in different enterprise scenarios. One such example is creating a request to fetch results from an existing enterprise API with specific requirements. For example, given a user query:
Retrieve id 7410e652-639d-402e-984e-8fd7025f0aac 8bb21b93-2ddf-4a63-af63-ddb6b1be49a1, ref GLPNT0005GUJOGGE GLSBR000BGASOBRE, nmg 0000234GLCHL0200ARTINDIUS'
The expected API request is:
[{"uuid":["7410e652-639d-402e-984e-8fd7025f0aac","8bb21b93-2ddf-4a63-af63-ddb6b1be49a1"],"page":0,"size":10}, {"objref":["GLPNT0005GUJOGGE","GLSBR000BGASOBRE"],"page":0,"size":10},{"nmgs":["0000234GLCHL0200ARTINDIUS"],"page":0,"size":10}]
This kind of task poses a major challenge, namely, the model must be very precise at identifying the codes in the query, based on their alphanumeric patterns, and matching these codes with the right parameter - for example, the code GLSBR000BGASOBRE
needs to map to
objref
. The task is even more challenging when multiple APIs are available, and the model has to deal with a plethora of parameters.
Solution
In this notebook, we propose a simple but effective solution to the challenge defined above: We create a Langchain ReAct Agent that has access to a deterministic tool that extracts the alphanumeric patterns in the query, and returns a dictionary in which the keys are the parameters, and the values the extracted patterns. The output of the tool is then used to generate the final request.
Using such a tool is just one of the possibilities that the Agent has to generate the query. As we will see below, when a more semantic understanding of the query is required, we can ignore the tool and leverage the linguistic capabilities of the LLM powering the Agent to generate the final output.
With this approach, we bring together the best of two worlds: the ability of LLMs to use tools and generate outputs and the reliability and efficiency of deterministic functions.
Step 1: Setup
Step 2: Define the Tool and the Agent
Here we create a tool which implements the deterministic function to extract alphanumeric strings from the user’s query and match them to the right parameter.
Step 3: Run the Agent
Let’s now test the Agent we just defined!
In the reasoning chain above, we can see that the Agent uses the tool we provided it to extract the strings in the query. The output of the tool is then used to generate the request.
As mentioned above, the Agent can use the tool when specific alphanumeric patterns have to be extracted from the query; however, it can also generate the output based on its semantic understanding of the query. For example:
The Agent runs the tool to check if any target string was in the query, then it generated the request body based on its understanding.
Finally, the two paths to generation - deterministic and semantic - can be applied in parallel by the Agent, as shown below:
Conclusions
In this notebook we showed how Agents can be used to solve a real-world use case, in which the goal is to create API requests based on the user’s query. We did it by providing the Agent with a deterministic tool to extract relevant alphanumeric strings in the query, and matching them to the right parameter name. In parallel, the Agent can leverage the semantic understanding of the query provided by the LLM powering it.