.NET: Added GettingStarted example demonstrating Dapr as an agent provider (#1615)

* Added example demonstrating creating an AIAgent using the Microsoft.AI.Extensions implementation of IChatClient using Dapr as the inference backend provider - in this example, using Ollama

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>

* Update dotnet/samples/GettingStarted/AgentProviders/Agent_With_Dapr/README.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Added copyright statement at top of file

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>

* Update dotnet/agent-framework-dotnet.slnx

That's odd the IDE added it a second time.

Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>

* Address review nits: configurable Dapr gRPC endpoint and document VersionOverride

Make the Dapr sidecar gRPC endpoint configurable via the DAPR_GRPC_ENDPOINT environment variable
(defaulting to http://localhost:3501) and document it in the README. Add a comment explaining why the
Microsoft.Extensions.* VersionOverride entries are needed and when they can be removed.

---------

Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
Co-authored-by: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com>
This commit is contained in:
Whit Waldo
2026-07-22 14:28:00 -05:00
committed by GitHub
parent 12b23250f4
commit ddb0622f9c
7 changed files with 121 additions and 0 deletions
+1
View File
@@ -122,6 +122,7 @@
<PackageVersion Include="Hyperlight.HyperlightSandbox.Api" Version="0.4.0" />
<PackageVersion Include="Hyperlight.HyperlightSandbox.Guest.Python" Version="0.4.0" />
<!-- Inference SDKs -->
<PackageVersion Include="Dapr.AI.Microsoft.Extensions" Version="1.18.4" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI" Version="0.10.0" />
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="2.0.0" />
<PackageVersion Include="OllamaSharp" Version="5.4.8" />
+1
View File
@@ -29,6 +29,7 @@
<Project Path="samples/02-agents/AgentProviders/azure/Agent_With_AzureOpenAIChatCompletion/Agent_With_AzureOpenAIChatCompletion.csproj" />
<Project Path="samples/02-agents/AgentProviders/azure/Agent_With_AzureOpenAIResponses/Agent_With_AzureOpenAIResponses.csproj" />
<Project Path="samples/02-agents/AgentProviders/custom/Agent_With_CustomImplementation/Agent_With_CustomImplementation.csproj" />
<Project Path="samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Agent_With_Dapr.csproj" />
<Project Path="samples/02-agents/AgentProviders/github-copilot/Agent_With_GitHubCopilot/Agent_With_GitHubCopilot.csproj" />
<Project Path="samples/02-agents/AgentProviders/google-gemini/Agent_With_GoogleGemini/Agent_With_GoogleGemini.csproj" />
<Project Path="samples/02-agents/AgentProviders/ollama/Agent_With_Ollama/Agent_With_Ollama.csproj" />
@@ -44,6 +44,12 @@ See the README.md for each sample for the prerequisites for that sample.
| --- | --- |
| [Custom Implementation](./custom/Agent_With_CustomImplementation/) | Create an AIAgent with a custom implementation |
### [Dapr](./dapr/)
| Sample | Description |
| --- | --- |
| [Agent with Dapr](./dapr/Agent_With_Dapr/) | Create an AIAgent using Dapr's Conversation building block as the inference backend |
### [Foundry](./foundry/)
See [foundry/README.md](./foundry/README.md) for the full list of Foundry agent samples,
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<CentralPackageTransitivePinningEnabled>false</CentralPackageTransitivePinningEnabled>
<NoWarn>$(NoWarn);DAPR_CONVERSATION</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapr.AI.Microsoft.Extensions" />
<!--
Dapr.AI.Microsoft.Extensions depends on Microsoft.Extensions.* 10.0.8, which is higher than the
versions pinned centrally in Directory.Packages.props. Central transitive pinning is disabled above
for this sample and these two direct references are overridden to that minimum. Remove the overrides
(and the CentralPackageTransitivePinningEnabled setting) once the central versions are >= 10.0.8.
-->
<PackageReference Include="Microsoft.Extensions.DependencyInjection" VersionOverride="10.0.8" />
<PackageReference Include="Microsoft.Extensions.Hosting" VersionOverride="10.0.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\src\Microsoft.Agents.AI\Microsoft.Agents.AI.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,11 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: ollama
spec:
type: conversation.ollama
metadata:
- name: model
value: llama3.2
- name: cacheTTL
value: 10m
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft. All rights reserved.
// This sample shows how to create and use a simple AI agent with Dapr as the backend.
// Dapr's Conversation building block is used here to route inference to Ollama.
using Dapr.AI.Conversation.Extensions;
using Dapr.AI.Microsoft.Extensions;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
// The Dapr sidecar's gRPC endpoint. This must match the --dapr-grpc-port used when starting
// the sidecar (see this sample's README). Override it with the DAPR_GRPC_ENDPOINT environment
// variable if you run the sidecar on a different port.
var daprGrpcEndpoint = Environment.GetEnvironmentVariable("DAPR_GRPC_ENDPOINT") ?? "http://localhost:3501";
// Register the Dapr Conversation client with dependency injection.
var app = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
// Configure the gRPC endpoint for the Dapr sidecar.
services.AddDaprConversationClient((_, builder) => builder.UseGrpcEndpoint(daprGrpcEndpoint));
// Provide the name of the Conversation component loaded in the sidecar to use.
services.AddDaprChatClient(opt => opt.ConversationComponentName = "ollama");
}).Build();
// Get an instance of the Dapr chat client from the dependency injection container.
using var scope = app.Services.CreateScope();
var daprChatClient = scope.ServiceProvider.GetRequiredService<IChatClient>();
// Use this chat client to construct an AIAgent.
AIAgent agent = daprChatClient.AsAIAgent(instructions: "You are good at telling jokes.", name: "Joker");
// Invoke the agent and output the text result.
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate."));
@@ -0,0 +1,37 @@
# Prerequisites
Before you begin, ensure you have the following prerequisites:
- .NET 10 SDK or later
- Docker installed and running on your machine
- Ollama installed
- Dapr CLI installed ([instructions](https://docs.dapr.io/getting-started/install-dapr-cli/))
You'll need to download a model from [Ollama's library](https://ollama.com/library) to get started. Open
a terminal and run the following, replacing `<model_name>` with the name of the model you want to use from
Ollama's library (e.g., `llama3.2`).
```powershell
ollama run <model_name>
```
Once it has downloaded and started running, update the component bundled with this example
in `./Components/conversation-ollama.yaml` to reflect the name of the model you just installed, modifying the value of
the `model` metadata property, then save your changes and close the file.
Next, start your Dapr sidecar and tell it where it can look for your components. If launching from this project's directory,
run the following; otherwise, replace `./Components` with the path to your components directory.
```powershell
dapr run --app-id agents --resources-path ./Components --dapr-grpc-port 3501
```
The sample connects to the sidecar at `http://localhost:3501` by default. If you start the sidecar on a
different gRPC port, set the `DAPR_GRPC_ENDPOINT` environment variable to match before running the sample.
Because the Dapr sidecar needs to continue running while your application is running, please open another terminal
window and run the following command from this project's directory to start the demo.
```powershell
dotnet run
```