Integrations
Integrate LLMs with REST APIs using OpenAPI Specs
Leverage OpenAPI specs to enable LLMs to call REST APIs, automating complex tasks and data interactions through intelligent function invocation.
Without it
Piece it together by hand, every time.
With it
Enable Large Language Models (LLMs) to intelligently interact with RESTful APIs by converting OpenAPI specifications into executable function calls. This allows for automated data retrieval, manipulation, and complex task execution through natural language instructions.
What you get
- Convert OpenAPI specifications into LLM-understandable function definitions.
- Utilize LLM's function-calling capabilities to generate API call parameters.
- Chain function calls to perform multi-step operations based on user input.
- Parse API responses to extract relevant information or trigger subsequent actions.
Use this prompt chain
Function-calling with an OpenAPI specification
Much of the internet is powered by RESTful APIs. Giving GPT the ability to call them opens up a world of possibilities. This notebook demonstrates how GPTs can be used to intelligently call APIs. It leverages OpenAPI specifications and chained function calls.
The OpenAPI Specification (OAS) is a universally accepted standard for describing the details of RESTful APIs in a format that machines can read and interpret. It enables both humans and computers to understand the capabilities of a service, and it can be leveraged to show GPT how to call APIs.
This notebook is divided into two main sections:
- How to convert a sample OpenAPI specification into a list of function definitions for the chat completions API.
- How to use the chat completions API to intelligently invoke these functions based on user instructions.
We recommend familiariazing yourself with function-calling before proceding.
How to convert an OpenAPI specification into function definitions
The example OpenAPI spec we use here was created using gpt-4. We will transform this sample spec into a set of function definitions that can be supplied to the chat completion API. The model, based on the provided user instructions, generates a JSON object containing the necessary arguments to call these functions.
Before we proceed, let's inspect this generated spec. OpenAPI specs include details about the API's endpoints, the operations they support, the parameters they accept, the requests they can handle, and the responses they return. The spec is defined in JSON format.
The endpoints in the spec include operations for:
- Listing all events
- Creating a new event
- Retrieving an event by ID
- Deleting an event by ID
- Updating an event name by ID
Each operation in the spec has an operationId, which we will use as the function name when we parse the spec into function specifications. The spec also includes schemas that define the data types and structures of the parameters for each operation.
You can see the schema here:
Now that we have a good understanding of the OpenAPI spec, we can proceed to parse it into function specifications.
We can write a simple openapi_to_functions function to generate a list of definitions, where each function is represented as a dictionary containing the following keys:
name: This corresponds to the operation identifier of the API endpoint as defined in the OpenAPI specification.description: This is a brief description or summary of the function, providing an overview of what the function does.parameters: This is a schema that defines the expected input parameters for the function. It provides information about the type of each parameter, whether it is required or optional, and other related details.
For each of the endpoints defined in the schema, we need to do the following:
Resolve JSON references: In an OpenAPI specification, it's common to use JSON references (also known as $ref) to avoid duplication. These references point to definitions that are used in multiple places. For example, if multiple API endpoints return the same object structure, that structure can be defined once and then referenced wherever it's needed. We need to resolve and replace these references with the content they point to.
Extract a name for the functions: We will simply use the operationId as the function name. Alternatively, we could use the endpoint path and operation as the function name.
Extract a description and parameters: We will iterate through the
description,summary,requestBodyandparametersfields to populate the function's description and parameters.
Here's the implementation:
How to call these functions with GPT
Now that we have these function definitions, we can leverage GPT to call them intelligently based on user inputs.
It's important to note that the chat completions API does not execute the function; instead, it generates the JSON that you can use to call the function in your own code.
For more information on function-calling, refer to our dedicated function-calling guide.
Conclusion
We have demonstrated how to convert OpenAPI specs into function specifications that can be given to GPT for it to intelligently call them, and shown how these can be chained together to perform complex operations.
Possible extensions of this system could include handling more complex user instructions that require conditional logic or looping, integrating with real APIs to perform actual operations, and improving error handling and validation to ensure the instructions are feasible and the function calls are successful.