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
Ascetic with OpenAPI and Tools validation
pip install pyapicaller
With swagger support
pip install pyapicaller[swagger]
With all options
pip install pyapicaller[yaml,swagger,openai]
Usage
Use operationId and generated swagger client to call the API. Swagger client will be autogenerated.
from apicaller.swagger import SwaggerCaller
# Use any OpenAPI spec file
OPENAPI_SPEC = "https://petstore3.swagger.io/api/v3/openapi.json"
swagger_caller = SwaggerCaller(OPENAPI_SPEC,
client_package='examples.petstore3.swagger_clients.swagger_client',
generate_client=True,
configuration={'host': 'https://petstore3.swagger.io/api/v3'})
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"
swagger_caller = SwaggerCaller(OPENAPI_SPEC,
client_package='examples.petstore3.swagger_clients.swagger_client',
generate_client=True,
configuration={'host': 'https://petstore3.swagger.io/api/v3'})
caller = OpenaiCaller(swagger_caller)
client = OpenAI()
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
Metadata
Release files for pyapicaller 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyapicaller-0.2.2.tar.gz | 7.7 kB | Without provenance | Details for pyapicaller-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyapicaller-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for pyapicaller-0.2.2-py3-none-any.whl |
Total release size: 16.0 kB
Release files / pyapicaller-0.2.2.tar.gz
| Download URL | pyapicaller-0.2.2.tar.gz |
|---|---|
| Size | 7.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
00fa7602daf7b1449938d5f355779c13b70a053f45b4bd44fecb23e241ab75e3
|
|
BLAKE2b-256 checksum How to use checksums |
d909a1513678dd1e9d055edd1f3be348e86845f1c8f8950ed3a472fa828a57e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.13
|
Release files / pyapicaller-0.2.2-py3-none-any.whl
| Download URL | pyapicaller-0.2.2-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
70bb7051b9bbf6b9c20442b4d8f7eb28f01943109dea83fa3053e16f41093e26
|
|
BLAKE2b-256 checksum How to use checksums |
224da64e8ed86e9dee435e835a015d3c059d7ad5587e7f2789a0e3ea117a5e23
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.13
|
Security for pyapicaller 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.