File size: 6,928 Bytes
cd35cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd40a43
 
 
 
 
 
 
 
 
 
cd35cc5
 
cd40a43
 
 
 
cd35cc5
 
 
 
 
cd40a43
 
 
 
 
 
 
 
cd35cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd40a43
 
 
 
 
 
 
cd35cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd40a43
 
 
 
cd35cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd40a43
 
 
 
cd35cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd40a43
 
 
cd35cc5
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
Anthropic Claude client for Pip's emotional intelligence.
Handles: Emotion analysis, action decisions, intervention logic.
"""

import os
import json
from typing import AsyncGenerator
import anthropic


class AnthropicClient:
    """Claude-powered emotional intelligence for Pip."""
    
    def __init__(self, api_key: str = None):
        """Initialize with optional custom API key."""
        self.api_key = api_key or os.getenv("ANTHROPIC_API_KEY")
        self.available = bool(self.api_key)
        
        if self.available:
            self.client = anthropic.Anthropic(api_key=self.api_key)
            self.async_client = anthropic.AsyncAnthropic(api_key=self.api_key)
        else:
            self.client = None
            self.async_client = None
            print("⚠️ Anthropic: No API key found - service disabled")
        
        self.model = "claude-sonnet-4-20250514"
    
    def is_available(self) -> bool:
        """Check if the client is available."""
        return self.available
    
    async def analyze_emotion(self, user_input: str, system_prompt: str) -> dict:
        """
        Analyze user's emotional state with nuance.
        Returns structured emotion data.
        """
        if not self.available or not self.async_client:
            return {
                "primary_emotions": ["neutral"],
                "intensity": 5,
                "pip_expression": "neutral",
                "intervention_needed": False
            }
        
        response = await self.async_client.messages.create(
            model=self.model,
            max_tokens=1024,
            system=system_prompt,
            messages=[
                {"role": "user", "content": user_input}
            ]
        )
        
        # Parse JSON response
        try:
            content = response.content[0].text
            # Try to extract JSON from the response
            if "```json" in content:
                content = content.split("```json")[1].split("```")[0]
            elif "```" in content:
                content = content.split("```")[1].split("```")[0]
            return json.loads(content.strip())
        except (json.JSONDecodeError, IndexError):
            # Fallback if JSON parsing fails
            return {
                "primary_emotions": ["neutral"],
                "intensity": 5,
                "concerning_flags": [],
                "underlying_needs": ["conversation"],
                "pip_expression": "neutral",
                "intervention_needed": False,
                "raw_response": response.content[0].text
            }
    
    async def decide_action(self, emotion_state: dict, system_prompt: str) -> dict:
        """
        Decide what action Pip should take based on emotional state.
        """
        if not self.available or not self.async_client:
            return {
                "action": "reflect",
                "image_style": "gentle",
                "voice_tone": "warm"
            }
        
        response = await self.async_client.messages.create(
            model=self.model,
            max_tokens=512,
            system=system_prompt,
            messages=[
                {"role": "user", "content": f"Emotion state: {json.dumps(emotion_state)}"}
            ]
        )
        
        try:
            content = response.content[0].text
            if "```json" in content:
                content = content.split("```json")[1].split("```")[0]
            elif "```" in content:
                content = content.split("```")[1].split("```")[0]
            return json.loads(content.strip())
        except (json.JSONDecodeError, IndexError):
            return {
                "action": "reflect",
                "image_style": "gentle",
                "voice_tone": "warm",
                "raw_response": response.content[0].text
            }
    
    async def generate_response_stream(
        self, 
        user_input: str, 
        emotion_state: dict,
        action: dict,
        system_prompt: str,
        conversation_history: list = None
    ) -> AsyncGenerator[str, None]:
        """
        Generate Pip's conversational response with streaming.
        """
        if not self.available or not self.async_client:
            yield "I'm here with you. Let me think about what you shared..."
            return
        
        messages = conversation_history or []
        
        # Add context about current emotional state
        context = f"""
[Current emotional context]
User's emotions: {emotion_state.get('primary_emotions', [])}
Intensity: {emotion_state.get('intensity', 5)}/10
Action to take: {action.get('action', 'reflect')}
Voice tone: {action.get('voice_tone', 'warm')}

[User's message]
{user_input}
"""
        messages.append({"role": "user", "content": context})
        
        async with self.async_client.messages.stream(
            model=self.model,
            max_tokens=1024,
            system=system_prompt,
            messages=messages
        ) as stream:
            async for text in stream.text_stream:
                yield text
    
    async def generate_intervention_response(
        self,
        user_input: str,
        emotion_state: dict,
        system_prompt: str
    ) -> AsyncGenerator[str, None]:
        """
        Generate a gentle intervention response for concerning emotional states.
        """
        if not self.available or not self.async_client:
            yield "I hear you, and I want you to know that what you're feeling matters. Take a moment to breathe..."
            return
        
        context = f"""
[INTERVENTION NEEDED]
User message: {user_input}
Detected emotions: {emotion_state.get('primary_emotions', [])}
Intensity: {emotion_state.get('intensity', 5)}/10
Concerning flags: {emotion_state.get('concerning_flags', [])}

Remember: Acknowledge briefly, then gently introduce curiosity/wonder.
Do NOT be preachy or clinical.
"""
        
        async with self.async_client.messages.stream(
            model=self.model,
            max_tokens=1024,
            system=system_prompt,
            messages=[{"role": "user", "content": context}]
        ) as stream:
            async for text in stream.text_stream:
                yield text
    
    async def generate_text(self, prompt: str) -> str:
        """
        Generate text response for a given prompt.
        Used for summaries and other text generation needs.
        """
        if not self.available or not self.async_client:
            return ""
        
        try:
            response = await self.async_client.messages.create(
                model=self.model,
                max_tokens=500,
                messages=[{"role": "user", "content": prompt}]
            )
            return response.content[0].text
        except Exception as e:
            print(f"Claude text generation error: {e}")
            return ""