Simple types & utilities built for the OpenAI Chat Completions API specification.
Project description
💭 chatspec
Simple types & utilities built for the OpenAI Chat Completions API specification.
📦 Installation
pip install chatspec
chatspec provides (very) quick utilities for working with objects used in the OpenAI chat
completions API specification. I use Instructor
for all of my structured outputs, so Pydantic is a core part of this library. The point
of this library is to provide a common interface for methods that I have found myself
needing to replicate across various projects.
📚 Examples
Use chatspec for easy conversion or formatting of messages, tools, and more!
Convert python functions, pydantic models, dataclasses, and more to tool calling format
import chatspec
# dont define anything...
def my_web_tool(url: str) -> str:
return "Hello, world!"
tool = chatspec.convert_to_tool(my_web_tool)
print(tool)
Output
{
'type': 'function',
'function': {'name': 'my_web_tool', 'parameters': {'type': 'object', 'properties': {'url': {'type': 'string'}}, 'required': ['url'], 'additionalProperties': False}}
}
# or write a full docstring
def my_better_web_tool(url: str) -> str:
"""
This is a tool that can be used to get the title of a website.
Args:
url: The URL of the website to get the title of.
"""
return "Hello, world!"
tool = chatspec.convert_to_tool(my_better_web_tool)
print(tool)
Output
{
'type': 'function',
'function': {
'name': 'my_better_web_tool',
'parameters': {
'type': 'object',
'properties': {'url': {'type': 'string', 'description': 'The URL of the website to get the title of.'}},
'required': ['url'],
'additionalProperties': False
},
'description': 'This is a tool that can be used to get the title of a website.\n'
}
}
Manage message threads easily with State
import chatspec
# create a message store (state)
state = chatspec.State()
# we can add messages to the state normally
state.add_messages(
[
{"role": "user", "content": "hello i am steve"},
{"role": "assistant", "content": "hello steve i am yu"},
]
)
# or directly
state.add_message("no i am me who are you?")
# define a role too
state.add_message("i just told you i that i am yu", role="assistant")
# lets see our messages
print(state.messages)
# >>> [
# >>> {'content': 'hello i am steve', 'role': 'user'},
# >>> {'content': 'hello steve i am yu', 'role': 'assistant'},
# >>> {'content': 'no i am me who are you?', 'role': 'user'},
# >>> {'content': 'i just told you i that i am yu', 'role': 'assistant'}
# >>> ]
# we can have different states interact
# states also have their own system context, with a special
# 'identification_context' attribute
state2 = chatspec.State()
state2.identification_context['name'] = "yu"
# we can send messages between states
state.receive_message_from(state2, "i am me")
# lets see what yu sent
print(state.last_message)
# >>> {'content': '[EXTERNAL from yu]: i am me', 'role': 'user'}
Project details
Release history Release notifications | RSS feed
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 chatspec-0.0.1.tar.gz.
File metadata
- Download URL: chatspec-0.0.1.tar.gz
- Upload date:
- Size: 113.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
75947959c1bd0cf74089bb84237dc02cc09515466c79ed47ebdb0113309d6e7e
|
|
| MD5 |
74d508381fe5e39d29516c8ae4a0ef98
|
|
| BLAKE2b-256 |
2596d57846978e56f8ef45eb07810f40d9ba87f9fe6c743adc1dce1228b62b87
|
File details
Details for the file chatspec-0.0.1-py3-none-any.whl.
File metadata
- Download URL: chatspec-0.0.1-py3-none-any.whl
- Upload date:
- Size: 26.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f63dd8bb949aef5b997627412a0c9644c608368288ab824afa9d40ee13ccc95c
|
|
| MD5 |
7f1ff7f15ffbc4d14d1ff0927eeb76cc
|
|
| BLAKE2b-256 |
6ed5a73a5019793d5485c6dff3d1d16ebdede216fb668732037ab203c3373133
|