ollama_rs/generation/chat/
request.rs

1use serde::Serialize;
2
3use crate::{
4    generation::{
5        parameters::{FormatType, KeepAlive},
6        tools::ToolInfo,
7    },
8    models::ModelOptions,
9};
10
11use super::ChatMessage;
12
13/// A chat message request to Ollama.
14#[derive(Debug, Clone, Serialize)]
15pub struct ChatMessageRequest {
16    #[serde(rename = "model")]
17    pub model_name: String,
18    pub messages: Vec<ChatMessage>,
19    #[serde(skip_serializing_if = "Vec::is_empty")]
20    pub tools: Vec<ToolInfo>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub options: Option<ModelOptions>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub template: Option<String>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub format: Option<FormatType>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub keep_alive: Option<KeepAlive>,
29    /// Must be false if tools are provided
30    pub(crate) stream: bool,
31    pub think: Option<bool>,
32}
33
34impl ChatMessageRequest {
35    pub fn new(model_name: String, messages: Vec<ChatMessage>) -> Self {
36        Self {
37            model_name,
38            messages,
39            options: None,
40            template: None,
41            format: None,
42            keep_alive: None,
43            // Stream value will be overwritten by Ollama::send_chat_messages_stream() and Ollama::send_chat_messages() methods
44            stream: false,
45            tools: vec![],
46            think: None,
47        }
48    }
49
50    /// Additional model parameters listed in the documentation for the Modelfile
51    pub fn options(mut self, options: ModelOptions) -> Self {
52        self.options = Some(options);
53        self
54    }
55
56    /// The full prompt or prompt template (overrides what is defined in the Modelfile)
57    pub fn template(mut self, template: String) -> Self {
58        self.template = Some(template);
59        self
60    }
61
62    /// The format to return a response in.
63    pub fn format(mut self, format: FormatType) -> Self {
64        self.format = Some(format);
65        self
66    }
67
68    /// Used to control how long a model stays loaded in memory, by default models are unloaded after 5 minutes of inactivity
69    pub fn keep_alive(mut self, keep_alive: KeepAlive) -> Self {
70        self.keep_alive = Some(keep_alive);
71        self
72    }
73
74    /// Tools that are available to the LLM.
75    pub fn tools(mut self, tools: Vec<ToolInfo>) -> Self {
76        self.tools = tools;
77        self
78    }
79
80    /// Used to control whether thinking/reasoning models will think before responding
81    pub fn think(mut self, think: bool) -> Self {
82        self.think = Some(think);
83        self
84    }
85}