foundation_models/generation/
mod.rs1use crate::ffi;
4
5#[derive(Debug, Clone, Copy, PartialEq)]
7#[non_exhaustive]
8#[derive(Default)]
9pub enum SamplingMode {
10 #[default]
12 Default,
13 Greedy,
15 TopK(u32),
17 TopP(f64),
20}
21
22#[derive(Debug, Clone, Copy, Default)]
36pub struct GenerationOptions {
37 temperature: Option<f64>,
38 max_tokens: Option<u32>,
39 sampling: SamplingMode,
40}
41
42impl GenerationOptions {
43 #[must_use]
45 pub const fn new() -> Self {
46 Self {
47 temperature: None,
48 max_tokens: None,
49 sampling: SamplingMode::Default,
50 }
51 }
52
53 #[must_use]
56 pub const fn with_temperature(mut self, temperature: f64) -> Self {
57 self.temperature = Some(temperature);
58 self
59 }
60
61 #[must_use]
63 pub const fn with_maximum_response_tokens(mut self, tokens: u32) -> Self {
64 self.max_tokens = Some(tokens);
65 self
66 }
67
68 #[must_use]
70 pub const fn with_sampling(mut self, mode: SamplingMode) -> Self {
71 self.sampling = mode;
72 self
73 }
74
75 pub(crate) fn to_ffi(self) -> ffi::FFIGenerationOptions {
77 let (mode_code, top_k, top_p) = match self.sampling {
78 SamplingMode::Default => (0, 0, 0.0),
79 SamplingMode::Greedy => (1, 0, 0.0),
80 SamplingMode::TopK(k) => (2, i32::try_from(k).unwrap_or(i32::MAX), 0.0),
81 SamplingMode::TopP(p) => (3, 0, p),
82 };
83 ffi::FFIGenerationOptions {
84 temperature: self.temperature.unwrap_or(f64::NAN),
85 maximum_response_tokens: self
86 .max_tokens
87 .map_or(0, |t| i32::try_from(t).unwrap_or(i32::MAX)),
88 sampling_mode: mode_code,
89 top_k,
90 top_p,
91 }
92 }
93}