-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathapp.cs
More file actions
51 lines (42 loc) · 1.95 KB
/
app.cs
File metadata and controls
51 lines (42 loc) · 1.95 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
#:package Azure.AI.OpenAI@2.8.0-beta.1
#:package Microsoft.Extensions.AI@10.3.0
#:package Azure.Identity@1.18.0
#:package Microsoft.Extensions.AI.OpenAI@10.3.0
#:package Microsoft.Extensions.Configuration.UserSecrets@10.0.3
#:property UserSecretsId=genai-beginners-dotnet
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using System.ComponentModel;
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
var endpoint = config["AzureOpenAI:Endpoint"]
?? throw new InvalidOperationException("Set AzureOpenAI:Endpoint in User Secrets. See: https://github.com/microsoft/Generative-AI-for-beginners-dotnet/blob/main/01-IntroductionToGenerativeAI/setup-azure-openai.md");
var deploymentName = config["AzureOpenAI:Deployment"] ?? "gpt-5-mini";
IChatClient client = new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential())
.GetChatClient(deploymentName)
.AsIChatClient()
.AsBuilder()
.UseFunctionInvocation()
.Build();
[Description("Get the weather")]
static string GetWeather()
{
var temperature = Random.Shared.Next(5, 20);
var condition = Random.Shared.Next(0, 1) == 0 ? "sunny" : "rainy";
return $"The weather is {temperature} degree C and {condition}";
}
var chatOptions = new ChatOptions
{
Tools = [AIFunctionFactory.Create(GetWeather)],
ModelId = deploymentName
};
client.AsBuilder()
.UseFunctionInvocation()
.Build();
var funcCallingResponseOne = await client.GetResponseAsync("What is today's date?", chatOptions);
var funcCallingResponseTwo = await client.GetResponseAsync("Why don't you tell me about today's temperature?", chatOptions);
var funcCallingResponseThree = await client.GetResponseAsync("Should I bring an umbrella with me today?", chatOptions);
Console.WriteLine($"Response 1: {funcCallingResponseOne}");
Console.WriteLine($"Response 2: {funcCallingResponseTwo}");
Console.WriteLine($"Response 3: {funcCallingResponseThree}");