Skip to main content

Python module, that provides a simple way to create and manage ChatGPT tools by python decorators

Project description

openai_tools_decorator

A lightweight Python library that streamlines creating and invoking “tools” (functions) in your OpenAI ChatCompletion-based projects. It lets you register and call both synchronous and asynchronous functions via decorators.

Installation

pip install openai_tools_decorator

Quick Start

1. Import and Initialization

from openai_tools_decorator import OpenAIT

client = OpenAIT()

2. Adding Tools

Wrap your function (sync or async) with @client.add_tool(...). The decorator registers it and provides a JSON Schema describing the function’s parameters:

@client.add_tool(
    {
        "description": "Get current weather for a city",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "The city name in English"}
            },
            "required": ["city"]
        },
    }
)
def get_weather(city: str):
    # Or async def get_weather(...) if you prefer
    return f"Weather in {city}: 25°C"

3. Using Tools with Chat

When you call run_with_tool(...) or run_with_tool_by_thread_id(...), the ChatCompletion model can opt to invoke any matching tool. For example:

user_input = "How cold is it in Moscow right now?"
response = await client.run_with_tool(
    user_input,
    messages=[],
    model="gpt-4o"
)
print(response)  # The assistant’s response, possibly including a tool call

Example

import asyncio
import aiohttp
from openai_tools_decorator import OpenAIT

client = OpenAIT()
api_key = "<YOUR_API_KEY>"

async def fetch_url(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            return await resp.text()

@client.add_tool(
    {
        "description": "Fetch weather from an API",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {
                    "type": "string",
                    "description": "The city name in English"
                }
            },
            "required": ["city"]
        },
    }
)
async def get_weather(city: str):
    url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
    return await fetch_url(url)

async def main():
    question = "What's the temperature in London?"
    result = await client.run_with_tool(question, messages=[])
    print("Assistant says:", result)

asyncio.run(main())

Key Points

  • You can decorate both synchronous and asynchronous functions.
  • Tools get automatically registered and described for the OpenAI model.
  • The model decides whether or not to call your tool during the conversation.

License

Distributed under MIT or any other license of your choice. Contributions and feedback are always welcome!

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

openai_tools_decorator-1.0.2.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

openai_tools_decorator-1.0.2-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file openai_tools_decorator-1.0.2.tar.gz.

File metadata

  • Download URL: openai_tools_decorator-1.0.2.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for openai_tools_decorator-1.0.2.tar.gz
Algorithm Hash digest
SHA256 6e62e8829d1e207dcd5c004ce4cf6de2c3a4b47e002bc3f5c73e71b2d1c59393
MD5 f8457f89ad559420c48681e4f6e2721f
BLAKE2b-256 23ac937c4163ceb6406be03c5011c36092b4a8c756d9faa0da9074f59d84ff1d

See more details on using hashes here.

File details

Details for the file openai_tools_decorator-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_tools_decorator-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 06360ab0adf0ff71a221931cd27dea77941d248824d12252165e8d087867e6d1
MD5 5990205ef2a8989fdbe2cce9d62e5d70
BLAKE2b-256 24b86cd2470aab229fdfa75f001dd428d06143661e86c694a2eceac496a574c3

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