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'
}
}
Metadata
Release files for openai-toolgen 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| openai_toolgen-0.3.1.tar.gz | 4.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| openai_toolgen-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.5 kB
Release files / openai_toolgen-0.3.1.tar.gz
| Download URL | openai_toolgen-0.3.1.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1c9b9387dd9c9d74bcb0f857c49c5ee6b5c23b7676926c442b01de62d63f8d1e
|
|
BLAKE2b-256 checksum How to use checksums |
a622cf33a6c8b03fee34dc75f6164914a37be6bd99fc508505c1f0ae9a6c3f6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.9
|
Release files / openai_toolgen-0.3.1-py3-none-any.whl
| Download URL | openai_toolgen-0.3.1-py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e81f2c4ae52ced87779d411927236ab01a627bf4f3d23598af8b2f677c1bffe3
|
|
BLAKE2b-256 checksum How to use checksums |
a295403c2b81a84ec8a1f3e4059ddc68528937e5c2027072dacd57d85e3f6f16
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.9
|