llm_agent/params.rs
1use crate::{Agent, AgentTool, InstructionParam, Toolkit};
2use llm_sdk::{AudioOptions, LanguageModel, Modality, ReasoningOptions, ResponseFormatOption};
3use std::sync::Arc;
4
5/// Parameters required to create a new agent.
6/// # Default Values
7/// - `instructions`: `vec![]`
8/// - `tools`: `vec![]`
9/// - `response_format`: `ResponseFormatOption::Text`
10/// - `max_turns`: 10
11/// - `temperature`: `None`
12/// - `top_p`: `None`
13/// - `top_k`: `None`
14/// - `presence_penalty`: `None`
15/// - `frequency_penalty`: `None`
16/// - `modalities`: `None`
17/// - `audio`: `None`
18/// - `reasoning`: `None`
19pub struct AgentParams<TCtx> {
20 pub name: String,
21 /// The default language model to use for the agent.
22 pub model: Arc<dyn LanguageModel + Send + Sync>,
23 /// Instructions to be added to system messages when executing the agent.
24 /// This can include formatting instructions or other guidance for the
25 /// agent.
26 pub instructions: Vec<InstructionParam<TCtx>>,
27 /// The tools that the agent can use to perform tasks.
28 pub tools: Vec<Arc<dyn AgentTool<TCtx>>>,
29 /// Optional toolkits that can provide dynamic tools and system prompts for
30 /// each session.
31 pub toolkits: Vec<Arc<dyn Toolkit<TCtx>>>,
32 /// The expected format of the response. Either text or structured output.
33 pub response_format: ResponseFormatOption,
34 /// Max number of turns for agent to run to protect against infinite loops.
35 pub max_turns: usize,
36 /// Amount of randomness injected into the response. Ranges from 0.0 to 1.0
37 pub temperature: Option<f64>,
38 /// An alternative to sampling with temperature, called nucleus sampling,
39 /// where the model considers the results of the tokens with `top_p`
40 /// probability mass. Ranges from 0.0 to 1.0
41 pub top_p: Option<f64>,
42 /// Only sample from the top K options for each subsequent token.
43 /// Used to remove 'long tail' low probability responses.
44 /// Must be a non-negative integer.
45 pub top_k: Option<i32>,
46 /// Positive values penalize new tokens based on whether they appear in the
47 /// text so far, increasing the model's likelihood to talk about new
48 /// topics.
49 pub presence_penalty: Option<f64>,
50 /// Positive values penalize new tokens based on their existing frequency in
51 /// the text so far, decreasing the model's likelihood to repeat the
52 /// same line verbatim.
53 pub frequency_penalty: Option<f64>,
54 /// The modalities that the model should support.
55 pub modalities: Option<Vec<Modality>>,
56 /// Options for audio generation.
57 pub audio: Option<AudioOptions>,
58 /// Options for reasoning generation.
59 pub reasoning: Option<ReasoningOptions>,
60}
61
62impl<TCtx> AgentParams<TCtx>
63where
64 TCtx: Send + Sync + 'static,
65{
66 pub fn new(name: &str, model: Arc<dyn LanguageModel + Send + Sync>) -> Self {
67 Self {
68 name: name.to_string(),
69 model,
70 instructions: Vec::new(),
71 tools: Vec::new(),
72 toolkits: Vec::new(),
73 response_format: ResponseFormatOption::Text,
74 max_turns: 10,
75 temperature: None,
76 top_p: None,
77 top_k: None,
78 presence_penalty: None,
79 frequency_penalty: None,
80 audio: None,
81 reasoning: None,
82 modalities: None,
83 }
84 }
85
86 /// Add an instruction
87 #[must_use]
88 pub fn add_instruction(mut self, instruction: impl Into<InstructionParam<TCtx>>) -> Self {
89 self.instructions.push(instruction.into());
90 self
91 }
92
93 /// Set the instructions
94 #[must_use]
95 pub fn instructions(mut self, instructions: Vec<InstructionParam<TCtx>>) -> Self {
96 self.instructions = instructions;
97 self
98 }
99
100 /// Add a tool
101 #[must_use]
102 pub fn add_tool(mut self, tool: impl AgentTool<TCtx> + 'static) -> Self {
103 self.tools.push(Arc::new(tool));
104 self
105 }
106
107 /// Add a toolkit
108 #[must_use]
109 pub fn add_toolkit(mut self, toolkit: impl Toolkit<TCtx> + 'static) -> Self {
110 self.toolkits.push(Arc::new(toolkit));
111 self
112 }
113
114 /// Set the toolkits
115 #[must_use]
116 pub fn toolkits(mut self, toolkits: Vec<Arc<dyn Toolkit<TCtx>>>) -> Self {
117 self.toolkits = toolkits;
118 self
119 }
120
121 /// Set the response format
122 #[must_use]
123 pub fn response_format(mut self, response_format: ResponseFormatOption) -> Self {
124 self.response_format = response_format;
125 self
126 }
127
128 /// Set the max turns
129 #[must_use]
130 pub fn max_turns(mut self, max_turns: usize) -> Self {
131 self.max_turns = max_turns;
132 self
133 }
134
135 /// Set the temperature for sampling
136 /// Amount of randomness injected into the response. Ranges from 0.0 to 1.0
137 #[must_use]
138 pub fn temperature(mut self, temperature: f64) -> Self {
139 self.temperature = Some(temperature);
140 self
141 }
142
143 /// Set the `top_p` for nucleus sampling
144 /// An alternative to sampling with temperature, called nucleus sampling,
145 /// where the model considers the results of the tokens with `top_p`
146 /// probability mass. Ranges from 0.0 to 1.0
147 #[must_use]
148 pub fn top_p(mut self, top_p: f64) -> Self {
149 self.top_p = Some(top_p);
150 self
151 }
152
153 /// Set the `top_k` for sampling
154 /// Only sample from the top K options for each subsequent token.
155 /// Used to remove 'long tail' low probability responses.
156 /// Must be a non-negative integer.
157 #[must_use]
158 pub fn top_k(mut self, top_k: i32) -> Self {
159 self.top_k = Some(top_k);
160 self
161 }
162
163 /// Set the presence penalty
164 /// Positive values penalize new tokens based on whether they appear in the
165 /// text so far, increasing the model's likelihood to talk about new
166 /// topics.
167 #[must_use]
168 pub fn presence_penalty(mut self, presence_penalty: f64) -> Self {
169 self.presence_penalty = Some(presence_penalty);
170 self
171 }
172
173 /// Set the frequency penalty
174 /// Positive values penalize new tokens based on their existing frequency in
175 /// the text so far, decreasing the model's likelihood to repeat the
176 /// same line verbatim.
177 #[must_use]
178 pub fn frequency_penalty(mut self, frequency_penalty: f64) -> Self {
179 self.frequency_penalty = Some(frequency_penalty);
180 self
181 }
182
183 /// Set the modalities that the model should support.
184 #[must_use]
185 pub fn modalities(mut self, modalities: Vec<Modality>) -> Self {
186 self.modalities = Some(modalities);
187 self
188 }
189
190 /// Set the audio options for generation.
191 #[must_use]
192 pub fn audio(mut self, audio: AudioOptions) -> Self {
193 self.audio = Some(audio);
194 self
195 }
196
197 /// Set the reasoning options for generation.
198 #[must_use]
199 pub fn reasoning(mut self, reasoning: ReasoningOptions) -> Self {
200 self.reasoning = Some(reasoning);
201 self
202 }
203
204 #[must_use]
205 pub fn build(self) -> Agent<TCtx> {
206 Agent::new(self)
207 }
208}