Skip to main content

chatutils

chatutils is a Python library for text processing, rich display, and LLM helpers.

Installation

pip install -U chatutils

Example

"""
Example script demonstrating `chatutils` in a realistic LLM workflow.
"""

import os
import chatutils
import openai

# We use Groq as an example LLM provider,
# as they provide their latest agent under the same name
model_name = "groq/compound"
client = openai.OpenAI(
    base_url="https://api.groq.com/openai/v1",
    api_key=os.environ.get("GROQ_API_KEY"),
)

# 2. The Payload: Construct role-based messages cleanly
messages = [
    chatutils.system("You are the state-of-the-art AI."),
    chatutils.user(
        """

        Write a minimal Python function to beep upon unexpected error,
        with an example usage in `if __name__ == "__main__"` clause.
        """
    ),  # This gets automatically formatted internally
]

# Display **exactly** what we are sending to the LLM.
# You see the indented prompt is properly formatted
chatutils.print_chat_messages(messages)

# Fire the request (assuming success because we are optimists)
response = client.chat.completions.create(model=model_name, messages=messages)
content = chatutils.get_completion(response)  # obtain response.choices[0].message.content

chatutils.print_panel(content, title="Response", border_style="green")

# Save the context & completion
messages.append(chatutils.assistant(content))
cache_filepath = (
    ".llm_cache/messages--"
    f"{model_name.replace('/', '_')}.{chatutils.hash_messages(messages)}"
    ".jsonl"
)
chatutils.save_messages(messages, cache_filepath)

code = chatutils.parse(content, index=-1)  # Parse the last code block
chatutils.write(code, "beep.py")

The same code is available in ./example.py:

Release files for chatutils 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for chatutils 0.1.0
File Interpreter ABI Platform
chatutils-0.1.0-py3-none-any.whl Python 3 none any Details

Release files / chatutils-0.1.0-py3-none-any.whl

Download URL chatutils-0.1.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4442c41d518ce8d2cbcf589ac66cdab37c62b6fd973b088c8a372cc22268ff49
BLAKE2b-256 checksum
How to use checksums
895467c6691c4ffb71cb64338868fe062b243ada9775054149d37e34304db462
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.1

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page