Description
The Bot class extends the Model class to create a conversational AI entity. It lets you specify custom rules, assign a name, configure time formats, and generate responses that can include images or file uploads.
- Purpose: Provide a flexible chatbot built on a specific Model.
- Customization: Define conversation rules, set a bot name, and attach images or files to user prompts.
- Integration: Pass in an existing Model, or let Bot initialize one automatically.
Attributes
| Attribute | Type | Description |
|---|---|---|
| bot_rules | str | Conversation rules guiding the bot's replies; the getter auto-appends the bot's name. |
| bot_name | str | Human-readable name of the bot (default: "AI Bot"), also inserted into the rules. |
| bot_time_format | str | Datetime format string used internally (default: "%d/%m/%Y %H:%M:%S"). |
| bot_Model | Model | Underlying Model instance that interacts with the OpenAI client. |
Methods
| Method | Returns | Purpose |
|---|---|---|
| bot_Model (getter) | Model | Retrieves the underlying Model used by this bot. |
| bot_rules (getter) | str | Gets the current rules, automatically including the bot's name. |
| bot_rules (setter) | None | Updates the rules or falls back to a default rule set. |
| bot_name (getter) | str | Retrieves the bot's current name. |
| bot_name (setter) | None | Assigns a new bot name and updates the rules accordingly. |
| bot_time_format (getter) | str | Returns the bot's current time-format string. |
| bot_time_format (setter) | None | Sets a new time-format; raises ValueError on invalid format. |
| response | dict[str, Any] | Generates a reply to a user prompt, optionally handling history, images, or file uploads and returning token-usage stats. |
Example
Code
from chatweaver import Bot, Model
# 1 . Using an existing Model
existing_model = Model(api_key="sk-1234567890abcdef1234...", model="gpt-4o")
bot = Bot(
cw_model=existing_model,
rules="You are a helpful assistant with a witty sense of humor.",
name="FriendlyBot"
)
# 2 . Or let Bot create a new Model internally
another_bot = Bot(
api_key="sk-0987654321abcdef5678...", # forwarded to Model's constructor
rules="You are a formal assistant that provides concise answers.",
name="ConciseBot"
)
# 3 . Generating a response to a user prompt
response_data = bot.response(
prompt="What is the capital of France?",
user="Alice",
image_path=None,
file_path=None
)
# Check response
print("Bot response:", response_data["content"])
print("Token usage:", response_data["total_tokens"])