Skip to main content

OpenAPI Service Client

PyPI - Version PyPI - Python Version

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:

  1. Install openai package (pip install openai)
  2. 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.
  3. 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:

  1. Install anthropic package (pip install anthropic)
  2. 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.
  3. 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:

  1. Install cohere package (pip install cohere)
  2. 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.
  3. 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.

Metadata

Release files for openapi-service-client 0.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openapi-service-client 0.0.10
File Size Uploaded
openapi_service_client-0.0.10.tar.gz 30.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openapi-service-client 0.0.10
File Interpreter ABI Platform
openapi_service_client-0.0.10-py3-none-any.whl Python 3 none any Details

Total release size: 55.8 kB

Release files / openapi_service_client-0.0.10.tar.gz

Download URL openapi_service_client-0.0.10.tar.gz
Size 30.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6dfad65eddaff0d9a5eb1fe232d32e9a76e71e5112b7b2fe1235448f9b4ba63d
BLAKE2b-256 checksum
How to use checksums
ce0afa50b3f9846dda3f858360256a9d42eeb35c8ed170a9386a1fdab7ffdd47
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.27.0

Release files / openapi_service_client-0.0.10-py3-none-any.whl

Download URL openapi_service_client-0.0.10-py3-none-any.whl
Size 25.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d8414aa1c1c7a541ff1c82f5ceae840981b0acef5b4b859192010f7de9228a5
BLAKE2b-256 checksum
How to use checksums
9309cb8005c60a8df55e879a791ae5a30a3592a20336ff2a9690ed69ffcfe9c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.27.0

Release history Release notifications | RSS feed

This release

0.0.10 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page