Skip to main content
Weaviate Docs (migrated from docs.weaviate.io) Docs

Search documentation

Type to search this documentation.

On this pageOverview

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.

Python
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)
JavaScript/TypeScript
import { ChatMessage } from 'weaviate-agents';

const conversation: ChatMessage[] = [
    {
        role: "user",
        content: "Hi!"
    },
    {
        role: "assistant",
        content: "Hello! How can I assist you today?"
    },
    {
        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.",
    },
    {
        role: "assistant",
        content: "I can help with that. What specific information are you looking for?",
    },
    {
        role: "user",
        content: "What's the average wind speed, the max wind speed, and the min wind speed",
    }
]

const response = await 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.

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.

Python
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?"
)
JavaScript/TypeScript
const messageHistory: ChatMessage[] = []

async function useQA(query: string): Promise<string> {
    messageHistory.push({ role: "user", content: query })
    const response = await qa.ask(messageHistory)
    messageHistory.push({ role: "assistant", content: response.finalAnswer })
    return response.finalAnswer
}

await useQA(
    "I have some questions about the weather data. " +
    "You can assume the temperature is in Fahrenheit " +
    "and the wind speed is in mph."
)

await useQA(
    "What's the average wind speed, the max wind speed, " +
    "and the min wind speed?"
)

Have a question or feedback? Here's how to reach us.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu