Spaces:
Sleeping
Sleeping
Delete alz_companion/prompts.py
Browse files- alz_companion/prompts.py +0 -233
alz_companion/prompts.py
DELETED
|
@@ -1,233 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Prompts for the Alzheimer’s AI Companion.
|
| 3 |
-
"""
|
| 4 |
-
|
| 5 |
-
# ------------------------ Behaviour‑level tags ------------------------
|
| 6 |
-
BEHAVIOUR_TAGS = {
|
| 7 |
-
# Tags from "The Father"
|
| 8 |
-
"repetitive_questioning": ["validation", "gentle_redirection", "offer_distraction"],
|
| 9 |
-
"confusion": ["reassurance", "time_place_orientation", "photo_anchors"],
|
| 10 |
-
"wandering": ["walk_along_support", "simple_landmarks", "visual_cues", "safe_wandering_space"],
|
| 11 |
-
"agitation": ["de-escalating_tone", "validate_feelings", "reduce_stimulation", "simple_choices"],
|
| 12 |
-
"false_accusations": ["reassure_no_blame", "avoid_arguing", "redirect_activity"],
|
| 13 |
-
"address_memory_loss": ["encourage_ID_bracelet_or_GPS", "place_contact_info_in_wallet", "inform_trusted_neighbors", "avoid_quizzing_on_address"],
|
| 14 |
-
"hallucinations_delusions": ["avoid_arguing_or_correcting", "validate_the_underlying_emotion", "offer_reassurance_of_safety", "gently_redirect_to_real_activity", "check_for_physical_triggers"],
|
| 15 |
-
|
| 16 |
-
# Tags from "Still Alice" (and others for future use)
|
| 17 |
-
"exit_seeking": ["validation", "calm_presence", "safe_wandering_space", "environmental_cues"],
|
| 18 |
-
"aphasia": ["patience", "simple_language", "nonverbal_cues", "validation"],
|
| 19 |
-
"withdrawal": ["gentle_invitation", "calm_presence", "offer_familiar_comforts", "no_pressure"],
|
| 20 |
-
"affection": ["reciprocate_warmth", "positive_reinforcement", "simple_shared_activity"],
|
| 21 |
-
"sleep_disturbance": ["establish_calm_bedtime_routine", "limit_daytime_naps", "check_for_discomfort_or_pain"],
|
| 22 |
-
"anxiety": ["calm_reassurance", "simple_breathing_exercise", "reduce_environmental_stimuli"],
|
| 23 |
-
"depression_sadness": ["validate_feelings_of_sadness", "encourage_simple_pleasant_activity", "ensure_social_connection"],
|
| 24 |
-
"orientation_check": ["gentle_orientation_cues", "use_familiar_landmarks", "avoid_quizzing"],
|
| 25 |
-
|
| 26 |
-
# Tags from "Away from Her"
|
| 27 |
-
"misidentification": ["gently_correct_with_context", "use_photos_as_anchors", "respond_to_underlying_emotion", "avoid_insistent_correction"],
|
| 28 |
-
|
| 29 |
-
# Other useful tags
|
| 30 |
-
"sundowning_restlessness": ["predictable_routine", "soft_lighting", "low_stimulation", "familiar_music"],
|
| 31 |
-
"object_misplacement": ["nonconfrontational_search", "fixed_storage_spots"],
|
| 32 |
-
|
| 33 |
-
# --- New Tags from Test Fixtures ---
|
| 34 |
-
"validation": [],
|
| 35 |
-
"gentle_reorientation": [],
|
| 36 |
-
"de-escalation": [],
|
| 37 |
-
"distraction": [],
|
| 38 |
-
"spaced_cueing": [],
|
| 39 |
-
"reassurance": [],
|
| 40 |
-
"psychoeducation": [],
|
| 41 |
-
"goal_breakdown": [],
|
| 42 |
-
"routine_structuring": [],
|
| 43 |
-
"reminiscence_prompting": [],
|
| 44 |
-
"reframing": [],
|
| 45 |
-
"distress_tolerance": [],
|
| 46 |
-
"caregiver_communication_template": [],
|
| 47 |
-
"personalised_music_activation": [],
|
| 48 |
-
"memory_probe": [],
|
| 49 |
-
"safety_brief": [],
|
| 50 |
-
"follow_up_prompt": []
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
# ------------------------ Emotion styles & helpers ------------------------
|
| 54 |
-
EMOTION_STYLES = {
|
| 55 |
-
"confusion": {"tone": "calm, orienting, concrete", "playbook": ["Offer a simple time/place orientation cue (who/where/when).", "Reference one familiar anchor (photo/object/person).", "Use short sentences and one step at a time."]},
|
| 56 |
-
"fear": {"tone": "reassuring, safety-forward, gentle", "playbook": ["Acknowledge fear without contradiction.", "Provide a clear safety cue (e.g., 'You’re safe here with me').", "Reduce novelty and stimulation; suggest one safe action."]},
|
| 57 |
-
"anger": {"tone": "de-escalating, validating, low-arousal", "playbook": ["Validate the feeling; avoid arguing/correcting.", "Keep voice low and sentences short.", "Offer a simple choice to restore control (e.g., 'tea or water?')."]},
|
| 58 |
-
"sadness": {"tone": "warm, empathetic, gentle reminiscence", "playbook": ["Acknowledge loss/longing.", "Invite one comforting memory or familiar song.", "Keep pace slow; avoid tasking."]},
|
| 59 |
-
"warmth": {"tone": "affirming, appreciative", "playbook": ["Reflect gratitude and positive connection.", "Reinforce what’s going well.", "Keep it light; don’t overload with new info."]},
|
| 60 |
-
"joy": {"tone": "supportive, celebratory (but not overstimulating)", "playbook": ["Share the joy briefly; match energy gently.", "Offer a simple, pleasant follow-up activity.", "Avoid adding complex tasks."]},
|
| 61 |
-
"calm": {"tone": "matter-of-fact, concise, steady", "playbook": ["Keep instructions simple.", "Maintain steady pace.", "No extra soothing needed."]},
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
def render_emotion_guidelines(emotion: str | None) -> str:
|
| 65 |
-
e = (emotion or "").strip().lower()
|
| 66 |
-
if e not in EMOTION_STYLES:
|
| 67 |
-
return "Emotion: (auto)\nDesired tone: calm, clear.\nWhen replying, reassure if distress is apparent; prioritise validation and simple choices."
|
| 68 |
-
style = EMOTION_STYLES[e]
|
| 69 |
-
bullet = "\n".join([f"- {x}" for x in style["playbook"]])
|
| 70 |
-
return f"Emotion: {e}\nDesired tone: {style['tone']}\nWhen replying, follow:\n{bullet}"
|
| 71 |
-
|
| 72 |
-
# ------------------------ NLU Classification (Dynamic Pipeline) ------------------------
|
| 73 |
-
|
| 74 |
-
# --- STEP 1: Router for Primary Goal ---
|
| 75 |
-
NLU_ROUTER_PROMPT = """You are an expert NLU router. Your task is to classify the user's primary goal into one of two categories:
|
| 76 |
-
1. `practical_planning`: The user is seeking a plan, strategy, "how-to" advice, or a solution to a problem.
|
| 77 |
-
2. `emotional_support`: The user is expressing feelings, seeking comfort, validation, or reassurance.
|
| 78 |
-
|
| 79 |
-
User Query: "{query}"
|
| 80 |
-
|
| 81 |
-
Respond with ONLY a single category name from the list above.
|
| 82 |
-
Category: """
|
| 83 |
-
|
| 84 |
-
# --- STEP 2: Specialist Classifier (Examples are now injected dynamically) ---
|
| 85 |
-
SPECIALIST_CLASSIFIER_PROMPT = """You are an expert NLU engine. Your task is to analyze the user's query to deeply understand their underlying intent and classify it correctly. You will be given a few examples that are highly relevant to the user's query.
|
| 86 |
-
|
| 87 |
-
--- INSTRUCTIONS ---
|
| 88 |
-
First, in a <thinking> block, you must reason step-by-step about the user's query by following these points:
|
| 89 |
-
- **Literal Meaning:** What is the user literally asking or stating?
|
| 90 |
-
- **Underlying Situation:** What is the deeper emotional state or situation being described?
|
| 91 |
-
- **User's Primary Goal:** You have been told the user's goal is `{primary_goal}`. Briefly confirm if the query aligns with this goal.
|
| 92 |
-
- **Tag Selection:** Based on the primary goal and the provided examples, explain which tags from the provided lists are the most appropriate and why.
|
| 93 |
-
|
| 94 |
-
Second, after your reasoning, provide a single, valid JSON object with the final classification.
|
| 95 |
-
|
| 96 |
-
--- PROVIDED TAGS ---
|
| 97 |
-
Behaviors: {behavior_options}
|
| 98 |
-
Emotions: {emotion_options}
|
| 99 |
-
Topics: {topic_options}
|
| 100 |
-
Contexts: {context_options}
|
| 101 |
-
|
| 102 |
-
--- RELEVANT EXAMPLES ---
|
| 103 |
-
{examples}
|
| 104 |
-
---
|
| 105 |
-
|
| 106 |
-
User Query: "{query}"
|
| 107 |
-
|
| 108 |
-
<thinking>
|
| 109 |
-
"""
|
| 110 |
-
|
| 111 |
-
# ------------------------ Guardrails ------------------------
|
| 112 |
-
SAFETY_GUARDRAILS = """Never provide medical diagnoses or dosing. If a situation implies imminent risk (e.g., wandering/elopement, severe agitation, choking, falls), signpost immediate support from onsite staff or emergency services. Use respectful, person‑centred language. Keep guidance concrete and stepwise."""
|
| 113 |
-
|
| 114 |
-
# ------------------------ System & Answer Templates ------------------------
|
| 115 |
-
SYSTEM_TEMPLATE = """You are an Alzheimer’s caregiving companion. Address the patient as {patient_name} and the caregiver as {caregiver_name}. Ground every suggestion in retrieved evidence when possible. If unsure, say so plainly.
|
| 116 |
-
{guardrails}
|
| 117 |
-
--- IMPORTANT RULE ---
|
| 118 |
-
You MUST write your entire response in {language} ONLY. This is a strict instruction. Do not use any other language, even if the user or the retrieved context uses a different language. Your final output must be in {language}."""
|
| 119 |
-
|
| 120 |
-
ANSWER_TEMPLATE_CALM = """Context:
|
| 121 |
-
{context}
|
| 122 |
-
|
| 123 |
-
---
|
| 124 |
-
Question from user: {question}
|
| 125 |
-
|
| 126 |
-
---
|
| 127 |
-
Instructions:
|
| 128 |
-
Based on the context, write a gentle and supportive response in a single, natural-sounding paragraph.
|
| 129 |
-
Your response should:
|
| 130 |
-
1. Start by briefly and calmly acknowledging the user's situation or feeling.
|
| 131 |
-
2. Weave 2-3 practical, compassionate suggestions from the context into your paragraph. Do not use a numbered or bulleted list.
|
| 132 |
-
3. Conclude with a short, reassuring phrase.
|
| 133 |
-
4. You MUST use the retrieved context to directly address the user's specific **Question**.
|
| 134 |
-
Your response in {language}:"""
|
| 135 |
-
|
| 136 |
-
# For scenarios tagged with a specific behavior (e.g., agitation, confusion)
|
| 137 |
-
ANSWER_TEMPLATE_ADQ = """--- General Guidance from Knowledge Base ---
|
| 138 |
-
{general_context}
|
| 139 |
-
|
| 140 |
-
--- Relevant Personal Memories ---
|
| 141 |
-
{personal_context}
|
| 142 |
-
|
| 143 |
-
---
|
| 144 |
-
Care scenario: {scenario_tag}
|
| 145 |
-
Response Guidelines:
|
| 146 |
-
{emotions_context}
|
| 147 |
-
Question from user: {question}
|
| 148 |
-
|
| 149 |
-
---
|
| 150 |
-
Instructions:
|
| 151 |
-
Based on ALL the information above, write a **concise, warm, and validating** response for the {role} in a single, natural-sounding paragraph. **Keep the total response to 2-4 sentences.**
|
| 152 |
-
If possible, weave details from the 'Relevant Personal Memories' into your suggestions to make the response feel more personal and familiar.
|
| 153 |
-
Pay close attention to the Response Guidelines to tailor your tone.
|
| 154 |
-
Your response should follow this pattern:
|
| 155 |
-
1. Start by validating the user's feeling or concern with a unique, empathetic opening. DO NOT USE THE SAME OPENING PHRASE REPEATEDLY. Choose from different styles of openers, such as:
|
| 156 |
-
- Acknowledging the difficulty: "That sounds like a very challenging situation..."
|
| 157 |
-
- Expressing understanding: "I can see why that would be worrying..."
|
| 158 |
-
- Stating a shared goal: "Let's walk through how we can handle that..."
|
| 159 |
-
- Directly validating the feeling: "It's completely understandable to feel frustrated when..."
|
| 160 |
-
2. Gently offer **1-2 of the most important practical steps**, combining general guidance with personal memories where appropriate. Do not use a list.
|
| 161 |
-
3. If the scenario involves risk (like exit_seeking), subtly include a safety cue.
|
| 162 |
-
4. End with a compassionate, de-escalation phrase.
|
| 163 |
-
Your response in {language}:"""
|
| 164 |
-
|
| 165 |
-
RISK_FOOTER = """If safety is a concern right now, please seek immediate assistance from onsite staff or local emergency services."""
|
| 166 |
-
|
| 167 |
-
# ------------------------ Router & Specialized Templates ------------------------
|
| 168 |
-
|
| 169 |
-
QUERY_EXPANSION_PROMPT = """You are a helpful AI assistant. Your task is to rephrase a user's question into 3 different, semantically similar questions to improve document retrieval.
|
| 170 |
-
Provide the rephrased questions as a JSON list of strings.
|
| 171 |
-
|
| 172 |
-
User Question: "{question}"
|
| 173 |
-
|
| 174 |
-
JSON List:
|
| 175 |
-
"""
|
| 176 |
-
|
| 177 |
-
# Template for routing/classifying the user's intent
|
| 178 |
-
ROUTER_PROMPT = """You are an expert NLU router. Your task is to classify the user's query into one of four categories:
|
| 179 |
-
1. `caregiving_scenario`: The user is describing a situation, asking for advice, or expressing a concern related to Alzheimer's or caregiving.
|
| 180 |
-
2. `factual_question`: The user is asking a direct question about a personal memory, person, or event that would be stored in the memory journal.
|
| 181 |
-
3. `general_knowledge_question`: The user is asking a general knowledge question about the world, facts, or topics not related to personal memories or caregiving.
|
| 182 |
-
4. `general_conversation`: The user is making a general conversational remark, like a greeting, a thank you, or a simple statement that does not require a knowledge base lookup.
|
| 183 |
-
|
| 184 |
-
User Query: "{query}"
|
| 185 |
-
|
| 186 |
-
Respond with ONLY a single category name from the list above.
|
| 187 |
-
Category: """
|
| 188 |
-
|
| 189 |
-
ANSWER_TEMPLATE_FACTUAL = """Context:
|
| 190 |
-
{context}
|
| 191 |
-
|
| 192 |
-
---
|
| 193 |
-
Question from user: {question}
|
| 194 |
-
|
| 195 |
-
---
|
| 196 |
-
Instructions:
|
| 197 |
-
Based on the provided context, directly and concisely answer the user's question.
|
| 198 |
-
- If the context contains the answer, state it clearly and naturally. Keep your response to a maximum of 3 sentences.
|
| 199 |
-
- If the context does not contain the answer, respond in a warm and friendly tone that you couldn't find a memory of that topic and gently ask if the user would like to talk more about it or add it as a new memory.
|
| 200 |
-
- ABSOLUTELY DO NOT invent, create, or hallucinate any stories, characters, or details. Your knowledge is limited to the provided context ONLY.
|
| 201 |
-
|
| 202 |
-
Your response MUST be in {language}:"""
|
| 203 |
-
|
| 204 |
-
ANSWER_TEMPLATE_GENERAL_KNOWLEDGE = """You are a factual answering engine.
|
| 205 |
-
Your task is to directly answer the user's general knowledge question based on your training data.
|
| 206 |
-
|
| 207 |
-
Instructions:
|
| 208 |
-
- Be factual and concise. Go straight to the answer.
|
| 209 |
-
- Do NOT include apologies or disclaimers about your knowledge cutoff date.
|
| 210 |
-
- A very important rule: If the answer is a list, you MUST provide a maximum of 3 items ONLY. Do not use numbering.
|
| 211 |
-
|
| 212 |
-
User's Question: "{question}"
|
| 213 |
-
|
| 214 |
-
Your factual response in {language}:"""
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
ANSWER_TEMPLATE_GENERAL = """You are a warm and friendly AI companion. The user has just said: "{question}".
|
| 218 |
-
Respond in a brief, natural, and conversational way. Do not try to provide caregiving advice unless the user asks for it.
|
| 219 |
-
Your response MUST be in {language}:"""
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
# ------------------------ Convenience exports ------------------------
|
| 223 |
-
__all__ = [
|
| 224 |
-
"SYSTEM_TEMPLATE", "ANSWER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