Skip to content

Commit 3b03467

Browse files
committed
lint
Signed-off-by: Filinto Duran <[email protected]>
1 parent e6aa6f6 commit 3b03467

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Example demonstrating asynchronous streaming conversation API.
5+
6+
This example shows how to use the Dapr async conversation streaming API with the echo component
7+
for testing purposes. In production, you would replace 'echo' with an actual LLM component
8+
like 'openai', 'anthropic', etc.
9+
10+
Prerequisites:
11+
- Dapr sidecar running with conversation components
12+
- Use tools/run_dapr_dev.py to start a development sidecar with echo component
13+
"""
14+
15+
import asyncio
16+
from dapr.aio.clients import DaprClient
17+
from dapr.clients.grpc._request import ConversationInput
18+
19+
async def main():
20+
print('🧪 Testing asynchronous streaming conversation with echo component...')
21+
22+
try:
23+
async with DaprClient() as d:
24+
print('✓ Connected to Dapr sidecar (async)')
25+
26+
# Test basic async streaming conversation
27+
print('\n📡 Testing async streaming conversation...')
28+
inputs = [
29+
ConversationInput(content="Hello from async Python SDK streaming test!", role='user')
30+
]
31+
32+
chunks_received = []
33+
final_usage = None
34+
async for chunk in d.converse_stream_alpha1(
35+
name='echo',
36+
inputs=inputs,
37+
context_id='async-test-session-456'
38+
):
39+
if chunk.result and chunk.result.result:
40+
print(f'📦 Async chunk: "{chunk.result.result}"')
41+
chunks_received.append(chunk.result.result)
42+
if chunk.context_id:
43+
print(f'🆔 Async context ID: {chunk.context_id}')
44+
if chunk.usage:
45+
print(f'📊 Async usage: {chunk.usage.prompt_tokens} prompt + {chunk.usage.completion_tokens} completion = {chunk.usage.total_tokens} total tokens')
46+
final_usage = chunk.usage
47+
48+
print(f'\n✅ Async success! Received {len(chunks_received)} chunks')
49+
print(f'📝 Full async response: {"".join(chunks_received)}')
50+
if final_usage:
51+
print(f'💰 Total async usage: {final_usage.total_tokens} tokens')
52+
else:
53+
print('ℹ️ No usage information available (echo component doesn\'t provide token counts)')
54+
55+
# Test multiple concurrent conversations
56+
print('\n🔄 Testing concurrent conversations...')
57+
58+
async def run_conversation(message, session_id):
59+
inputs = [ConversationInput(content=message, role='user')]
60+
chunks = []
61+
async for chunk in d.converse_stream_alpha1(
62+
name='echo',
63+
inputs=inputs,
64+
context_id=session_id
65+
):
66+
if chunk.result and chunk.result.result:
67+
chunks.append(chunk.result.result)
68+
return f"Session {session_id}: {''.join(chunks)}"
69+
70+
# Run 3 conversations concurrently
71+
tasks = [
72+
run_conversation("First conversation", "session-1"),
73+
run_conversation("Second conversation", "session-2"),
74+
run_conversation("Third conversation", "session-3")
75+
]
76+
77+
results = await asyncio.gather(*tasks)
78+
for result in results:
79+
print(f'🎯 {result}')
80+
81+
except Exception as e:
82+
print(f'❌ Async error: {e}')
83+
print('\n💡 Make sure to start the Dapr sidecar with:')
84+
print(' python tools/run_dapr_dev.py')
85+
86+
if __name__ == '__main__':
87+
asyncio.run(main())
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)