-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice_agent.py
More file actions
685 lines (565 loc) · 28.6 KB
/
voice_agent.py
File metadata and controls
685 lines (565 loc) · 28.6 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
"""
Voice Agent with basic conversation and memory capabilities
"""
import os
import time
import uuid
import logging
from typing import List, Dict, Any, Optional
from dataclasses import dataclass
from datetime import datetime
import json
import speech_recognition as sr
import pyttsx3
from openai import OpenAI
from dotenv import load_dotenv
# Import our long-term memory system
from long_term_memory import LongTermMemory, MemoryWriter
# Import proactive interaction system
from proactive_agent import ProactiveVoiceAgentMixin
# Import user profile system
from user_profile import UserProfileManager
# Load environment variables
load_dotenv()
@dataclass
class ConversationTurn:
"""Represents a single turn in the conversation"""
id: str
timestamp: datetime
user_input: str
agent_response: str
audio_duration: Optional[float] = None
class SessionMemory:
"""Manages short-term conversation memory for the current session"""
def __init__(self, window_size: int = 6):
self.window_size = window_size
self.turns: List[ConversationTurn] = []
self.session_id = str(uuid.uuid4())
self.created_at = datetime.now()
def add_turn(self, user_input: str, agent_response: str, audio_duration: Optional[float] = None) -> str:
"""Add a new conversation turn to memory"""
turn_id = str(uuid.uuid4())
turn = ConversationTurn(
id=turn_id,
timestamp=datetime.now(),
user_input=user_input,
agent_response=agent_response,
audio_duration=audio_duration
)
self.turns.append(turn)
# Keep only the most recent turns within window size
if len(self.turns) > self.window_size:
self.turns = self.turns[-self.window_size:]
return turn_id
def get_conversation_context(self) -> str:
"""Get conversation history as a formatted string for LLM context"""
if not self.turns:
return "This is the start of our conversation."
context_parts = ["Recent conversation history:"]
for turn in self.turns:
context_parts.append(f"User: {turn.user_input}")
context_parts.append(f"Assistant: {turn.agent_response}")
return "\n".join(context_parts)
def get_session_stats(self) -> Dict[str, Any]:
"""Get session statistics"""
return {
"session_id": self.session_id,
"created_at": self.created_at.isoformat(),
"total_turns": len(self.turns),
"window_size": self.window_size
}
class VoiceAgent(ProactiveVoiceAgentMixin):
"""Main Voice Agent class handling ASR, LLM, TTS, and proactive interactions"""
def __init__(self, user_id: str = "default_user"):
# User identification
self.user_id = user_id
# Initialize OpenAI client
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
self.model = os.getenv("VOICE_AGENT_MODEL", "gpt-4")
# Initialize speech components with improved settings
self.recognizer = sr.Recognizer()
self.microphone = sr.Microphone()
# Listening mode preference
self.listening_mode = os.getenv("VOICE_LISTENING_MODE", "patient") # patient, quick, or relaxed
# Initialize TTS
self.tts_engine = pyttsx3.init()
self._configure_tts()
# Initialize session memory
window_size = int(os.getenv("CONVERSATION_WINDOW_SIZE", "6"))
self.memory = SessionMemory(window_size=window_size)
# Initialize long-term memory system
try:
self.long_term_memory = LongTermMemory(user_id=user_id)
self.memory_writer = MemoryWriter(self.client)
self.ltm_enabled = True
logging.info("Long-term memory system initialized")
except Exception as e:
logging.warning(f"Long-term memory initialization failed: {e}")
self.long_term_memory = None
self.memory_writer = None
self.ltm_enabled = False
# Initialize user profile system
try:
self.profile_manager = UserProfileManager()
self.profile_manager.set_profile_extractor(self.client)
self.user_profile = self.profile_manager.get_profile(user_id)
self.profile_enabled = True
logging.info("User profile system initialized")
except Exception as e:
logging.warning(f"User profile initialization failed: {e}")
self.profile_manager = None
self.user_profile = None
self.profile_enabled = False
# Enhanced system prompt with memory awareness
self.system_prompt = """You are a helpful voice assistant with a friendly and conversational personality.
You have both short-term memory (current conversation) and long-term memory (facts from previous conversations).
When relevant memories from past conversations are provided, naturally incorporate them into your responses.
Keep your responses concise and natural for spoken conversation - typically 1-3 sentences.
If you remember something about the user from a previous conversation, feel free to mention it naturally."""
print(f"Voice Agent initialized for user '{user_id}' with session ID: {self.memory.session_id}")
# Initialize proactive agent
self.init_proactive_agent()
# Clean up expired memories on startup
if self.ltm_enabled:
try:
cleaned = self.long_term_memory.cleanup_expired_memories()
if cleaned > 0:
print(f"Cleaned up {cleaned} expired memories")
except Exception as e:
logging.warning(f"Memory cleanup failed: {e}")
def _configure_tts(self):
"""Configure text-to-speech settings"""
try:
voices = self.tts_engine.getProperty('voices')
if voices:
voice_id = int(os.getenv("TTS_VOICE_ID", "0"))
if voice_id < len(voices):
self.tts_engine.setProperty('voice', voices[voice_id].id)
# Set speech rate
self.tts_engine.setProperty('rate', 200)
self.tts_engine.setProperty('volume', 0.9)
except Exception as e:
print(f"Warning: Could not configure TTS: {e}")
def listen(self,
mode: str = "patient",
timeout: int = 10,
phrase_time_limit: int = 30) -> Optional[str]:
"""
Convert speech to text using improved ASR with better pause detection
Args:
mode: "patient" (waits for natural pauses), "quick" (original behavior), or "relaxed" (very patient)
timeout: How long to wait for speech to start (seconds)
phrase_time_limit: Maximum time for one phrase (seconds)
"""
# Configure recognizer based on mode
if mode == "patient":
# Better settings for natural conversation
self.recognizer.energy_threshold = 300
self.recognizer.dynamic_energy_threshold = True
self.recognizer.pause_threshold = 1.2 # Wait 1.2 seconds of silence before stopping
self.recognizer.phrase_threshold = 0.3 # Need 0.3 seconds of speech to start
timeout = 10
phrase_time_limit = 30
print("🎤 Listening patiently... (take your time, I'll wait for pauses)")
elif mode == "relaxed":
# Very patient for complex thoughts
self.recognizer.energy_threshold = 250
self.recognizer.dynamic_energy_threshold = True
self.recognizer.pause_threshold = 2.0 # Wait 2 seconds of silence
self.recognizer.phrase_threshold = 0.3
timeout = 15
phrase_time_limit = 60
print("🎤 Relaxed listening mode... (speak naturally, think as you go)")
else: # "quick" mode - original behavior
self.recognizer.energy_threshold = 4000
self.recognizer.pause_threshold = 0.8
timeout = 5
phrase_time_limit = 10
print("🎤 Quick listening mode...")
try:
# Adjust for ambient noise with more time in patient modes
adjustment_time = 1.0 if mode in ["patient", "relaxed"] else 0.5
with self.microphone as source:
print("🔧 Adjusting for background noise...")
self.recognizer.adjust_for_ambient_noise(source, duration=adjustment_time)
# Listen for audio with improved settings
with self.microphone as source:
audio = self.recognizer.listen(
source,
timeout=timeout,
phrase_time_limit=phrase_time_limit
)
print("🎯 Processing your speech...")
# Convert to text using Google Speech Recognition
language = os.getenv("ASR_LANGUAGE", "en-US")
text = self.recognizer.recognize_google(audio, language=language)
print(f"✅ You said: {text}")
return text
except sr.WaitTimeoutError:
print("⏱️ No speech detected - say something or I'll check for proactive opportunities")
return None
except sr.UnknownValueError:
print("🤔 I heard you but couldn't understand - could you repeat that?")
return None
except sr.RequestError as e:
print(f"❌ Speech recognition error: {e}")
return None
except Exception as e:
print(f"❌ Unexpected error during speech recognition: {e}")
return None
def generate_response(self, user_input: str) -> str:
"""Generate response using LLM with conversation context, long-term memory, and user profile"""
try:
# Get short-term conversation context
session_context = self.memory.get_conversation_context()
# Get relevant long-term memories
ltm_context = ""
if self.ltm_enabled:
try:
ltm_context = self.long_term_memory.get_conversation_context(user_input, max_memories=3)
except Exception as e:
logging.warning(f"Long-term memory retrieval failed: {e}")
ltm_context = ""
# Get personalized context from user profile
profile_context = ""
if self.profile_enabled:
try:
profile_context = self.profile_manager.get_personalized_context(self.user_id)
except Exception as e:
logging.warning(f"Profile context retrieval failed: {e}")
profile_context = ""
# Combine all contexts
full_context = session_context
if ltm_context and ltm_context != "No relevant memories found.":
full_context += f"\n\n{ltm_context}"
if profile_context:
full_context += f"\n\nUser Profile Context:\n{profile_context}"
# Prepare system prompt with profile-aware instructions
enhanced_system_prompt = self.system_prompt
if self.profile_enabled and self.user_profile:
style_instructions = []
# Add style preferences
if self.user_profile.style.formality == "formal":
style_instructions.append("Use formal, professional language")
elif self.user_profile.style.formality == "friendly":
style_instructions.append("Use warm, friendly language")
if not self.user_profile.style.concise:
style_instructions.append("Provide detailed, comprehensive responses")
if self.user_profile.style.emoji_use:
style_instructions.append("Feel free to use appropriate emojis")
if self.user_profile.style.humor_level == "none":
style_instructions.append("Keep responses serious and straightforward")
elif self.user_profile.style.humor_level == "high":
style_instructions.append("You can be playful and humorous when appropriate")
# Add safety considerations
if self.user_profile.safety_overrides.avoid_topics:
avoid_list = ", ".join(self.user_profile.safety_overrides.avoid_topics)
style_instructions.append(f"Avoid discussing: {avoid_list}")
if style_instructions:
enhanced_system_prompt += f"\n\nPersonalization instructions: {'; '.join(style_instructions)}."
# Prepare messages for the LLM
messages = [
{"role": "system", "content": enhanced_system_prompt},
{"role": "user", "content": f"{full_context}\n\nCurrent user input: {user_input}"}
]
# Generate response
response = self.client.chat.completions.create(
model=self.model,
messages=messages,
max_tokens=150,
temperature=0.7
)
agent_response = response.choices[0].message.content.strip()
print(f"Agent response: {agent_response}")
return agent_response
except Exception as e:
error_msg = f"Sorry, I encountered an error: {str(e)}"
print(error_msg)
return "I'm sorry, I had trouble processing that. Could you please try again?"
def speak(self, text: str):
"""Convert text to speech using TTS"""
try:
print("Speaking...")
self.tts_engine.say(text)
self.tts_engine.runAndWait()
except Exception as e:
print(f"TTS error: {e}")
print(f"Text response: {text}")
def set_listening_mode(self, mode: str) -> str:
"""Set the listening mode for speech recognition"""
valid_modes = ["patient", "quick", "relaxed"]
mode = mode.lower().strip()
if mode in valid_modes:
self.listening_mode = mode
if mode == "patient":
return "Listening mode set to patient. I'll wait for natural pauses in your speech."
elif mode == "quick":
return "Listening mode set to quick. I'll respond faster but may cut you off."
elif mode == "relaxed":
return "Listening mode set to relaxed. Take your time, I'll wait for long pauses."
else:
return f"Invalid listening mode. Use: {', '.join(valid_modes)}"
def process_turn(self) -> bool:
"""Process a single conversation turn"""
try:
# Listen for user input with improved pause detection
user_input = self.listen(mode=self.listening_mode)
if user_input is None:
# Check for proactive opportunity during silence
if self.proactive_enabled:
proactive_message = self.check_for_proactive_opportunity(pause_duration=5.0)
if proactive_message:
print(f"🤖 Proactive: {proactive_message}")
self.speak(proactive_message)
# Store the proactive interaction as a turn
self.memory.add_turn("[proactive pause]", proactive_message)
return True # Continue listening
# Check for exit commands
if user_input.lower() in ['exit', 'quit', 'goodbye', 'stop']:
farewell = "Goodbye! Thanks for chatting with me."
print(farewell)
self.speak(farewell)
return False
# Check for listening mode commands
if user_input.lower().startswith('set listening'):
new_mode = user_input.lower().replace('set listening', '').strip()
response = self.set_listening_mode(new_mode)
print(f"Agent: {response}")
self.speak(response)
return True
# Check for proactivity control commands
if user_input.lower().startswith('set proactivity'):
level = user_input.lower().replace('set proactivity', '').strip()
response = self.set_proactivity_level(level)
print(f"Agent: {response}")
self.speak(response)
return True
# Check for profile management commands
if user_input.lower() == 'show profile' or user_input.lower() == 'my profile':
if self.profile_enabled:
profile_info = self._get_profile_summary()
print(f"Agent: {profile_info}")
self.speak(profile_info)
else:
response = "Profile system is not available."
print(f"Agent: {response}")
self.speak(response)
return True
# Check for decline signals (for proactive learning)
decline_phrases = ['not interested', 'no thanks', 'maybe later', 'not now']
if any(phrase in user_input.lower() for phrase in decline_phrases):
if self.proactive_enabled and hasattr(self, 'proactive_agent'):
# Extract topic from recent conversation for decline handling
recent_context = self.memory.get_conversation_context()
self.proactive_agent.handle_user_decline(recent_context.split()[-10:]) # Last 10 words as topic
# Generate response
start_time = time.time()
agent_response = self.generate_response(user_input)
processing_time = time.time() - start_time
# Speak response
self.speak(agent_response)
# Store in short-term memory
turn_id = self.memory.add_turn(user_input, agent_response, processing_time)
# Extract and store long-term memories
if self.ltm_enabled:
self._store_long_term_memories(user_input, agent_response, turn_id)
# Update user profile based on conversation
if self.profile_enabled:
self._update_user_profile(user_input, agent_response)
# After regular interaction, brief pause then check for follow-up opportunities
time.sleep(1.0) # Brief pause to see if user continues
if self.proactive_enabled:
proactive_message = self.check_for_proactive_opportunity(pause_duration=2.0)
if proactive_message:
print(f"🤖 Follow-up: {proactive_message}")
self.speak(proactive_message)
# Store the proactive follow-up
self.memory.add_turn("[proactive follow-up]", proactive_message)
return True
except KeyboardInterrupt:
print("\nConversation interrupted by user")
return False
except Exception as e:
print(f"Error processing turn: {e}")
return True
def _store_long_term_memories(self, user_input: str, agent_response: str, source: str):
"""Extract and store long-term memories from conversation turn"""
try:
# Extract memories using the memory writer
memories = self.memory_writer.extract_memories(
user_input,
agent_response,
self.user_id,
f"session:{self.memory.session_id}:turn:{source}"
)
if memories:
# Store memories in long-term memory
stored_count = self.long_term_memory.store_memories(memories)
if stored_count > 0:
print(f"💾 Stored {stored_count} new memories")
except Exception as e:
logging.warning(f"Failed to store long-term memories: {e}")
def _update_user_profile(self, user_input: str, agent_response: str):
"""Update user profile based on conversation"""
if not self.profile_enabled:
return
try:
# Create conversation context for profile extraction
conversation_context = f"User: {user_input}\nAssistant: {agent_response}"
# Update profile based on conversation
changes = self.profile_manager.update_profile_from_conversation(
self.user_id,
conversation_context
)
if changes:
# Refresh user profile
self.user_profile = self.profile_manager.get_profile(self.user_id)
print(f"👤 Profile updated: {', '.join(changes)}")
except Exception as e:
logging.warning(f"Failed to update user profile: {e}")
def _get_profile_summary(self) -> str:
"""Get a user-friendly summary of their profile"""
if not self.profile_enabled or not self.user_profile:
return "Profile system is not available."
summary_parts = []
# Basic info
if self.user_profile.name != "User":
summary_parts.append(f"Your name is {self.user_profile.name}.")
# Interests
if self.user_profile.interests:
interests_str = ", ".join(self.user_profile.interests)
summary_parts.append(f"Your interests include: {interests_str}.")
# Communication style
style_parts = []
if self.user_profile.style.formality != "casual":
style_parts.append(f"you prefer {self.user_profile.style.formality} communication")
if not self.user_profile.style.concise:
style_parts.append("you like detailed responses")
if self.user_profile.style.emoji_use:
style_parts.append("you enjoy emojis")
if style_parts:
summary_parts.append(f"I know that {', '.join(style_parts)}.")
# Proactivity
summary_parts.append(f"Your proactivity level is set to {self.user_profile.proactivity_level.name.lower()}.")
# Privacy
allowed_domains = self.user_profile.domain_consents.get_allowed_domains()
summary_parts.append(f"You've allowed me to remember information in {len(allowed_domains)} categories.")
if not summary_parts:
return "I don't have much information about your preferences yet. Feel free to tell me about your interests!"
return " ".join(summary_parts)
def run_conversation(self):
"""Main conversation loop"""
print("\n" + "="*50)
print("Voice Agent Started!")
print("Say something to begin the conversation.")
print("Say 'exit', 'quit', 'goodbye', or 'stop' to end.")
# Show proactivity status
if self.proactive_enabled:
level = self.proactive_agent.proactivity_level.name
print(f"🤖 Proactive interactions: {level}")
print("💡 Say 'set proactivity off/low/normal/high' to adjust")
# Show listening mode status
print(f"🎤 Listening mode: {self.listening_mode}")
print("🔧 Say 'set listening patient/quick/relaxed' to adjust")
print(" • patient: waits for natural pauses (recommended)")
print(" • quick: faster response, may cut you off")
print(" • relaxed: very patient, good for long thoughts")
print("Press Ctrl+C to interrupt.")
print("="*50 + "\n")
# Initial greeting
greeting = "Hello! I'm your voice assistant. How can I help you today?"
print(f"Agent: {greeting}")
self.speak(greeting)
try:
while True:
if not self.process_turn():
break
# Brief pause between turns
time.sleep(0.5)
except KeyboardInterrupt:
print("\n\nConversation ended by user")
# Show session stats
stats = self.memory.get_session_stats()
print(f"\nSession completed:")
print(f"- Session ID: {stats['session_id']}")
print(f"- Total turns: {stats['total_turns']}")
print(f"- Duration: {datetime.now() - self.memory.created_at}")
# Show long-term memory stats
if self.ltm_enabled:
try:
ltm_stats = self.long_term_memory.get_memory_stats()
print(f"\nLong-term memory stats:")
print(f"- Total memories: {ltm_stats.get('total_memories', 0)}")
if ltm_stats.get('by_type'):
print(f"- By type: {ltm_stats['by_type']}")
if ltm_stats.get('by_domain'):
print(f"- By domain: {ltm_stats['by_domain']}")
except Exception as e:
logging.warning(f"Failed to get memory stats: {e}")
# Show proactivity stats
if self.proactive_enabled:
try:
proactive_stats = self.proactive_agent.get_proactivity_stats()
print(f"\nProactivity stats:")
print(f"- Level: {proactive_stats['proactivity_level']}")
print(f"- Conversation time: {proactive_stats['conversation_minutes']} minutes")
if proactive_stats['last_proactive']:
print(f"- Last proactive: {proactive_stats['last_proactive']}")
print(f"- Declined topics: {proactive_stats['declined_topics_count']}")
except Exception as e:
logging.warning(f"Failed to get proactivity stats: {e}")
# Show user profile stats
if self.profile_enabled:
try:
profile_stats = self.profile_manager.get_profile_stats(self.user_id)
print(f"\nUser profile stats:")
print(f"- User: {profile_stats['name']}")
print(f"- Interests: {profile_stats['interests_count']}")
print(f"- Allowed domains: {len(profile_stats['allowed_domains'])}")
print(f"- Proactivity preference: {profile_stats['proactivity_level']}")
print(f"- Profile updates: {profile_stats['total_changes']}")
except Exception as e:
logging.warning(f"Failed to get profile stats: {e}")
def get_memory_summary(self) -> str:
"""Get a summary of current memory state"""
summary_parts = []
# Session memory
session_stats = self.memory.get_session_stats()
summary_parts.append(f"Session: {session_stats['total_turns']} turns")
# Long-term memory
if self.ltm_enabled:
try:
ltm_stats = self.long_term_memory.get_memory_stats()
total = ltm_stats.get('total_memories', 0)
summary_parts.append(f"Long-term: {total} memories")
except:
summary_parts.append("Long-term: unavailable")
else:
summary_parts.append("Long-term: disabled")
# Proactivity status
if self.proactive_enabled:
level = self.proactive_agent.proactivity_level.name.lower()
summary_parts.append(f"Proactive: {level}")
else:
summary_parts.append("Proactive: disabled")
# Profile status
if self.profile_enabled and self.user_profile:
interests_count = len(self.user_profile.interests)
summary_parts.append(f"Profile: {self.user_profile.name} ({interests_count} interests)")
else:
summary_parts.append("Profile: disabled")
return " | ".join(summary_parts)
def main():
"""Main entry point"""
# Check for required environment variables
if not os.getenv("OPENAI_API_KEY"):
print("Error: OPENAI_API_KEY environment variable is required")
print("Please set your OpenAI API key in the environment or .env file")
return
try:
agent = VoiceAgent()
agent.run_conversation()
except Exception as e:
print(f"Fatal error: {e}")
if __name__ == "__main__":
main()