|
| 1 | +import os |
| 2 | +import time |
| 3 | + |
| 4 | +from memu import MemuClient |
| 5 | + |
| 6 | + |
| 7 | +def print_chat_response(response, message_num: int): |
| 8 | + """Print chat response with detailed token usage.""" |
| 9 | + print(f"\n🤖 Chat Response #{message_num}:") |
| 10 | + print(f" {response.message}") |
| 11 | + |
| 12 | + print("\n📊 Token Usage:") |
| 13 | + usage = response.chat_token_usage |
| 14 | + print(f" Total Tokens: {usage.total_tokens}") |
| 15 | + print(f" Prompt Tokens: {usage.prompt_tokens}") |
| 16 | + print(f" Completion Tokens: {usage.completion_tokens}") |
| 17 | + |
| 18 | + if usage.prompt_tokens_breakdown: |
| 19 | + breakdown = usage.prompt_tokens_breakdown |
| 20 | + print(" 📈 Token Breakdown:") |
| 21 | + print(f" - Current Query: {breakdown.current_query}") |
| 22 | + print(f" - Short Term Context: {breakdown.short_term_context}") |
| 23 | + print(f" - User Profile: {breakdown.user_profile}") |
| 24 | + print(f" - Retrieved Memory: {breakdown.retrieved_memory}") |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + """Main chat demonstration function.""" |
| 29 | + print("🚀 MemU Chat API Demo") |
| 30 | + print("=" * 50) |
| 31 | + |
| 32 | + # Initialize MemU client |
| 33 | + memu_client = MemuClient( |
| 34 | + base_url="https://api.memu.so", |
| 35 | + api_key=os.getenv("MEMU_API_KEY") |
| 36 | + ) |
| 37 | + |
| 38 | + user_id = "chat_demo_user" |
| 39 | + agent_id = "chat_demo_assistant" |
| 40 | + user_name = "Demo User" |
| 41 | + agent_name = "Chat Assistant" |
| 42 | + |
| 43 | + print("\n💬 Starting chat session...") |
| 44 | + print("-" * 50) |
| 45 | + |
| 46 | + # Chat examples demonstrating different scenarios |
| 47 | + chat_examples = [ |
| 48 | + { |
| 49 | + "message": "Hello! Can you help me with some hiking advice?", |
| 50 | + "kwargs": {"temperature": 0.7, "max_tokens": 150}, |
| 51 | + "description": "Basic chat with memory retrieval" |
| 52 | + }, |
| 53 | + { |
| 54 | + "message": "I'm planning to cook Italian food tonight. What should I make?", |
| 55 | + "kwargs": {"temperature": 0.8, "max_tokens": 200}, |
| 56 | + "description": "Query related to pasta making from memory" |
| 57 | + }, |
| 58 | + { |
| 59 | + "message": "I'm feeling overwhelmed at work and considering a career change. Any advice?", |
| 60 | + "kwargs": {"temperature": 0.6, "max_tokens": 180}, |
| 61 | + "description": "Career advice drawing from previous conversation" |
| 62 | + }, |
| 63 | + { |
| 64 | + "message": "What are some easy vegetables I can grow in my garden this spring?", |
| 65 | + "kwargs": {"temperature": 0.5, "max_tokens": 160}, |
| 66 | + "description": "Gardening advice with memory context" |
| 67 | + }, |
| 68 | + { |
| 69 | + "message": "I want to start learning a new language. What's the best approach?", |
| 70 | + "kwargs": {"temperature": 0.7, "max_tokens": 170}, |
| 71 | + "description": "Language learning guidance" |
| 72 | + } |
| 73 | + ] |
| 74 | + |
| 75 | + # Conduct the chat session |
| 76 | + for i, example in enumerate(chat_examples, 1): |
| 77 | + print(f"\n👤 User Message #{i}: {example['message']}") |
| 78 | + print(f" Context: {example['description']}") |
| 79 | + print(f" LLM Parameters: {example['kwargs']}") |
| 80 | + |
| 81 | + try: |
| 82 | + # Send chat message |
| 83 | + response = memu_client.chat( |
| 84 | + user_id=user_id, |
| 85 | + user_name=user_name, |
| 86 | + agent_id=agent_id, |
| 87 | + agent_name=agent_name, |
| 88 | + message=example['message'], |
| 89 | + max_context_tokens=4000, |
| 90 | + **example['kwargs'] |
| 91 | + ) |
| 92 | + |
| 93 | + # Print detailed response |
| 94 | + print_chat_response(response, i) |
| 95 | + |
| 96 | + except Exception as e: |
| 97 | + print(f" ❌ Chat error: {e}") |
| 98 | + |
| 99 | + # Small delay between messages |
| 100 | + time.sleep(1) |
| 101 | + |
| 102 | + # Close the client |
| 103 | + memu_client.close() |
| 104 | + |
| 105 | + print("\n✨ Chat API demo completed!") |
| 106 | + print("=" * 50) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + main() |
0 commit comments