Chat Interactions
QueryMT provides comprehensive support for chat-based interactions with Large Language Models, enabling you to build conversational AI applications.
Key Components
-
querymt::chat::ChatMessage: Represents a single message in a conversation. Key attributes include:role: Indicates who sent the message (querymt::chat::ChatRole::Userorquerymt::chat::ChatRole::Assistant).message_type: Specifies the nature of the content (e.g.,querymt::chat::MessageType::Text,Image,ToolUse,ToolResult).content: The primary text content of the message.- Source:
crates/querymt/src/chat/mod.rs
-
querymt::chat::ChatResponse: A trait representing the LLM's response to a chat request. It provides methods to access:text(): The textual content of the LLM's reply.tool_calls(): A list ofquerymt::ToolCallobjects if the LLM decided to use one or more tools.thinking(): Optional "thoughts" or reasoning steps from the model, if supported and enabled.usage(): Optional token usage information (querymt::Usage).- Source:
crates/querymt/src/chat/mod.rs
-
querymt::chat::ChatCall: The single request value every chat entry point takes. It carries themessages, optionaltoolsand per-requestGenerationOptions. Build it withChatCall::new(&messages), then chain.tools(&tools)/.maybe_tools(opt)and.options(opts). A new request parameter is a new field here, never a new provider method. -
querymt::chat::ChatProvider: The trait LLM providers implement. It has three methods:chat(&self, call: &ChatCall<'_>): Sends one request and returns aChatOutput.chat_stream(&self, call: &ChatCall<'_>): Streams the response asStreamChunks (returnsNotImplementedunless the provider supports streaming).capabilities(&self): Reports aChatCapabilities { streaming, options }so callers can check streaming and per-request option support before sending.- Source:
crates/querymt/src/chat/mod.rs
How It Works
- Construct Messages: Your application assembles a sequence of
ChatMessageobjects representing the conversation history. UseChatMessage::user()for user messages andChatMessage::assistant()for assistant messages. This typically starts with alternatingUserandAssistantmessages. - Initiate Chat: You call
chat(orchat_stream) on anLLMProviderinstance with aChatCallholding the message history and, optionally, the available tools. - Provider Interaction: The
LLMProvider(or its underlying implementation likeHTTPLLMProvider) formats the request according to the specific LLM's API, sends it, and receives the raw response. - Parse Response: The provider parses the raw response into an object implementing
ChatResponse. - Handle Response: Your application processes the
ChatResponse:- If
text()is present, it's the LLM's textual reply. - If
tool_calls()is present, the LLM wants to execute one or more functions. Your application needs to:- Execute these functions.
- Send the results back to the LLM as new
ChatMessages (typically withMessageType::ToolResult). - Continue the chat loop.
- If
Example Flow (Conceptual)
Per-request generation options
Some settings, such as the output-token cap or the reasoning effort, are normally fixed when a provider is built. querymt::chat::GenerationOptions overrides them for a single chat request, without rebuilding the provider:
- A set field overrides the configured value for that request only. An unset field leaves the configured value in effect.
- Providers report what they honour through
ChatProvider::capabilities(); itsoptionsfield is aGenerationOptionSupport. Query it before relying on an override. Options a provider does not honour are ignored and the request still succeeds.GenerationOptionSupport::retain_supporteddrops the unsupported overrides from aGenerationOptionsvalue. - A call without options produces a request identical to one made before options existed.
- Wrappers (tool and validation decorators, the HTTP adapter, mesh and remote providers, the Extism host) forward the whole
ChatCall, so options and any future field cannot be dropped silently. - Plugins and remote peers built against an older contract are rejected with an actionable "rebuild the plugin" / "upgrade both peers" error rather than silently ignoring options. A remote host answers the contract handshake with the capabilities of the provider it builds for the request.
- Today the Anthropic provider honours both fields. Other providers report no support until they are updated.
Source: crates/querymt/src/chat/mod.rs
QueryMT's chat system is designed to be flexible, supporting simple Q&A, complex multi-turn dialogues, and sophisticated interactions involving external tools. The Tool and ToolChoice mechanisms provide fine-grained control over how LLMs can utilize functions.