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

pip install pyapicaller

Usage

Use operationId and generated swagger to call the API. I already added petstore swagger client to the examples folder.

https://editor.swagger.io/?url=https://raw.githubusercontent.com/swagger-api/swagger-petstore/refs/heads/master/src/main/resources/openapi.yaml

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


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.1.10.tar.gz (6.6 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.1.10-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyapicaller-0.1.10.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

Hashes for pyapicaller-0.1.10.tar.gz
Algorithm Hash digest
SHA256 87763012fd140b7473cb3874834fbc68aec7b493be5fdd0a98a95b1faf4c2f34
MD5 d60694bfd0daf347aa0094c1a73eaec4
BLAKE2b-256 8a2ce57e8d2612099101472f3995eb101eb9fadc3089e2a2e0887ee9edb2cdec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyapicaller-0.1.10-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.1

File hashes

Hashes for pyapicaller-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 1eeef7d3c0700815abbef572866b0271858e9ec1736a5744473822e718977ca7
MD5 d932345c1befde3e0a7434ec40e9cebe
BLAKE2b-256 59fc85532c7070e3f49b8928bc00c2d94febf2df54eb6d2dcc29ce2ef57b9448

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