Spaces:
Runtime error
Runtime error
| """ | |
| Conversation Summarization Example | |
| Demonstrates automatic summarization of long conversations | |
| """ | |
| from agents.core.coordinator import AgentCoordinator | |
| def simulate_long_conversation(): | |
| """Simulate a long conversation to trigger summarization""" | |
| print("=" * 60) | |
| print("CONVERSATION SUMMARIZATION DEMO") | |
| print("=" * 60) | |
| coordinator = AgentCoordinator(user_id="demo_user") | |
| # Simulate 20 conversation turns | |
| conversations = [ | |
| ("Tôi 25 tuổi, nam, 70kg, 175cm", "Cảm ơn thông tin..."), | |
| ("Tôi muốn giảm cân", "Để giảm cân hiệu quả..."), | |
| ("Nên ăn bao nhiêu calo?", "Với thông tin của bạn..."), | |
| ("Tôi nên tập gì?", "Bạn nên tập cardio..."), | |
| ("Bao lâu thì thấy kết quả?", "Thường sau 2-4 tuần..."), | |
| ("Tôi có thể ăn gì?", "Bạn nên ăn nhiều rau xanh..."), | |
| ("Sáng nên ăn gì?", "Bữa sáng nên có protein..."), | |
| ("Tối nên ăn gì?", "Bữa tối nên nhẹ..."), | |
| ("Tôi có thể ăn trái cây không?", "Có, nhưng hạn chế..."), | |
| ("Nên tập mấy lần 1 tuần?", "Nên tập 3-4 lần..."), | |
| ("Mỗi lần tập bao lâu?", "Mỗi lần 30-45 phút..."), | |
| ("Tôi nên uống bao nhiêu nước?", "Nên uống 2-3 lít..."), | |
| ("Có nên nhịn ăn không?", "Không nên nhịn ăn..."), | |
| ("Tôi có thể ăn đêm không?", "Nên tránh ăn đêm..."), | |
| ("Làm sao để không đói?", "Ăn nhiều protein..."), | |
| ("Tôi bị đau đầu khi tập", "Có thể do thiếu nước..."), | |
| ("Nên bổ sung gì?", "Có thể bổ sung vitamin..."), | |
| ("Tôi có cần whey protein không?", "Không bắt buộc..."), | |
| ("Khi nào nên nghỉ?", "Nên nghỉ 1-2 ngày..."), | |
| ("Làm sao biết đang giảm cân đúng?", "Theo dõi cân nặng..."), | |
| ] | |
| chat_history = [] | |
| for i, (user_msg, bot_msg) in enumerate(conversations, 1): | |
| chat_history.append((user_msg, bot_msg)) | |
| # Show progress | |
| if i % 5 == 0: | |
| print(f"\n📊 After {i} turns:") | |
| stats = coordinator.get_conversation_stats(chat_history) | |
| print(f" Total turns: {stats['total_turns']}") | |
| print(f" Estimated tokens: {stats['estimated_tokens']}") | |
| print(f" Should summarize: {stats['should_summarize']}") | |
| print(f"\n" + "=" * 60) | |
| print("BEFORE SUMMARIZATION") | |
| print("=" * 60) | |
| print(f"Total conversation turns: {len(chat_history)}") | |
| # Trigger summarization | |
| print(f"\n" + "=" * 60) | |
| print("APPLYING SUMMARIZATION") | |
| print("=" * 60) | |
| # This happens automatically in coordinator.handle_query() | |
| # But we can also do it manually: | |
| from utils.conversation_summarizer import get_summarizer | |
| summarizer = get_summarizer() | |
| result = summarizer.summarize_conversation( | |
| chat_history, | |
| user_profile=coordinator.memory.get_full_profile(), | |
| keep_recent=5 | |
| ) | |
| print(f"\n📝 SUMMARY:") | |
| print(result['summary']) | |
| print(f"\n💬 RECENT HISTORY ({len(result['recent_history'])} turns):") | |
| for user_msg, bot_msg in result['recent_history']: | |
| print(f" User: {user_msg}") | |
| print(f" Bot: {bot_msg[:50]}...") | |
| print(f"\n" + "=" * 60) | |
| print("AFTER SUMMARIZATION") | |
| print("=" * 60) | |
| print(f"Summarized turns: {result['summarized_turns']}") | |
| print(f"Kept recent turns: {len(result['recent_history'])}") | |
| print(f"Total context size: {result['summarized_turns'] + len(result['recent_history'])} → {len(result['recent_history']) + 1} (summary + recent)") | |
| # Show compressed history | |
| compressed = summarizer.compress_history(chat_history, target_turns=10) | |
| print(f"\n📦 Compressed history: {len(chat_history)} → {len(compressed)} turns") | |
| print(f" Token reduction: ~{((len(chat_history) - len(compressed)) / len(chat_history) * 100):.0f}%") | |
| def test_automatic_summarization(): | |
| """Test automatic summarization in coordinator""" | |
| print("\n\n" + "=" * 60) | |
| print("AUTOMATIC SUMMARIZATION TEST") | |
| print("=" * 60) | |
| coordinator = AgentCoordinator(user_id="test_user") | |
| # Create long history | |
| chat_history = [ | |
| (f"Câu hỏi {i}", f"Câu trả lời {i}") | |
| for i in range(1, 21) | |
| ] | |
| print(f"Initial history: {len(chat_history)} turns") | |
| # This will trigger automatic summarization | |
| response = coordinator.handle_query( | |
| "Tôi muốn tóm tắt cuộc trò chuyện", | |
| chat_history | |
| ) | |
| print(f"\n✅ Automatic summarization triggered!") | |
| print(f" Response: {response[:100]}...") | |
| if __name__ == '__main__': | |
| simulate_long_conversation() | |
| test_automatic_summarization() | |
| print("\n" + "=" * 60) | |
| print("✅ Summarization Demo Complete!") | |
| print("=" * 60) | |