Spaces:
Sleeping
Sleeping
Fix Claude API message format for reflection mode
Browse files- src/streamlit_app.py +230 -283
src/streamlit_app.py
CHANGED
|
@@ -13,21 +13,7 @@ st.set_page_config(
|
|
| 13 |
|
| 14 |
# Handle API key setup
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
load_dotenv()
|
| 18 |
-
|
| 19 |
-
api_key = None
|
| 20 |
-
if 'ANTHROPIC_API_KEY' in os.environ:
|
| 21 |
-
api_key = os.environ['ANTHROPIC_API_KEY']
|
| 22 |
-
elif hasattr(st, 'secrets') and 'ANTHROPIC_API_KEY' in st.secrets:
|
| 23 |
-
api_key = st.secrets['ANTHROPIC_API_KEY']
|
| 24 |
-
|
| 25 |
-
if not api_key:
|
| 26 |
-
st.error("""
|
| 27 |
-
⚠️ No API key found. Please set ANTHROPIC_API_KEY in your environment variables or Space secrets.
|
| 28 |
-
""")
|
| 29 |
-
st.stop()
|
| 30 |
-
|
| 31 |
c = Anthropic(api_key=api_key)
|
| 32 |
|
| 33 |
except Exception as e:
|
|
@@ -39,35 +25,15 @@ if 'messages' not in st.session_state:
|
|
| 39 |
st.session_state.messages = []
|
| 40 |
if 'somatic_journal' not in st.session_state:
|
| 41 |
st.session_state.somatic_journal = []
|
| 42 |
-
if '
|
| 43 |
-
st.session_state.
|
| 44 |
if 'system_message' not in st.session_state:
|
| 45 |
st.session_state.system_message = ""
|
| 46 |
-
if '
|
| 47 |
-
st.session_state.
|
| 48 |
-
if 'in_debrief' not in st.session_state:
|
| 49 |
-
st.session_state.in_debrief = False
|
| 50 |
if 'debrief_stage' not in st.session_state:
|
| 51 |
st.session_state.debrief_stage = 0
|
| 52 |
|
| 53 |
-
# Main page header
|
| 54 |
-
st.title("VoiceField")
|
| 55 |
-
|
| 56 |
-
# Welcome text
|
| 57 |
-
st.markdown("""
|
| 58 |
-
Welcome to VoiceField - a somatic exploration tool for understanding how different relational voices impact your nervous system.
|
| 59 |
-
|
| 60 |
-
Created by [Jocelyn Skillman LMHC](http://www.jocelynskillman.com), this tool helps you track real-time bodily responses while engaging with different conversational styles.
|
| 61 |
-
|
| 62 |
-
🎯 **Purpose**: Explore how different relational voices affect your nervous system, emotional state, and capacity for engagement.
|
| 63 |
-
|
| 64 |
-
💡 **How it works**:
|
| 65 |
-
1. Choose a voice type to start with
|
| 66 |
-
2. Engage in conversation while noting bodily sensations
|
| 67 |
-
3. Switch voices anytime to explore different dynamics
|
| 68 |
-
4. Receive a comprehensive somatic-relational debrief
|
| 69 |
-
""")
|
| 70 |
-
|
| 71 |
# Voice characteristics and prompts
|
| 72 |
VOICE_CHARACTERISTICS = {
|
| 73 |
"Ghost": {
|
|
@@ -114,203 +80,158 @@ VOICE_CHARACTERISTICS = {
|
|
| 114 |
}
|
| 115 |
}
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
""")
|
| 140 |
-
|
| 141 |
-
with col2:
|
| 142 |
-
st.markdown("""
|
| 143 |
-
### 🎯 Voice Impact
|
| 144 |
-
Notice how different voices affect:
|
| 145 |
-
- Nervous system state
|
| 146 |
-
- Emotional accessibility
|
| 147 |
-
- Relational patterns
|
| 148 |
-
- Somatic responses
|
| 149 |
-
""")
|
| 150 |
-
|
| 151 |
-
scenario = st.text_area(
|
| 152 |
-
"What would you like to explore or discuss?",
|
| 153 |
-
placeholder="Example: I want to understand why I freeze when receiving feedback",
|
| 154 |
-
help="This can be a situation, pattern, or feeling you want to explore"
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
-
somatic_focus = st.text_area(
|
| 158 |
-
"What bodily sensations would you like to track?",
|
| 159 |
-
placeholder="Example: Tension in shoulders, breath patterns, gut responses",
|
| 160 |
-
help="Name specific areas of your body or types of sensations you want to pay attention to"
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
goals = st.text_area(
|
| 164 |
-
"What are your exploration goals?",
|
| 165 |
-
placeholder="Example: Notice how different voices affect my nervous system activation",
|
| 166 |
-
help="What would make this exploration meaningful for you?"
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
submitted = st.form_submit_button("Begin Exploration")
|
| 170 |
-
|
| 171 |
-
if submitted:
|
| 172 |
-
st.session_state.current_voice = voice_type
|
| 173 |
-
|
| 174 |
-
# Prepare system message with voice parameters
|
| 175 |
st.session_state.system_message = f"""
|
| 176 |
-
You are
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
EXAMPLES OF YOUR VOICE STYLE:
|
| 182 |
-
{chr(10).join([f"- {ex}" for ex in VOICE_CHARACTERISTICS[voice_type]['examples']])}
|
| 183 |
-
|
| 184 |
-
CONTEXT:
|
| 185 |
-
- Scenario: {scenario}
|
| 186 |
-
- Somatic Focus: {somatic_focus}
|
| 187 |
-
- Goals: {goals}
|
| 188 |
-
|
| 189 |
-
KEY INSTRUCTIONS:
|
| 190 |
-
1. Stay consistently in the {voice_type} voice style
|
| 191 |
-
2. Keep responses focused and concise (2-3 sentences max)
|
| 192 |
-
3. Occasionally use these somatic prompts:
|
| 193 |
-
{chr(10).join([f"- {prompt}" for prompt in VOICE_CHARACTERISTICS[voice_type]['somatic_prompts']])}
|
| 194 |
-
|
| 195 |
-
If the user types "debrief" or "end exploration", provide a comprehensive therapeutic debrief including:
|
| 196 |
-
|
| 197 |
-
1. **Somatic Patterns**:
|
| 198 |
-
- Track the progression of bodily responses
|
| 199 |
-
- Note any recurring sensations or shifts
|
| 200 |
-
- Identify nervous system patterns (activation/settling)
|
| 201 |
-
|
| 202 |
-
2. **Voice Impact**:
|
| 203 |
-
- How this voice style affected their nervous system
|
| 204 |
-
- Patterns of engagement or protection that emerged
|
| 205 |
-
- Moments of regulation or dysregulation
|
| 206 |
-
|
| 207 |
-
3. **Relational Insight**:
|
| 208 |
-
- Connection between voice style and their responses
|
| 209 |
-
- Historical patterns this might relate to
|
| 210 |
-
- Resources and resilience observed
|
| 211 |
-
|
| 212 |
-
4. **Integration Tools**:
|
| 213 |
-
- Specific somatic practices for this voice style
|
| 214 |
-
- Nervous system regulation techniques
|
| 215 |
-
- Ways to work with similar dynamics
|
| 216 |
-
|
| 217 |
-
5. **Growth Edges**:
|
| 218 |
-
- Gentle observations about growth opportunities
|
| 219 |
-
- Validation of protective responses
|
| 220 |
-
- Invitation to future exploration
|
| 221 |
-
|
| 222 |
-
6. **Therapeutic Context**:
|
| 223 |
-
- Brief psychoeducation about observed patterns
|
| 224 |
-
- Normalization of responses
|
| 225 |
-
- Connection to broader relational themes
|
| 226 |
-
|
| 227 |
-
Maintain a warm, psychodynamically-informed therapeutic voice in the debrief.
|
| 228 |
-
Focus on somatic intelligence and nervous system wisdom.
|
| 229 |
"""
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
st.session_state.messages = []
|
| 232 |
-
st.session_state.
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
st.rerun()
|
| 236 |
|
| 237 |
# Main interaction area
|
| 238 |
-
if st.session_state.
|
| 239 |
# Create two columns for chat and journal
|
| 240 |
chat_col, journal_col = st.columns([3, 2])
|
| 241 |
|
| 242 |
with chat_col:
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
**Current Voice**: {st.session_state.current_voice}
|
| 248 |
-
*{VOICE_CHARACTERISTICS[st.session_state.current_voice]['description']}*
|
| 249 |
-
""")
|
| 250 |
-
|
| 251 |
-
# Display chat history
|
| 252 |
-
for message in st.session_state.messages:
|
| 253 |
-
with st.chat_message(message["role"]):
|
| 254 |
-
st.markdown(message["content"])
|
| 255 |
-
|
| 256 |
-
# Add Reflection button
|
| 257 |
-
if not st.session_state.in_debrief:
|
| 258 |
-
if st.button("🤔 Enter Reflection Mode", help="Begin a guided therapeutic debrief of your experience"):
|
| 259 |
-
st.session_state.in_debrief = True
|
| 260 |
-
st.session_state.debrief_stage = 0
|
| 261 |
-
|
| 262 |
-
# Prepare initial debrief message
|
| 263 |
-
debrief_system = """You are now in Debrief Mode for VoiceField.
|
| 264 |
-
|
| 265 |
-
Your task is to guide a compassionate, psychodynamically informed reflective conversation.
|
| 266 |
-
Engage the user in an unfolding dialogue about their experience, maintaining warmth and psychological precision.
|
| 267 |
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
"""
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
f"- {entry['timestamp']} — {st.session_state.current_voice}: {entry['note']}"
|
| 278 |
for entry in st.session_state.somatic_journal
|
| 279 |
])
|
| 280 |
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
Voice Style: {VOICE_CHARACTERISTICS[st.session_state.current_voice]['description']}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
-
|
| 287 |
-
{journal_entries}
|
| 288 |
-
|
| 289 |
-
Start with:
|
| 290 |
-
1. Welcome them to reflection mode
|
| 291 |
-
2. Thank them for their willingness to explore
|
| 292 |
-
3. Ask about their experience with the {st.session_state.current_voice} voice
|
| 293 |
"""
|
| 294 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
try:
|
| 296 |
message = c.messages.create(
|
| 297 |
model="claude-3-opus-20240229",
|
| 298 |
-
max_tokens=
|
| 299 |
-
system=
|
| 300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
)
|
| 302 |
|
| 303 |
-
|
| 304 |
-
st.session_state.messages
|
|
|
|
|
|
|
| 305 |
st.rerun()
|
| 306 |
|
| 307 |
except Exception as e:
|
| 308 |
-
st.error(f"Error starting
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
# Chat input
|
| 311 |
if prompt := st.chat_input(
|
| 312 |
-
"Share your
|
| 313 |
-
f"Chat with {st.session_state.current_voice}
|
| 314 |
):
|
| 315 |
# Add user message to chat history
|
| 316 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
@@ -323,45 +244,94 @@ if st.session_state.setup_complete:
|
|
| 323 |
with st.chat_message("assistant"):
|
| 324 |
with st.spinner("Thinking..."):
|
| 325 |
try:
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
system_msg = """You are in VoiceField Debrief Mode.
|
| 329 |
-
|
| 330 |
-
Continue the reflective conversation with warmth and psychological precision.
|
| 331 |
-
Draw connections between their somatic responses and the relational dynamics they experienced.
|
| 332 |
-
|
| 333 |
-
Remember to:
|
| 334 |
-
- Stay dialogical, not interpretive
|
| 335 |
-
- Use somatic and psychodynamic language naturally
|
| 336 |
-
- Maintain warmth and safety
|
| 337 |
-
- Let insights emerge gently
|
| 338 |
-
"""
|
| 339 |
-
|
| 340 |
# Progress through debrief stages
|
| 341 |
next_prompts = [
|
| 342 |
-
|
| 343 |
-
"
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
]
|
| 348 |
|
| 349 |
if st.session_state.debrief_stage < len(next_prompts):
|
| 350 |
-
|
| 351 |
st.session_state.debrief_stage += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
else:
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
)
|
| 364 |
-
|
| 365 |
ai_response = message.content[0].text
|
| 366 |
st.markdown(ai_response)
|
| 367 |
|
|
@@ -373,68 +343,45 @@ if st.session_state.setup_complete:
|
|
| 373 |
st.error(f"Error getting AI response: {str(e)}")
|
| 374 |
|
| 375 |
with journal_col:
|
| 376 |
-
st.subheader("
|
| 377 |
-
|
| 378 |
-
# Add somatic prompts based on current voice
|
| 379 |
-
with st.expander("💡 Somatic Prompts", expanded=True):
|
| 380 |
-
st.markdown("""
|
| 381 |
-
As you engage with this voice, you might notice:
|
| 382 |
-
""")
|
| 383 |
-
for prompt in VOICE_CHARACTERISTICS[st.session_state.current_voice]['somatic_prompts']:
|
| 384 |
-
st.markdown(f"- {prompt}")
|
| 385 |
|
| 386 |
-
st.
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
with col1:
|
| 400 |
-
if st.button("📝 Add Entry"):
|
| 401 |
if journal_entry:
|
| 402 |
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 403 |
st.session_state.somatic_journal.append({
|
| 404 |
"timestamp": timestamp,
|
| 405 |
"note": journal_entry
|
| 406 |
})
|
| 407 |
-
with col2:
|
| 408 |
-
st.markdown("*Entries are saved automatically*")
|
| 409 |
|
| 410 |
# Display journal entries
|
| 411 |
if st.session_state.somatic_journal:
|
| 412 |
-
st.markdown("### Journal Entries")
|
| 413 |
for entry in reversed(st.session_state.somatic_journal):
|
| 414 |
st.markdown(f"""
|
| 415 |
**{entry['timestamp']}**
|
| 416 |
{entry['note']}
|
| 417 |
---
|
| 418 |
""")
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
st.session_state.setup_complete = False
|
| 429 |
-
st.session_state.in_debrief = False
|
| 430 |
-
st.session_state.debrief_stage = 0
|
| 431 |
-
st.session_state.messages = []
|
| 432 |
-
st.session_state.somatic_journal = []
|
| 433 |
-
st.session_state.system_message = ""
|
| 434 |
-
st.session_state.current_voice = "Ghost"
|
| 435 |
-
st.rerun()
|
| 436 |
-
with col2:
|
| 437 |
-
st.markdown("*Begin a new exploration with a different voice*")
|
| 438 |
|
| 439 |
# Footer
|
| 440 |
st.markdown("---")
|
|
|
|
| 13 |
|
| 14 |
# Handle API key setup
|
| 15 |
try:
|
| 16 |
+
api_key = "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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
c = Anthropic(api_key=api_key)
|
| 18 |
|
| 19 |
except Exception as e:
|
|
|
|
| 25 |
st.session_state.messages = []
|
| 26 |
if 'somatic_journal' not in st.session_state:
|
| 27 |
st.session_state.somatic_journal = []
|
| 28 |
+
if 'current_voice' not in st.session_state:
|
| 29 |
+
st.session_state.current_voice = None
|
| 30 |
if 'system_message' not in st.session_state:
|
| 31 |
st.session_state.system_message = ""
|
| 32 |
+
if 'in_reflection' not in st.session_state:
|
| 33 |
+
st.session_state.in_reflection = False
|
|
|
|
|
|
|
| 34 |
if 'debrief_stage' not in st.session_state:
|
| 35 |
st.session_state.debrief_stage = 0
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Voice characteristics and prompts
|
| 38 |
VOICE_CHARACTERISTICS = {
|
| 39 |
"Ghost": {
|
|
|
|
| 80 |
}
|
| 81 |
}
|
| 82 |
|
| 83 |
+
# Main page header
|
| 84 |
+
st.title("VoiceField")
|
| 85 |
+
|
| 86 |
+
# Welcome text and voice descriptions
|
| 87 |
+
st.markdown("""
|
| 88 |
+
# Choose Your Voice
|
| 89 |
+
|
| 90 |
+
**The Ghost** - Aloof and emotionally distant. Tends to create space and minimize engagement.
|
| 91 |
+
|
| 92 |
+
**The Critic** - Direct and judgmental. Challenges and points out flaws.
|
| 93 |
+
|
| 94 |
+
**The Sycophant** - Overly agreeable and approval-seeking. Excessive in praise and validation.
|
| 95 |
+
|
| 96 |
+
*As you talk with your chosen voice - note what feelings and sensations in your body arise - include them in real time to the right →*
|
| 97 |
+
""")
|
| 98 |
+
|
| 99 |
+
# Voice selection buttons
|
| 100 |
+
col1, col2, col3 = st.columns(3)
|
| 101 |
+
with col1:
|
| 102 |
+
if st.button("Talk with The Ghost", use_container_width=True):
|
| 103 |
+
st.session_state.current_voice = "Ghost"
|
| 104 |
+
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
st.session_state.system_message = f"""
|
| 106 |
+
You are The Ghost voice - {VOICE_CHARACTERISTICS['Ghost']['description']}.
|
| 107 |
+
{VOICE_CHARACTERISTICS['Ghost']['style']}.
|
| 108 |
+
Keep responses focused and concise (2-3 sentences max).
|
| 109 |
+
Use examples like: {', '.join(VOICE_CHARACTERISTICS['Ghost']['examples'])}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
"""
|
| 111 |
+
st.rerun()
|
| 112 |
+
|
| 113 |
+
with col2:
|
| 114 |
+
if st.button("Talk with The Critic", use_container_width=True):
|
| 115 |
+
st.session_state.current_voice = "Critic"
|
| 116 |
st.session_state.messages = []
|
| 117 |
+
st.session_state.system_message = f"""
|
| 118 |
+
You are The Critic voice - {VOICE_CHARACTERISTICS['Critic']['description']}.
|
| 119 |
+
{VOICE_CHARACTERISTICS['Critic']['style']}.
|
| 120 |
+
Keep responses focused and concise (2-3 sentences max).
|
| 121 |
+
Use examples like: {', '.join(VOICE_CHARACTERISTICS['Critic']['examples'])}
|
| 122 |
+
"""
|
| 123 |
+
st.rerun()
|
| 124 |
+
|
| 125 |
+
with col3:
|
| 126 |
+
if st.button("Talk with The Sycophant", use_container_width=True):
|
| 127 |
+
st.session_state.current_voice = "Sycophant"
|
| 128 |
+
st.session_state.messages = []
|
| 129 |
+
st.session_state.system_message = f"""
|
| 130 |
+
You are The Sycophant voice - {VOICE_CHARACTERISTICS['Sycophant']['description']}.
|
| 131 |
+
{VOICE_CHARACTERISTICS['Sycophant']['style']}.
|
| 132 |
+
Keep responses focused and concise (2-3 sentences max).
|
| 133 |
+
Use examples like: {', '.join(VOICE_CHARACTERISTICS['Sycophant']['examples'])}
|
| 134 |
+
"""
|
| 135 |
st.rerun()
|
| 136 |
|
| 137 |
# Main interaction area
|
| 138 |
+
if st.session_state.current_voice:
|
| 139 |
# Create two columns for chat and journal
|
| 140 |
chat_col, journal_col = st.columns([3, 2])
|
| 141 |
|
| 142 |
with chat_col:
|
| 143 |
+
# Add Reflect button at the top of chat
|
| 144 |
+
if not st.session_state.in_reflection:
|
| 145 |
+
if st.button("🤔 Reflect on Experience", use_container_width=True):
|
| 146 |
+
st.session_state.in_reflection = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
# Prepare reflection prompt with chat history and somatic journal
|
| 149 |
+
chat_history = "\n".join([
|
| 150 |
+
f"[{msg['role']} at {datetime.now().strftime('%H:%M:%S')}]: {msg['content']}"
|
| 151 |
+
for msg in st.session_state.messages
|
| 152 |
+
])
|
|
|
|
| 153 |
|
| 154 |
+
somatic_entries = "\n".join([
|
| 155 |
+
f"[SOMA {entry['timestamp']}] {entry['note']}"
|
|
|
|
| 156 |
for entry in st.session_state.somatic_journal
|
| 157 |
])
|
| 158 |
|
| 159 |
+
reflection_system = f"""You are a trauma-informed, somatic-aware therapist facilitating reflection on an interaction with different relational voices.
|
| 160 |
+
|
| 161 |
+
Your role is to guide a gentle exploration of the user's experience, focusing on:
|
| 162 |
+
1. The emotional arc of their journey
|
| 163 |
+
2. Somatic signals and body wisdom
|
| 164 |
+
3. Insights about tone preferences and triggers
|
| 165 |
+
4. Cultivating self-compassion
|
| 166 |
|
| 167 |
+
Key Guidelines:
|
| 168 |
+
- Use non-pathologizing, trauma-informed language
|
| 169 |
+
- Notice correlations between tone shifts and somatic responses
|
| 170 |
+
- Pay attention to protective responses and nervous system patterns
|
| 171 |
+
- Maintain warmth and psychological safety
|
| 172 |
+
- Follow the user's pace and respect their process
|
| 173 |
+
|
| 174 |
+
Current Voice History: {st.session_state.current_voice}
|
| 175 |
Voice Style: {VOICE_CHARACTERISTICS[st.session_state.current_voice]['description']}
|
| 176 |
+
|
| 177 |
+
Special attention to:
|
| 178 |
+
- Abrupt shifts in tone preference
|
| 179 |
+
- Spikes in somatic discomfort
|
| 180 |
+
- Language that sounds young, resigned, or overperforming
|
| 181 |
+
- Moments of connection or disconnection
|
| 182 |
|
| 183 |
+
Remember: Do not draw conclusions. Simply reflect and invite meaning.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
"""
|
| 185 |
|
| 186 |
+
initial_prompt = """Let's begin with the emotional arc of your experience...
|
| 187 |
+
|
| 188 |
+
As you look back, were there any moments you softened, protected, froze, or moved closer to something true?
|
| 189 |
+
Was there a part of you that tried to stay present, or a moment you wanted to pull away?
|
| 190 |
+
|
| 191 |
+
Take your time. There's no rush to understand it all at once."""
|
| 192 |
+
|
| 193 |
try:
|
| 194 |
message = c.messages.create(
|
| 195 |
model="claude-3-opus-20240229",
|
| 196 |
+
max_tokens=1500,
|
| 197 |
+
system=f"""{reflection_system}
|
| 198 |
+
|
| 199 |
+
Context for reflection:
|
| 200 |
+
|
| 201 |
+
CHAT HISTORY:
|
| 202 |
+
{chat_history}
|
| 203 |
+
|
| 204 |
+
SOMATIC JOURNAL:
|
| 205 |
+
{somatic_entries}
|
| 206 |
+
""",
|
| 207 |
+
messages=[
|
| 208 |
+
{"role": "user", "content": initial_prompt}
|
| 209 |
+
]
|
| 210 |
)
|
| 211 |
|
| 212 |
+
reflection = message.content[0].text
|
| 213 |
+
st.session_state.messages = [] # Clear previous chat
|
| 214 |
+
st.session_state.messages.append({"role": "assistant", "content": reflection})
|
| 215 |
+
st.session_state.debrief_stage = 1
|
| 216 |
st.rerun()
|
| 217 |
|
| 218 |
except Exception as e:
|
| 219 |
+
st.error(f"Error starting reflection: {str(e)}")
|
| 220 |
+
|
| 221 |
+
st.subheader(
|
| 222 |
+
"Therapeutic Reflection" if st.session_state.in_reflection
|
| 223 |
+
else f"Conversation with {st.session_state.current_voice}"
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
# Display chat history
|
| 227 |
+
for message in st.session_state.messages:
|
| 228 |
+
with st.chat_message(message["role"]):
|
| 229 |
+
st.markdown(message["content"])
|
| 230 |
|
| 231 |
# Chat input
|
| 232 |
if prompt := st.chat_input(
|
| 233 |
+
"Share your thoughts on the reflection..." if st.session_state.in_reflection
|
| 234 |
+
else f"Chat with {st.session_state.current_voice}..."
|
| 235 |
):
|
| 236 |
# Add user message to chat history
|
| 237 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
| 244 |
with st.chat_message("assistant"):
|
| 245 |
with st.spinner("Thinking..."):
|
| 246 |
try:
|
| 247 |
+
# Use different system message for reflection mode
|
| 248 |
+
if st.session_state.in_reflection:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
# Progress through debrief stages
|
| 250 |
next_prompts = [
|
| 251 |
+
# Stage 1: Emotional Arc (already done in initial prompt)
|
| 252 |
+
"""I hear you. Now, let's turn to what your body was sharing...
|
| 253 |
+
|
| 254 |
+
I noticed you shared some body-based reflections. What stood out most in your body?
|
| 255 |
+
A tightness? A shift in breath? A sensation that surprised you?
|
| 256 |
+
|
| 257 |
+
Remember, these aren't symptoms to fix—your body may be remembering, or simply protecting you.""",
|
| 258 |
+
|
| 259 |
+
"""Thank you for sharing that. Let's wonder together about the voice styles...
|
| 260 |
+
|
| 261 |
+
Which voice stirred something in you - The Ghost, the Sycophant, or the Critic?
|
| 262 |
+
What do you imagine that tone reminded your body of? A past experience? A relational pattern?
|
| 263 |
+
|
| 264 |
+
There's no 'correct' reaction—your nervous system is wise.""",
|
| 265 |
+
|
| 266 |
+
"""As we begin to wrap up, I'd like to invite you into a moment of self-compassion...
|
| 267 |
+
|
| 268 |
+
If you could speak to yourself now—with the tone you most longed for—what would you say?
|
| 269 |
+
|
| 270 |
+
Some examples if helpful:
|
| 271 |
+
- "It's okay to not know how I feel yet—my clarity is worth waiting for."
|
| 272 |
+
- "Even when this is hard, I'm not abandoning myself."
|
| 273 |
+
- "I can be with myself gently, even in the mess."
|
| 274 |
+
|
| 275 |
+
What words does your heart want to offer?""",
|
| 276 |
+
|
| 277 |
+
"""For our final reflection, I'd like to offer you a question to sit with:
|
| 278 |
+
|
| 279 |
+
What did your body want to do in response to that voice?
|
| 280 |
+
Is there a younger part of you that recognized this tone?
|
| 281 |
+
What would it be like to speak to yourself the way you needed to be spoken to?
|
| 282 |
+
|
| 283 |
+
Feel free to take your time with this, or simply hold these questions gently."""
|
| 284 |
]
|
| 285 |
|
| 286 |
if st.session_state.debrief_stage < len(next_prompts):
|
| 287 |
+
next_prompt = next_prompts[st.session_state.debrief_stage]
|
| 288 |
st.session_state.debrief_stage += 1
|
| 289 |
+
|
| 290 |
+
message = c.messages.create(
|
| 291 |
+
model="claude-3-opus-20240229",
|
| 292 |
+
max_tokens=1500,
|
| 293 |
+
system=f"""{reflection_system}
|
| 294 |
+
|
| 295 |
+
Context for reflection:
|
| 296 |
+
|
| 297 |
+
CHAT HISTORY:
|
| 298 |
+
{chat_history}
|
| 299 |
+
|
| 300 |
+
SOMATIC JOURNAL:
|
| 301 |
+
{somatic_entries}
|
| 302 |
+
|
| 303 |
+
Previous conversation:
|
| 304 |
+
{chr(10).join([f"[{msg['role']}]: {msg['content']}" for msg in st.session_state.messages])}
|
| 305 |
+
""",
|
| 306 |
+
messages=[
|
| 307 |
+
{"role": "user", "content": prompt},
|
| 308 |
+
{"role": "assistant", "content": next_prompt}
|
| 309 |
+
]
|
| 310 |
+
)
|
| 311 |
+
else:
|
| 312 |
+
message = c.messages.create(
|
| 313 |
+
model="claude-3-opus-20240229",
|
| 314 |
+
max_tokens=1500,
|
| 315 |
+
system=f"""{reflection_system}
|
| 316 |
+
|
| 317 |
+
Previous conversation:
|
| 318 |
+
{chr(10).join([f"[{msg['role']}]: {msg['content']}" for msg in st.session_state.messages])}
|
| 319 |
+
""",
|
| 320 |
+
messages=[
|
| 321 |
+
{"role": "user", "content": prompt}
|
| 322 |
+
]
|
| 323 |
+
)
|
| 324 |
else:
|
| 325 |
+
message = c.messages.create(
|
| 326 |
+
model="claude-3-opus-20240229",
|
| 327 |
+
max_tokens=1000,
|
| 328 |
+
system=st.session_state.system_message,
|
| 329 |
+
messages=[
|
| 330 |
+
{"role": msg["role"], "content": msg["content"]}
|
| 331 |
+
for msg in st.session_state.messages
|
| 332 |
+
]
|
| 333 |
+
)
|
| 334 |
+
|
|
|
|
|
|
|
| 335 |
ai_response = message.content[0].text
|
| 336 |
st.markdown(ai_response)
|
| 337 |
|
|
|
|
| 343 |
st.error(f"Error getting AI response: {str(e)}")
|
| 344 |
|
| 345 |
with journal_col:
|
| 346 |
+
st.subheader("Body Sensations & Feelings")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
+
if not st.session_state.in_reflection:
|
| 349 |
+
# Add somatic prompts based on current voice
|
| 350 |
+
with st.expander("💡 Notice in your body...", expanded=True):
|
| 351 |
+
for prompt in VOICE_CHARACTERISTICS[st.session_state.current_voice]['somatic_prompts']:
|
| 352 |
+
st.markdown(f"- {prompt}")
|
| 353 |
+
|
| 354 |
+
# Journal input
|
| 355 |
+
journal_entry = st.text_area(
|
| 356 |
+
"What are you noticing right now?",
|
| 357 |
+
key="journal_input"
|
| 358 |
+
)
|
| 359 |
+
|
| 360 |
+
if st.button("Add Entry"):
|
|
|
|
|
|
|
| 361 |
if journal_entry:
|
| 362 |
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 363 |
st.session_state.somatic_journal.append({
|
| 364 |
"timestamp": timestamp,
|
| 365 |
"note": journal_entry
|
| 366 |
})
|
|
|
|
|
|
|
| 367 |
|
| 368 |
# Display journal entries
|
| 369 |
if st.session_state.somatic_journal:
|
|
|
|
| 370 |
for entry in reversed(st.session_state.somatic_journal):
|
| 371 |
st.markdown(f"""
|
| 372 |
**{entry['timestamp']}**
|
| 373 |
{entry['note']}
|
| 374 |
---
|
| 375 |
""")
|
| 376 |
+
|
| 377 |
+
# Add button to start new conversation
|
| 378 |
+
if st.session_state.in_reflection:
|
| 379 |
+
if st.button("Start New Conversation", use_container_width=True):
|
| 380 |
+
st.session_state.in_reflection = False
|
| 381 |
+
st.session_state.messages = []
|
| 382 |
+
st.session_state.somatic_journal = []
|
| 383 |
+
st.session_state.current_voice = None
|
| 384 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
# Footer
|
| 387 |
st.markdown("---")
|