pyapicaller


Namepyapicaller JSON
Version 0.2.0 PyPI version JSON
download
home_pageNone
SummaryEasily connect any API to OpenAI function calling
upload_time2025-02-05 12:56:26
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseMIT
keywords api caller python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pyapicaller",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "API, caller, Python",
    "author": null,
    "author_email": "Roman Medvedev <pypi@romavm.dev>",
    "download_url": "https://files.pythonhosted.org/packages/2d/a7/8da25321f73f441f69e571574080ad43bec7871d6f64ac6b0c009322d935/pyapicaller-0.2.0.tar.gz",
    "platform": null,
    "description": "# API Caller\n\n[![GitHub license](https://img.shields.io/github/license/Romamo/pyapicaller)](\n\nEasily connect any API to OpenAI function calling.\n\n## Description\n\nWith 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.\n\nThe current version supports OpenAPI specifications and generated Swagger clients. Additional API specifications will be supported in future releases.\n\nFor 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)\n\n## Prerequisites\n\nTo use this project, you need to have a generated Swagger Python client. \nYou can generate this client from your OpenAPI specification with\n[Swagger Codegen](https://github.com/swagger-api/swagger-codegen) or [Swagger Editor](https://editor.swagger.io/).\n\nRequires Python 3.9+\n\n## Install\nAscetic with OpenAPI and Tools validation \n```bash\npip install pyapicaller\n```\nWith swagger support\n```bash\npip install pyapicaller[swagger]\n```\nWith all options\n```bash\npip install pyapicaller[yaml,swagger,openai]\n```\n## Usage\n\nUse operationId and generated swagger client to call the API. Swagger client will be autogenerated.\n\n```python\nfrom apicaller.swagger import SwaggerCaller\n\n# Use any OpenAPI spec file\nOPENAPI_SPEC = \"https://petstore3.swagger.io/api/v3/openapi.json\"\n\nswagger_caller = SwaggerCaller(OPENAPI_SPEC,\n                               client_package='examples.petstore3.swagger_clients.swagger_client',\n                               generate_client=True,\n                               configuration={'host': 'https://petstore3.swagger.io/api/v3'})\n\npet = swagger_caller.call_api('getPetById', pet_id=5)\nprint(pet)\n```\nUse OpenAI to really call API\n```python\nfrom openai import OpenAI\nfrom apicaller import OpenaiCaller, SwaggerCaller\n\n# Use any OpenAPI spec file\nOPENAPI_SPEC = \"https://petstore3.swagger.io/api/v3/openapi.json\"\n\nswagger_caller = SwaggerCaller(OPENAPI_SPEC,\n                               client_package='examples.petstore3.swagger_clients.swagger_client',\n                               generate_client=True,\n                               configuration={'host': 'https://petstore3.swagger.io/api/v3'})\ncaller = OpenaiCaller(swagger_caller)\nclient = OpenAI()\n\n\ndef get_openai_response(tools, messages):\n    return client.chat.completions.create(\n        model='gpt-4o-mini',\n        tools=tools,\n        tool_choice=\"auto\",  # \"auto\" means the model can pick between generating a message or calling a function.\n        temperature=0,\n        messages=messages,\n    )\n\n\ntools = caller.get_tools()\nmessages = [\n    {\"role\": \"user\", \"content\": \"Get pet id 5\"},\n]\n\nfor num_calls in range(5):\n    response = get_openai_response(tools, messages)\n    if response.choices[0].message.tool_calls:\n        messages.append(response.choices[0].message)\n\n        # Call the API(magic) \n        content = caller.call_function(response.choices[0].message.tool_calls[0].function)\n        \n        messages.append({\"role\": \"tool\",\n                         \"content\": content,\n                         \"tool_call_id\": response.choices[0].message.tool_calls[0].id})\n    else:\n        print(response.choices[0].message.content)\n        break\n\n```\n\n```text\nThe details for pet ID 5 are as follows:\n\n- **Name:** doggie\n- **Category:** Dogs\n- **Photo URLs:** [\"string\"]\n- **Tags:** [{\"id\": 0, \"name\": \"string\"}]\n- **Status:** available\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Easily connect any API to OpenAI function calling",
    "version": "0.2.0",
    "project_urls": {
        "homepage": "https://github.com/Romamo/pyapicaller"
    },
    "split_keywords": [
        "api",
        " caller",
        " python"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "838448754ccbc542362f5d2b51438d9d03b5e807216700b52ff1fe433a607405",
                "md5": "7da45c7d410f9effb436b1ac83d06cbc",
                "sha256": "dd1b9f9eec3c756937c932a9d905b94aab98b2495c1352151498e1ff6e1fb403"
            },
            "downloads": -1,
            "filename": "pyapicaller-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7da45c7d410f9effb436b1ac83d06cbc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 8209,
            "upload_time": "2025-02-05T12:56:24",
            "upload_time_iso_8601": "2025-02-05T12:56:24.348077Z",
            "url": "https://files.pythonhosted.org/packages/83/84/48754ccbc542362f5d2b51438d9d03b5e807216700b52ff1fe433a607405/pyapicaller-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "2da78da25321f73f441f69e571574080ad43bec7871d6f64ac6b0c009322d935",
                "md5": "45c7f0987eab4415765ee65ee99ba220",
                "sha256": "558a3495545b047b3029179d7acfee9e14977acbebf5f88dfc1aa5739f0a89f1"
            },
            "downloads": -1,
            "filename": "pyapicaller-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "45c7f0987eab4415765ee65ee99ba220",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 9085,
            "upload_time": "2025-02-05T12:56:26",
            "upload_time_iso_8601": "2025-02-05T12:56:26.460728Z",
            "url": "https://files.pythonhosted.org/packages/2d/a7/8da25321f73f441f69e571574080ad43bec7871d6f64ac6b0c009322d935/pyapicaller-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-05 12:56:26",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Romamo",
    "github_project": "pyapicaller",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pyapicaller"
}
        
Elapsed time: 2.07943s