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("Call this function when the user wants to know the temperature")
def get_temperature(
        location: Annotated[str, "Location to fetch the tempereature from"],
        unit: Annotated[Unit, "Temperature will be returned in this unit"] = Unit.Celcius
    ):
    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 Distribution

openai_toolgen-0.3.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

openai_toolgen-0.3.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file openai_toolgen-0.3.0.tar.gz.

File metadata

  • Download URL: openai_toolgen-0.3.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for openai_toolgen-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c94e2f4adb2a3eff0cdda3537526844af5e77d364704010fe48ec59bcf04b85b
MD5 2c793b96e5ec8b2e45e3e8c0a8d1fd0f
BLAKE2b-256 cd4317ed9172d9e765c148a0d3d00722f9070b511d6bd9c599e8b9723b1c82a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: openai_toolgen-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8a29c4858965ffaef8227cea5a97ae22e4e66b82131d4cbba8c6574e4fa0f963
MD5 fda2256b40ffc201d7e661e4e1f236b6
BLAKE2b-256 f854a755001cd1ee0ba55f37792da32682f9aedfc5c73c3568428cb912092061

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