diff --git a/docs/ai/advanced/sample-implementations.md b/docs/ai/advanced/sample-implementations.md new file mode 100644 index 0000000000000..76b258120a7c2 --- /dev/null +++ b/docs/ai/advanced/sample-implementations.md @@ -0,0 +1,38 @@ +--- +title: "Sample implementations of IChatClient and IEmbeddingGenerator" +description: Learn more about the IChatClient and IEmbeddingGenerator interfaces, see simple implementations, and find links to concrete implementations. +ms.topic: article +ms.date: 05/28/2025 +--- + +# Sample implementations of IChatClient and IEmbeddingGenerator + +.NET libraries that provide clients for language models and services can provide implementations of the and interfaces. Any consumers of the interfaces are then able to interoperate seamlessly with these models and services via the abstractions. + +## The `IChatClient` interface + +The 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. + +The following sample implements `IChatClient` to show the general structure. + +:::code language="csharp" source="./snippets/sample-implementations/SampleChatClient.cs"::: + +For more realistic, concrete implementations of `IChatClient`, see: + +- [AzureAIInferenceChatClient.cs](https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.AI.AzureAIInference/AzureAIInferenceChatClient.cs) +- [OpenAIChatClient.cs](https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs) +- [Microsoft.Extensions.AI chat clients](https://github.com/dotnet/extensions/tree/main/src/Libraries/Microsoft.Extensions.AI/ChatCompletion) + +## The `IEmbeddingGenerator` interface + +The 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 class. + +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`, provide the concrete embedding vector data. For example, an `Embedding` exposes a `ReadOnlyMemory Vector { get; }` property for access to its embedding data. + +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. + +The following code shows how the `SampleEmbeddingGenerator` class implements the `IEmbeddingGenerator` interface. It has a primary constructor that accepts an endpoint and model ID, which are used to identify the generator. It also implements the method to generate embeddings for a collection of input values. + +:::code language="csharp" source="./snippets/sample-implementations/SampleEmbeddingGenerator.cs"::: + +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). diff --git a/docs/ai/advanced/snippets/sample-implementations/Implementations.csproj b/docs/ai/advanced/snippets/sample-implementations/Implementations.csproj new file mode 100644 index 0000000000000..efb828e6cb8ee --- /dev/null +++ b/docs/ai/advanced/snippets/sample-implementations/Implementations.csproj @@ -0,0 +1,13 @@ + + + + net9.0 + enable + enable + + + + + + + diff --git a/docs/ai/snippets/microsoft-extensions-ai/AI.Shared/SampleChatClient.cs b/docs/ai/advanced/snippets/sample-implementations/SampleChatClient.cs similarity index 93% rename from docs/ai/snippets/microsoft-extensions-ai/AI.Shared/SampleChatClient.cs rename to docs/ai/advanced/snippets/sample-implementations/SampleChatClient.cs index 616abbb121370..0b8039cca7906 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/AI.Shared/SampleChatClient.cs +++ b/docs/ai/advanced/snippets/sample-implementations/SampleChatClient.cs @@ -1,9 +1,11 @@ using System.Runtime.CompilerServices; using Microsoft.Extensions.AI; -public sealed class SampleChatClient(Uri endpoint, string modelId) : IChatClient +public sealed class SampleChatClient(Uri endpoint, string modelId) + : IChatClient { - public ChatClientMetadata Metadata { get; } = new(nameof(SampleChatClient), endpoint, modelId); + public ChatClientMetadata Metadata { get; } = + new(nameof(SampleChatClient), endpoint, modelId); public async Task GetResponseAsync( IEnumerable chatMessages, diff --git a/docs/ai/snippets/microsoft-extensions-ai/AI.Shared/SampleEmbeddingGenerator.cs b/docs/ai/advanced/snippets/sample-implementations/SampleEmbeddingGenerator.cs similarity index 84% rename from docs/ai/snippets/microsoft-extensions-ai/AI.Shared/SampleEmbeddingGenerator.cs rename to docs/ai/advanced/snippets/sample-implementations/SampleEmbeddingGenerator.cs index ddf1e6b53aa28..2b08d28b25a22 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/AI.Shared/SampleEmbeddingGenerator.cs +++ b/docs/ai/advanced/snippets/sample-implementations/SampleEmbeddingGenerator.cs @@ -16,10 +16,10 @@ public async Task>> GenerateAsync( await Task.Delay(100, cancellationToken); // Create random embeddings. - return new GeneratedEmbeddings>( - from value in values + return [.. from value in values select new Embedding( - Enumerable.Range(0, 384).Select(_ => Random.Shared.NextSingle()).ToArray())); + Enumerable.Range(0, 384) + .Select(_ => Random.Shared.NextSingle()).ToArray())]; } public object? GetService(Type serviceType, object? serviceKey) => diff --git a/docs/ai/microsoft-extensions-ai.md b/docs/ai/microsoft-extensions-ai.md index d2e3521ea75fe..5e204f1963888 100644 --- a/docs/ai/microsoft-extensions-ai.md +++ b/docs/ai/microsoft-extensions-ai.md @@ -43,7 +43,6 @@ The following subsections show specific [IChatClient](#the-ichatclient-interface The following sections show specific [IEmbeddingGenerator](#the-iembeddinggenerator-interface) usage examples: -- [Sample implementation](#sample-implementation) - [Create embeddings](#create-embeddings) - [Pipelines of functionality](#pipelines-of-functionality) @@ -51,7 +50,7 @@ The following sections show specific [IEmbeddingGenerator](#the-iembeddinggenera The 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. -.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. +.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). #### Request a chat response @@ -195,25 +194,13 @@ If you don't know ahead of time whether the service is stateless or stateful, yo ### The `IEmbeddingGenerator` interface -The 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 class. +The 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 class. -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`, provide the concrete embedding vector data. For example, an `Embedding` exposes a `ReadOnlyMemory Vector { get; }` property for access to its embedding data. +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 , provide the concrete embedding vector data. For example, an `Embedding` exposes a `ReadOnlyMemory Vector { get; }` property for access to its embedding data. 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. -#### Sample implementation - -The following sample implementation of `IEmbeddingGenerator` shows the general structure. - -:::code language="csharp" source="snippets/microsoft-extensions-ai/AI.Shared/SampleEmbeddingGenerator.cs"::: - -The preceding code: - -- Defines a class named `SampleEmbeddingGenerator` that implements the `IEmbeddingGenerator>` interface. -- Has a primary constructor that accepts an endpoint and model ID, which are used to identify the generator. -- Implements the `GenerateAsync` method to generate embeddings for a collection of input values. - -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. +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). #### Create embeddings @@ -247,7 +234,7 @@ In this way, the `RateLimitingEmbeddingGenerator` can be composed with other `IE You can start building with `Microsoft.Extensions.AI` in the following ways: -- **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. +- **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). - **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. - **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. - **Ecosystem contributors**: If you're interested in contributing to the ecosystem, consider writing custom middleware components. diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.AddMessages/ConsoleAI.AddMessages.csproj b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.AddMessages/ConsoleAI.AddMessages.csproj index 821fdd5c951db..e64080331bc30 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.AddMessages/ConsoleAI.AddMessages.csproj +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.AddMessages/ConsoleAI.AddMessages.csproj @@ -7,6 +7,10 @@ enable + + + + diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.AddMessages/Program.cs b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.AddMessages/Program.cs index af06bf9c80829..b2e0466ce0d74 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.AddMessages/Program.cs +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.AddMessages/Program.cs @@ -1,7 +1,8 @@ using Microsoft.Extensions.AI; +using OllamaSharp; -IChatClient client = new SampleChatClient( - new Uri("http://coolsite.ai"), "target-ai-model"); +IChatClient client = new OllamaApiClient( + new Uri("http://localhost:11434/"), "phi3:mini"); // List history = []; diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeClientMiddleware/ConsoleAI.ConsumeClientMiddleware.csproj b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeClientMiddleware/ConsoleAI.ConsumeClientMiddleware.csproj index bec56340237d7..697bf0cf24c7a 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeClientMiddleware/ConsoleAI.ConsumeClientMiddleware.csproj +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeClientMiddleware/ConsoleAI.ConsumeClientMiddleware.csproj @@ -9,6 +9,7 @@ + diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeClientMiddleware/Program.cs b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeClientMiddleware/Program.cs index d59952ee6f485..cf72dad6c4d06 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeClientMiddleware/Program.cs +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeClientMiddleware/Program.cs @@ -3,12 +3,17 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using OllamaSharp; // HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); +IChatClient client = new OllamaApiClient( + new Uri("http://localhost:11434/"), + "phi3:mini"); + builder.Services.AddChatClient(services => - new SampleChatClient(new Uri("http://localhost"), "test") + client .AsBuilder() .UseDistributedCache() .UseRateLimiting() diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeRateLimitingEmbedding/ConsoleAI.ConsumeRateLimitingEmbedding.csproj b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeRateLimitingEmbedding/ConsoleAI.ConsumeRateLimitingEmbedding.csproj index b615dd1b868c2..6e0c682243e90 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeRateLimitingEmbedding/ConsoleAI.ConsumeRateLimitingEmbedding.csproj +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeRateLimitingEmbedding/ConsoleAI.ConsumeRateLimitingEmbedding.csproj @@ -7,6 +7,10 @@ enable + + + + diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeRateLimitingEmbedding/Program.cs b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeRateLimitingEmbedding/Program.cs index b6bb41eb7e5af..4ce9b0ca7d089 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeRateLimitingEmbedding/Program.cs +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.ConsumeRateLimitingEmbedding/Program.cs @@ -1,9 +1,10 @@ using Microsoft.Extensions.AI; +using OllamaSharp; using System.Threading.RateLimiting; IEmbeddingGenerator> generator = new RateLimitingEmbeddingGenerator( - new SampleEmbeddingGenerator(new Uri("http://coolsite.ai"), "target-ai-model"), + new OllamaApiClient(new Uri("http://localhost:11434/"), "phi3:mini"), new ConcurrencyLimiter(new() { PermitLimit = 1, diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CreateEmbeddings/ConsoleAI.CreateEmbeddings.csproj b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CreateEmbeddings/ConsoleAI.CreateEmbeddings.csproj index b615dd1b868c2..6e0c682243e90 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CreateEmbeddings/ConsoleAI.CreateEmbeddings.csproj +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CreateEmbeddings/ConsoleAI.CreateEmbeddings.csproj @@ -7,6 +7,10 @@ enable + + + + diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CreateEmbeddings/Program.cs b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CreateEmbeddings/Program.cs index dfe3a9e5da91d..de2641558fe00 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CreateEmbeddings/Program.cs +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CreateEmbeddings/Program.cs @@ -1,9 +1,9 @@ // using Microsoft.Extensions.AI; +using OllamaSharp; IEmbeddingGenerator> generator = - new SampleEmbeddingGenerator( - new Uri("http://coolsite.ai"), "target-ai-model"); + new OllamaApiClient(new Uri("http://localhost:11434/"), "phi3:mini"); foreach (Embedding embedding in await generator.GenerateAsync(["What is AI?", "What is .NET?"])) diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CustomEmbeddingsMiddle/ConsoleAI.CustomEmbeddingsMiddle.csproj b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CustomEmbeddingsMiddle/ConsoleAI.CustomEmbeddingsMiddle.csproj index 5cd740ab9bb58..f83ec782c16dc 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CustomEmbeddingsMiddle/ConsoleAI.CustomEmbeddingsMiddle.csproj +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CustomEmbeddingsMiddle/ConsoleAI.CustomEmbeddingsMiddle.csproj @@ -9,6 +9,7 @@ + diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CustomEmbeddingsMiddle/Program.cs b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CustomEmbeddingsMiddle/Program.cs index f35794faf6dff..97314bfb4e72d 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CustomEmbeddingsMiddle/Program.cs +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.CustomEmbeddingsMiddle/Program.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; +using OllamaSharp; using OpenTelemetry.Trace; // Configure OpenTelemetry exporter @@ -14,7 +15,7 @@ // Explore changing the order of the intermediate "Use" calls to see // what impact that has on what gets cached and traced. IEmbeddingGenerator> generator = new EmbeddingGeneratorBuilder>( - new SampleEmbeddingGenerator(new Uri("http://coolsite.ai"), "target-ai-model")) + new OllamaApiClient(new Uri("http://localhost:11434/"), "phi3:mini")) .UseDistributedCache( new MemoryDistributedCache( Options.Create(new MemoryDistributedCacheOptions()))) diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetResponseAsyncArgs/ConsoleAI.GetResponseAsyncArgs.csproj b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetResponseAsyncArgs/ConsoleAI.GetResponseAsyncArgs.csproj index b615dd1b868c2..6e0c682243e90 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetResponseAsyncArgs/ConsoleAI.GetResponseAsyncArgs.csproj +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetResponseAsyncArgs/ConsoleAI.GetResponseAsyncArgs.csproj @@ -7,6 +7,10 @@ enable + + + + diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetResponseAsyncArgs/Program.cs b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetResponseAsyncArgs/Program.cs index 92bb0e9f6891b..0dfb9ddbb5364 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetResponseAsyncArgs/Program.cs +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetResponseAsyncArgs/Program.cs @@ -1,7 +1,8 @@ using Microsoft.Extensions.AI; +using OllamaSharp; -IChatClient client = new SampleChatClient( - new Uri("http://coolsite.ai"), "target-ai-model"); +IChatClient client = new OllamaApiClient( + new Uri("http://localhost:11434/"), "phi3:mini"); // Console.WriteLine(await client.GetResponseAsync( diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetStreamingResponseAsync/ConsoleAI.GetStreamingResponseAsync.csproj b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetStreamingResponseAsync/ConsoleAI.GetStreamingResponseAsync.csproj index b615dd1b868c2..6e0c682243e90 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetStreamingResponseAsync/ConsoleAI.GetStreamingResponseAsync.csproj +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetStreamingResponseAsync/ConsoleAI.GetStreamingResponseAsync.csproj @@ -7,6 +7,10 @@ enable + + + + diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetStreamingResponseAsync/Program.cs b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetStreamingResponseAsync/Program.cs index 37f80109796ce..a3e5531344276 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetStreamingResponseAsync/Program.cs +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.GetStreamingResponseAsync/Program.cs @@ -1,7 +1,8 @@ using Microsoft.Extensions.AI; +using OllamaSharp; -IChatClient client = new SampleChatClient( - new Uri("http://coolsite.ai"), "target-ai-model"); +IChatClient client = new OllamaApiClient( + new Uri("http://localhost:11434/"), "phi3:mini"); // await foreach (ChatResponseUpdate update in client.GetStreamingResponseAsync("What is AI?")) diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.UseTelemetry/ConsoleAI.UseTelemetry.csproj b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.UseTelemetry/ConsoleAI.UseTelemetry.csproj index 4b83cfa2dfb35..1b4af64b74041 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.UseTelemetry/ConsoleAI.UseTelemetry.csproj +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.UseTelemetry/ConsoleAI.UseTelemetry.csproj @@ -8,6 +8,7 @@ + diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.UseTelemetry/Program.cs b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.UseTelemetry/Program.cs index db1c7a2502712..4a0b7fc620d48 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.UseTelemetry/Program.cs +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI.UseTelemetry/Program.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.AI; +using OllamaSharp; using OpenTelemetry.Trace; // Configure OpenTelemetry exporter. @@ -8,10 +9,10 @@ .AddConsoleExporter() .Build(); -var sampleChatClient = new SampleChatClient( - new Uri("http://coolsite.ai"), "target-ai-model"); +IChatClient ollamaClient = new OllamaApiClient( + new Uri("http://localhost:11434/"), "phi3:mini"); -IChatClient client = new ChatClientBuilder(sampleChatClient) +IChatClient client = new ChatClientBuilder(ollamaClient) .UseOpenTelemetry( sourceName: sourceName, configure: c => c.EnableSensitiveData = true) diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI/ConsoleAI.csproj b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI/ConsoleAI.csproj index a001a0f99a5b2..a5e809fa597f1 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI/ConsoleAI.csproj +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI/ConsoleAI.csproj @@ -9,6 +9,7 @@ + diff --git a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI/Program.cs b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI/Program.cs index f92735dee5e72..4543467a9a112 100644 --- a/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI/Program.cs +++ b/docs/ai/snippets/microsoft-extensions-ai/ConsoleAI/Program.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.AI; +using OllamaSharp; -IChatClient client = new SampleChatClient( - new Uri("http://coolsite.ai"), "target-ai-model"); +IChatClient client = new OllamaApiClient( + new Uri("http://localhost:11434/"), "phi3:mini"); Console.WriteLine(await client.GetResponseAsync("What is AI?")); diff --git a/docs/ai/toc.yml b/docs/ai/toc.yml index b2094a9ffad56..5b2b95be506f7 100644 --- a/docs/ai/toc.yml +++ b/docs/ai/toc.yml @@ -91,6 +91,10 @@ items: href: tutorials/evaluate-with-reporting.md - name: "Evaluate response safety with caching and reporting" href: tutorials/evaluate-safety.md +- name: Advanced + items: + - name: Sample interface implementations + href: advanced/sample-implementations.md - name: Resources items: - name: API reference