1+ #!/usr/bin/env python3
2+
3+ """
4+ Comprehensive example demonstrating the Dapr streaming conversation API.
5+
6+ This example demonstrates all features of the streaming conversation API including:
7+ - Basic streaming conversation
8+ - Usage tracking (token consumption)
9+ - PII scrubbing
10+ - Temperature control
11+ - Error handling
12+
13+ Prerequisites:
14+ - Dapr sidecar running with conversation components
15+ - Use tools/run_dapr_dev.py to start a development sidecar with echo component
16+ """
17+
18+ import time
19+ from dapr .clients import DaprClient
20+ from dapr .clients .grpc ._request import ConversationInput
21+
22+ def main ():
23+ print ('🚀 Demonstrating Dapr streaming conversation API features...' )
24+
25+ try :
26+ with DaprClient () as d :
27+ print ('✓ Connected to Dapr sidecar' )
28+
29+ # Test basic streaming conversation
30+ print ('\n 📡 Testing basic streaming conversation...' )
31+ inputs = [
32+ ConversationInput (content = "Hello from Python SDK streaming test!" , role = 'user' )
33+ ]
34+
35+ chunks_received = []
36+ final_usage = None
37+ for chunk in d .converse_stream_alpha1 (
38+ name = 'echo' ,
39+ inputs = inputs ,
40+ context_id = 'sync-test-session-123'
41+ ):
42+ if chunk .result and chunk .result .result :
43+ print (f'📦 Chunk: "{ chunk .result .result } "' )
44+ chunks_received .append (chunk .result .result )
45+ if chunk .context_id :
46+ print (f'🆔 Context ID: { chunk .context_id } ' )
47+ if chunk .usage :
48+ print (f'📊 Usage: { chunk .usage .prompt_tokens } prompt + { chunk .usage .completion_tokens } completion = { chunk .usage .total_tokens } total tokens' )
49+ final_usage = chunk .usage
50+
51+ print (f'\n ✅ Success! Received { len (chunks_received )} chunks' )
52+ print (f'📝 Full response: { "" .join (chunks_received )} ' )
53+ if final_usage :
54+ print (f'💰 Total usage: { final_usage .total_tokens } tokens' )
55+ else :
56+ print ('ℹ️ No usage information available (echo component doesn\' t provide token counts)' )
57+
58+ # Test with PII scrubbing
59+ print ('\n 🔒 Testing PII scrubbing...' )
60+ pii_inputs = [
61+ ConversationInput (content = "My phone number is +1234567890" , role = 'user' , scrub_pii = True )
62+ ]
63+
64+ scrubbed_chunks = []
65+ for chunk in d .converse_stream_alpha1 (
66+ name = 'echo' ,
67+ inputs = pii_inputs ,
68+ scrub_pii = True
69+ ):
70+ if chunk .result and chunk .result .result :
71+ print (f'📦 Scrubbed chunk: "{ chunk .result .result } "' )
72+ scrubbed_chunks .append (chunk .result .result )
73+
74+ scrubbed_response = "" .join (scrubbed_chunks )
75+ print (f'📝 Scrubbed response: { scrubbed_response } ' )
76+
77+ if "<PHONE_NUMBER>" in scrubbed_response :
78+ print ('✅ PII scrubbing working correctly!' )
79+ else :
80+ print ('⚠️ PII scrubbing may not be working as expected' )
81+
82+ # Test with temperature parameter
83+ print ('\n 🌡️ Testing with temperature parameter...' )
84+ temp_inputs = [
85+ ConversationInput (content = "Test with temperature setting" , role = 'user' )
86+ ]
87+
88+ temp_chunks = []
89+ for chunk in d .converse_stream_alpha1 (
90+ name = 'echo' ,
91+ inputs = temp_inputs ,
92+ temperature = 0.7
93+ ):
94+ if chunk .result and chunk .result .result :
95+ temp_chunks .append (chunk .result .result )
96+
97+ print (f'📝 Temperature test response: { "" .join (temp_chunks )} ' )
98+
99+ except Exception as e :
100+ print (f'❌ Error: { e } ' )
101+ print ('\n 💡 Make sure to start the Dapr sidecar with:' )
102+ print (' python tools/run_dapr_dev.py' )
103+
104+ if __name__ == '__main__' :
105+ main ()
0 commit comments