Skip to main content

llm/builder/
llm_builder.rs

1use secrecy::SecretString;
2
3use crate::chat::ReasoningEffort;
4
5use super::{backend::LLMBackend, state::BuilderState};
6
7/// Builder for configuring and instantiating LLM providers.
8pub struct LLMBuilder {
9    pub(super) state: BuilderState,
10}
11
12impl Default for LLMBuilder {
13    fn default() -> Self {
14        Self {
15            state: BuilderState::new(),
16        }
17    }
18}
19
20impl LLMBuilder {
21    /// Creates a new empty builder instance with default values.
22    pub fn new() -> Self {
23        Self::default()
24    }
25
26    /// Sets the backend provider to use.
27    pub fn backend(mut self, backend: LLMBackend) -> Self {
28        self.state.backend = Some(backend);
29        self
30    }
31
32    /// Sets the API key for authentication.
33    pub fn api_key(mut self, key: impl Into<String>) -> Self {
34        self.state.api_key = Some(SecretString::new(key.into()));
35        self
36    }
37
38    /// Sets the base URL for API requests.
39    pub fn base_url(mut self, url: impl Into<String>) -> Self {
40        self.state.base_url = Some(url.into());
41        self
42    }
43
44    /// Sets the model identifier to use.
45    pub fn model(mut self, model: impl Into<String>) -> Self {
46        self.state.model = Some(model.into());
47        self
48    }
49
50    /// Sets the maximum number of tokens to generate.
51    pub fn max_tokens(mut self, max_tokens: u32) -> Self {
52        self.state.max_tokens = Some(max_tokens);
53        self
54    }
55
56    /// Sets the temperature for controlling response randomness (0.0-1.0).
57    pub fn temperature(mut self, temperature: f32) -> Self {
58        self.state.temperature = Some(temperature);
59        self
60    }
61
62    /// Sets the system prompt/context.
63    pub fn system(mut self, system: impl Into<String>) -> Self {
64        self.state.system = Some(system.into());
65        self
66    }
67
68    /// Sets the reasoning effort level.
69    pub fn reasoning_effort(mut self, reasoning_effort: ReasoningEffort) -> Self {
70        self.state.reasoning_effort = Some(reasoning_effort.to_string());
71        self
72    }
73
74    /// Sets the reasoning flag.
75    pub fn reasoning(mut self, reasoning: bool) -> Self {
76        self.state.reasoning = Some(reasoning);
77        self
78    }
79
80    /// Sets the reasoning budget tokens.
81    pub fn reasoning_budget_tokens(mut self, reasoning_budget_tokens: u32) -> Self {
82        self.state.reasoning_budget_tokens = Some(reasoning_budget_tokens);
83        self
84    }
85
86    /// Sets the request timeout in seconds.
87    pub fn timeout_seconds(mut self, timeout_seconds: u64) -> Self {
88        self.state.timeout_seconds = Some(timeout_seconds);
89        self
90    }
91
92    /// No-op for compatibility (streaming handled by provider traits).
93    pub fn stream(self, _stream: bool) -> Self {
94        self
95    }
96
97    /// Sets whether to normalize responses.
98    pub fn normalize_response(mut self, normalize_response: bool) -> Self {
99        self.state.normalize_response = Some(normalize_response);
100        self
101    }
102
103    /// Sets the top_p sampling parameter.
104    pub fn top_p(mut self, top_p: f32) -> Self {
105        self.state.top_p = Some(top_p);
106        self
107    }
108
109    /// Sets the top_k sampling parameter.
110    pub fn top_k(mut self, top_k: u32) -> Self {
111        self.state.top_k = Some(top_k);
112        self
113    }
114}