-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathapp.cs
More file actions
49 lines (38 loc) · 1.65 KB
/
app.cs
File metadata and controls
49 lines (38 loc) · 1.65 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
#:package Microsoft.Extensions.AI.Ollama@9.7.0-preview.1.25356.2
#:package Microsoft.Extensions.Configuration.UserSecrets@10.0.3
#:property UserSecretsId=genai-beginners-dotnet
using Microsoft.Extensions.AI;
// Create chat client (using Ollama as an example, but this works with any IChatClient)
IChatClient client = new OllamaChatClient(new Uri("http://localhost:11434"), "phi4-mini");
// Create a conversation history list
// This is the CORRECT way to initialize a conversation with a system message
List<ChatMessage> conversation = new()
{
new ChatMessage(ChatRole.System, "You are a good assistance with short and smart answers")
};
bool loopCheck = true;
while (loopCheck)
{
Console.WriteLine("conversation/press any key to Exit app");
var askCommand = Console.ReadLine();
if (askCommand == "conversation")
{
// Get user input
string question = Console.ReadLine() ?? "";
// Add user message to conversation history
conversation.Add(new ChatMessage(ChatRole.User, question));
// Get response from the AI model
var response = await client.GetResponseAsync(conversation);
// IMPORTANT: The response object has a .Text property, NOT .Messages
// To add the assistant's response to the conversation history:
// CORRECT way:
conversation.Add(new ChatMessage(ChatRole.Assistant, response.Text));
// INCORRECT way (this will cause a compile error):
// conversation.Add(response.Messages); // ❌ response doesn't have .Messages property
Console.WriteLine($"AI: {response.Text}");
}
else
{
loopCheck = false;
}
}