|
1 | 1 | using Azure; |
2 | 2 | using Azure.AI.Inference; |
3 | 3 | using Azure.Identity; |
| 4 | +using System.Text.Json; |
4 | 5 |
|
5 | 6 | var endpoint = "https://models.inference.ai.azure.com"; |
6 | 7 | var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN"); // Your GitHub Access Token |
|
22 | 23 | } |
23 | 24 |
|
24 | 25 | 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 |
25 | 59 | var options = new ChatCompletionsOptions(chatHistory) |
26 | 60 | { |
27 | | - Model = "Phi-3-medium-4k-instruct" |
| 61 | + Model = "gpt-4o-mini", |
| 62 | + Tools = { def } |
28 | 63 | }; |
29 | 64 |
|
| 65 | + // call the model |
| 66 | + |
30 | 67 | ChatCompletions? response = await client.CompleteAsync(options); |
31 | 68 | 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 | + |
32 | 80 | Console.WriteLine($"Assistant response: {content}"); |
33 | | - |
| 81 | + // Console.WriteLine($"Function call: {functionCall?.Name}"); |
| 82 | + |
| 83 | + // check if tool call, if so, call the tool |
34 | 84 |
|
35 | 85 | } |
36 | 86 |
|
37 | 87 | // TODO: |
| 88 | +// get a function call to work on the llm - CHECK |
38 | 89 | // list tools on mcp server |
39 | 90 | // convert tool call to a tool on llm |
40 | 91 | // do user prompt |
|
0 commit comments