Chat

Description

The Chat class extends Bot to manage an entire multi-turn conversation session. It keeps an ordered history of messages (TextNode objects), enforces reply limits, and tracks metadata such as title, creation date, and cumulative token cost.
  • Purpose: Provide a structured environment for handling multiple user-assistant interactions in one session.
  • Key Features: Maintains conversation history, applies reply-pair limits, supports image/file attachments, and integrates seamlessly with a Bot (and underlying Model).
  • Metadata: Stores creation date, chat title, and calculates cost from token usage across the conversation.

Attributes

Attribute Type Description
chat_time_format str Datetime format string for timestamps (default: "%d/%m/%Y %H:%M:%S").
chat_replies_limit int | None Max allowed user+assistant pairs (infinite if None).
chat_history list[TextNode] Ordered list of TextNode messages comprising the conversation.
chat_user str Identifier of the user who is chatting.
chat_creation_date str Timestamp of when the chat session was created.
chat_replies int Current number of user+assistant reply pairs (computed).
chat_cost int Cumulative token count across the conversation (computed).
chat_title str Descriptive title of the chat session.
chat_Bot Bot Underlying Bot that generates responses.

Methods

Method Returns Purpose
chat_Bot (getter) Bot Retrieves the underlying Bot instance.
chat_time_format (getter) str Returns the datetime format used for timestamps.
chat_time_format (setter) None Validates and sets a new time-format string.
chat_replies_limit (getter) int | float('inf') Gets the maximum allowed reply pairs (∞ if unlimited).
chat_replies_limit (setter) None Sets a new reply-pair limit; accepts int or None.
chat_history (getter) list[TextNode] Returns the conversation history.
chat_history (setter) None Replaces the history with a list of TextNodes or dicts convertible to them.
chat_user (getter) str Retrieves the user identifier.
chat_user (setter) None Sets a new user identifier.
chat_creation_date (getter) str Returns the chat's creation date string.
chat_creation_date (setter) None Validates and sets a new creation date string.
chat_replies (getter) int Computes the current number of reply pairs from history.
chat_cost (getter) int Sums token usage across the chat history.
chat_title (getter) str Gets the chat session title.
chat_title (setter) None Assigns a new chat title.
set_all Chat | None Bulk-update multiple private attributes across Chat/Bot/Model.
get_response str Generates an assistant reply, appends messages to history, and returns the response content.

Example

Code
from chatweaver import Chat, Bot, TextNode

# 1. Creating a Chat with a custom Bot
my_bot = Bot(api_key="sk-1234567890abcdef1234...", rules="You are a friendly assistant.")
my_chat = Chat(
    cw_bot=my_bot,
    title="My AI Discussion",
    replies_limit=5,
    user="Alice"
)

# 2. Getting a response
assistant_reply = my_chat.get_response("Hello, can you tell me a joke?")
print("Assistant says:", assistant_reply)

# 3. Accessing chat history
for node in my_chat.chat_history:
    print(node.role, node.content, sep=": ")

# 4. Viewing total token usage
print("Total token usage:", my_chat.chat_cost)

# 5. Changing chat title
my_chat.chat_title = "Jokes and More"

# 6. Updating multiple attributes at once
my_chat.set_all(
    _Chat__replies_limit=None,            # Remove reply limit
    _Bot__rules="Stay professional in your answers."
)

print("Updated chat limit:", my_chat.chat_replies_limit)
print("Updated bot rules:", my_chat.chat_Bot.bot_rules)