ChatBot.chat()
Send a message to the chatbot and get a response within a conversation context.
Usage
ChatBot.chat(
message,
conversation=None,
)This method creates or updates a conversation by adding the user’s message and the chatbot’s response. If no conversation is provided, a new one is automatically created. This is the primary way to interact with the chatbot while maintaining conversation history and context.
Parameters
message: Union[str, Attachments]-
The user’s message to send to the chatbot. Can be:
- A string message
- An Attachments object containing files and an optional prompt
conversation: Optional[Conversation] = None- An existing conversation to continue. If not provided, a new conversation is created automatically.
Returns
Conversation- The conversation object containing the full message history including the new user message and chatbot response.
Examples
Basic single-message chat
import talk_box as tb
bot = tb.ChatBot().model("gpt-4").temperature(0.7)
convo = bot.chat("Hello! How are you?")
print(convo.get_last_message().content)Chat with file attachments
from talk_box import ChatBot
from talk_box.attachments import Attachments
bot = ChatBot().model("gpt-4")
# Create attachments
attachments = Attachments().with_prompt("What's in these files?")
attachments.add_file("document.pdf")
attachments.add_file("image.png")
# Chat with attachments
convo = bot.chat(attachments)
print(convo.get_last_message().content)Continuing a conversation
# Start a conversation
convo = bot.chat("What's machine learning?")
# Continue the same conversation
convo = bot.chat("Can you give me an example?", conversation=convo)
# View full conversation history
for msg in convo.get_messages():
print(f"{msg.role}: {msg.content}")