Skip to content

Commit bff18d6

Browse files
committed
adding function calling support, still wip for ch3, needs client
1 parent f8a8684 commit bff18d6

File tree

1 file changed

+53
-2
lines changed
  • 03-GettingStarted/03-llm-client/solution/dotnet

1 file changed

+53
-2
lines changed

03-GettingStarted/03-llm-client/solution/dotnet/Program.cs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Azure;
22
using Azure.AI.Inference;
33
using Azure.Identity;
4+
using System.Text.Json;
45

56
var endpoint = "https://models.inference.ai.azure.com";
67
var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN"); // Your GitHub Access Token
@@ -22,19 +23,69 @@
2223
}
2324

2425
chatHistory.Add(new ChatRequestUserMessage(userMessage));
26+
27+
// define tools
28+
FunctionDefinition addFunction = new FunctionDefinition("add")
29+
{
30+
Description = "adds two numbers",
31+
Parameters = BinaryData.FromObjectAsJson(new
32+
{
33+
Type = "object",
34+
Properties = new
35+
{
36+
a = new
37+
{
38+
Type = "integer",
39+
Description = "the first number to add"
40+
},
41+
b = new
42+
{
43+
Type = "integer",
44+
Description = "the second number to add"
45+
}
46+
}
47+
},
48+
new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })
49+
};
50+
51+
52+
ChatCompletionsToolDefinition def = new ChatCompletionsToolDefinition(addFunction);
53+
54+
/*
55+
tool choice requires --enable-auto-tool-choice and --tool-call-parser to be set","type"
56+
*/
57+
58+
// define options
2559
var options = new ChatCompletionsOptions(chatHistory)
2660
{
27-
Model = "Phi-3-medium-4k-instruct"
61+
Model = "gpt-4o-mini",
62+
Tools = { def }
2863
};
2964

65+
// call the model
66+
3067
ChatCompletions? response = await client.CompleteAsync(options);
3168
var content = response.Content;
69+
70+
71+
ChatCompletionsToolCall? calls = response.ToolCalls.FirstOrDefault();
72+
for (int i = 0; i < response.ToolCalls.Count; i++)
73+
{
74+
var call = response.ToolCalls[i];
75+
Console.WriteLine($"Tool call {i}: {call.Name} with arguments {call.Arguments}");
76+
//it works!!, Tool call 0: add with arguments {"a":2,"b":4}
77+
}
78+
79+
3280
Console.WriteLine($"Assistant response: {content}");
33-
81+
// Console.WriteLine($"Function call: {functionCall?.Name}");
82+
83+
// check if tool call, if so, call the tool
3484

3585
}
3686

3787
// TODO:
88+
// get a function call to work on the llm - CHECK
3889
// list tools on mcp server
3990
// convert tool call to a tool on llm
4091
// do user prompt

0 commit comments

Comments
 (0)