1515- Use tools/run_dapr_dev.py to start a development sidecar with echo component
1616"""
1717
18- import time
1918from dapr .clients import DaprClient
2019from dapr .clients .grpc ._request import ConversationInput
2120
21+
22+ def basic_streaming_test (d ):
23+ """Test basic streaming conversation."""
24+ print ('\n 📡 Testing basic streaming conversation...' )
25+ inputs = [ConversationInput (content = 'Hello from Python SDK streaming test!' , role = 'user' )]
26+
27+ chunks_received = []
28+ final_usage = None
29+ for chunk in d .converse_stream_alpha1 (
30+ name = 'echo' , inputs = inputs , context_id = 'sync-test-session-123'
31+ ):
32+ if chunk .result and chunk .result .result :
33+ print (f'📦 Chunk: "{ chunk .result .result } "' )
34+ chunks_received .append (chunk .result .result )
35+ if chunk .context_id :
36+ print (f'🆔 Context ID: { chunk .context_id } ' )
37+ if chunk .usage :
38+ prompt_tokens = chunk .usage .prompt_tokens
39+ completion_tokens = chunk .usage .completion_tokens
40+ total_tokens = chunk .usage .total_tokens
41+ usage_parts = [
42+ f'📊 Usage: { prompt_tokens } prompt' ,
43+ f'{ completion_tokens } completion' ,
44+ f'{ total_tokens } total tokens' ,
45+ ]
46+ print (' + ' .join (usage_parts [:2 ]) + ' = ' + usage_parts [2 ])
47+ final_usage = chunk .usage
48+
49+ print (f'\n ✅ Success! Received { len (chunks_received )} chunks' )
50+ print (f'📝 Full response: { "" .join (chunks_received )} ' )
51+ if final_usage :
52+ print (f'💰 Total usage: { final_usage .total_tokens } tokens' )
53+ else :
54+ no_usage_msg = 'ℹ️ No usage information available'
55+ echo_note = " (echo component doesn't provide token counts)"
56+ print (no_usage_msg + echo_note )
57+
58+
59+ def pii_scrubbing_test (d ):
60+ """Test PII scrubbing functionality."""
61+ print ('\n 🔒 Testing PII scrubbing...' )
62+ pii_inputs = [
63+ ConversationInput (content = 'My phone number is +1234567890' , role = 'user' , scrub_pii = True )
64+ ]
65+
66+ scrubbed_chunks = []
67+ for chunk in d .converse_stream_alpha1 (name = 'echo' , inputs = pii_inputs , scrub_pii = True ):
68+ if chunk .result and chunk .result .result :
69+ print (f'📦 Scrubbed chunk: "{ chunk .result .result } "' )
70+ scrubbed_chunks .append (chunk .result .result )
71+
72+ scrubbed_response = '' .join (scrubbed_chunks )
73+ print (f'📝 Scrubbed response: { scrubbed_response } ' )
74+
75+ if '<PHONE_NUMBER>' in scrubbed_response :
76+ print ('✅ PII scrubbing working correctly!' )
77+ else :
78+ print ('⚠️ PII scrubbing may not be working as expected' )
79+
80+
81+ def temperature_test (d ):
82+ """Test temperature parameter."""
83+ print ('\n 🌡️ Testing with temperature parameter...' )
84+ temp_inputs = [ConversationInput (content = 'Test with temperature setting' , role = 'user' )]
85+
86+ temp_chunks = []
87+ for chunk in d .converse_stream_alpha1 (name = 'echo' , inputs = temp_inputs , temperature = 0.7 ):
88+ if chunk .result and chunk .result .result :
89+ temp_chunks .append (chunk .result .result )
90+
91+ print (f'📝 Temperature test response: { "" .join (temp_chunks )} ' )
92+
93+
2294def main ():
2395 print ('🚀 Demonstrating Dapr streaming conversation API features...' )
24-
96+
2597 try :
2698 with DaprClient () as d :
2799 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 )} ' )
100+ basic_streaming_test (d )
101+ pii_scrubbing_test (d )
102+ temperature_test (d )
98103
99104 except Exception as e :
100105 print (f'❌ Error: { e } ' )
101106 print ('\n 💡 Make sure to start the Dapr sidecar with:' )
102107 print (' python tools/run_dapr_dev.py' )
103108
109+
104110if __name__ == '__main__' :
105- main ()
111+ main ()
0 commit comments