A client library for invoking APIs based on provided OpenAPI specifications
Project description
OpenAPI Service Client
OpenAPI Service Client is a Python library that enables effortless integration between Large Language Models (LLMs) and services defined by OpenAPI specifications. It provides a simple and intuitive way to invoke REST services using the function-calling JSON format, making it easy to integrate with LLM-generated function calls.
Motivation
The OpenAPI Service Client library aims to simplify the process of invoking OpenAPI-defined services using function-calling payloads from various LLM providers. By abstracting away the complexities of making HTTP requests, handling authentication, preparing invocation payloads, and processing responses, it allows users to easily invoke underlying services with LLM-generated function calls.
The library works with several LLM providers, including OpenAI, Anthropic, and Cohere. It uses a common method, config.get_tools_definitions()
, to abstract each provider's unique and peculiar ways of defining functions. Similarly, differences in how these providers output function calls are handled uniformly through the LLMProvider
and its FunctionPayloadExtractor
.
Thanks to these two abstractions, users can simply pass the config.get_tools_definitions()
output as the tools definition when creating function calls and pass the entire chat completion response to service.invoke()
. This significantly simplifies the integration process and allows for a true plug-and-play experience across different LLM providers. See the examples below for more details.
Features
- Plug-and-Play LLM Integration: Easily integrate with LLM-generated function calls for various providers, including OpenAI, Anthropic, and Cohere.
- OpenAPI Compliance: Automatically handle REST service invocations and support various authentication strategies (API key, HTTP authentication, OAuth2).
- Customizable and Extensible: Offers flexible configuration options and an extensible architecture to accommodate additional LLM providers and function-calling formats.
Installation
You can install OpenAPI Service Client using pip:
pip install openapi-service-client
Usage
To use OpenAPIServiceClient
, follow these steps to configure and invoke operations on your target API (aka tool/service) defined by an OpenAPI specification.
OpenAI Example
To run the OpenAI example below, you need:
- Install openai package (
pip install openai
) - OpenAI API key. You can obtain an OpenAI API key by signing up for an account on the OpenAI platform. See https://platform.openai.com/ for more details.
- SerperDev API key. This api key is required to access the SerperDev Google search engine API. See https://serper.dev/ for a quick signup and free credits.
import os
from openai import OpenAI
from openapi_service_client.client import OpenAPIServiceClient
from openapi_service_client.client_configuration import ClientConfigurationBuilder
# Configure the API client
builder = ClientConfigurationBuilder()
config = (
builder.with_openapi_spec("https://bit.ly/serper_dev_spec_yaml")
.with_credentials(os.getenv("SERPERDEV_API_KEY"))
.build()
)
# Setup the OpenAI API client...
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# and send the chat message to create a function-calling response completion
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Do a google search: Who was Nikola Tesla?"}],
tools=config.get_tools_definitions()
)
# Initialize the OpenAPIServiceClient with configuration
serper_api = OpenAPIServiceClient(config)
# Simply pass the LLM response and invoke the service
# The LLM specific function-calling payload is extracted and processed automatically
service_response = serper_api.invoke(response)
print(service_response)
Anthropic Example
To run the Anthropic Claude Opus example below, you need:
- Install anthropic package (
pip install anthropic
) - Anthropic API key. You can obtain an Anthropic API key by signing up for an account on the Anthropic platform. See https://www.anthropic.com/ for more details.
- SerperDev API key. This api key is required to access the SerperDev Google search engine API. See https://serper.dev/ for a quick signup and free credits.
import os
import anthropic
from openapi_service_client.client import OpenAPIServiceClient
from openapi_service_client.client_configuration import ClientConfigurationBuilder
from openapi_service_client.providers import AnthropicLLMProvider
# Configure the API client
builder = ClientConfigurationBuilder()
config = (
builder.with_openapi_spec("https://bit.ly/serper_dev_spec_yaml")
.with_credentials(os.getenv("SERPERDEV_API_KEY"))
.with_provider(AnthropicLLMProvider())
.build()
)
# Setup the Anthropic API client
client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
# and send the chat message to create a function-calling response completion
response = client.beta.tools.messages.create(
model="claude-3-opus-20240229",
max_tokens=1024,
tools=config.get_tools_definitions(),
messages=[{"role": "user", "content": "Do a google search: Who was Nikola Tesla?"}],
)
# Initialize the OpenAPIServiceClient with configuration
serper_api = OpenAPIServiceClient(config)
# Simply pass the LLM response and invoke the service
# The LLM specific function-calling payload is extracted and processed automatically
service_response = serper_api.invoke(response)
print(service_response)
Cohere Example
To run the Cohere Command-R example below, you need:
- Install cohere package (
pip install cohere
) - Cohere API key. You can obtain Cohere API key by signing up for an account on the Cohere platform. See https://cohere.ai/ for more details.
- SerperDev API key. This api key is required to access the SerperDev Google search engine API. See https://serper.dev/ for a quick signup and free credits.
import os
import cohere
from openapi_service_client.client import OpenAPIServiceClient
from openapi_service_client.client_configuration import ClientConfigurationBuilder
from openapi_service_client.providers import CohereLLMProvider
# Configure the API client
builder = ClientConfigurationBuilder()
config = (
builder.with_openapi_spec("https://bit.ly/serper_dev_spec_yaml")
.with_credentials(os.getenv("SERPERDEV_API_KEY"))
.with_provider(CohereLLMProvider())
.build()
)
# Setup the Cohere client...
client = cohere.Client(api_key=os.getenv("COHERE_API_KEY"))
# and send the chat message to create a function-calling response completion
response = client.chat(
model="command-r",
preamble="A preamble aka system prompt goes here.",
tools=config.get_tools_definitions(),
message="Do a google search: Who was Nikola Tesla?",
)
# Initialize the OpenAPIServiceClient with configuration
serper_api = OpenAPIServiceClient(config)
# Simply pass the LLM response and invoke the service
# The LLM specific function-calling payload is extracted and processed automatically
service_response = serper_api.invoke(response)
print(service_response)
How It Works
OpenAPIServiceClient
simplifies the process of invoking REST services defined by OpenAPI specifications. It takes care of the complexities involved in making HTTP requests, handling authentication, and processing responses.
When you provide an OpenAPI specification file to the client, it parses the specification and sets up the necessary request payloads and configurations to interact with the API based on the provided specification. You can then invoke specific operations using the OpenAI function-calling JSON format, which specifies the operation name and its arguments.
The client handles the REST invocation by constructing the appropriate HTTP request based on the OpenAPI specification. It takes care of parameter placing (path, query, requestBody etc.), payload formatting, authentication, and error handling. The response from the API is then returned to the caller for further processing.
By leveraging the OpenAPI specification, OpenAPIServiceClient
eliminates the need for manual request setup and simplifies the integration process. It allows you to focus on working with LLM-generated function calls and seamlessly invoke the underlying services.
Note how in the examples above, the service invocation function in OpenAPIServiceClient
directly accepts and processes function-calling payloads from various LLM providers. The client ensures that payloads are correctly identified and extracted, regardless of the LLM source, offering a truly plug-and-play experience.
Contributing
Contributions to OpenAPI Service Client are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.
License
OpenAPI Service Client is open-source software licensed under the Apache License 2.0. See the LICENSE file for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file openapi_service_client-0.0.10.tar.gz
.
File metadata
- Download URL: openapi_service_client-0.0.10.tar.gz
- Upload date:
- Size: 30.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6dfad65eddaff0d9a5eb1fe232d32e9a76e71e5112b7b2fe1235448f9b4ba63d |
|
MD5 | e30be6692da62fa02a184b2cead620b3 |
|
BLAKE2b-256 | ce0afa50b3f9846dda3f858360256a9d42eeb35c8ed170a9386a1fdab7ffdd47 |
File details
Details for the file openapi_service_client-0.0.10-py3-none-any.whl
.
File metadata
- Download URL: openapi_service_client-0.0.10-py3-none-any.whl
- Upload date:
- Size: 25.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d8414aa1c1c7a541ff1c82f5ceae840981b0acef5b4b859192010f7de9228a5 |
|
MD5 | 633d3b55b4fea0e9a2393bc52ae1fa22 |
|
BLAKE2b-256 | 9309cb8005c60a8df55e879a791ae5a30a3592a20336ff2a9690ed69ffcfe9c5 |