Skip to content

Commit 65b0657

Browse files
CopilotIEvangelistgewarren
authored
Update Ollama Microsoft.Extensions.AI integration documentation to use current recommended patterns (#4145)
* Initial plan * Update Ollama Microsoft.Extensions.AI integration to use current recommended patterns Co-authored-by: IEvangelist <[email protected]> * Fix Microsoft.Extensions.AI reference to include NuGet package link Co-authored-by: IEvangelist <[email protected]> * Update method references to use xrefs for better documentation linking Co-authored-by: gewarren <[email protected]> * Improve readability by wrapping xref links in parentheses Co-authored-by: gewarren <[email protected]> * Revert xref changes from 48b1cd6 as those APIs don't exist in documentation Co-authored-by: IEvangelist <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: IEvangelist <[email protected]> Co-authored-by: gewarren <[email protected]>
1 parent 6d476fc commit 65b0657

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

docs/community-toolkit/ollama.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,20 @@ Then the connection string will be retrieved from the `ConnectionStrings` config
194194

195195
### Integration with `Microsoft.Extensions.AI`
196196

197-
The Microsoft.Extensions.AI library provides an abstraction over the Ollama client API, using generic interfaces. OllamaSharp supports these interfaces, and they can be registered using the `AddOllamaSharpChatClient` and `AddOllamaSharpEmbeddingGenerator` extension methods. These methods will also register the `IOllamaClientApi` instances with the dependency injection container, and have keyed versions for multiple instances.
197+
The [📦 Microsoft.Extensions.AI](https://www.nuget.org/packages/Microsoft.Extensions.AI) NuGet package provides an abstraction over the Ollama client API, using generic interfaces. OllamaSharp supports these interfaces, and they can be registered by chaining either the <xref:Microsoft.Extensions.AI.IChatClient> or <xref:Microsoft.Extensions.AI.IEmbeddingGenerator`2> registration methods to the `AddOllamaClientApi` method.
198+
199+
To register an `IChatClient`, chain the `AddChatClient` method to the `AddOllamaClientApi` method:
200+
201+
```csharp
202+
builder.AddOllamaClientApi("llama")
203+
.AddChatClient();
204+
```
205+
206+
Similarly, to register an `IEmbeddingGenerator`, chain the `AddEmbeddingGenerator` method:
198207

199208
```csharp
200-
builder.AddOllamaSharpChatClient("llama");
209+
builder.AddOllamaClientApi("llama")
210+
.AddEmbeddingGenerator();
201211
```
202212

203213
After adding `IChatClient` to the builder, you can get the `IChatClient` instance using dependency injection. For example, to retrieve your context object from service:
@@ -209,6 +219,28 @@ public class ExampleService(IChatClient chatClient)
209219
}
210220
```
211221

222+
### Add keyed Microsoft.Extensions.AI clients
223+
224+
There might be situations where you want to register multiple AI client instances with different connection names. To register keyed AI clients, use the keyed versions of the registration methods:
225+
226+
```csharp
227+
builder.AddOllamaClientApi("chat")
228+
.AddKeyedChatClient("chat");
229+
builder.AddOllamaClientApi("embeddings")
230+
.AddKeyedEmbeddingGenerator("embeddings");
231+
```
232+
233+
Then you can retrieve the AI client instances using dependency injection. For example, to retrieve the clients from an example service:
234+
235+
```csharp
236+
public class ExampleService(
237+
[FromKeyedServices("chat")] IChatClient chatClient,
238+
[FromKeyedServices("embeddings")] IEmbeddingGenerator<string, Embedding<float>> embeddingGenerator)
239+
{
240+
// Use AI clients...
241+
}
242+
```
243+
212244
## See also
213245

214246
- [Ollama](https://ollama.com)

0 commit comments

Comments
 (0)