March 7th, 2025

Integration of AWS Bedrock Agents in Semantic Kernel

Overview of AWS Bedrock Agents

AWS Bedrock Agents provide a managed service that facilitates the experimentation and rapid deployment of AI agents. Users can leverage proprietary AWS models as well as a diverse selection of models from various providers available on AWS Bedrock.

Semantic Kernel’s Integration with AWS Bedrock

Semantic Kernel now integrates with AWS Bedrock Agents, enabling users to leverage kernel functions alongside features such as code interpretation and Retrieval-Augmented Generation (RAG) powered by the AWS knowledge base. If you manage resources on AWS and are exploring a multi-cloud AI solution, this integration provides a valuable opportunity.

Experimental

This is an experimental feature. We are actively collecting feedback and working on moving it to release candidate status.

Getting Started

Prerequisites
  1. You need to have an AWS account and access to the foundation models.
  2. AWS CLI installed and configured.
  3. A Bedrock Agent Resource Role Arn.

AWS Region

You must also configure the AWS region, or you will have to create custom AWS clients for the Bedrock service. See details in our GitHub repository.

Resource Role Arn

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.

Foundational Model

You need to make sure you have permission to access the foundation model. You can find the model ID in the AWS documentation. 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.

Create an agent

We provide a convenient method to create a new agent if you would like to perform agent creation in code.

In Python, do the following

from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent

agent_name = "[YOUR AGENT NAME]"
instructions = "[YOUR AGENT INSTRUCTIONS]"
agent_resource_role_arn = "[YOUR AGENT RESOURCE ROLE ARN]"
foundation_model = "[YOUR FOUNDATIONLA MODEL]"

bedrock_agent = await BedrockAgent.create_and_prepare_agent(
  agent_name,
  instructions,
  agent_resource_role_arn=agent_resource_role_arn,
  foundation_model=foundation_model,
)

In .Net, do the following

using Amazon.BedrockAgent;
using Microsoft.SemanticKernel.Agents.Bedrock;
using Microsoft.SemanticKernel.Agents.Bedrock.Extensions;
...
var client = new AmazonBedrockAgentClient();
var agentModel = await client.CreateAndPrepareAgentAsync(new()
{
  AgentName = "[YOUR AGENT NAME]",
  Description = "[YOUR AGENT DESCRIPTION]",
  Instruction = "[YOUR AGENT INSTRUCTION]",
  AgentResourceRoleArn = "[YOUR AGENT RESOURCE ROLE ARN]",
  FoundationModel = "[YOUR FOUNDATIONLA MODEL]",
});
var bedrockAgent = new BedrockAgent(agentModel, client);
Use an existing agent

You can also retrieve agents that you created previously and use it in Semantic Kernel.

In Python, do the following

import boto3
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent

AGENT_ID = "[YOUR AGENT ID]"

client = boto3.client("bedrock-agent")
agent_model = client.get_agent(agentId=AGENT_ID)["agent"]
bedrock_agent = BedrockAgent(agent_model)

In .Net, do the following

using Amazon.BedrockAgent;
using Microsoft.SemanticKernel.Agents.Bedrock;
...
var client = new AmazonBedrockAgentClient();
var getAgentResponse = await this.Client.GetAgentAsync(new() { AgentId = "[YOUR AGENT ID]" });
var bedrockAgent = new BedrockAgent(getAgentResponse.Agent, client);
Interacting with the agent

Interacting with the agent is simple and intuitive.

In Python, simply do the following

session_id = BedrockAgent.create_session_id()
response = await bedrock_agent.get_response(session_id, "your query")
print(response)

In .Net, simple do the following

var responses = bedrockAgent.InvokeAsync(BedrockAgent.CreateSessionId(), UserQuery, null);
await foreach (var response in responses)
{
    Console.WriteLine(response.Content);
}

For more advanced usage patterns and features including streaming, code interpretation, and kernel function, please visit our GitHub repository:

Python: semantic-kernel/python/samples/concepts/agents/bedrock_agent at main · microsoft/semantic-kernel

.Net: semantic-kernel/dotnet/samples/GettingStartedWithAgents/BedrockAgent at main · microsoft/semantic-kernel

Happy coding!

The Semantic Kernel team is dedicated to empowering developers by providing access to the latest advancements in the industry. We encourage you to leverage your creativity and build remarkable solutions with SK! Please reach out if you have any questions or feedback through our Semantic Kernel GitHub Discussion Channel. We look forward to hearing from you! We would also love your support, if you’ve enjoyed using Semantic Kernel, give us a star on GitHub.

Author

0 comments