Skip to main content

A simple library for creating and manipulating chat and message objects for LLM applications

Project description

chat-object

doctest coverage license python version PyPI Downloads

A simple library for creating and managing chat objects and messages for LLM applications.

Installation

From PyPI:

pip install chat-object

From GitHub:

pip install git+https://github.com/fresh-milkshake/chat-object.git

Or from source:

git clone https://github.com/fresh-milkshake/chat-object.git
cd chat-object
pip install -e .

Quick Start

Basic Chat Usage

Create a chat object and add messages to it:

import openai
from chat_object import Chat, Message, Role

client = openai.OpenAI()

chat = Chat(
    Message(Role.System, "You are a helpful assistant"),
    Message(Role.User, "Hello!")
)

response = client.chat.completions.create(
    model="gpt-5-nano",
    messages=chat.as_dict()
)

print(response.choices[0].message.content)

Using the Prompt Class

The Prompt class automatically handles indentation and formatting:

from chat_object import Prompt

# Clean indentation automatically
prompt = Prompt("""
    You are a helpful assistant.
    Please help me with the following task:
    
    def example_function():
        return "hello world"
    
    Explain what this function does.
""")

# Multiple arguments are joined with newlines
prompt = Prompt(
    "You are a helpful assistant.",
    "Please be concise in your responses.",
    "Focus on practical solutions."
)

# String operations work naturally
prompt += "\n\nAdditional context here"

QOL Features for Quick Development

Use convenience functions for faster development:

from chat_object import chat, msg_user, msg_system, msg_assistant, prmt

# Quick chat creation
chat_obj = chat(
    msg_system("You are a helpful assistant."),
    msg_user("Hello!"),
    msg_assistant("Hi there! How can I help you today?")
)

# Quick prompt creation
prompt = prmt("You are a helpful assistant.")

# Convert to dict for API calls
messages = chat_obj.as_dict()

[!TIP] See examples folder for more comprehensive examples.

Features

  • Well-tested code: Comprehensive test coverage with doctests throughout the codebase (90% coverage)
  • Type safety: Full type hints and enum-based roles
  • Backward compatibility: seamless integration with existing APIs like OpenAI, Anthropic, Together, Ollama, etc.
  • QOL features: Quick and easy message creation with msg_user, msg_assistant, msg_system, prmt, msgs, chat (Recommended, but not required). Pretty rich example usage of qol features is in examples/openai_use_case.py.

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

chat_object-2.0.1.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

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

chat_object-2.0.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file chat_object-2.0.1.tar.gz.

File metadata

  • Download URL: chat_object-2.0.1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.11

File hashes

Hashes for chat_object-2.0.1.tar.gz
Algorithm Hash digest
SHA256 d206eceddee550327d1a055c8f10aed4d912cff0d456e7ccc7a88d93c897a21c
MD5 6ea4e82640de4c0189c692008bf1ff92
BLAKE2b-256 b7c2c25915d4795a82c1ac64cd2baba03b420cba1518d009915a7734495e1ed6

See more details on using hashes here.

File details

Details for the file chat_object-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for chat_object-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f0173b56b362fb7694d678cba4a04524947a7a73717cda3068579427df5f93bc
MD5 6896d769cc3f882dca8d81f8fcd7f4af
BLAKE2b-256 40b39eeabe8e1ef86b4820f5cd38dd93d57552feb79877f46b4ea9f0dff7874c

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