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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openai_toolgen-0.3.1.tar.gz.
File metadata
- Download URL: openai_toolgen-0.3.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c9b9387dd9c9d74bcb0f857c49c5ee6b5c23b7676926c442b01de62d63f8d1e
|
|
| MD5 |
c04cd2d04dde1d7eefdf81f65002644a
|
|
| BLAKE2b-256 |
a622cf33a6c8b03fee34dc75f6164914a37be6bd99fc508505c1f0ae9a6c3f6e
|
File details
Details for the file openai_toolgen-0.3.1-py3-none-any.whl.
File metadata
- Download URL: openai_toolgen-0.3.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e81f2c4ae52ced87779d411927236ab01a627bf4f3d23598af8b2f677c1bffe3
|
|
| MD5 |
1cb90a608491378d5dee382643ff0d13
|
|
| BLAKE2b-256 |
a295403c2b81a84ec8a1f3e4059ddc68528937e5c2027072dacd57d85e3f6f16
|