Skip to main content

Easily connect any API to OpenAI function calling

Project description

API Caller

[GitHub license](

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyapicaller-0.2.2.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyapicaller-0.2.2-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file pyapicaller-0.2.2.tar.gz.

File metadata

  • Download URL: pyapicaller-0.2.2.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for pyapicaller-0.2.2.tar.gz
Algorithm Hash digest
SHA256 00fa7602daf7b1449938d5f355779c13b70a053f45b4bd44fecb23e241ab75e3
MD5 17f2d85697e6851d4ad91c8bb6ea7748
BLAKE2b-256 d909a1513678dd1e9d055edd1f3be348e86845f1c8f8950ed3a472fa828a57e5

See more details on using hashes here.

File details

Details for the file pyapicaller-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: pyapicaller-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for pyapicaller-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 70bb7051b9bbf6b9c20442b4d8f7eb28f01943109dea83fa3053e16f41093e26
MD5 1336ee21bba9775e4cd261ee334c412d
BLAKE2b-256 224da64e8ed86e9dee435e835a015d3c059d7ad5587e7f2789a0e3ea117a5e23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page