Skip to main content

ollama_oxide/inference/
model_options.rs

1//! Model options primitive type
2
3use serde::{Deserialize, Serialize};
4
5use super::StopSetting;
6
7/// Runtime options that control model behavior
8///
9/// These options can be used to customize embedding generation.
10/// All fields are optional and will use model defaults if not specified.
11///
12/// # Example
13///
14/// ```no_run
15/// use ollama_oxide::ModelOptions;
16///
17/// let options = ModelOptions::default()
18///     .with_temperature(0.7)
19///     .with_num_ctx(4096);
20/// ```
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
22pub struct ModelOptions {
23    /// Random seed for reproducible outputs
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub seed: Option<i64>,
26
27    /// Controls randomness in generation (higher = more random)
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub temperature: Option<f32>,
30
31    /// Limits next token selection to the K most likely
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub top_k: Option<i32>,
34
35    /// Cumulative probability threshold for nucleus sampling
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub top_p: Option<f32>,
38
39    /// Minimum probability threshold for token selection
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub min_p: Option<f32>,
42
43    /// Context length size (number of tokens)
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub num_ctx: Option<i32>,
46
47    /// Maximum number of tokens to generate
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub num_predict: Option<i32>,
50
51    /// Stop sequences that will halt generation
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub stop: Option<StopSetting>,
54}
55
56impl ModelOptions {
57    /// Create empty options (all defaults)
58    pub fn new() -> Self {
59        Self::default()
60    }
61
62    /// Set the random seed
63    pub fn with_seed(mut self, seed: i64) -> Self {
64        self.seed = Some(seed);
65        self
66    }
67
68    /// Set the temperature
69    pub fn with_temperature(mut self, temperature: f32) -> Self {
70        self.temperature = Some(temperature);
71        self
72    }
73
74    /// Set the top_k value
75    pub fn with_top_k(mut self, top_k: i32) -> Self {
76        self.top_k = Some(top_k);
77        self
78    }
79
80    /// Set the top_p value
81    pub fn with_top_p(mut self, top_p: f32) -> Self {
82        self.top_p = Some(top_p);
83        self
84    }
85
86    /// Set the min_p value
87    pub fn with_min_p(mut self, min_p: f32) -> Self {
88        self.min_p = Some(min_p);
89        self
90    }
91
92    /// Set the context length
93    pub fn with_num_ctx(mut self, num_ctx: i32) -> Self {
94        self.num_ctx = Some(num_ctx);
95        self
96    }
97
98    /// Set the max tokens to generate
99    pub fn with_num_predict(mut self, num_predict: i32) -> Self {
100        self.num_predict = Some(num_predict);
101        self
102    }
103
104    /// Set stop sequences
105    pub fn with_stop(mut self, stop: impl Into<StopSetting>) -> Self {
106        self.stop = Some(stop.into());
107        self
108    }
109
110    /// Check if any options are set
111    pub fn is_empty(&self) -> bool {
112        self.seed.is_none()
113            && self.temperature.is_none()
114            && self.top_k.is_none()
115            && self.top_p.is_none()
116            && self.min_p.is_none()
117            && self.num_ctx.is_none()
118            && self.num_predict.is_none()
119            && self.stop.is_none()
120    }
121}