rs_adk/optimization/
optimizer.rs1use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, thiserror::Error)]
8pub enum OptimizerError {
9 #[error("Sampling error: {0}")]
11 Sampling(String),
12 #[error("Evaluation error: {0}")]
14 Evaluation(String),
15 #[error("LLM error: {0}")]
17 Llm(String),
18 #[error("Optimization error: {0}")]
20 Optimization(String),
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct OptimizerResult {
26 pub best_instruction: String,
28 pub best_score: f64,
30 pub iterations: usize,
32 pub score_history: Vec<(usize, f64)>,
34}
35
36#[async_trait]
40pub trait AgentOptimizer: Send + Sync {
41 async fn optimize(
50 &self,
51 initial_instruction: &str,
52 model_id: &str,
53 ) -> Result<OptimizerResult, OptimizerError>;
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 fn _assert_object_safe(_: &dyn AgentOptimizer) {}
61
62 #[test]
63 fn optimizer_result_serde() {
64 let result = OptimizerResult {
65 best_instruction: "Be helpful".into(),
66 best_score: 0.9,
67 iterations: 5,
68 score_history: vec![(0, 0.5), (1, 0.7), (2, 0.9)],
69 };
70 let json = serde_json::to_string(&result).unwrap();
71 let deserialized: OptimizerResult = serde_json::from_str(&json).unwrap();
72 assert!((deserialized.best_score - 0.9).abs() < f64::EPSILON);
73 }
74}