Skip to content

Commit d8ababc

Browse files
authored
Update code sample for new Ollama approach (#43258)
* Update packages and code sample for new Ollama approach
1 parent 8b79865 commit d8ababc

File tree

3 files changed

+52
-40
lines changed

3 files changed

+52
-40
lines changed

docs/ai/quickstarts/quickstart-local-ai.md

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ Complete the following steps to create a .NET console app that will connect to y
5454
dotnet new console
5555
```
5656

57-
1. Add the Semantic Kernel SDK package to your app:
57+
1. Add the [Semantic Kernel SDK](https://www.nuget.org/packages/Microsoft.SemanticKernel) and the [Semantic Kernel Ollama Connector](https://www.nuget.org/packages/Microsoft.SemanticKernel.Connectors.Ollama/1.25.0-alpha) packages to your app:
5858

5959
```dotnetcli
6060
dotnet add package Microsoft.SemanticKernel
61+
dotnet add package Microsoft.SemanticKernel.Connectors.Ollama
6162
```
6263

6364
1. Open the new app in your editor of choice, such as Visual Studio Code.
@@ -72,45 +73,7 @@ The Semantic Kernel SDK provides many services and features to connect to AI mod
7273
7374
1. Open the _Program.cs_ file and replace the contents of the file with the following code:
7475
75-
```csharp
76-
using Microsoft.SemanticKernel;
77-
using Microsoft.SemanticKernel.ChatCompletion;
78-
79-
// Create a kernel with OpenAI chat completion
80-
#pragma warning disable SKEXP0010
81-
Kernel kernel = Kernel.CreateBuilder()
82-
.AddOpenAIChatCompletion(
83-
modelId: "phi3:mini",
84-
endpoint: new Uri("http://localhost:11434"),
85-
apiKey: "")
86-
.Build();
87-
88-
var aiChatService = kernel.GetRequiredService<IChatCompletionService>();
89-
var chatHistory = new ChatHistory();
90-
91-
while (true)
92-
{
93-
// Get user prompt and add to chat history
94-
Console.WriteLine("Your prompt:");
95-
var userPrompt = Console.ReadLine();
96-
chatHistory.Add(new ChatMessageContent(AuthorRole.User, userPrompt));
97-
98-
// Stream the AI response and add to chat history
99-
Console.WriteLine("AI Response:");
100-
var response = "";
101-
await foreach(var item in
102-
aiChatService.GetStreamingChatMessageContentsAsync(chatHistory))
103-
{
104-
Console.Write(item.Content);
105-
response += item.Content;
106-
}
107-
chatHistory.Add(new ChatMessageContent(AuthorRole.Assistant, response));
108-
Console.WriteLine();
109-
}
110-
```
111-
112-
> [!NOTE]
113-
> The `#pragma warning disable SKEXP0010` line is included due to the experimental state of some Semantic Kernel SDK features.
76+
:::code language="csharp" source="snippets/local-ai/program.cs" :::
11477
11578
The preceding code accomplishes the following tasks:
11679
- Creates a `Kernel` object and uses it to retrieve a chat completion service.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.SemanticKernel;
2+
using Microsoft.SemanticKernel.ChatCompletion;
3+
4+
// Create a kernel with OpenAI chat completion
5+
// Warning due to the experimental state of some Semantic Kernel SDK features.
6+
#pragma warning disable SKEXP0070
7+
Kernel kernel = Kernel.CreateBuilder()
8+
.AddOllamaChatCompletion(
9+
modelId: "phi3:mini",
10+
endpoint: new Uri("http://localhost:11434"))
11+
.Build();
12+
13+
var aiChatService = kernel.GetRequiredService<IChatCompletionService>();
14+
var chatHistory = new ChatHistory();
15+
16+
while (true)
17+
{
18+
// Get user prompt and add to chat history
19+
Console.WriteLine("Your prompt:");
20+
var userPrompt = Console.ReadLine();
21+
chatHistory.Add(new ChatMessageContent(AuthorRole.User, userPrompt));
22+
23+
// Stream the AI response and add to chat history
24+
Console.WriteLine("AI Response:");
25+
var response = "";
26+
await foreach(var item in
27+
aiChatService.GetStreamingChatMessageContentsAsync(chatHistory))
28+
{
29+
Console.Write(item.Content);
30+
response += item.Content;
31+
}
32+
chatHistory.Add(new ChatMessageContent(AuthorRole.Assistant, response));
33+
Console.WriteLine();
34+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.SemanticKernel" Version="1.25.0" />
12+
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Ollama" Version="1.22.0-alpha" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)