Metadata-Version: 2.2
Name: pyapicaller
Version: 0.2.2
Summary: Easily connect any API to OpenAI function calling
Author-email: Roman Medvedev <pypi@romavm.dev>
License: MIT
Project-URL: homepage, https://github.com/Romamo/pyapicaller
Keywords: API,caller,Python
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: jsonref
Provides-Extra: yaml
Requires-Dist: pyyaml; extra == "yaml"
Provides-Extra: swagger
Requires-Dist: urllib3>=1.15; extra == "swagger"
Requires-Dist: six>=1.10; extra == "swagger"
Requires-Dist: certifi; extra == "swagger"
Requires-Dist: python-dateutil; extra == "swagger"
Provides-Extra: openai
Requires-Dist: openai; extra == "openai"

# API Caller

[![GitHub license](https://img.shields.io/github/license/Romamo/pyapicaller)](

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](https://cookbook.openai.com/examples/function_calling_with_an_openapi_spec)

## 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](https://github.com/swagger-api/swagger-codegen) or [Swagger Editor](https://editor.swagger.io/).

Requires Python 3.9+

## Install
Ascetic with OpenAPI and Tools validation 
```bash
pip install pyapicaller
```
With swagger support
```bash
pip install pyapicaller[swagger]
```
With all options
```bash
pip install pyapicaller[yaml,swagger,openai]
```
## Usage

Use operationId and generated swagger client to call the API. Swagger client will be autogenerated.

```python
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
```python
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

```

```text
The details for pet ID 5 are as follows:

- **Name:** doggie
- **Category:** Dogs
- **Photo URLs:** ["string"]
- **Tags:** [{"id": 0, "name": "string"}]
- **Status:** available
```
