-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
61 lines (51 loc) · 2.23 KB
/
Copy pathProgram.cs
File metadata and controls
61 lines (51 loc) · 2.23 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
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using OllamaSharp;
using OpenAI;
var config = new ConfigurationBuilder()
.AddUserSecrets<Program>(optional: true)
.AddEnvironmentVariables()
.Build();
IChatClient chatClient = ChooseClient(config, args.FirstOrDefault());
List<ChatMessage> history =
[
new(ChatRole.System, """
You are Chef Byte, a friendly recipe assistant. Suggest one recipe per message.
Include the name, a one-line description, key ingredients, and total time.
Politely decline non-food topics.
"""),
new(ChatRole.User, "I have chicken, garlic, and lemon. What can I make?")
];
ChatResponse response = await chatClient.GetResponseAsync(history);
Console.WriteLine($"[Assistant]: {response.Text}");
history.AddMessages(response);
history.Add(new ChatMessage(ChatRole.User, "Can I use thighs instead of breast?"));
Console.WriteLine();
Console.Write("[Assistant streaming]: ");
await foreach (var update in chatClient.GetStreamingResponseAsync(history))
{
Console.Write(update.Text);
}
Console.WriteLine();
static IChatClient ChooseClient(IConfiguration config, string? profile)
{
profile ??= config["Provider"] ?? "ollama";
return profile.ToLowerInvariant() switch
{
"openai" => new OpenAIClient(
config["OpenAI:ApiKey"] ?? Environment.GetEnvironmentVariable("OPENAI_API_KEY")
?? throw new InvalidOperationException("Set OpenAI:ApiKey or OPENAI_API_KEY."))
.GetChatClient(config["OpenAI:Model"] ?? "gpt-4o-mini")
.AsIChatClient(),
"github" => new OpenAIClient(
new System.ClientModel.ApiKeyCredential(
config["GitHub:Token"] ?? Environment.GetEnvironmentVariable("GITHUB_TOKEN")
?? throw new InvalidOperationException("Set GitHub:Token or GITHUB_TOKEN.")),
new OpenAIClientOptions { Endpoint = new Uri("https://models.inference.ai.azure.com") })
.GetChatClient(config["GitHub:Model"] ?? "gpt-4o-mini")
.AsIChatClient(),
_ => new OllamaApiClient(
new Uri(config["Ollama:Endpoint"] ?? "http://localhost:11434"),
config["Ollama:Model"] ?? "phi4-mini"),
};
}