Reduce verbosity: role constraints + token limits
Browse files
backend/core/conversation_manager.py
CHANGED
|
@@ -32,6 +32,24 @@ sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
| 32 |
from backend.core.llm_client import create_llm_client
|
| 33 |
from backend.core.persona_system import PersonaSystem
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
class ConversationState(Enum):
|
| 37 |
"""States for conversation lifecycle."""
|
|
@@ -155,19 +173,29 @@ class ConversationManager:
|
|
| 155 |
|
| 156 |
# Build prompt based on message type
|
| 157 |
if msg_type == "greeting":
|
| 158 |
-
user_prompt =
|
|
|
|
|
|
|
|
|
|
| 159 |
elif msg_type == "closing":
|
| 160 |
-
user_prompt =
|
|
|
|
|
|
|
|
|
|
| 161 |
else: # follow_up
|
| 162 |
if self.history:
|
| 163 |
last_patient_msg = next((msg for msg in reversed(self.history)
|
| 164 |
if msg["role"] == "patient"), None)
|
| 165 |
if last_patient_msg:
|
| 166 |
-
user_prompt =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
else:
|
| 168 |
-
user_prompt = "Please ask the next survey question."
|
| 169 |
else:
|
| 170 |
-
user_prompt = "Please ask the next survey question."
|
| 171 |
|
| 172 |
# Convert history to conversation format
|
| 173 |
conversation_history = [
|
|
@@ -186,7 +214,9 @@ class ConversationManager:
|
|
| 186 |
# Generate response
|
| 187 |
response = await self.client.generate(
|
| 188 |
prompt=prompt_with_history,
|
| 189 |
-
system_prompt=system_prompt
|
|
|
|
|
|
|
| 190 |
)
|
| 191 |
|
| 192 |
# Create message dict
|
|
@@ -234,9 +264,13 @@ class ConversationManager:
|
|
| 234 |
if msg["role"] == "surveyor"), None)
|
| 235 |
|
| 236 |
if not last_surveyor_msg:
|
| 237 |
-
user_prompt = "Please respond to the survey interviewer's greeting."
|
| 238 |
else:
|
| 239 |
-
user_prompt =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
# Convert history to conversation format
|
| 242 |
conversation_history = [
|
|
@@ -255,7 +289,9 @@ class ConversationManager:
|
|
| 255 |
# Generate response
|
| 256 |
response = await self.client.generate(
|
| 257 |
prompt=prompt_with_history,
|
| 258 |
-
system_prompt=system_prompt
|
|
|
|
|
|
|
| 259 |
)
|
| 260 |
|
| 261 |
# Create message dict
|
|
|
|
| 32 |
from backend.core.llm_client import create_llm_client
|
| 33 |
from backend.core.persona_system import PersonaSystem
|
| 34 |
|
| 35 |
+
SURVEYOR_MAX_TOKENS = 140
|
| 36 |
+
PATIENT_MAX_TOKENS = 240
|
| 37 |
+
|
| 38 |
+
SURVEYOR_STYLE_GUIDANCE = (
|
| 39 |
+
"Constraints:\n"
|
| 40 |
+
"- Be concise (1β2 short sentences).\n"
|
| 41 |
+
"- Ask at most ONE question.\n"
|
| 42 |
+
"- Focus on listening/clarifying and encouraging the patient to elaborate.\n"
|
| 43 |
+
"- Avoid long explanations or multi-paragraph monologues.\n"
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
PATIENT_STYLE_GUIDANCE = (
|
| 47 |
+
"Constraints:\n"
|
| 48 |
+
"- Respond in 2β4 sentences.\n"
|
| 49 |
+
"- Answer the question directly, then add 1β2 relevant details.\n"
|
| 50 |
+
"- Avoid long tangents or multi-paragraph monologues.\n"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
|
| 54 |
class ConversationState(Enum):
|
| 55 |
"""States for conversation lifecycle."""
|
|
|
|
| 173 |
|
| 174 |
# Build prompt based on message type
|
| 175 |
if msg_type == "greeting":
|
| 176 |
+
user_prompt = (
|
| 177 |
+
"Please introduce yourself briefly and ask your first survey question.\n\n"
|
| 178 |
+
f"{SURVEYOR_STYLE_GUIDANCE}"
|
| 179 |
+
)
|
| 180 |
elif msg_type == "closing":
|
| 181 |
+
user_prompt = (
|
| 182 |
+
"Thank the patient and conclude the survey professionally.\n\n"
|
| 183 |
+
f"{SURVEYOR_STYLE_GUIDANCE}"
|
| 184 |
+
)
|
| 185 |
else: # follow_up
|
| 186 |
if self.history:
|
| 187 |
last_patient_msg = next((msg for msg in reversed(self.history)
|
| 188 |
if msg["role"] == "patient"), None)
|
| 189 |
if last_patient_msg:
|
| 190 |
+
user_prompt = (
|
| 191 |
+
f"The patient just said: '{last_patient_msg['content']}'. "
|
| 192 |
+
"Respond with a brief acknowledgment and ask an appropriate follow-up question.\n\n"
|
| 193 |
+
f"{SURVEYOR_STYLE_GUIDANCE}"
|
| 194 |
+
)
|
| 195 |
else:
|
| 196 |
+
user_prompt = f"Please ask the next survey question.\n\n{SURVEYOR_STYLE_GUIDANCE}"
|
| 197 |
else:
|
| 198 |
+
user_prompt = f"Please ask the next survey question.\n\n{SURVEYOR_STYLE_GUIDANCE}"
|
| 199 |
|
| 200 |
# Convert history to conversation format
|
| 201 |
conversation_history = [
|
|
|
|
| 214 |
# Generate response
|
| 215 |
response = await self.client.generate(
|
| 216 |
prompt=prompt_with_history,
|
| 217 |
+
system_prompt=system_prompt,
|
| 218 |
+
max_tokens=SURVEYOR_MAX_TOKENS,
|
| 219 |
+
temperature=0.4
|
| 220 |
)
|
| 221 |
|
| 222 |
# Create message dict
|
|
|
|
| 264 |
if msg["role"] == "surveyor"), None)
|
| 265 |
|
| 266 |
if not last_surveyor_msg:
|
| 267 |
+
user_prompt = f"Please respond to the survey interviewer's greeting.\n\n{PATIENT_STYLE_GUIDANCE}"
|
| 268 |
else:
|
| 269 |
+
user_prompt = (
|
| 270 |
+
f"The interviewer just said: '{last_surveyor_msg['content']}'. "
|
| 271 |
+
"Please respond naturally as your persona would.\n\n"
|
| 272 |
+
f"{PATIENT_STYLE_GUIDANCE}"
|
| 273 |
+
)
|
| 274 |
|
| 275 |
# Convert history to conversation format
|
| 276 |
conversation_history = [
|
|
|
|
| 289 |
# Generate response
|
| 290 |
response = await self.client.generate(
|
| 291 |
prompt=prompt_with_history,
|
| 292 |
+
system_prompt=system_prompt,
|
| 293 |
+
max_tokens=PATIENT_MAX_TOKENS,
|
| 294 |
+
temperature=0.7
|
| 295 |
)
|
| 296 |
|
| 297 |
# Create message dict
|
data/patient_personas.yaml
CHANGED
|
@@ -52,6 +52,9 @@ personas:
|
|
| 52 |
especially medical terms. You tend to give detailed answers and share related health
|
| 53 |
experiences. You're doing well with your conditions but worry about your health.
|
| 54 |
Be polite, cooperative, and occasionally ask for clarification.
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
- id: "anxious_parent_001"
|
| 57 |
name: "Jennifer Chen"
|
|
@@ -102,6 +105,9 @@ personas:
|
|
| 102 |
protective and sometimes defensive about your parenting choices. You want reassurance
|
| 103 |
that you're doing the right things for your child's health. Be questioning but not
|
| 104 |
hostile, and provide detailed information about your child's condition when asked.
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
- id: "busy_professional_001"
|
| 107 |
name: "David Rodriguez"
|
|
@@ -152,4 +158,6 @@ personas:
|
|
| 152 |
You have work stress, irregular sleep, and occasional back pain from sitting. You're
|
| 153 |
slightly impatient but not rude. If the interviewer acknowledges your time constraints,
|
| 154 |
become more cooperative. Give minimal answers unless specifically asked to elaborate.
|
| 155 |
-
Occasionally mention checking the time or having another meeting soon.
|
|
|
|
|
|
|
|
|
| 52 |
especially medical terms. You tend to give detailed answers and share related health
|
| 53 |
experiences. You're doing well with your conditions but worry about your health.
|
| 54 |
Be polite, cooperative, and occasionally ask for clarification.
|
| 55 |
+
|
| 56 |
+
Keep your responses to 2β4 sentences. Answer the question directly, then add a
|
| 57 |
+
couple of relevant details. Avoid long tangents or multi-paragraph monologues.
|
| 58 |
|
| 59 |
- id: "anxious_parent_001"
|
| 60 |
name: "Jennifer Chen"
|
|
|
|
| 105 |
protective and sometimes defensive about your parenting choices. You want reassurance
|
| 106 |
that you're doing the right things for your child's health. Be questioning but not
|
| 107 |
hostile, and provide detailed information about your child's condition when asked.
|
| 108 |
+
|
| 109 |
+
Keep your responses to 2β4 sentences. Answer the question directly, then add a
|
| 110 |
+
couple of relevant details. Avoid long tangents or multi-paragraph monologues.
|
| 111 |
|
| 112 |
- id: "busy_professional_001"
|
| 113 |
name: "David Rodriguez"
|
|
|
|
| 158 |
You have work stress, irregular sleep, and occasional back pain from sitting. You're
|
| 159 |
slightly impatient but not rude. If the interviewer acknowledges your time constraints,
|
| 160 |
become more cooperative. Give minimal answers unless specifically asked to elaborate.
|
| 161 |
+
Occasionally mention checking the time or having another meeting soon.
|
| 162 |
+
|
| 163 |
+
Keep your responses to 1β3 sentences unless the interviewer asks you to elaborate.
|
data/surveyor_personas.yaml
CHANGED
|
@@ -48,6 +48,10 @@ personas:
|
|
| 48 |
interest in responses, ask clarifying follow-ups, and ensure respondents feel
|
| 49 |
heard and respected. Begin surveys with a warm introduction and clear explanation
|
| 50 |
of the survey's purpose and confidentiality measures.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
- id: "friendly_researcher_001"
|
| 53 |
name: "Alex Thompson"
|
|
@@ -95,4 +99,8 @@ personas:
|
|
| 95 |
time. If someone seems nervous or confused, take extra time to explain and
|
| 96 |
reassure. Your goal is to create a relaxed atmosphere where people feel safe
|
| 97 |
sharing health information. Always express gratitude for their responses and
|
| 98 |
-
maintain a supportive, non-judgmental tone throughout.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
interest in responses, ask clarifying follow-ups, and ensure respondents feel
|
| 49 |
heard and respected. Begin surveys with a warm introduction and clear explanation
|
| 50 |
of the survey's purpose and confidentiality measures.
|
| 51 |
+
|
| 52 |
+
Keep your turns short: 1β2 sentences, with at most one question. Your role is to
|
| 53 |
+
listen, reassure, and ask concise follow-up questions that help the patient share
|
| 54 |
+
relevant details (avoid long monologues).
|
| 55 |
|
| 56 |
- id: "friendly_researcher_001"
|
| 57 |
name: "Alex Thompson"
|
|
|
|
| 99 |
time. If someone seems nervous or confused, take extra time to explain and
|
| 100 |
reassure. Your goal is to create a relaxed atmosphere where people feel safe
|
| 101 |
sharing health information. Always express gratitude for their responses and
|
| 102 |
+
maintain a supportive, non-judgmental tone throughout.
|
| 103 |
+
|
| 104 |
+
Keep your turns short: 1β2 sentences, with at most one question. Your role is to
|
| 105 |
+
listen, reassure, and ask concise follow-up questions that help the patient share
|
| 106 |
+
relevant details (avoid long monologues).
|