Interacting with API
Almost all of the web applications relies on RESTful APIs. Enabling LLM to interact with them expand its practical utility.
This tutorial showcases how LLM can be used to make API calls through tool calling.
Prerequisite - Sample Event Management Server
We are going to use a simple Event Management server, and showcase how to interact with it.
Request Tools
There are 4 Request tools that can be used. This allows LLM to call the GET, POST, PUT, DELETE tools when necessary.
Step 1: Add the Start Node
The Start node is the entry point of your flow
Step 2: Add the Agent Node
Next, add an Agent node. In this setup, the agent is configured with four main tools: GET, POST, PUT, and DELETE. Each tool is set up to perform a specific type of API request.
Tool 1: GET (Retrieve Events)
Purpose: Fetch a list of events or a specific event from the API.
Configuration Inputs:
URL:
http://localhost:5566/events
Name:
get_events
Description: Describe when to use this tool. For example:
Use this when you need to get events
Headers: (Optional) Add any required HTTP headers.
Query Params Schema: A JSON schema of the API which allows LLM to know about the URL structure, which path and query parameters to generate. For instance:
Tool 2: POST (Create Event)
Purpose: Create a new event in the system.
Configuration Inputs:
URL:
http://localhost:5566/events
Name:
create_event
Description:
Use this when you want to create a new event.
Headers: (Optional) Add any required HTTP headers.
Body: Hard-coded body object that will override the body generated by LLM
Body Schema: A JSON schema of the API request body which allows LLM to know how to automatically generate the correct JSON body. For instance:
Tool 3: PUT (Update Event)
Purpose: Update an existing event’s details.
Configuration Inputs:
URL:
http://localhost:5566/events
Name:
update_event
Description:
Use this when you want to update an event.
Headers: (Optional) Add any required HTTP headers.
Body: Hard-coded body object that will override the body generated by LLM
Body Schema: A JSON schema of the API request body which allows LLM to know how to automatically generate the correct JSON body. For instance:
Tool 4: DELETE (Delete Event)
Purpose: Remove an event from the system.
Configuration Inputs:
URL:
http://localhost:5566/events
Name:
delete_event
Description:
Use this when you need to delete an event.
Headers: (Optional) Add any required HTTP headers.
Query Params Schema: A JSON schema of the API which allows LLM to know about the URL structure, which path and query parameters to generate. For instance:
How the Agent Uses These Tools
The agent can dynamically select which tool to use based on the user’s request or the flow’s logic.
Each tool is mapped to a specific HTTP method and endpoint, with clearly defined input schemas.
The agent leverages the LLM to interpret user input, fill in the required parameters, and make the appropriate API call.
Certainly! Here are some Example Interactions for your flow, including sample user queries and the expected behavior for each, mapped to the corresponding tool (GET, POST, PUT, DELETE):
Example Interactions
1. Retrieve Events (GET)
Sample Query:
"Show me all upcoming events."
Expected Behavior:
The agent selects the GET tool.
It sends a GET request to
http://localhost:5566/events
.The agent returns a list of all events to the user.
Sample Query:
"Get the details for event with ID 12345."
Expected Behavior:
The agent selects the GET tool.
It sends a GET request to
http://localhost:5566/events/12345
.The agent returns the details for the event with ID
12345
.
2. Create a New Event (POST)
Sample Query:
"Create a new event called 'AI Conference' on 2024-07-15 at Tech Hall."
Expected Behavior:
The agent selects the POST tool.
It sends a POST request to
http://localhost:5566/events
with the body:The agent confirms the event was created and may return the new event’s details.
3. Update an Event (PUT)
Sample Query:
"Change the location of the event 'AI Conference' on 2024-07-15 to 'Main Auditorium'."
Expected Behavior:
The agent selects the PUT tool.
It sends a PUT request to
http://localhost:5566/events
with the updated event details:The agent confirms the event was updated.
4. Delete an Event (DELETE)
Sample Query:
"Delete the event with ID 12345."
Expected Behavior:
The agent selects the DELETE tool.
It sends a DELETE request to
http://localhost:5566/events/12345
.The agent confirms the event was deleted.
Complete Flow
OpenAPI Toolkit
Using the Event Management API, we can generate an OpenAPI YAML file:
Step 1: Add the Start Node
Step 2: Add the Agent Node
Next, add an Agent node. In this setup, the agent is configured with only 1 tool - OpenAPI Toolkit
Tool: OpenAPI Toolkit
Purpose: Fetch the list of APIs from YAML file and turn every APIs into list of tools
Configuration Inputs:
YAML File: The OpenAPI YAML file
Return Direct: Whether to return the response from API directly
Headers: (Optional) Add any required HTTP headers.
Remove null parameters: Remove all keys with null values from the parsed arguments
Custom Code: Customize how response is being returned
Example Interactions:
We can use the same sample queries from previous example to test it out:
Calling API Sequentially
Best Practices
Interacting with APIs is typically used when you want an agent to fetch the most up-to-date information. For example, an agent might retrieve your calendar availability, project status, or other real-time data.
It is often helpful to explicitly include the current time in the system prompt. Flowise provides a variable called
{{current_date_time}}
, which retrieves the current date and time. This allows the LLM to be aware of the present moment, so if you ask about your availability for today, the model can reference the correct date. Otherwise, it may rely on its last training cutoff date, which would return outdated information. For example:
Last updated