-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
38 lines (32 loc) · 1.1 KB
/
Copy pathProgram.cs
File metadata and controls
38 lines (32 loc) · 1.1 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
#pragma warning disable SKEXP0010
using Microsoft.SemanticKernel;
using System.Text;
using Microsoft.SemanticKernel.ChatCompletion;
// Initialize the Semantic kernel
var kernelBuilder = Kernel.CreateBuilder();
var kernel = kernelBuilder
.AddOpenAIChatCompletion( // We use Semantic Kernel OpenAI API
modelId: "phi3",
apiKey: null,
endpoint: new Uri("http://localhost:11434"))
.Build();
// Create a new chat
var ai = kernel.GetRequiredService<IChatCompletionService>();
ChatHistory chat = new("You will only answer questions about penguins.");
StringBuilder builder = new();
// User question & answer loop
while (true)
{
Console.Write("Question: ");
chat.AddUserMessage(Console.ReadLine()!);
builder.Clear();
// Get the AI response streamed back to the console
await foreach (var message in ai.GetStreamingChatMessageContentsAsync(chat, kernel: kernel))
{
Console.Write(message);
builder.Append(message.Content);
}
Console.WriteLine();
chat.AddAssistantMessage(builder.ToString());
Console.WriteLine();
}