Easily connect any API to OpenAI function calling
Project description
API Caller
[](
Easily connect any API to OpenAI function calling.
Description
With the introduction of OpenAI's function calling, a new era of AI-powered API interaction has begun. This project aims to provide a simple and effective way to connect any API to OpenAI function calling.
The current version supports OpenAPI specifications and generated Swagger clients. Additional API specifications will be supported in future releases.
For more inspiration, check out the original post that influenced this project: Function calling with an OpenAPI specification
Prerequisites
To use this project, you need to have a generated Swagger Python client. You can generate this client from your OpenAPI specification with Swagger Codegen or Swagger Editor.
Requires Python 3.9+
Install
pip install pyapicaller
Usage
Use operationId and generated swagger to call the API. I already added petstore swagger client to the examples folder.
from apicaller.swagger import SwaggerCaller
# Use any OpenAPI spec file
OPENAPI_SPEC = "https://petstore3.swagger.io/api/v3/openapi.json"
# Generate swagger client and copy the client package to the current directory
CLIENT_PACKAGE = "swagger_client"
swagger_caller = SwaggerCaller(CLIENT_PACKAGE, OPENAPI_SPEC)
# Generate swagger client if it does not exist
swagger_caller.generate()
sys.path.insert(0, 'generated_clients')
pet = swagger_caller.call_api('getPetById', pet_id=5)
print(pet)
Use OpenAI to really call API
from openai import OpenAI
from apicaller import OpenaiCaller, SwaggerCaller
# Use any OpenAPI spec file
OPENAPI_SPEC = "https://petstore3.swagger.io/api/v3/openapi.json"
# Generate swagger client and copy the client package to the current directory
CLIENT_PACKAGE = "swagger_client"
client = OpenAI()
swagger_caller = SwaggerCaller(CLIENT_PACKAGE, OPENAPI_SPEC)
caller = OpenaiCaller(swagger_caller)
def get_openai_response(tools, messages):
return client.chat.completions.create(
model='gpt-4o-mini',
tools=tools,
tool_choice="auto", # "auto" means the model can pick between generating a message or calling a function.
temperature=0,
messages=messages,
)
tools = caller.get_tools()
messages = [
{"role": "user", "content": "Get pet id 5"},
]
for num_calls in range(5):
response = get_openai_response(tools, messages)
if response.choices[0].message.tool_calls:
messages.append(response.choices[0].message)
# Call the API(magic)
content = caller.call_function(response.choices[0].message.tool_calls[0].function)
messages.append({"role": "tool",
"content": content,
"tool_call_id": response.choices[0].message.tool_calls[0].id})
else:
print(response.choices[0].message.content)
break
The details for pet ID 5 are as follows:
- **Name:** doggie
- **Category:** Dogs
- **Photo URLs:** ["string"]
- **Tags:** [{"id": 0, "name": "string"}]
- **Status:** available
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyapicaller-0.1.6.tar.gz.
File metadata
- Download URL: pyapicaller-0.1.6.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
90d901bed9861c9138c36859b361aa7b8784245cb229db586923f4a5c5dce02c
|
|
| MD5 |
585b15745347089c89094dbf7805ea90
|
|
| BLAKE2b-256 |
12b5feca65f76ca78a03df1e2f68a8167ea6065f2c86a3f40e8aa6b1882068c2
|
File details
Details for the file pyapicaller-0.1.6-py3-none-any.whl.
File metadata
- Download URL: pyapicaller-0.1.6-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcddff79ca857bf39571ffd65329e022e66d83be27472d72eea81eb2d675fb67
|
|
| MD5 |
4a5564fa22317cf2c8a2d51d3f7a56d4
|
|
| BLAKE2b-256 |
798f6df0a399b477ab2b5693ac30b4ce39a14b24966c3830daa82bfb5695aa32
|