|
| 1 | +--- |
| 2 | +title: Agent based on any IChatClient |
| 3 | +description: Learn how to use the Microsoft Agent Framework with any IChatClient implementation. |
| 4 | +zone_pivot_groups: programming-languages |
| 5 | +author: westey-m |
| 6 | +ms.topic: tutorial |
| 7 | +ms.author: westey |
| 8 | +ms.date: 09/25/2025 |
| 9 | +ms.service: semantic-kernel |
| 10 | +--- |
| 11 | + |
| 12 | +# Agent based on any IChatClient |
| 13 | + |
| 14 | +::: zone pivot="programming-language-csharp" |
| 15 | + |
| 16 | +The Microsoft Agent Framework supports creating agents for any inference service that provides a [`Microsoft.Extensions.AI.IChatClient`](/dotnet/ai/microsoft-extensions-ai#the-ichatclient-interface) implementation. This means that there is a very broad range of services that can be used to create agents, including open source models that can be run locally. |
| 17 | + |
| 18 | +In this document, we will use Ollama as an example. |
| 19 | + |
| 20 | +## Getting Started |
| 21 | + |
| 22 | +Add the required NuGet packages to your project. |
| 23 | + |
| 24 | +```powershell |
| 25 | +dotnet add package Microsoft.Extensions.AI.Agents |
| 26 | +``` |
| 27 | + |
| 28 | +You will also need to add the package for the specific `IChatClient` implementation you want to use. In this example, we will use [OllamaSharp](https://www.nuget.org/packages/OllamaSharp/). |
| 29 | + |
| 30 | +```powershell |
| 31 | +dotnet add package OllamaSharp |
| 32 | +``` |
| 33 | + |
| 34 | +## Creating a ChatClientAgent |
| 35 | + |
| 36 | +To create an agent based on the `IChatClient` interface, you can use the `ChatClientAgent` class. |
| 37 | +The `ChatClientAgent` class takes `IChatClient` as a constructor parameter. |
| 38 | + |
| 39 | +First, create an `OllamaApiClient` to access the Ollama service. |
| 40 | + |
| 41 | +```csharp |
| 42 | +using System; |
| 43 | +using Microsoft.Extensions.AI.Agents; |
| 44 | +using OllamaSharp; |
| 45 | + |
| 46 | +using OllamaApiClient chatClient = new(new Uri("http://localhost:11434"), "phi3"); |
| 47 | +``` |
| 48 | + |
| 49 | +The `OllamaApiClient` implements the `IChatClient` interface, so you can use it to create a `ChatClientAgent`. |
| 50 | + |
| 51 | +```csharp |
| 52 | +AIAgent agent = new ChatClientAgent( |
| 53 | + chatClient, |
| 54 | + instructions: "You are good at telling jokes.", |
| 55 | + name: "Joker"); |
| 56 | +``` |
| 57 | + |
| 58 | +> [!IMPORTANT] |
| 59 | +> To ensure that you get the most out of your agent, make sure to choose a service and model that is well-suited for conversational tasks and supports function calling. |
| 60 | +
|
| 61 | +::: zone-end |
| 62 | +::: zone pivot="programming-language-python" |
| 63 | + |
| 64 | +Documentation coming soon. |
| 65 | + |
| 66 | +::: zone-end |
| 67 | + |
| 68 | +## Using the Agent |
| 69 | + |
| 70 | +The agent is a standard `AIAgent` and supports all standard agent operations. |
| 71 | + |
| 72 | +See the [Agent getting started tutorials](../../../tutorials/overview.md) for more information on how to run and interact with agents. |
0 commit comments