Skip to main content

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

@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):
    return f"Weather in {city}: 25°C"

3. Using Tools with Chat

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

4. Removing Tools

To remove a tool, use remove_tool with function's name as an argument:

client.remove_tool("get_weather")

If the tool is not found, remove_tool will raise ValueError.

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 register **sync and async ** functions.
  • Tools are automatically registered and described for the OpenAI model.
  • The model decides whether to call a tool during the dialogue.
  • You can quickly remove unnecessary tools using remove_tool.

License

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

Metadata

Release files for openai-tools-decorator 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openai-tools-decorator 1.0.3
File Size Uploaded
openai_tools_decorator-1.0.3.tar.gz 10.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openai-tools-decorator 1.0.3
File Interpreter ABI Platform
openai_tools_decorator-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 20.7 kB

Release files / openai_tools_decorator-1.0.3.tar.gz

Download URL openai_tools_decorator-1.0.3.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
45421a1371b36b2da94d2bbfd40584b7b22205741c10543964d2dad70291d0bd
BLAKE2b-256 checksum
How to use checksums
99a163977a62ca72cca44aad5e43b740fc445829ff7de2d95f857955167a759d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.6

Release files / openai_tools_decorator-1.0.3-py3-none-any.whl

Download URL openai_tools_decorator-1.0.3-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2e2e92a531e17e754106b4a415cdef17210a1a2ca31d5c42b01de960e641f0d1
BLAKE2b-256 checksum
How to use checksums
dd0def8e5792dbd84a5558f14c19c1fcae74e23a215a76f2a94607a006a0843a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.6

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page