Skip to main content

walrus_core/model/
request.rs

1//! Chat request type.
2
3use crate::model::{Message, Tool, ToolChoice};
4use compact_str::CompactString;
5use serde::{Deserialize, Serialize};
6
7/// A chat completion request.
8///
9/// Contains everything needed to make an LLM call: model, messages, tools,
10/// and streaming hints. Provider implementations convert this to their
11/// wire format via `From<Request>`.
12#[derive(Debug, Clone, Deserialize, Serialize)]
13pub struct Request {
14    /// The model to use.
15    pub model: CompactString,
16
17    /// The conversation messages.
18    #[serde(default)]
19    pub messages: Vec<Message>,
20
21    /// Whether to enable thinking.
22    pub think: bool,
23
24    /// The tools available for this request.
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    pub tools: Option<Vec<Tool>>,
27
28    /// Controls which tool is called by the model.
29    #[serde(default, skip_serializing_if = "Option::is_none")]
30    pub tool_choice: Option<ToolChoice>,
31
32    /// Whether to return usage information in stream mode.
33    pub usage: bool,
34}
35
36impl Request {
37    /// Create a new request for the given model.
38    pub fn new(model: impl Into<CompactString>) -> Self {
39        Self {
40            model: model.into(),
41            messages: Vec::new(),
42            think: false,
43            tools: None,
44            tool_choice: None,
45            usage: false,
46        }
47    }
48
49    /// Set the messages for this request.
50    pub fn with_messages(mut self, messages: Vec<Message>) -> Self {
51        self.messages = messages;
52        self
53    }
54
55    /// Set the tools for this request.
56    pub fn with_tools(mut self, tools: Vec<Tool>) -> Self {
57        self.tools = Some(tools);
58        self
59    }
60
61    /// Set the tool choice for this request.
62    pub fn with_tool_choice(mut self, tool_choice: ToolChoice) -> Self {
63        self.tool_choice = Some(tool_choice);
64        self
65    }
66
67    /// Enable or disable thinking/reasoning mode.
68    pub fn with_think(mut self, think: bool) -> Self {
69        self.think = think;
70        self
71    }
72}
73
74impl Default for Request {
75    fn default() -> Self {
76        Self {
77            model: "deepseek-chat".into(),
78            messages: Vec::new(),
79            think: false,
80            tools: None,
81            tool_choice: None,
82            usage: false,
83        }
84    }
85}