Skip to main content

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


Download files

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

Source Distribution

chatspec-0.0.2.tar.gz (124.9 kB view details)

Uploaded Source

Built Distribution

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

chatspec-0.0.2-py3-none-any.whl (36.1 kB view details)

Uploaded Python 3

File details

Details for the file chatspec-0.0.2.tar.gz.

File metadata

  • Download URL: chatspec-0.0.2.tar.gz
  • Upload date:
  • Size: 124.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for chatspec-0.0.2.tar.gz
Algorithm Hash digest
SHA256 140f9c2e8b31692b192e1ea48856f1d9fac1a6c9bcd4a805f75abec642cbe5f3
MD5 e77d363855c63baed3a44c69c4181678
BLAKE2b-256 73a35feac4d5c9c948ddf0391e02374d0471d3bdc53beaa929446b34323535bc

See more details on using hashes here.

File details

Details for the file chatspec-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: chatspec-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 36.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for chatspec-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bb3fcd804aa9377f626d0151223c3d375ba3c7ace50f7370a0cb2ed531a74bae
MD5 1f7bb0313ceb73d3dcd53e04e777534f
BLAKE2b-256 977f4b6615181d89db2749cc032b657ab38c1a30a61e2b1a5bf1b69122f1081e

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