Skip to content

Commit

Permalink
.Net: Add Bedrock Agent to .Net SDK (#10443)
Browse files Browse the repository at this point in the history
### Motivation and Context
<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->
This PR adds the Bedrock Agent to the .Net SDK. It's equivalent to this
[PR](#10307) to the
Python SDK.

### Description
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
Integrate AWS Bedrock Agent to SK .Net. The integration includes the
following features:
1. Create a new Bedrock agent in code.
2. Retrieve an existing Bedrock agent in code.
3. Chat with the agent in streaming and non-streaming mode.
4. Enable code interpreter.
5. Function calling and execution.
6. Linking an AWS knowledge base.

Note: tests will be included in a separate PR.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [X] The code builds clean without any errors or warnings
- [X] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [X] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄
---

For more details, open the [Copilot Workspace
session](https://copilot-workspace.githubnext.com/microsoft/semantic-kernel/pull/10443?shareId=9bcffc75-6f2e-4ccb-864d-9b10890c22b1).

---------

Co-authored-by: Chris <[email protected]>
  • Loading branch information
TaoChenOSU and crickman authored Feb 19, 2025
1 parent a9b20b1 commit 7137770
Show file tree
Hide file tree
Showing 27 changed files with 1,950 additions and 11 deletions.
3 changes: 3 additions & 0 deletions dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AWSSDK.BedrockAgent" Version="3.7.416.4" />
<PackageVersion Include="AWSSDK.BedrockAgentRuntime" Version="3.7.418.3" />
<PackageVersion Include="AWSSDK.BedrockRuntime" Version="4.0.0-preview.5" />
<PackageVersion Include="AWSSDK.Core" Version="4.0.0-preview.5" />
<PackageVersion Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.301" />
Expand Down Expand Up @@ -60,6 +62,7 @@
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
<PackageVersion Include="System.IO.Packaging" Version="8.0.1" />
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Memory.Data" Version="8.0.1" />
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
<PackageVersion Include="System.Numerics.Tensors" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
Expand Down
9 changes: 9 additions & 0 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plugins.AI.UnitTests", "src
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Connectors.Postgres.UnitTests", "src\Connectors\Connectors.Postgres.UnitTests\Connectors.Postgres.UnitTests.csproj", "{2A1EC0DA-AD01-4421-AADC-1DFF65C71CCC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agents.Bedrock", "src\Agents\Bedrock\Agents.Bedrock.csproj", "{8C658E1E-83C8-4127-B8BF-27A638A45DDD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1196,6 +1198,12 @@ Global
{2A1EC0DA-AD01-4421-AADC-1DFF65C71CCC}.Publish|Any CPU.Build.0 = Debug|Any CPU
{2A1EC0DA-AD01-4421-AADC-1DFF65C71CCC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A1EC0DA-AD01-4421-AADC-1DFF65C71CCC}.Release|Any CPU.Build.0 = Release|Any CPU
{8C658E1E-83C8-4127-B8BF-27A638A45DDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C658E1E-83C8-4127-B8BF-27A638A45DDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C658E1E-83C8-4127-B8BF-27A638A45DDD}.Publish|Any CPU.ActiveCfg = Publish|Any CPU
{8C658E1E-83C8-4127-B8BF-27A638A45DDD}.Publish|Any CPU.Build.0 = Publish|Any CPU
{8C658E1E-83C8-4127-B8BF-27A638A45DDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C658E1E-83C8-4127-B8BF-27A638A45DDD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1360,6 +1368,7 @@ Global
{0C64EC81-8116-4388-87AD-BA14D4B59974} = {D6D598DF-C17C-46F4-B2B9-CDE82E2DE132}
{03ACF9DD-00C9-4F2B-80F1-537E2151AF5F} = {D6D598DF-C17C-46F4-B2B9-CDE82E2DE132}
{2A1EC0DA-AD01-4421-AADC-1DFF65C71CCC} = {5A7028A7-4DDF-4E4F-84A9-37CE8F8D7E89}
{8C658E1E-83C8-4127-B8BF-27A638A45DDD} = {6823CD5E-2ABE-41EB-B865-F86EC13F0CF9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBDC56A3-86AD-4323-AA0F-201E59123B83}
Expand Down
1 change: 1 addition & 0 deletions dotnet/samples/Concepts/Concepts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<ProjectReference Include="..\..\src\Agents\AzureAI\Agents.AzureAI.csproj" />
<ProjectReference Include="..\..\src\Agents\Core\Agents.Core.csproj" />
<ProjectReference Include="..\..\src\Agents\OpenAI\Agents.OpenAI.csproj" />
<ProjectReference Include="..\..\src\Agents\Bedrock\Agents.Bedrock.csproj" />
<ProjectReference Include="..\..\src\Connectors\Connectors.Google\Connectors.Google.csproj" />
<ProjectReference Include="..\..\src\Connectors\Connectors.HuggingFace\Connectors.HuggingFace.csproj" />
<ProjectReference Include="..\..\src\Connectors\Connectors.Memory.AzureAISearch\Connectors.Memory.AzureAISearch.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace GettingStarted.AzureAgents;

/// <summary>
/// Demonstrate using code-interpreter on <see cref="AzureAIAgent"/> .
/// Demonstrate using <see cref="AzureAIAgent"/> with file search.
/// </summary>
public class Step05_AzureAIAgent_FileSearch(ITestOutputHelper output) : BaseAzureAgentTest(output)
{
Expand Down
38 changes: 38 additions & 0 deletions dotnet/samples/GettingStartedWithAgents/BedrockAgent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Concept samples on how to use AWS Bedrock agents

## Pre-requisites

1. You need to have an AWS account and [access to the foundation models](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access-permissions.html)
2. [AWS CLI installed](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) and [configured](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html#configuration)

## Before running the samples

You need to set up some user secrets to run the samples.

### `BedrockAgent:AgentResourceRoleArn`

On your AWS console, go to the IAM service and go to **Roles**. Find the role you want to use and click on it. You will find the ARN in the summary section.

```
dotnet user-secrets set "BedrockAgent:AgentResourceRoleArn" "arn:aws:iam::...:role/..."
```

### `BedrockAgent:FoundationModel`

You need to make sure you have permission to access the foundation model. You can find the model ID in the [AWS documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html). To see the models you have access to, find the policy attached to your role you should see a list of models you have access to under the `Resource` section.

```
dotnet user-secrets set "BedrockAgent:FoundationModel" "..."
```

### How to add the `bedrock:InvokeModelWithResponseStream` action to an IAM policy

1. Open the [IAM console](https://console.aws.amazon.com/iam/).
2. On the left navigation pane, choose `Roles` under `Access management`.
3. Find the role you want to edit and click on it.
4. Under the `Permissions policies` tab, click on the policy you want to edit.
5. Under the `Permissions defined in this policy` section, click on the service. You should see **Bedrock** if you already have access to the Bedrock agent service.
6. Click on the service, and then click `Edit`.
7. On the right, you will be able to add an action. Find the service and search for `InvokeModelWithResponseStream`.
8. Check the box next to the action and then scroll all the way down and click `Next`.
9. Follow the prompts to save the changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.SemanticKernel.Agents.Bedrock;
using Microsoft.SemanticKernel.Agents.Bedrock.Extensions;

namespace GettingStarted.BedrockAgents;

/// <summary>
/// This example demonstrates how to interact with a <see cref="BedrockAgent"/> in the most basic way.
/// </summary>
public class Step01_BedrockAgent(ITestOutputHelper output) : BaseBedrockAgentTest(output)
{
private const string UserQuery = "Why is the sky blue in one sentence?";

/// <summary>
/// Demonstrates how to create a new <see cref="BedrockAgent"/> and interact with it.
/// The agent will respond to the user query.
/// </summary>
[Fact]
public async Task UseNewAgentAsync()
{
// Create the agent
var bedrockAgent = await this.CreateAgentAsync("Step01_BedrockAgent");

// Respond to user input
try
{
var responses = bedrockAgent.InvokeAsync(BedrockAgent.CreateSessionId(), UserQuery, null);
await foreach (var response in responses)
{
this.Output.WriteLine(response.Content);
}
}
finally
{
await this.Client.DeleteAgentAsync(new() { AgentId = bedrockAgent.Id });
}
}

/// <summary>
/// Demonstrates how to create a new <see cref="BedrockAgent"/> and interact with it using streaming.
/// The agent will respond to the user query.
/// </summary>
[Fact]
public async Task UseNewAgentStreamingAsync()
{
// Create the agent
var bedrockAgent = await this.CreateAgentAsync("Step01_BedrockAgent_Streaming");

// Respond to user input
try
{
var streamingResponses = bedrockAgent.InvokeStreamingAsync(BedrockAgent.CreateSessionId(), UserQuery, null);
await foreach (var response in streamingResponses)
{
this.Output.WriteLine(response.Content);
}
}
finally
{
await this.Client.DeleteAgentAsync(new() { AgentId = bedrockAgent.Id });
}
}

protected override async Task<BedrockAgent> CreateAgentAsync(string agentName)
{
// Create a new agent on the Bedrock Agent service and prepare it for use
var agentModel = await this.Client.CreateAndPrepareAgentAsync(this.GetCreateAgentRequest(agentName));
// Create a new BedrockAgent instance with the agent model and the client
// so that we can interact with the agent using Semantic Kernel contents.
return new BedrockAgent(agentModel, this.Client);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Reflection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.Bedrock;
using Microsoft.SemanticKernel.Agents.Bedrock.Extensions;

namespace GettingStarted.BedrockAgents;

/// <summary>
/// This example demonstrates how to interact with a <see cref="BedrockAgent"/> with code interpreter enabled.
/// </summary>
public class Step02_BedrockAgent_CodeInterpreter(ITestOutputHelper output) : BaseBedrockAgentTest(output)
{
private const string UserQuery = @"Create a bar chart for the following data:
Panda 5
Tiger 8
Lion 3
Monkey 6
Dolphin 2";

/// <summary>
/// Demonstrates how to create a new <see cref="BedrockAgent"/> with code interpreter enabled and interact with it.
/// The agent will respond to the user query by creating a Python code that will be executed by the code interpreter.
/// The output of the code interpreter will be a file containing the bar chart, which will be returned to the user.
/// </summary>
[Fact]
public async Task UseAgentWithCodeInterpreterAsync()
{
// Create the agent
var bedrockAgent = await this.CreateAgentAsync("Step02_BedrockAgent_CodeInterpreter");

// Respond to user input
try
{
BinaryContent? binaryContent = null;
var responses = bedrockAgent.InvokeAsync(BedrockAgent.CreateSessionId(), UserQuery, null);
await foreach (var response in responses)
{
if (response.Content != null)
{
this.Output.WriteLine(response.Content);
}
if (binaryContent == null && response.Items.Count > 0)
{
binaryContent = response.Items.OfType<BinaryContent>().FirstOrDefault();
}
}

if (binaryContent == null)
{
throw new InvalidOperationException("No file found in the response.");
}

// Save the file to the same directory as the test assembly
var filePath = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!,
binaryContent.Metadata!["Name"]!.ToString()!);
this.Output.WriteLine($"Saving file to {filePath}");
binaryContent.WriteToFile(filePath, overwrite: true);

// Expected output:
// Here is the bar chart for the given data:
// [A bar chart showing the following data:
// Panda 5
// Tiger 8
// Lion 3
// Monkey 6
// Dolphin 2]
// Saving file to ...
}
finally
{
await this.Client.DeleteAgentAsync(new() { AgentId = bedrockAgent.Id });
}
}

protected override async Task<BedrockAgent> CreateAgentAsync(string agentName)
{
// Create a new agent on the Bedrock Agent service and prepare it for use
var agentModel = await this.Client.CreateAndPrepareAgentAsync(this.GetCreateAgentRequest(agentName));
// Create a new BedrockAgent instance with the agent model and the client
// so that we can interact with the agent using Semantic Kernel contents.
var bedrockAgent = new BedrockAgent(agentModel, this.Client);
// Create the code interpreter action group and prepare the agent for interaction
await bedrockAgent.CreateCodeInterpreterActionGroupAsync();

return bedrockAgent;
}
}
Loading

0 comments on commit 7137770

Please sign in to comment.