-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Add Bedrock Agent to .Net SDK (#10443)
### 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
1 parent
a9b20b1
commit 7137770
Showing
27 changed files
with
1,950 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
38 changes: 38 additions & 0 deletions
38
dotnet/samples/GettingStartedWithAgents/BedrockAgent/README.md
This file contains 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,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. |
73 changes: 73 additions & 0 deletions
73
dotnet/samples/GettingStartedWithAgents/BedrockAgent/Step01_BedrockAgent.cs
This file contains 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,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); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
dotnet/samples/GettingStartedWithAgents/BedrockAgent/Step02_BedrockAgent_CodeInterpreter.cs
This file contains 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,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; | ||
} | ||
} |
Oops, something went wrong.