Skip to main content

Utility methods to manipulate LLM inputs and outputs

Project description

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:

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

chatutils-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file chatutils-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: chatutils-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for chatutils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4442c41d518ce8d2cbcf589ac66cdab37c62b6fd973b088c8a372cc22268ff49
MD5 7108f1b730bb9a823394f58adbebfc66
BLAKE2b-256 895467c6691c4ffb71cb64338868fe062b243ada9775054149d37e34304db462

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