11import os
22import time
3-
3+ from typing import ContextManager , Iterator
44from memu import MemuClient
5+ from memu .sdk .python .models import ChatResponse , ChatResponseStream
56
67
7- def print_chat_response (response , message_num : int ):
8+ def print_chat_response (response : ChatResponse , message_num : int ):
89 """Print chat response with detailed token usage."""
910 print (f"\n 🤖 Chat Response #{ message_num } :" )
1011 print (f" { response .message } " )
@@ -24,6 +25,40 @@ def print_chat_response(response, message_num: int):
2425 print (f" - Retrieved Memory: { breakdown .retrieved_memory } " )
2526
2627
28+ def print_chat_response_stream (response : ContextManager [Iterator [ChatResponseStream ]], message_num : int ):
29+ print (f"\n 🤖 Chat Response #{ message_num } (Stream):" )
30+ print (" 💬" , end = "" , flush = True )
31+
32+ chat_token_usage = None
33+
34+ # Context manager version is safer for it ensures the .close() in the finally block is called
35+ with response as response_iterator :
36+ for chunk in response_iterator :
37+ if chunk .error :
38+ print (f" ❌ Error: { chunk .error } " )
39+ break
40+ if chunk .message :
41+ print (f"{ chunk .message } " , end = "" , flush = True )
42+ if chunk .chat_token_usage :
43+ chat_token_usage = chunk .chat_token_usage
44+ if chunk .stream_ended :
45+ print ()
46+
47+ if chat_token_usage :
48+ print ("\n 📊 Token Usage:" )
49+ print (f" Total Tokens: { chat_token_usage .total_tokens } " )
50+ print (f" Prompt Tokens: { chat_token_usage .prompt_tokens } " )
51+ print (f" Completion Tokens: { chat_token_usage .completion_tokens } " )
52+
53+ if chat_token_usage .prompt_tokens_breakdown :
54+ breakdown = chat_token_usage .prompt_tokens_breakdown
55+ print (" 📈 Token Breakdown:" )
56+ print (f" - Current Query: { breakdown .current_query } " )
57+ print (f" - Short Term Context: { breakdown .short_term_context } " )
58+ print (f" - User Profile: { breakdown .user_profile } " )
59+ print (f" - Retrieved Memory: { breakdown .retrieved_memory } " )
60+
61+
2762def main ():
2863 """Main chat demonstration function."""
2964 print ("🚀 MemU Chat API Demo" )
@@ -73,7 +108,7 @@ def main():
73108 ]
74109
75110 # Conduct the chat session
76- for i , example in enumerate (chat_examples , 1 ):
111+ for i , example in enumerate (chat_examples [: 3 ] , 1 ):
77112 print (f"\n 👤 User Message #{ i } : { example ['message' ]} " )
78113 print (f" Context: { example ['description' ]} " )
79114 print (f" LLM Parameters: { example ['kwargs' ]} " )
@@ -99,6 +134,34 @@ def main():
99134 # Small delay between messages
100135 time .sleep (1 )
101136
137+ # Conduct the chat session with stream
138+ for i , example in enumerate (chat_examples [3 :], 4 ):
139+ print (f"\n 👤 User Message #{ i } : { example ['message' ]} " )
140+ print (f" Context: { example ['description' ]} " )
141+ print (f" LLM Parameters: { example ['kwargs' ]} " )
142+
143+ try :
144+ # Send chat message
145+ response = memu_client .chat (
146+ user_id = user_id ,
147+ user_name = user_name ,
148+ agent_id = agent_id ,
149+ agent_name = agent_name ,
150+ message = example ['message' ],
151+ max_context_tokens = 4000 ,
152+ ** example ['kwargs' ],
153+ stream = True ,
154+ )
155+
156+ # Print detailed response
157+ print_chat_response_stream (response , i )
158+
159+ except Exception as e :
160+ print (f" ❌ Chat error: { e } " )
161+
162+ # Small delay between messages
163+ time .sleep (1 )
164+
102165 # Close the client
103166 memu_client .close ()
104167
0 commit comments