Skip to content
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

.Net: Add Bedrock Agent to .Net SDK #10443

Merged
merged 32 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ea795b4
Add Bedrock Agent to .Net SDK
TaoChenOSU Feb 6, 2025
61fecf1
Merge branch 'main' into TaoChenOSU/add-bedrock-agent-dotnet
TaoChenOSU Feb 6, 2025
e311fe6
Simple invoke is working
TaoChenOSU Feb 9, 2025
441207b
Kernel function WIP
TaoChenOSU Feb 11, 2025
9a8e06c
Kernel function done
TaoChenOSU Feb 11, 2025
0c907cf
Merge branch 'main' into TaoChenOSU/add-bedrock-agent-dotnet
TaoChenOSU Feb 11, 2025
5f1279d
Formatting
TaoChenOSU Feb 11, 2025
bac8486
Formatting 2
TaoChenOSU Feb 11, 2025
2771168
Reformat usings
TaoChenOSU Feb 11, 2025
1476281
Bedrock agent trace
TaoChenOSU Feb 12, 2025
2695ddf
Example output
TaoChenOSU Feb 12, 2025
4799321
Knowledge base
TaoChenOSU Feb 12, 2025
d15fc29
Bedrock agent channel
TaoChenOSU Feb 12, 2025
9f84837
Remove unit test files
TaoChenOSU Feb 12, 2025
2a58212
Fix input ordering
TaoChenOSU Feb 13, 2025
be3be0f
Remove import
TaoChenOSU Feb 13, 2025
dc114eb
Add project reference to fix build
TaoChenOSU Feb 13, 2025
204fa90
READMEs
TaoChenOSU Feb 13, 2025
0d1b6e8
Fix non-streaming invoke
TaoChenOSU Feb 13, 2025
250a27b
Minor updates
TaoChenOSU Feb 13, 2025
48e499a
Add array type support
TaoChenOSU Feb 14, 2025
1a97422
Merge branch 'main' into TaoChenOSU/add-bedrock-agent-dotnet
TaoChenOSU Feb 14, 2025
0377d07
Fix pipeline
TaoChenOSU Feb 14, 2025
bffc79e
Merge branch 'main' into TaoChenOSU/add-bedrock-agent-dotnet
crickman Feb 18, 2025
9c20a80
Address comments 1st batch
TaoChenOSU Feb 18, 2025
c5488b6
Address comments 2nd batch
TaoChenOSU Feb 19, 2025
1914b7a
Fix pipeline
TaoChenOSU Feb 19, 2025
ab212e5
Fix pipeline
TaoChenOSU Feb 19, 2025
7c560bf
Fix pipeline
TaoChenOSU Feb 19, 2025
8c991fe
Merge branch 'main' into TaoChenOSU/add-bedrock-agent-dotnet
TaoChenOSU Feb 19, 2025
dd90d28
Uncomment test code
TaoChenOSU Feb 19, 2025
4bc6141
Address comments 3rd batch
TaoChenOSU Feb 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading