-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathreal_llm_providers_example.py
More file actions
1265 lines (1063 loc) Β· 54.3 KB
/
real_llm_providers_example.py
File metadata and controls
1265 lines (1063 loc) Β· 54.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ------------------------------------------------------------
# Copyright 2025 The Dapr Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------
"""
Real LLM Providers Example for Dapr Conversation API (Alpha2)
This example demonstrates how to use real LLM providers (OpenAI, Anthropic, etc.)
with the Dapr Conversation API Alpha2. It showcases the latest features including:
- Advanced message types (user, system, assistant, developer, tool)
- Automatic parameter conversion (raw Python values)
- Enhanced tool calling capabilities
- Multi-turn conversations
- Decorator-based tool definition
- Both sync and async implementations
Prerequisites:
1. Set up API keys in .env file (copy from .env.example)
2. For manual mode: Start Dapr sidecar manually
Usage:
# requires manual Dapr sidecar setup
python examples/conversation/real_llm_providers_example.py
# Show help
python examples/conversation/real_llm_providers_example.py --help
Environment Variables:
OPENAI_API_KEY: OpenAI API key
ANTHROPIC_API_KEY: Anthropic API key
MISTRAL_API_KEY: Mistral API key
DEEPSEEK_API_KEY: DeepSeek API key
GOOGLE_API_KEY: Google AI (Gemini) API key
"""
import asyncio
import json
import os
import sys
import tempfile
from pathlib import Path
from typing import Any, Dict, List, Optional
import yaml
# Add the parent directory to the path so we can import local dapr sdk
# uncomment if running from development version
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
# Load environment variables from .env file if available
try:
from dotenv import load_dotenv
DOTENV_AVAILABLE = True
except ImportError:
DOTENV_AVAILABLE = False
print('β οΈ python-dotenv not installed. Install with: pip install python-dotenv')
from dapr.aio.clients import DaprClient as AsyncDaprClient
from dapr.clients import DaprClient
from dapr.clients.grpc import conversation
def create_weather_tool() -> conversation.ConversationTools:
"""Create a weather tool for testing Alpha2 tool calling using full JSON schema in parameters approach."""
conversation.unregister_tool('get_weather')
function = conversation.ConversationToolsFunction(
name='get_weather',
description='Get the current weather for a location',
parameters={
'type': 'object',
'properties': {
'location': {'type': 'string', 'description': 'The city and state or country'},
'unit': {
'type': 'string',
'enum': ['celsius', 'fahrenheit'],
'description': 'Temperature unit',
},
},
'required': ['location'],
},
)
return conversation.ConversationTools(function=function)
def create_calculator_tool() -> conversation.ConversationTools:
"""Create a calculator tool using full JSON schema in parameters approach."""
conversation.unregister_tool('calculate') # cleanup
function = conversation.ConversationToolsFunction(
name='calculate',
description='Perform mathematical calculations',
parameters={
'type': 'object',
'properties': {
'expression': {
'type': 'string',
'description': "Mathematical expression to evaluate (e.g., '2+2', 'sqrt(16)')",
}
},
'required': ['expression'],
},
)
return conversation.ConversationTools(function=function)
def create_time_tool() -> conversation.ConversationTools:
"""Create a simple tool with no parameters using full JSON schema in parameters approach."""
conversation.unregister_tool('get_current_time')
function = conversation.ConversationToolsFunction(
name='get_current_time',
description='Get the current date and time',
parameters={'type': 'object', 'properties': {}, 'required': []},
)
return conversation.ConversationTools(function=function)
def create_search_tool() -> conversation.ConversationTools:
"""Create a more complex tool with multiple parameter types and constraints using full JSON schema in parameters approach."""
conversation.unregister_tool('web_search')
function = conversation.ConversationToolsFunction(
name='web_search',
description='Search the web for information',
parameters={
'type': 'object',
'properties': {
'query': {'type': 'string', 'description': 'Search query'},
'limit': {
'type': 'integer',
'description': 'Maximum number of results',
'minimum': 1,
'maximum': 10,
'default': 5,
},
'include_images': {
'type': 'boolean',
'description': 'Whether to include image results',
'default': False,
},
'domains': {
'type': 'array',
'items': {'type': 'string'},
# 'description': 'Limit search to specific domains',
},
},
'required': ['query'],
},
)
return conversation.ConversationTools(function=function)
def create_tool_from_typed_function_example() -> conversation.ConversationTools:
"""Demonstrate creating tools from typed Python functions - Best DevEx for most cases.
This shows the most advanced approach: define a typed function and automatically
generate the complete tool schema from type hints and docstrings.
"""
from enum import Enum
from typing import List, Optional
conversation.unregister_tool('find_restaurants')
# Define the tool behavior as a regular Python function with type hints
class PriceRange(Enum):
BUDGET = 'budget'
MODERATE = 'moderate'
EXPENSIVE = 'expensive'
def find_restaurants(
location: str,
cuisine: str = 'any',
price_range: PriceRange = PriceRange.MODERATE,
max_results: int = 5,
dietary_restrictions: Optional[List[str]] = None,
) -> str:
"""Find restaurants in a specific location.
Args:
location: The city or neighborhood to search
cuisine: Type of cuisine (italian, chinese, mexican, etc.)
price_range: Budget preference for dining
max_results: Maximum number of restaurant recommendations
dietary_restrictions: Special dietary needs (vegetarian, gluten-free, etc.)
"""
# This would contain actual implementation
return f'Found restaurants in {location} serving {cuisine} food'
# Create the tool using the from_function class method
function = conversation.ConversationToolsFunction.from_function(find_restaurants)
return conversation.ConversationTools(function=function)
def create_tool_from_tool_decorator_example() -> conversation.ConversationTools:
"""Demonstrate creating tools from typed Python functions - Best DevEx for most cases.
This shows the most advanced approach: define a typed function and automatically
generate the complete tool schema from type hints and docstrings.
"""
from enum import Enum
from typing import List, Optional
conversation.unregister_tool('find_restaurants')
# Define the tool behavior as a regular Python function with type hints
class PriceRange(Enum):
MODERATE = 'moderate'
EXPENSIVE = 'expensive'
@conversation.tool
def find_restaurants(
location: str,
cuisine: str = 'any',
price_range: PriceRange = PriceRange.MODERATE,
max_results: int = 5,
dietary_restrictions: Optional[List[str]] = None,
) -> str:
"""Find restaurants in a specific location.
Args:
location: The city or neighborhood to search
cuisine: Type of cuisine (italian, chinese, mexican, etc.)
price_range: Budget preference for dining
max_results: Maximum number of restaurant recommendations
dietary_restrictions: Special dietary needs (vegetarian, gluten-free, etc.)
"""
# This would contain actual implementation
return f'Found restaurants in {location} serving {cuisine} food'
return conversation.ConversationTools(function=find_restaurants)
def execute_weather_tool(location: str, unit: str = 'fahrenheit') -> str:
"""Simulate weather tool execution."""
temp = '72Β°F' if unit == 'fahrenheit' else '22Β°C'
return f'The weather in {location} is sunny with a temperature of {temp}.'
def convert_llm_response_to_conversation_input(
result_message: conversation.ConversationResultAlpha2Message,
) -> conversation.ConversationMessage:
"""Convert ConversationResultMessage (from LLM response) to ConversationMessage (for conversation input).
This standalone utility function makes it easy to append LLM responses to conversation history
and reuse them as input for subsequent conversation turns in multi-turn scenarios.
Args:
result_message: ConversationResultMessage from LLM response (choice.message)
Returns:
ConversationMessage suitable for input to next conversation turn
Example:
>>> import dapr.clients.grpc.conversation
>>> client = DaprClient()
>>> response = client.converse_alpha2(name="openai", inputs=[input_alpha2], tools=[tool])
>>> choice = response.outputs[0].choices[0]
>>>
>>> # Convert LLM response to conversation message
>>> conversation_history = []
>>> assistant_message = convert_llm_response_to_conversation_input(choice.message)
>>> conversation_history.append(assistant_message)
>>>
>>> # Use in next turn
>>> next_input = conversation.ConversationInputAlpha2(messages=conversation_history)
>>> next_response = client.converse_alpha2(name="openai", inputs=[next_input])
"""
# Convert content string to ConversationMessageContent list
content = []
if result_message.content:
content = [conversation.ConversationMessageContent(text=result_message.content)]
# Convert tool_calls if present (they're already the right type)
tool_calls = result_message.tool_calls or []
# Create assistant message (since LLM responses are always assistant messages)
return conversation.ConversationMessage(
of_assistant=conversation.ConversationMessageOfAssistant(
content=content, tool_calls=tool_calls
)
)
class RealLLMProviderTester:
"""Test real LLM providers with Dapr Conversation API Alpha2."""
def __init__(self):
self.available_providers = {}
self.component_configs = {}
self.components_dir = None
def load_environment(self) -> None:
"""Load environment variables from .env file if available."""
if DOTENV_AVAILABLE:
env_file = Path(__file__).parent / '.env'
if env_file.exists():
load_dotenv(env_file)
print(f'π Loaded environment from {env_file}')
else:
print(f'β οΈ No .env file found at {env_file}')
print(' Copy .env.example to .env and add your API keys')
else:
print('β οΈ python-dotenv not available, using system environment variables')
def detect_available_providers(self) -> Dict[str, Dict[str, Any]]:
"""Detect which LLM providers are available based on API keys."""
providers = {}
# OpenAI
if os.getenv('OPENAI_API_KEY'):
providers['openai'] = {
'display_name': 'OpenAI GPT-5-mini',
'component_type': 'conversation.openai',
'api_key_env': 'OPENAI_API_KEY',
'metadata': [
{'name': 'key', 'value': os.getenv('OPENAI_API_KEY')},
{'name': 'model', 'value': 'gpt-5-mini-2025-08-07'},
],
}
# Anthropic
if os.getenv('ANTHROPIC_API_KEY'):
providers['anthropic'] = {
'display_name': 'Anthropic Claude Sonnet 4',
'component_type': 'conversation.anthropic',
'api_key_env': 'ANTHROPIC_API_KEY',
'metadata': [
{'name': 'key', 'value': os.getenv('ANTHROPIC_API_KEY')},
{'name': 'model', 'value': 'claude-sonnet-4-20250514'},
],
}
# Mistral
if os.getenv('MISTRAL_API_KEY'):
providers['mistral'] = {
'display_name': 'Mistral Large',
'component_type': 'conversation.mistral',
'api_key_env': 'MISTRAL_API_KEY',
'metadata': [
{'name': 'key', 'value': os.getenv('MISTRAL_API_KEY')},
{'name': 'model', 'value': 'mistral-large-latest'},
],
}
# DeepSeek
if os.getenv('DEEPSEEK_API_KEY'):
providers['deepseek'] = {
'display_name': 'DeepSeek V3',
'component_type': 'conversation.deepseek',
'api_key_env': 'DEEPSEEK_API_KEY',
'metadata': [
{'name': 'key', 'value': os.getenv('DEEPSEEK_API_KEY')},
{'name': 'model', 'value': 'deepseek-chat'},
],
}
# Google AI (Gemini)
if os.getenv('GOOGLE_API_KEY'):
providers['google'] = {
'display_name': 'Google Gemini 2.5 Flash',
'component_type': 'conversation.googleai',
'api_key_env': 'GOOGLE_API_KEY',
'metadata': [
{'name': 'key', 'value': os.getenv('GOOGLE_API_KEY')},
{'name': 'model', 'value': 'gemini-2.5-flash'},
],
}
return providers
def create_component_configs(self, selected_providers: Optional[List[str]] = None) -> str:
"""Create Dapr component configurations for available providers (those with API keys exposed)."""
# Create temporary directory for components
self.components_dir = tempfile.mkdtemp(prefix='dapr-llm-components-')
# If no specific providers selected, use OpenAI as default (most reliable)
if not selected_providers:
selected_providers = (
['openai']
if 'openai' in self.available_providers
else list(self.available_providers.keys())[:1]
)
for provider_id in selected_providers:
if provider_id not in self.available_providers:
continue
config = self.available_providers[provider_id]
component_config = {
'apiVersion': 'dapr.io/v1alpha1',
'kind': 'Component',
'metadata': {'name': provider_id},
'spec': {
'type': config['component_type'],
'version': 'v1',
'metadata': config['metadata'],
},
}
# Write component file
component_file = Path(self.components_dir) / f'{provider_id}.yaml'
with open(component_file, 'w') as f:
yaml.dump(component_config, f, default_flow_style=False)
print(f'π Created component: {component_file}')
return self.components_dir
def test_basic_conversation_alpha2(self, provider_id: str) -> None:
"""Test basic Alpha2 conversation with a provider."""
print(
f'\n㪠Testing Alpha2 basic conversation with {self.available_providers[provider_id]["display_name"]}'
)
try:
with DaprClient() as client:
# Create Alpha2 conversation input with sophisticated message structure
user_message = conversation.create_user_message(
"Hello! Please respond with exactly: 'Hello from Dapr Alpha2!'"
)
input_alpha2 = conversation.ConversationInputAlpha2(messages=[user_message])
# Use new parameter conversion (raw Python values automatically converted)
response = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2],
temperature=1,
parameters={
'temperature': 0.7,
'max_tokens': 100,
'top_p': 0.9,
},
)
if response.outputs and response.outputs[0].choices:
choice = response.outputs[0].choices[0]
print(f'β
Alpha2 Response: {choice.message.content}')
print(f'π Finish reason: {choice.finish_reason}')
else:
print('β No Alpha2 response received')
except Exception as e:
print(f'β Alpha2 basic conversation error: {e}')
def test_multi_turn_conversation_alpha2(self, provider_id: str) -> None:
"""Test multi-turn Alpha2 conversation with different message types."""
print(
f'\nπ Testing Alpha2 multi-turn conversation with {self.available_providers[provider_id]["display_name"]}'
)
try:
with DaprClient() as client:
# Create a multi-turn conversation with system, user, and assistant messages
system_message = conversation.create_system_message(
'You are a helpful AI assistant. Be concise.'
)
user_message1 = conversation.create_user_message('What is 2+2?')
assistant_message = conversation.create_assistant_message('2+2 equals 4.')
user_message2 = conversation.create_user_message('What about 3+3?')
input_alpha2 = conversation.ConversationInputAlpha2(
messages=[system_message, user_message1, assistant_message, user_message2]
)
response = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2],
temperature=1,
parameters={
'max_tokens': 150,
},
)
if response.outputs and response.outputs[0].choices:
print(
f'β
Multi-turn conversation processed {len(response.outputs[0].choices)} message(s)'
)
for i, choice in enumerate(response.outputs[0].choices):
print(f' Response {i + 1}: {choice.message.content[:100]}...')
else:
print('β No multi-turn response received')
except Exception as e:
print(f'β Multi-turn conversation error: {e}')
def test_tool_calling_alpha2(self, provider_id: str) -> None:
"""Test Alpha2 tool calling with a provider."""
print(
f'\nπ§ Testing Alpha2 tool calling with {self.available_providers[provider_id]["display_name"]}'
)
try:
with DaprClient() as client:
weather_tool = create_weather_tool()
user_message = conversation.create_user_message(
"What's the weather like in San Francisco?"
)
input_alpha2 = conversation.ConversationInputAlpha2(messages=[user_message])
response = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2],
tools=[weather_tool],
tool_choice='auto',
temperature=1,
parameters={
'max_tokens': 500,
},
)
if response.outputs and response.outputs[0].choices:
choice = response.outputs[0].choices[0]
print(f'π Finish reason: {choice.finish_reason}')
if choice.finish_reason == 'tool_calls' and choice.message.tool_calls:
print(f'π§ Tool calls made: {len(choice.message.tool_calls)}')
for tool_call in choice.message.tool_calls:
print(f' Tool: {tool_call.function.name}')
print(f' Arguments: {tool_call.function.arguments}')
# Execute the tool to show the workflow
try:
args = json.loads(tool_call.function.arguments)
weather_result = execute_weather_tool(
args.get('location', 'San Francisco'),
args.get('unit', 'fahrenheit'),
)
print(f'π€οΈ Tool executed: {weather_result}')
# Demonstrate tool result message (for multi-turn tool workflows)
tool_result_message = conversation.create_tool_message(
tool_id=tool_call.id,
name=tool_call.function.name,
content=weather_result,
)
print(
'β
Alpha2 tool calling demonstration completed! Tool Result Message:'
)
print(tool_result_message)
except json.JSONDecodeError:
print('β οΈ Could not parse tool arguments')
else:
print(f'π¬ Regular response: {choice.message.content}')
else:
print('β No tool calling response received')
except Exception as e:
print(f'β Alpha2 tool calling error: {e}')
def test_parameter_conversion(self, provider_id: str) -> None:
"""Test the new parameter conversion feature."""
print(
f'\nπ Testing parameter conversion with {self.available_providers[provider_id]["display_name"]}'
)
try:
with DaprClient() as client:
user_message = conversation.create_user_message(
'Tell me about the different tool creation approaches available.'
)
input_alpha2 = conversation.ConversationInputAlpha2(messages=[user_message])
# Demonstrate different tool creation approaches
weather_tool = create_weather_tool() # Simple properties approach
calc_tool = create_calculator_tool() # Full JSON schema approach
time_tool = create_time_tool() # No parameters approach
search_tool = create_search_tool() # Complex schema with arrays, etc.
print(
f'β
Created {len([weather_tool, calc_tool, time_tool, search_tool])} tools with different approaches!'
)
# Test various parameter types that are automatically converted
response = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2],
tools=[weather_tool, calc_tool, time_tool, search_tool],
temperature=1,
parameters={
# Raw Python values - automatically converted to GrpcAny
'max_tokens': 200, # int
'top_p': 1.0, # float
'frequency_penalty': 0.0, # float
'presence_penalty': 0.0, # float
'stream': False, # bool
'tool_choice': 'none', # string
'model': 'gpt-4o-mini', # string (provider-specific)
},
)
if response.outputs and response.outputs[0].choices:
choice = response.outputs[0].choices[0]
print('β
Parameter conversion successful!')
print('β
Tool creation helpers working perfectly!')
print(f' Response: {choice.message.content[:100]}...')
else:
print('β Parameter conversion test failed')
except Exception as e:
print(f'β Parameter conversion error: {e}')
def test_multi_turn_tool_calling_alpha2(self, provider_id: str) -> None:
"""Test multi-turn Alpha2 tool calling with proper context accumulation."""
print(
f'\nππ§ Testing multi-turn tool calling with {self.available_providers[provider_id]["display_name"]}'
)
try:
with DaprClient() as client:
weather_tool = create_weather_tool()
conversation_history = []
# Turn 1: User asks about weather (include tools)
print('\n--- Turn 1: Initial weather query ---')
user_message1 = conversation.create_user_message(
"What's the weather like in San Francisco? Use one of the tools available."
)
conversation_history.append(user_message1)
print(f'π Request 1 context: {len(conversation_history)} messages + tools')
input_alpha2_turn1 = conversation.ConversationInputAlpha2(
messages=conversation_history
)
response1 = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2_turn1],
tools=[weather_tool], # Tools included in turn 1
tool_choice='auto',
temperature=1,
parameters={
'max_tokens': 500,
},
)
# Check all outputs and choices for tool calls
tool_calls_found = []
assistant_messages = []
for output_idx, output in enumerate(response1.outputs or []):
for choice_idx, choice in enumerate(output.choices or []):
print(
f'π Checking output {output_idx}, choice {choice_idx}: finish_reason={choice.finish_reason}, choice: {choice}'
)
# Convert and collect all assistant messages
assistant_message = convert_llm_response_to_conversation_input(
choice.message
)
assistant_messages.append(assistant_message)
# Check for tool calls in this choice
if choice.message.tool_calls:
# if not choice.message.tool_calls[0].id:
# choice.message.tool_calls[0].id = "1"
tool_calls_found.extend(choice.message.tool_calls)
print(
f'π§ Found {len(choice.message.tool_calls)} tool call(s) in output {output_idx}, choice {choice_idx}'
)
# Use the first assistant message for conversation history (most providers return one)
if assistant_messages:
for assistant_message in assistant_messages:
conversation_history.append(assistant_message)
print(
f'β
Added assistant message to history (from {len(assistant_messages)} total messages)'
)
if tool_calls_found:
# Use the first tool call for demonstration
tool_call = tool_calls_found[0]
print(
f'π§ Processing tool call: {tool_call.function.name} (found {len(tool_calls_found)} total tool calls)'
)
# Execute the tool
args = json.loads(tool_call.function.arguments)
weather_result = execute_weather_tool(
args.get('location', 'San Francisco'), args.get('unit', 'fahrenheit')
)
print(f'π€οΈ Tool result: {weather_result}')
# Add tool result to conversation history
tool_result_message = conversation.create_tool_message(
tool_id=tool_call.id, name=tool_call.function.name, content=weather_result
)
conversation_history.append(tool_result_message)
# Turn 2: LLM processes tool result (accumulate context + tools)
print('\n--- Turn 2: LLM processes tool result ---')
print(f'π Request 2 context: {len(conversation_history)} messages + tools')
input_alpha2_turn2 = conversation.ConversationInputAlpha2(
messages=conversation_history
)
response2 = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2_turn2],
tools=[weather_tool], # Tools carried forward to turn 2
temperature=1,
parameters={
'max_tokens': 500,
},
)
if response2.outputs and response2.outputs[0].choices:
choice2 = response2.outputs[0].choices[0]
print(f'π€ LLM response with tool context: {choice2.message.content}')
# Add LLM's response to accumulated history using utility
assistant_message2 = convert_llm_response_to_conversation_input(
choice2.message
)
conversation_history.append(assistant_message2)
# Turn 3: Follow-up question (full context + tools)
print('\n--- Turn 3: Follow-up question using accumulated context ---')
user_message2 = conversation.create_user_message(
'Should I bring an umbrella? Also, what about the weather in New York?'
)
conversation_history.append(user_message2)
print(f'π Request 3 context: {len(conversation_history)} messages + tools')
print('π Accumulated context includes:')
print(' β’ Original user query about San Francisco')
print(" β’ Assistant's tool call intention")
print(' β’ Weather tool execution result')
print(" β’ Assistant's weather summary")
print(' β’ New user follow-up question')
input_alpha2_turn3 = conversation.ConversationInputAlpha2(
messages=conversation_history
)
response3 = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2_turn3],
tools=[weather_tool], # Tools still available in turn 3
tool_choice='auto',
temperature=1,
parameters={
'max_tokens': 500,
},
)
if response3.outputs and response3.outputs[0].choices:
choice3 = response3.outputs[0].choices[0]
if choice3.finish_reason == 'tool_calls' and choice3.message.tool_calls:
print(
f'π§ Follow-up tool call: {choice3.message.tool_calls[0].function.name}'
)
# Execute second tool call
tool_call3 = choice3.message.tool_calls[0]
# if not tool_call3.id:
# tool_call3.id = "2"
args3 = json.loads(tool_call3.function.arguments)
weather_result3 = execute_weather_tool(
args3.get('location', 'New York'),
args3.get('unit', 'fahrenheit'),
)
print(f'π€οΈ Second tool result: {weather_result3}')
# Could continue accumulating context for turn 4...
print(
'β
Multi-turn tool calling with proper context accumulation successful!'
)
print(
f'π Final context: {len(conversation_history)} messages + tools available for next turn'
)
else:
print(
f'π¬ Follow-up response using accumulated context: {choice3.message.content}'
)
print(
'β
Multi-turn conversation with proper context accumulation successful!'
)
print(f'π Final context: {len(conversation_history)} messages')
else:
print(
'β οΈ No tool calls found in any output/choice - continuing with regular conversation flow'
)
# Could continue with regular multi-turn conversation without tools
if not assistant_messages:
print('β No assistant messages received in first turn')
except Exception as e:
print(f'β Multi-turn tool calling error: {e}')
def test_multi_turn_tool_calling_alpha2_tool_helpers(self, provider_id: str) -> None:
"""Test multi-turn Alpha2 tool calling with proper context accumulation using higher level abstractions."""
print(
f'\nππ§ Testing multi-turn tool calling with {self.available_providers[provider_id]["display_name"]}'
)
# using decorator
@conversation.tool
def get_weather(location: str, unit: str = 'fahrenheit') -> str:
"""Get the current weather for a location."""
# This is a mock implementation. Replace with actual weather API call.
temp = '72Β°F' if unit == 'fahrenheit' else '22Β°C'
return f'The weather in {location} is sunny with a temperature of {temp}.'
try:
with DaprClient() as client:
conversation_history = [] # our context to pass to the LLM on each turn
# Turn 1: User asks about weather (include tools)
print('\n--- Turn 1: Initial weather query ---')
user_message1 = conversation.create_user_message(
"What's the weather like in San Francisco? Use one of the tools available."
)
conversation_history.append(user_message1)
print(f'π Request 1 context: {len(conversation_history)} messages + tools')
input_alpha2_turn1 = conversation.ConversationInputAlpha2(
messages=conversation_history
)
response1 = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2_turn1],
tools=conversation.get_registered_tools(), # using registered tools (automatically registered by the decorator)
tool_choice='auto',
temperature=1,
parameters={
'max_tokens': 500,
},
)
def append_response_to_history(
response: conversation.ConversationResponseAlpha2, skip_execution: bool = False
):
"""Helper to append response to history and execute tool calls."""
for msg in response.to_assistant_messages():
conversation_history.append(msg)
if not msg.of_assistant.tool_calls:
continue
for _tool_call in msg.of_assistant.tool_calls:
print(f'Executing tool call: {_tool_call.function.name}')
# execute the tool called by the LLM
if not skip_execution:
output = conversation.execute_registered_tool(
_tool_call.function.name, _tool_call.function.arguments
)
print(f'Tool output: {output}')
else:
output = 'tool execution skipped'
# append a result to history
conversation_history.append(
conversation.create_tool_message(
tool_id=_tool_call.id,
name=_tool_call.function.name,
content=output,
)
)
append_response_to_history(response1)
# Turn 2: LLM processes tool result (accumulate context + tools)
print('\n--- Turn 2: LLM processes tool result ---')
print(f'π Request 2 context: {len(conversation_history)} messages + tools')
input_alpha2_turn2 = conversation.ConversationInputAlpha2(
messages=conversation_history
)
response2 = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2_turn2],
tools=conversation.get_registered_tools(),
temperature=1,
parameters={
'max_tokens': 500,
},
)
# Turn 3: Follow-up question (full context + tools)
append_response_to_history(response2)
print('\n--- Turn 3: Follow-up question using accumulated context ---')
user_message2 = conversation.create_user_message(
'Should I bring an umbrella? Also, what about the weather in New York?'
)
conversation_history.append(user_message2)
print(f'π Request 3 context: {len(conversation_history)} messages + tools')
print('π Accumulated context includes:')
input_alpha2_turn3 = conversation.ConversationInputAlpha2(
messages=conversation_history
)
response3 = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2_turn3],
tools=conversation.get_registered_tools(),
tool_choice='auto',
temperature=1,
parameters={
'max_tokens': 500,
},
)
append_response_to_history(response3)
print(f'π Request 4 context: {len(conversation_history)} messages + tools')
print('π Expect response about the umbrella:')
input_alpha2_turn4 = conversation.ConversationInputAlpha2(
messages=conversation_history
)
response4 = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2_turn4],
tools=conversation.get_registered_tools(),
tool_choice='auto',
temperature=1,
parameters={
'max_tokens': 500,
},
)
append_response_to_history(response4, skip_execution=False)
print('Full conversation history trace:')
print(' Tools available:')
for tool in conversation.get_registered_tools():
print(f' - {tool.function.name}({tool.function.parameters["properties"]})')
for msg in conversation_history:
msg.trace_print(2)
except Exception as e:
print(f'β Multi-turn tool calling error: {e}')
finally:
conversation.unregister_tool('get_weather')
def test_function_to_schema_approach(self, provider_id: str) -> None:
"""Test the best DevEx for most cases: function-to-JSON-schema automatic tool creation."""
print(
f'\nπ― Testing function-to-schema approach with {self.available_providers[provider_id]["display_name"]}'
)
try:
with DaprClient() as client:
# Create a tool using the typed function approach
restaurant_tool = create_tool_from_typed_function_example()
print(restaurant_tool)
user_message = conversation.create_user_message(
'I want to find Italian restaurants in San Francisco with a moderate price range.'
)
input_alpha2 = conversation.ConversationInputAlpha2(messages=[user_message])
response = client.converse_alpha2(
name=provider_id,
inputs=[input_alpha2],
tools=[restaurant_tool],
tool_choice='auto',
temperature=1,
parameters={
'max_tokens': 500,
},
)
if response.outputs and response.outputs[0].choices:
choice = response.outputs[0].choices[0]
print(f'π Finish reason: {choice.finish_reason}')
if choice.finish_reason == 'tool_calls' and choice.message.tool_calls:
print('π― Function-to-schema tool calling successful!')
for tool_call in choice.message.tool_calls:
print(f' Tool: {tool_call.function.name}')
print(f' Arguments: {tool_call.function.arguments}')
# This demonstrates the complete workflow
print('β
Auto-generated schema worked perfectly with real LLM!')
else:
print(f'π¬ Response: {choice.message.content}')
else:
print('β No function-to-schema response received')
except Exception as e:
print(f'β Function-to-schema approach error: {e}')