Skip to content

Commit d686384

Browse files
committed
add sample ichatclient and iembeddinggenerator impls
1 parent 2d452cf commit d686384

File tree

25 files changed

+129
-47
lines changed

25 files changed

+129
-47
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: "Sample implementations of IChatClient and IEmbeddingGenerator"
3+
description:
4+
ms.topic: article
5+
ms.date: 05/28/2025
6+
---
7+
8+
# Sample implementations of IChatClient and IEmbeddingGenerator
9+
10+
.NET libraries that provide clients for language models and services can provide an implementations of the <xref:Microsoft.Extensions.AI.IChatClient> and <xref:Microsoft.Extensions.AI.IEmbeddingGenerator`2> interfaces. Any consumers of the interfaces are then able to interoperate seamlessly with these models and services via the abstractions.
11+
12+
## The `IChatClient` interface
13+
14+
The <xref:Microsoft.Extensions.AI.IChatClient> interface defines a client abstraction responsible for interacting with AI services that provide chat capabilities. It includes methods for sending and receiving messages with multi-modal content (such as text, images, and audio), either as a complete set or streamed incrementally. Additionally, it allows for retrieving strongly typed services provided by the client or its underlying services.
15+
16+
.NET libraries that provide clients for language models and services can provide an implementation of the `IChatClient` interface. Any consumers of the interface are then able to interoperate seamlessly with these models and services via the abstractions.
17+
18+
The following sample implements `IChatClient` to show the general structure.
19+
20+
:::code language="csharp" source="./snippets/sample-implementations/SampleChatClient.cs":::
21+
22+
For more realistic, concrete implementations of `IChatClient`, see:
23+
24+
- [AzureAIInferenceChatClient.cs](https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.AI.AzureAIInference/AzureAIInferenceChatClient.cs)
25+
- [OpenAIChatClient.cs](https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs)
26+
- [Microsoft.Extensions.AI chat clients](https://github.com/dotnet/extensions/tree/main/src/Libraries/Microsoft.Extensions.AI/ChatCompletion)
27+
28+
## The `IEmbeddingGenerator<TInput,TEmbedding>` interface
29+
30+
The <xref:Microsoft.Extensions.AI.IEmbeddingGenerator`2> interface represents a generic generator of embeddings. Here, `TInput` is the type of input values being embedded, and `TEmbedding` is the type of generated embedding, which inherits from the <xref:Microsoft.Extensions.AI.Embedding> class.
31+
32+
The `Embedding` class serves as a base class for embeddings generated by an `IEmbeddingGenerator<TInput,TEmbedding>`. It's designed to store and manage the metadata and data associated with embeddings. Derived types, like `Embedding<T>`, provide the concrete embedding vector data. For example, an `Embedding<float>` exposes a `ReadOnlyMemory<float> Vector { get; }` property for access to its embedding data.
33+
34+
The `IEmbeddingGenerator<TInput,TEmbedding>` interface defines a method to asynchronously generate embeddings for a collection of input values, with optional configuration and cancellation support. It also provides metadata describing the generator and allows for the retrieval of strongly typed services that can be provided by the generator or its underlying services.
35+
36+
The following code shows how the `SampleEmbeddingGenerator` class implements the `IEmbeddingGenerator<TInput,TEmbedding>` interface. It has a primary constructor that accepts an endpoint and model ID, which are used to identify the generator. It also implements the <xref:Microsoft.Extensions.AI.IEmbeddingGenerator`2.GenerateAsync(System.Collections.Generic.IEnumerable{`0},Microsoft.Extensions.AI.EmbeddingGenerationOptions,System.Threading.CancellationToken)> method to generate embeddings for a collection of input values.
37+
38+
:::code language="csharp" source="./snippets/sample-implementations/SampleEmbeddingGenerator.cs":::
39+
40+
This sample implementation just generates random embedding vectors. For a more realistic, concrete implementation, see [OpenTelemetryEmbeddingGenerator.cs](https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.AI/Embeddings/OpenTelemetryEmbeddingGenerator.cs).
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.5.0" />
11+
</ItemGroup>
12+
13+
</Project>

