-
Notifications
You must be signed in to change notification settings - Fork 2.1k
.NET: Added GettingStarted example demonstrating Dapr as an agent provider #1615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
55993d7
Added example demonstrating creating an AIAgent using the Microsoft.A…
WhitWaldo 543e20f
Update dotnet/samples/GettingStarted/AgentProviders/Agent_With_Dapr/R…
WhitWaldo 4eef401
Added copyright statement at top of file
WhitWaldo 565c716
Merge branch 'main' into agent-with-dapr
WhitWaldo acfea95
Merge branch 'main' into agent-with-dapr
WhitWaldo f07d36e
Merge branch 'main' into agent-with-dapr
WhitWaldo 9b3bef5
Merge branch 'main' into agent-with-dapr
WhitWaldo bf24116
Merge branch 'main' into agent-with-dapr
WhitWaldo fa04bc4
Merge branch 'main' into agent-with-dapr
WhitWaldo 44fa01c
Merge branch 'main' into agent-with-dapr
WhitWaldo b177284
Merge branch 'main' into agent-with-dapr
westey-m 1139b64
Update dotnet/agent-framework-dotnet.slnx
WhitWaldo b8fb98c
Merge main into agent-with-dapr and relocate Dapr sample to 02-agents…
rogerbarreto 9a7ff43
Address review nits: configurable Dapr gRPC endpoint and document Ver…
rogerbarreto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Agent_With_Dapr.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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> | ||
11 changes: 11 additions & 0 deletions
11
...samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Components/conversation-ollama.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
36 changes: 36 additions & 0 deletions
36
dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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.")); |
37 changes: 37 additions & 0 deletions
37
dotnet/samples/02-agents/AgentProviders/dapr/Agent_With_Dapr/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.