diff --git a/dotnet/Directory.Packages.props b/dotnet/Directory.Packages.props index 73a7ab8bf25..3682dcdade6 100644 --- a/dotnet/Directory.Packages.props +++ b/dotnet/Directory.Packages.props @@ -122,6 +122,7 @@ + diff --git a/dotnet/agent-framework-dotnet.slnx b/dotnet/agent-framework-dotnet.slnx index 7bf9266d29c..325bfee86c9 100644 --- a/dotnet/agent-framework-dotnet.slnx +++ b/dotnet/agent-framework-dotnet.slnx @@ -29,6 +29,7 @@ + diff --git a/dotnet/samples/02-agents/AgentProviders/README.md b/dotnet/samples/02-agents/AgentProviders/README.md index 0a99f5bf094..6592d7cdf03 100644 --- a/dotnet/samples/02-agents/AgentProviders/README.md +++ b/dotnet/samples/02-agents/AgentProviders/README.md @@ -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, diff --git a/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Agent_With_Dapr.csproj b/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Agent_With_Dapr.csproj new file mode 100644 index 00000000000..1e1fa945e7d --- /dev/null +++ b/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Agent_With_Dapr.csproj @@ -0,0 +1,29 @@ + + + + Exe + net10.0 + + enable + enable + false + $(NoWarn);DAPR_CONVERSATION + + + + + + + + + + + + + + diff --git a/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Components/conversation-ollama.yaml b/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Components/conversation-ollama.yaml new file mode 100644 index 00000000000..35c14c4e066 --- /dev/null +++ b/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Components/conversation-ollama.yaml @@ -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 diff --git a/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Program.cs b/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Program.cs new file mode 100644 index 00000000000..bd607c5decf --- /dev/null +++ b/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Program.cs @@ -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(); + +// 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.")); diff --git a/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/README.md b/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/README.md new file mode 100644 index 00000000000..eb21ab25e50 --- /dev/null +++ b/dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/README.md @@ -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 `` with the name of the model you want to use from +Ollama's library (e.g., `llama3.2`). + +```powershell +ollama run +``` + +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 +```