-
Notifications
You must be signed in to change notification settings - Fork 4k
.Net: Add Bedrock Agent to .Net SDK #10443
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
32 commits
Select commit
Hold shift + click to select a range
ea795b4
Add Bedrock Agent to .Net SDK
TaoChenOSU 61fecf1
Merge branch 'main' into TaoChenOSU/add-bedrock-agent-dotnet
TaoChenOSU e311fe6
Simple invoke is working
TaoChenOSU 441207b
Kernel function WIP
TaoChenOSU 9a8e06c
Kernel function done
TaoChenOSU 0c907cf
Merge branch 'main' into TaoChenOSU/add-bedrock-agent-dotnet
TaoChenOSU 5f1279d
Formatting
TaoChenOSU bac8486
Formatting 2
TaoChenOSU 2771168
Reformat usings
TaoChenOSU 1476281
Bedrock agent trace
TaoChenOSU 2695ddf
Example output
TaoChenOSU 4799321
Knowledge base
TaoChenOSU d15fc29
Bedrock agent channel
TaoChenOSU 9f84837
Remove unit test files
TaoChenOSU 2a58212
Fix input ordering
TaoChenOSU be3be0f
Remove import
TaoChenOSU dc114eb
Add project reference to fix build
TaoChenOSU 204fa90
READMEs
TaoChenOSU 0d1b6e8
Fix non-streaming invoke
TaoChenOSU 250a27b
Minor updates
TaoChenOSU 48e499a
Add array type support
TaoChenOSU 1a97422
Merge branch 'main' into TaoChenOSU/add-bedrock-agent-dotnet
TaoChenOSU 0377d07
Fix pipeline
TaoChenOSU bffc79e
Merge branch 'main' into TaoChenOSU/add-bedrock-agent-dotnet
crickman 9c20a80
Address comments 1st batch
TaoChenOSU c5488b6
Address comments 2nd batch
TaoChenOSU 1914b7a
Fix pipeline
TaoChenOSU ab212e5
Fix pipeline
TaoChenOSU 7c560bf
Fix pipeline
TaoChenOSU 8c991fe
Merge branch 'main' into TaoChenOSU/add-bedrock-agent-dotnet
TaoChenOSU dd90d28
Uncomment test code
TaoChenOSU 4bc6141
Address comments 3rd batch
TaoChenOSU 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
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
38 changes: 38 additions & 0 deletions
38
dotnet/samples/GettingStartedWithAgents/BedrockAgent/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,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 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,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 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,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.
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.