Multi-turn conversations
The Query Agent transforms a natural language query into actionable searches. You can either pass a single string for the query, or provide more context by including a full conversation with previous message turns.
from weaviate.agents.classes import ChatMessage
conversation = [
ChatMessage(
role="user",
content=(
"I have some questions about the weather data. "
"You can assume the temperature is in Fahrenheit "
"and the wind speed is in mph."
)
),
ChatMessage(
role="assistant",
content=(
"I can help with that. "
"What specific information are you looking for?"
)
),
ChatMessage(
role="user",
content=(
"What's the average wind speed, the max wind speed, "
"and the min wind speed?"
)
)
]
response = qa.ask(conversation)
Each message in the conversation must have a role, being either "user" or "assistant", and content, being the text of the message.
The final message should be a user message, and it will be treated as the current user query to define the task.
Example: Iterative message history
In a chat-style application, you typically want each new user message to build on top of everything said so far, rather than asking the agent in isolation. To do this, keep a running list of ChatMessage objects and append both the user's query and the agent's reply to it after every turn. Pass the full list back into qa.ask() on the next call so the agent has the complete context.
The example below wraps this pattern in a simple way.
message_history: list[ChatMessage] = []
def use_qa(query: str) -> str:
message_history.append(
ChatMessage(role="user", content=query)
)
response = qa.ask(message_history)
message_history.append(
ChatMessage(role="assistant", content=response.final_answer)
)
return response.final_answer
use_qa(
"I have some questions about the weather data. "
"You can assume the temperature is in Fahrenheit "
"and the wind speed is in mph."
)
use_qa(
"What's the average wind speed, the max wind speed, "
"and the min wind speed?"
)
Questions and feedback
If you have any questions or feedback, let us know in the user forum.
