Skip to main content

crabtalk_core/model/
request.rs

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