docs/ai/snippets/microsoft-extensions-ai/AI.Shared/SampleChatClient.cs renamed to docs/ai/advanced/snippets/sample-implementations/SampleChatClient.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System.Runtime.CompilerServices;
22
using Microsoft.Extensions.AI;
33

4-
public sealed class SampleChatClient(Uri endpoint, string modelId) : IChatClient
4+
public sealed class SampleChatClient(Uri endpoint, string modelId)
5+
: IChatClient
56
{
6-
public ChatClientMetadata Metadata { get; } = new(nameof(SampleChatClient), endpoint, modelId);
7+
public ChatClientMetadata Metadata { get; } =
8+
new(nameof(SampleChatClient), endpoint, modelId);
79

810
public async Task<ChatResponse> GetResponseAsync(
911
IEnumerable<ChatMessage> chatMessages,

docs/ai/snippets/microsoft-extensions-ai/AI.Shared/SampleEmbeddingGenerator.cs renamed to docs/ai/advanced/snippets/sample-implementations/SampleEmbeddingGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public async Task<GeneratedEmbeddings<Embedding<float>>> GenerateAsync(
1616
await Task.Delay(100, cancellationToken);
1717

1818
// Create random embeddings.
19-
return new GeneratedEmbeddings<Embedding<float>>(
20-
from value in values
19+
return [.. from value in values
2120
select new Embedding<float>(
22-
Enumerable.Range(0, 384).Select(_ => Random.Shared.NextSingle()).ToArray()));
21+
Enumerable.Range(0, 384)
22+
.Select(_ => Random.Shared.NextSingle()).ToArray())];
2323
}
2424

2525
public object? GetService(Type serviceType, object? serviceKey) =>

docs/ai/conceptual/chain-of-thought-prompting.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
title: "Chain of Thought Prompting - .NET"
2+
title: "Chain of thought prompting"
33
description: "Learn how chain of thought prompting can simplify prompt engineering."
44
author: catbutler
5-
ms.topic: concept-article #Don't change.
5+
ms.topic: concept-article
66
ms.date: 11/24/2024
77

88
#customer intent: As a .NET developer, I want to understand what chain-of-thought prompting is and how it can help me save time and get better completions out of prompt engineering.
@@ -23,7 +23,7 @@ To use an instruction for chain of thought prompting, include a directive that t
2323

2424
```csharp
2525
prompt= """Instructions: Compare the pros and cons of EVs and petroleum-fueled vehicles.
26-
Break the task into steps, and output the result of each step as you perform it.""";
26+
Break the task into steps, and output the result of each step as you perform it.""";
2727
```
2828

2929
## Use chain of thought prompting in examples
@@ -35,16 +35,16 @@ prompt= """
3535
Instructions: Compare the pros and cons of EVs and petroleum-fueled vehicles.
3636
3737
Differences between EVs and petroleum-fueled vehicles:
38-
-
38+
-
39+
40+
Differences ordered according to overall impact, highest-impact first:
41+
1.
3942
40-
Differences ordered according to overall impact, highest-impact first:
41-
1.
42-
4343
Summary of vehicle type differences as pros and cons:
4444
Pros of EVs
4545
1.
4646
Pros of petroleum-fueled vehicles
47-
1.
47+
1.
4848
""";
4949
```
5050

docs/ai/microsoft-extensions-ai.md

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,14 @@ The following subsections show specific [IChatClient](#the-ichatclient-interface
4343

4444
The following sections show specific [IEmbeddingGenerator](#the-iembeddinggenerator-interface) usage examples:
4545

46-
- [Sample implementation](#sample-implementation)
4746
- [Create embeddings](#create-embeddings)
4847
- [Pipelines of functionality](#pipelines-of-functionality)
4948

5049
### The `IChatClient` interface
5150

5251
The <xref:Microsoft.Extensions.AI.IChatClient> interface defines a client abstraction responsible for interacting with AI services that provide chat capabilities. It includes methods for sending and receiving messages with multi-modal content (such as text, images, and audio), either as a complete set or streamed incrementally. Additionally, it allows for retrieving strongly typed services provided by the client or its underlying services.
5352

54-
.NET libraries that provide clients for language models and services can provide an implementation of the `IChatClient` interface. Any consumers of the interface are then able to interoperate seamlessly with these models and services via the abstractions.
53+
.NET libraries that provide clients for language models and services can provide an implementation of the `IChatClient` interface. Any consumers of the interface are then able to interoperate seamlessly with these models and services via the abstractions. You can see a simple implementation at [Sample implementations of IChatClient and IEmbeddingGenerator](advanced/sample-implementations.md).
5554

5655
#### Request a chat response
5756

@@ -195,25 +194,13 @@ If you don't know ahead of time whether the service is stateless or stateful, yo
195194

196195
### The `IEmbeddingGenerator` interface
197196

198-
The <xref:Microsoft.Extensions.AI.IEmbeddingGenerator`2> interface represents a generic generator of embeddings. Here, `TInput` is the type of input values being embedded, and `TEmbedding` is the type of generated embedding, which inherits from the <xref:Microsoft.Extensions.AI.Embedding> class.
197+
The <xref:Microsoft.Extensions.AI.IEmbeddingGenerator`2> interface represents a generic generator of embeddings. For the generic type parameters, `TInput` is the type of input values being embedded, and `TEmbedding` is the type of generated embedding, which inherits from the <xref:Microsoft.Extensions.AI.Embedding> class.
199198

200-
The `Embedding` class serves as a base class for embeddings generated by an `IEmbeddingGenerator`. It's designed to store and manage the metadata and data associated with embeddings. Derived types, like `Embedding<T>`, provide the concrete embedding vector data. For example, an `Embedding<float>` exposes a `ReadOnlyMemory<float> Vector { get; }` property for access to its embedding data.
199+
The `Embedding` class serves as a base class for embeddings generated by an `IEmbeddingGenerator`. It's designed to store and manage the metadata and data associated with embeddings. Derived types, like <xref:Microsoft.Extensions.AI.Embedding`1>, provide the concrete embedding vector data. For example, an `Embedding<float>` exposes a `ReadOnlyMemory<float> Vector { get; }` property for access to its embedding data.
201200

202201
The `IEmbeddingGenerator` interface defines a method to asynchronously generate embeddings for a collection of input values, with optional configuration and cancellation support. It also provides metadata describing the generator and allows for the retrieval of strongly typed services that can be provided by the generator or its underlying services.
203202

204-
#### Sample implementation
205-
206-
The following sample implementation of `IEmbeddingGenerator` shows the general structure.
207-
208-
:::code language="csharp" source="snippets/microsoft-extensions-ai/AI.Shared/SampleEmbeddingGenerator.cs":::
209-
210-
The preceding code:
211-
212-
- Defines a class named `SampleEmbeddingGenerator` that implements the `IEmbeddingGenerator<string, Embedding<float>>` interface.
213-
- Has a primary constructor that accepts an endpoint and model ID, which are used to identify the generator.
214-
- Implements the `GenerateAsync` method to generate embeddings for a collection of input values.
215-
216-
The sample implementation just generates random embedding vectors. You can find a concrete implementation in the [📦 Microsoft.Extensions.AI.OpenAI](https://www.nuget.org/packages/Microsoft.Extensions.AI.OpenAI) package.
203+
Most users don't need to implement the `IEmbeddingGenerator` interface. However, if you're a library author, you can see a simple implementation at [Sample implementations of IChatClient and IEmbeddingGenerator](advanced/sample-implementations.md).
217204

218205
#### Create embeddings
219206

@@ -247,7 +234,7 @@ In this way, the `RateLimitingEmbeddingGenerator` can be composed with other `IE
247234

248235
You can start building with `Microsoft.Extensions.AI` in the following ways:
249236

250-
- **Library developers**: If you own libraries that provide clients for AI services, consider implementing the interfaces in your libraries. This allows users to easily integrate your NuGet package via the abstractions.
237+
- **Library developers**: If you own libraries that provide clients for AI services, consider implementing the interfaces in your libraries. This allows users to easily integrate your NuGet package via the abstractions. For example implementations, see [Sample implementations of IChatClient and IEmbeddingGenerator](advanced/sample-implementations.md).
251238
- **Service consumers**: If you're developing libraries that consume AI services, use the abstractions instead of hardcoding to a specific AI service. This approach gives your consumers the flexibility to choose their preferred provider.
252239
- **Application developers**: Use the abstractions to simplify integration into your apps. This enables portability across models and services, facilitates testing and mocking, leverages middleware provided by the ecosystem, and maintains a consistent API throughout your app, even if you use different services in different parts of your application.
253240
- **Ecosystem contributors**: If you're interested in contributing to the ecosystem, consider writing custom middleware components.

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.AddMessages/ConsoleAI.AddMessages.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="OllamaSharp" Version="5.1.19" />
12+
</ItemGroup>
13+
1014
<ItemGroup>
1115
<ProjectReference Include="..\AI.Shared\AI.Shared.csproj" />
1216
</ItemGroup>

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.AddMessages/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using Microsoft.Extensions.AI;
2+
using OllamaSharp;
23

3-
IChatClient client = new SampleChatClient(
4-
new Uri("http://coolsite.ai"), "target-ai-model");
4+
IChatClient client = new OllamaApiClient(
5+
new Uri("http://localhost:11434/"), "phi3:mini");
56

67
// <Snippet1>
78
List<ChatMessage> history = [];

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeClientMiddleware/ConsoleAI.ConsumeClientMiddleware.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0-preview.3.25171.5" />
12+
<PackageReference Include="OllamaSharp" Version="5.1.19" />
1213
<ProjectReference Include="..\AI.Shared\AI.Shared.csproj" />
1314
</ItemGroup>
1415

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeClientMiddleware/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
using Microsoft.Extensions.AI;
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.Hosting;
6+
using OllamaSharp;
67

78
// <SnippetUse>
89
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
910

11+
IChatClient client = new OllamaApiClient(
12+
new Uri("http://localhost:11434/"),
13+
"phi3:mini");
14+
1015
builder.Services.AddChatClient(services =>
11-
new SampleChatClient(new Uri("http://localhost"), "test")
16+
client
1217
.AsBuilder()
1318
.UseDistributedCache()
1419
.UseRateLimiting()

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeRateLimitingEmbedding/ConsoleAI.ConsumeRateLimitingEmbedding.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="OllamaSharp" Version="5.1.19" />
12+
</ItemGroup>
13+
1014
<ItemGroup>
1115
<ProjectReference Include="..\AI.Shared\AI.Shared.csproj" />
1216
</ItemGroup>

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeRateLimitingEmbedding/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using Microsoft.Extensions.AI;
2+
using OllamaSharp;
23
using System.Threading.RateLimiting;
34

45
IEmbeddingGenerator<string, Embedding<float>> generator =
56
new RateLimitingEmbeddingGenerator(
6-
new SampleEmbeddingGenerator(new Uri("http://coolsite.ai"), "target-ai-model"),
7+
new OllamaApiClient(new Uri("http://localhost:11434/"), "phi3:mini"),
78
new ConcurrencyLimiter(new()
89
{
910
PermitLimit = 1,

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CreateEmbeddings/ConsoleAI.CreateEmbeddings.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="OllamaSharp" Version="5.1.19" />
12+
</ItemGroup>
13+
1014
<ItemGroup>
1115
<ProjectReference Include="..\AI.Shared\AI.Shared.csproj" />
1216
</ItemGroup>

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CreateEmbeddings/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// <Snippet1>
22
using Microsoft.Extensions.AI;
3+
using OllamaSharp;
34

45
IEmbeddingGenerator<string, Embedding<float>> generator =
5-
new SampleEmbeddingGenerator(
6-
new Uri("http://coolsite.ai"), "target-ai-model");
6+
new OllamaApiClient(new Uri("http://localhost:11434/"), "phi3:mini");
77

88
foreach (Embedding<float> embedding in
99
await generator.GenerateAsync(["What is AI?", "What is .NET?"]))

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CustomEmbeddingsMiddle/ConsoleAI.CustomEmbeddingsMiddle.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="10.0.0-preview.4.25258.110" />
12+
<PackageReference Include="OllamaSharp" Version="5.1.19" />
1213
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.12.0" />
1314
<ProjectReference Include="..\AI.Shared\AI.Shared.csproj" />
1415
</ItemGroup>

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CustomEmbeddingsMiddle/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Caching.Distributed;
33
using Microsoft.Extensions.Caching.Memory;
44
using Microsoft.Extensions.Options;
5+
using OllamaSharp;
56
using OpenTelemetry.Trace;
67

78
// Configure OpenTelemetry exporter
@@ -14,7 +15,7 @@
1415
// Explore changing the order of the intermediate "Use" calls to see
1516
// what impact that has on what gets cached and traced.
1617
IEmbeddingGenerator<string, Embedding<float>> generator = new EmbeddingGeneratorBuilder<string, Embedding<float>>(
17-
new SampleEmbeddingGenerator(new Uri("http://coolsite.ai"), "target-ai-model"))
18+
new OllamaApiClient(new Uri("http://localhost:11434/"), "phi3:mini"))
1819
.UseDistributedCache(
1920
new MemoryDistributedCache(
2021
Options.Create(new MemoryDistributedCacheOptions())))

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetResponseAsyncArgs/ConsoleAI.GetResponseAsyncArgs.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="OllamaSharp" Version="5.1.19" />
12+
</ItemGroup>
13+
1014
<ItemGroup>
1115
<ProjectReference Include="..\AI.Shared\AI.Shared.csproj" />
1216
</ItemGroup>

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetResponseAsyncArgs/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using Microsoft.Extensions.AI;
2+
using OllamaSharp;
23

3-
IChatClient client = new SampleChatClient(
4-
new Uri("http://coolsite.ai"), "target-ai-model");
4+
IChatClient client = new OllamaApiClient(
5+
new Uri("http://localhost:11434/"), "phi3:mini");
56

67
// <Snippet1>
78
Console.WriteLine(await client.GetResponseAsync(

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetStreamingResponseAsync/ConsoleAI.GetStreamingResponseAsync.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="OllamaSharp" Version="5.1.19" />
12+
</ItemGroup>
13+
1014
<ItemGroup>
1115
<ProjectReference Include="..\AI.Shared\AI.Shared.csproj" />
1216
</ItemGroup>

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetStreamingResponseAsync/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using Microsoft.Extensions.AI;
2+
using OllamaSharp;
23

3-
IChatClient client = new SampleChatClient(
4-
new Uri("http://coolsite.ai"), "target-ai-model");
4+
IChatClient client = new OllamaApiClient(
5+
new Uri("http://localhost:11434/"), "phi3:mini");
56

67
// <Snippet1>
78
await foreach (ChatResponseUpdate update in client.GetStreamingResponseAsync("What is AI?"))

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.UseTelemetry/ConsoleAI.UseTelemetry.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="OllamaSharp" Version="5.1.19" />
1112
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.12.0" />
1213
</ItemGroup>
1314

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.UseTelemetry/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.AI;
2+
using OllamaSharp;
23
using OpenTelemetry.Trace;
34

45
// Configure OpenTelemetry exporter.
@@ -8,10 +9,10 @@
89
.AddConsoleExporter()
910
.Build();
1011

11-
var sampleChatClient = new SampleChatClient(
12-
new Uri("http://coolsite.ai"), "target-ai-model");
12+
IChatClient ollamaClient = new OllamaApiClient(
13+
new Uri("http://localhost:11434/"), "phi3:mini");
1314

14-
IChatClient client = new ChatClientBuilder(sampleChatClient)
15+
IChatClient client = new ChatClientBuilder(ollamaClient)
1516
.UseOpenTelemetry(
1617
sourceName: sourceName,
1718
configure: c => c.EnableSensitiveData = true)

docs/ai/snippets/microsoft-extensions-ai/ConsoleAI/ConsoleAI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Microsoft.Extensions.AI" Version="9.5.0" />
12+
<PackageReference Include="OllamaSharp" Version="5.1.19" />
1213
</ItemGroup>
1314

1415
<ItemGroup>
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.AI;
2+
using OllamaSharp;
23

3-
IChatClient client = new SampleChatClient(
4-
new Uri("http://coolsite.ai"), "target-ai-model");
4+
IChatClient client = new OllamaApiClient(
5+
new Uri("http://localhost:11434/"), "phi3:mini");
56

67
Console.WriteLine(await client.GetResponseAsync("What is AI?"));

0 commit comments

Comments
 (0)