-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update code sample for new Ollama approach (#43258)
* Update packages and code sample for new Ollama approach
- Loading branch information
1 parent
8b79865
commit d8ababc
Showing
3 changed files
with
52 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
|
||
// Create a kernel with OpenAI chat completion | ||
// Warning due to the experimental state of some Semantic Kernel SDK features. | ||
#pragma warning disable SKEXP0070 | ||
Kernel kernel = Kernel.CreateBuilder() | ||
.AddOllamaChatCompletion( | ||
modelId: "phi3:mini", | ||
endpoint: new Uri("http://localhost:11434")) | ||
.Build(); | ||
|
||
var aiChatService = kernel.GetRequiredService<IChatCompletionService>(); | ||
var chatHistory = new ChatHistory(); | ||
|
||
while (true) | ||
{ | ||
// Get user prompt and add to chat history | ||
Console.WriteLine("Your prompt:"); | ||
var userPrompt = Console.ReadLine(); | ||
chatHistory.Add(new ChatMessageContent(AuthorRole.User, userPrompt)); | ||
|
||
// Stream the AI response and add to chat history | ||
Console.WriteLine("AI Response:"); | ||
var response = ""; | ||
await foreach(var item in | ||
aiChatService.GetStreamingChatMessageContentsAsync(chatHistory)) | ||
{ | ||
Console.Write(item.Content); | ||
response += item.Content; | ||
} | ||
chatHistory.Add(new ChatMessageContent(AuthorRole.Assistant, response)); | ||
Console.WriteLine(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.SemanticKernel" Version="1.25.0" /> | ||
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Ollama" Version="1.22.0-alpha" /> | ||
</ItemGroup> | ||
|
||
</Project> |