11// <complete_code>
22// <imports>
3- using System . Data ;
43using System . Text . Json ;
54using Microsoft . AI . Foundry . Local ;
65using Betalgo . Ranul . OpenAI . ObjectModels . RequestModels ;
6+ using Betalgo . Ranul . OpenAI . ObjectModels . ResponseModels ;
7+ using Betalgo . Ranul . OpenAI . ObjectModels . SharedModels ;
78using Microsoft . Extensions . Logging ;
89// </imports>
910
1011CancellationToken ct = CancellationToken . None ;
1112
1213// <tool_definitions>
1314// --- Tool definitions ---
14- var tools = new [ ]
15- {
16- new
15+ List < ToolDefinition > tools =
16+ [
17+ new ToolDefinition
1718 {
18- type = "function" ,
19- function = new
19+ Type = "function" ,
20+ Function = new FunctionDefinition ( )
2021 {
21- name = "get_weather" ,
22- description = "Get the current weather for a location" ,
23- parameters = new
22+ Name = "get_weather" ,
23+ Description = "Get the current weather for a location" ,
24+ Parameters = new PropertyDefinition ( )
2425 {
25- type = "object" ,
26- properties = new
26+ Type = "object" ,
27+ Properties = new Dictionary < string , PropertyDefinition > ( )
2728 {
28- location = new
29- {
30- type = "string" ,
31- description = "The city or location"
32- } ,
33- unit = new
34- {
35- type = "string" ,
36- @enum = new [ ] { "celsius" , "fahrenheit" } ,
37- description = "Temperature unit"
38- }
29+ { "location" , new PropertyDefinition ( ) { Type = "string" , Description = "The city or location" } } ,
30+ { "unit" , new PropertyDefinition ( ) { Type = "string" , Description = "Temperature unit (celsius or fahrenheit)" } }
3931 } ,
40- required = new [ ] { "location" }
32+ Required = [ "location" ]
4133 }
4234 }
4335 } ,
44- new
36+ new ToolDefinition
4537 {
46- type = "function" ,
47- function = new
38+ Type = "function" ,
39+ Function = new FunctionDefinition ( )
4840 {
49- name = "calculate" ,
50- description = "Perform a math calculation" ,
51- parameters = new
41+ Name = "calculate" ,
42+ Description = "Perform a math calculation" ,
43+ Parameters = new PropertyDefinition ( )
5244 {
53- type = "object" ,
54- properties = new
45+ Type = "object" ,
46+ Properties = new Dictionary < string , PropertyDefinition > ( )
5547 {
56- expression = new
57- {
58- type = "string" ,
59- description =
60- "The math expression to evaluate"
61- }
48+ { "expression" , new PropertyDefinition ( ) { Type = "string" , Description = "The math expression to evaluate" } }
6249 } ,
63- required = new [ ] { "expression" }
50+ Required = [ "expression" ]
6451 }
6552 }
6653 }
67- } ;
54+ ] ;
6855
6956// --- Tool implementations ---
7057string ExecuteTool ( string functionName , JsonElement arguments )
@@ -91,7 +78,7 @@ string ExecuteTool(string functionName, JsonElement arguments)
9178 . GetString ( ) ?? "" ;
9279 try
9380 {
94- var result = new DataTable ( )
81+ var result = new System . Data . DataTable ( )
9582 . Compute ( expression , null ) ;
9683 return JsonSerializer . Serialize ( new
9784 {
@@ -114,46 +101,6 @@ string ExecuteTool(string functionName, JsonElement arguments)
114101 } ) ;
115102 }
116103}
117-
118- string ProcessToolCalls (
119- List < ChatMessage > msgs ,
120- Betalgo . Ranul . OpenAI . ObjectModels . ResponseModels . ChatCompletionResponse resp ,
121- dynamic client )
122- {
123- var choice = resp . Choices [ 0 ] . Message ;
124-
125- while ( choice . ToolCalls is { Count : > 0 } )
126- {
127- msgs . Add ( choice ) ;
128-
129- foreach ( var toolCall in choice . ToolCalls )
130- {
131- var args = JsonDocument . Parse (
132- toolCall . FunctionCall . Arguments
133- ) . RootElement ;
134- Console . WriteLine (
135- $ " Tool call: { toolCall . FunctionCall . Name } ({ args } )"
136- ) ;
137-
138- var result = ExecuteTool (
139- toolCall . FunctionCall . Name , args
140- ) ;
141- msgs . Add ( new ChatMessage
142- {
143- Role = "tool" ,
144- ToolCallId = toolCall . Id ,
145- Content = result
146- } ) ;
147- }
148-
149- resp = ( ( Task < Betalgo . Ranul . OpenAI . ObjectModels . ResponseModels
150- . ChatCompletionResponse > ) client
151- . CompleteChatAsync ( msgs , ct , tools ) ) . Result ;
152- choice = resp . Choices [ 0 ] . Message ;
153- }
154-
155- return choice . Content ?? "" ;
156- }
157104// </tool_definitions>
158105
159106// <init>
@@ -189,6 +136,7 @@ await model.DownloadAsync(progress =>
189136Console . WriteLine ( "Model loaded and ready." ) ;
190137
191138var chatClient = await model . GetChatClientAsync ( ) ;
139+ chatClient . Settings . ToolChoice = ToolChoice . Auto ;
192140
193141var messages = new List < ChatMessage >
194142{
@@ -222,7 +170,7 @@ await model.DownloadAsync(progress =>
222170 } ) ;
223171
224172 var response = await chatClient . CompleteChatAsync (
225- messages , ct , tools
173+ messages , tools , ct
226174 ) ;
227175
228176 var choice = response . Choices [ 0 ] . Message ;
@@ -233,15 +181,15 @@ await model.DownloadAsync(progress =>
233181
234182 foreach ( var toolCall in choice . ToolCalls )
235183 {
236- var args = JsonDocument . Parse (
184+ var toolArgs = JsonDocument . Parse (
237185 toolCall . FunctionCall . Arguments
238186 ) . RootElement ;
239187 Console . WriteLine (
240- $ " Tool call: { toolCall . FunctionCall . Name } ({ args } )"
188+ $ " Tool call: { toolCall . FunctionCall . Name } ({ toolArgs } )"
241189 ) ;
242190
243191 var result = ExecuteTool (
244- toolCall . FunctionCall . Name , args
192+ toolCall . FunctionCall . Name , toolArgs
245193 ) ;
246194 messages . Add ( new ChatMessage
247195 {
@@ -252,7 +200,7 @@ await model.DownloadAsync(progress =>
252200 }
253201
254202 var finalResponse = await chatClient . CompleteChatAsync (
255- messages , ct , tools
203+ messages , tools , ct
256204 ) ;
257205 var answer = finalResponse . Choices [ 0 ] . Message . Content ?? "" ;
258206 messages . Add ( new ChatMessage
0 commit comments