Skip to main content

A library for generating tools for OpenAI projects

Project description

A super simple tool to generate tool specification for the openai API.

Install

pip install openai-toolgen

Example

Example using Openai Completions:

from typing import Annotated
from enum import Enum
from openai import OpenAI
from openai_toolgen import tool

class Unit(str, Enum):
    Celcius = "c"
    Farenheit = "f"

@tool
def get_temperature(
        location: Annotated[str, "Location to fetch the tempereature from"],
        unit: Annotated[Unit, "Temperature will be returned in this unit"] = Unit.Celcius
    ):
    """Call this function when the user wants to know the temperature"""

    return { "temperature": 32, "unit": unit.value, "location": location }

client = OpenAI()
messages = [{"role": "user", "content": "What's the temperature in Stockholm today?"}]
completion = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tool.export_all(),
    tool_choice="required"
)

Check out the tests for more details

Features

Enums

If the type of an argument is enum.Enums it will be properly serialized according to openai's spec.

>>> from openai_toolgen import tool
>>> from enum import Enum
>>> class Unit(str, enum):
...   celcius = "c"
...   farenheit = "f",
...
>>> @tool
...def get_temperature(unit: unit): pass
...
>>> tool.export_all()[0]['function']['parameters']['properties']
{
  'unit':
  {
    'type': 'string',
    'description': '',
    'enum': ['c', 'f']
  }
}

Parameter descriptions

It is possible to use typing.Annotated to provide a description to the LLM:

>>> from openai_toolgen import tool
>>> from typing import Annotated
>>> @tool
... def foo(bar:Annotated[str, "description of bar"]): pass
...
>>> tool.export_all()[0]['function']['parameters']['properties']
{
  'bar':
  {
    'type': 'string',
    'description': 'description of bar'
  }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

openai_toolgen-0.2.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file openai_toolgen-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: openai_toolgen-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for openai_toolgen-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 307d2267a6816a1131b9a616e790a10c8263d712ab199a3aaaec2a2cb71462bb
MD5 7eec1ba45f4439ed5f4cd9b52e2a50e4
BLAKE2b-256 e17577d534f3c01c4bc17fab4eb5a0a62bc197b963511fa9f8bfa037193c3627

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