forked from microsoft/Generative-AI-for-beginners-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
46 lines (39 loc) · 1.4 KB
/
Program.cs
File metadata and controls
46 lines (39 loc) · 1.4 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
#pragma warning disable SKEXP0001, SKEXP0003, SKEXP0010, SKEXP0011, SKEXP0050, SKEXP0052
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using System.Text;
var model = "deepseek-r1-distill-qwen-1.5b-directml-int4-awq-block-128-acc-level-4";
var baseUrl = "http://localhost:5272/v1/";
var apiKey = "unused";
// Create a chat completion service
var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(modelId: model, apiKey: apiKey, endpoint: new Uri(baseUrl))
.Build();
var chat = kernel.GetRequiredService<IChatCompletionService>();
var history = new ChatHistory();
history.AddSystemMessage("You are a useful chatbot. Always reply in a funny way with short answers.");
var settings = new OpenAIPromptExecutionSettings
{
MaxTokens = 50000,
Temperature = 1
};
while (true)
{
Console.Write("Q: ");
var userQuestion = Console.ReadLine();
if (string.IsNullOrWhiteSpace(userQuestion))
{
break;
}
history.AddUserMessage(userQuestion);
var responseBuilder = new StringBuilder();
Console.Write("AI: ");
await foreach (var message in chat.GetStreamingChatMessageContentsAsync(history, settings, kernel))
{
responseBuilder.Append(message.Content);
Console.Write(message.Content);
}
Console.WriteLine();
history.AddAssistantMessage(responseBuilder.ToString());
}