adk_eval/personas/
simulator.rs1use std::sync::Arc;
4
5use adk_core::model::{Llm, LlmRequest};
6use adk_core::types::Content;
7use futures::StreamExt;
8
9use super::profile::PersonaProfile;
10use crate::error::{EvalError, Result};
11
12pub struct UserSimulator {
41 llm: Arc<dyn Llm>,
43 persona: PersonaProfile,
45}
46
47impl UserSimulator {
48 pub fn new(llm: Arc<dyn Llm>, persona: PersonaProfile) -> Self {
50 Self { llm, persona }
51 }
52
53 pub fn persona(&self) -> &PersonaProfile {
55 &self.persona
56 }
57
58 pub async fn generate_message(&self, history: &[Content]) -> Result<Content> {
64 let system_prompt = self.build_system_prompt();
65
66 let mut contents = Vec::with_capacity(history.len() + 1);
67
68 contents.push(Content::new("user").with_text(&system_prompt));
70 contents
71 .push(Content::new("model").with_text("Understood. I will role-play as this persona."));
72
73 contents.extend_from_slice(history);
75
76 if history.is_empty() {
78 contents.push(
79 Content::new("user")
80 .with_text("Start the conversation as this persona. Send your first message."),
81 );
82 } else {
83 contents.push(
84 Content::new("user").with_text(
85 "Continue the conversation as this persona. Send your next message.",
86 ),
87 );
88 }
89
90 let request = LlmRequest::new(self.llm.name(), contents);
91
92 let mut stream = self
93 .llm
94 .generate_content(request, false)
95 .await
96 .map_err(|e| EvalError::ExecutionError(format!("LLM generation failed: {e}")))?;
97
98 let mut response_text = String::new();
100 while let Some(result) = stream.next().await {
101 let response =
102 result.map_err(|e| EvalError::ExecutionError(format!("LLM stream error: {e}")))?;
103 if let Some(content) = &response.content {
104 for part in &content.parts {
105 if let Some(text) = part.text() {
106 response_text.push_str(text);
107 }
108 }
109 }
110 }
111
112 if response_text.is_empty() {
113 return Err(EvalError::ExecutionError(
114 "LLM returned empty response for persona message".to_string(),
115 ));
116 }
117
118 Ok(Content::new("user").with_text(response_text))
119 }
120
121 fn build_system_prompt(&self) -> String {
123 let persona = &self.persona;
124 let traits = &persona.traits;
125
126 let mut prompt = format!(
127 "You are role-playing as a user persona named \"{}\".\n\
128 Description: {}\n\n\
129 Communication style: {}\n\
130 Verbosity: {:?}\n\
131 Expertise level: {:?}\n",
132 persona.name,
133 persona.description,
134 traits.communication_style,
135 traits.verbosity,
136 traits.expertise_level,
137 );
138
139 if !persona.goals.is_empty() {
140 prompt.push_str("\nGoals (pursue these during the conversation):\n");
141 for goal in &persona.goals {
142 prompt.push_str(&format!("- {goal}\n"));
143 }
144 }
145
146 if !persona.constraints.is_empty() {
147 prompt.push_str("\nConstraints (always respect these):\n");
148 for constraint in &persona.constraints {
149 prompt.push_str(&format!("- {constraint}\n"));
150 }
151 }
152
153 prompt.push_str(
154 "\nRespond ONLY with the persona's message. \
155 Do not include any meta-commentary, labels, or prefixes. \
156 Stay in character at all times.",
157 );
158
159 prompt
160 }
161}