Skip to content

Commit 32f311e

Browse files
TaoChenOSUmoonbox3
andauthoredMar 10, 2025
Add Bedrock agent retrieval samples in Python and .Net (microsoft#10857)
### 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. --> We are missing samples for how to retrieve an existing Bedrock agent in Python and .Net. ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> 1. Add two samples on how to retrieve existing Bedrock agents, one for Python, one for .Net. 2. Modify Python sample comments to follow our latest sample guide. ### 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 😄 Co-authored-by: Evan Mattson <[email protected]>
1 parent b08f22c commit 32f311e

11 files changed

+151
-54
lines changed
 

‎dotnet/samples/GettingStartedWithAgents/BedrockAgent/Step01_BedrockAgent.cs

+21
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ public async Task UseNewAgentAsync()
3737
}
3838
}
3939

40+
/// <summary>
41+
/// Demonstrates how to use an existing <see cref="BedrockAgent"/> and interact with it.
42+
/// The agent will respond to the user query.
43+
/// </summary>
44+
[Fact]
45+
public async Task UseExistingAgentAsync()
46+
{
47+
// Retrieve the agent
48+
// Replace "bedrock-agent-id" with the ID of the agent you want to use
49+
var agentId = "bedrock-agent-id";
50+
var getAgentResponse = await this.Client.GetAgentAsync(new() { AgentId = agentId });
51+
var bedrockAgent = new BedrockAgent(getAgentResponse.Agent, this.Client);
52+
53+
// Respond to user input
54+
var responses = bedrockAgent.InvokeAsync(BedrockAgent.CreateSessionId(), UserQuery, null);
55+
await foreach (var response in responses)
56+
{
57+
this.Output.WriteLine(response.Content);
58+
}
59+
}
60+
4061
/// <summary>
4162
/// Demonstrates how to create a new <see cref="BedrockAgent"/> and interact with it using streaming.
4263
/// The agent will respond to the user query.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
5+
import boto3
6+
7+
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent
8+
9+
"""
10+
The following sample demonstrates how to use an already existing
11+
Bedrock Agent within Semantic Kernel. This sample requires that you
12+
have an existing agent created either previously in code or via the
13+
AWS Console.
14+
This sample uses the following main component(s):
15+
- a Bedrock agent
16+
You will learn how to retrieve a Bedrock agent and talk to it.
17+
"""
18+
19+
# Replace "your-agent-id" with the ID of the agent you want to use
20+
AGENT_ID = "your-agent-id"
21+
22+
23+
async def main():
24+
client = boto3.client("bedrock-agent")
25+
agent_model = client.get_agent(agentId=AGENT_ID)["agent"]
26+
bedrock_agent = BedrockAgent(agent_model)
27+
session_id = BedrockAgent.create_session_id()
28+
29+
try:
30+
while True:
31+
user_input = input("User:> ")
32+
if user_input == "exit":
33+
print("\n\nExiting chat...")
34+
break
35+
36+
# Invoke the agent
37+
# The chat history is maintained in the session
38+
async for response in bedrock_agent.invoke(
39+
session_id=session_id,
40+
input_text=user_input,
41+
):
42+
print(f"Bedrock agent: {response}")
43+
except KeyboardInterrupt:
44+
print("\n\nExiting chat...")
45+
return False
46+
except EOFError:
47+
print("\n\nExiting chat...")
48+
return False
49+
50+
# Sample output (using anthropic.claude-3-haiku-20240307-v1:0):
51+
# User:> Hi, my name is John.
52+
# Bedrock agent: Hello John. How can I help you?
53+
# User:> What is my name?
54+
# Bedrock agent: Your name is John.
55+
56+
57+
if __name__ == "__main__":
58+
asyncio.run(main())

‎python/samples/concepts/agents/bedrock_agent/bedrock_agent_simple_chat.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent
66

7-
# This sample shows how to interact with a Bedrock agent in the simplest way.
8-
# This sample uses the following main component(s):
9-
# - a Bedrock agent
10-
# You will learn how to create a new Bedrock agent and talk to it.
7+
"""
8+
This sample shows how to interact with a Bedrock agent in the simplest way.
9+
This sample uses the following main component(s):
10+
- a Bedrock agent
11+
You will learn how to create a new Bedrock agent and talk to it.
12+
"""
1113

1214
AGENT_NAME = "semantic-kernel-bedrock-agent"
1315
INSTRUCTION = "You are a friendly assistant. You help people find information."

‎python/samples/concepts/agents/bedrock_agent/bedrock_agent_simple_chat_streaming.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent
66

7-
# This sample shows how to interact with a Bedrock agent via streaming in the simplest way.
8-
# This sample uses the following main component(s):
9-
# - a Bedrock agent
10-
# You will learn how to create a new Bedrock agent and talk to it.
7+
"""
8+
This sample shows how to interact with a Bedrock agent via streaming in the simplest way.
9+
This sample uses the following main component(s):
10+
- a Bedrock agent
11+
You will learn how to create a new Bedrock agent and talk to it.
12+
"""
1113

1214
AGENT_NAME = "semantic-kernel-bedrock-agent"
1315
INSTRUCTION = "You are a friendly assistant. You help people find information."

‎python/samples/concepts/agents/bedrock_agent/bedrock_agent_with_code_interpreter.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
from semantic_kernel.contents.binary_content import BinaryContent
88
from semantic_kernel.contents.chat_message_content import ChatMessageContent
99

10-
# This sample shows how to interact with a Bedrock agent that is capable of writing and executing code.
11-
# This sample uses the following main component(s):
12-
# - a Bedrock agent
13-
# You will learn how to create a new Bedrock agent and ask it a question that requires coding to answer.
14-
# After running this sample, a bar chart will be generated and saved to a file in the same directory
15-
# as this script.
10+
"""
11+
This sample shows how to interact with a Bedrock agent that is capable of writing and executing code.
12+
This sample uses the following main component(s):
13+
- a Bedrock agent
14+
You will learn how to create a new Bedrock agent and ask it a question that requires coding to answer.
15+
After running this sample, a bar chart will be generated and saved to a file in the same directory
16+
as this script.
17+
"""
1618

1719
AGENT_NAME = "semantic-kernel-bedrock-agent"
1820
INSTRUCTION = "You are a friendly assistant. You help people find information."

‎python/samples/concepts/agents/bedrock_agent/bedrock_agent_with_code_interpreter_streaming.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
from semantic_kernel.contents.binary_content import BinaryContent
88
from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent
99

10-
# This sample shows how to interact with a Bedrock agent that is capable of writing and executing code.
11-
# This sample uses the following main component(s):
12-
# - a Bedrock agent
13-
# You will learn how to create a new Bedrock agent and ask it a question that requires coding to answer.
14-
# After running this sample, a bar chart will be generated and saved to a file in the same directory
15-
# as this script.
10+
"""
11+
This sample shows how to interact with a Bedrock agent that is capable of writing and executing code.
12+
This sample uses the following main component(s):
13+
- a Bedrock agent
14+
You will learn how to create a new Bedrock agent and ask it a question that requires coding to answer.
15+
After running this sample, a bar chart will be generated and saved to a file in the same directory
16+
as this script.
17+
"""
1618

1719
AGENT_NAME = "semantic-kernel-bedrock-agent"
1820
INSTRUCTION = "You are a friendly assistant. You help people find information."

‎python/samples/concepts/agents/bedrock_agent/bedrock_agent_with_kernel_function.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
from semantic_kernel.functions.kernel_function_decorator import kernel_function
88
from semantic_kernel.kernel import Kernel
99

10-
# This sample shows how to interact with a Bedrock agent that is capable of using kernel functions.
11-
# This sample uses the following main component(s):
12-
# - a Bedrock agent
13-
# - a kernel function
14-
# - a kernel
15-
# You will learn how to create a new Bedrock agent and ask it a question that requires a kernel function to answer.
10+
"""
11+
This sample shows how to interact with a Bedrock agent that is capable of using kernel functions.
12+
This sample uses the following main component(s):
13+
- a Bedrock agent
14+
- a kernel function
15+
- a kernel
16+
You will learn how to create a new Bedrock agent and ask it a question that requires a kernel function to answer.
17+
"""
1618

1719
AGENT_NAME = "semantic-kernel-bedrock-agent"
1820
INSTRUCTION = "You are a friendly assistant. You help people find information."

‎python/samples/concepts/agents/bedrock_agent/bedrock_agent_with_kernel_function_simple.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent
77
from semantic_kernel.functions.kernel_function_decorator import kernel_function
88

9-
# This sample shows how to interact with a Bedrock agent that is capable of using kernel functions.
10-
# Instead of creating a kernel and adding plugins to it, you can directly pass the plugins to the
11-
# agent when creating it.
12-
# This sample uses the following main component(s):
13-
# - a Bedrock agent
14-
# - a kernel function
15-
# - a kernel
16-
# You will learn how to create a new Bedrock agent and ask it a question that requires a kernel function to answer.
9+
"""
10+
This sample shows how to interact with a Bedrock agent that is capable of using kernel functions.
11+
Instead of creating a kernel and adding plugins to it, you can directly pass the plugins to the
12+
agent when creating it.
13+
This sample uses the following main component(s):
14+
- a Bedrock agent
15+
- a kernel function
16+
- a kernel
17+
You will learn how to create a new Bedrock agent and ask it a question that requires a kernel function to answer.
18+
"""
1719

1820
AGENT_NAME = "semantic-kernel-bedrock-agent"
1921
INSTRUCTION = "You are a friendly assistant. You help people find information."

‎python/samples/concepts/agents/bedrock_agent/bedrock_agent_with_kernel_function_streaming.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
from semantic_kernel.functions.kernel_function_decorator import kernel_function
88
from semantic_kernel.kernel import Kernel
99

10-
# This sample shows how to interact with a Bedrock agent that is capable of using kernel functions.
11-
# This sample uses the following main component(s):
12-
# - a Bedrock agent
13-
# - a kernel function
14-
# - a kernel
15-
# You will learn how to create a new Bedrock agent and ask it a question that requires a kernel function to answer.
10+
"""
11+
This sample shows how to interact with a Bedrock agent that is capable of using kernel functions.
12+
This sample uses the following main component(s):
13+
- a Bedrock agent
14+
- a kernel function
15+
- a kernel
16+
You will learn how to create a new Bedrock agent and ask it a question that requires a kernel function to answer.
17+
"""
1618

1719
AGENT_NAME = "semantic-kernel-bedrock-agent"
1820
INSTRUCTION = "You are a friendly assistant. You help people find information."

‎python/samples/concepts/agents/bedrock_agent/bedrock_mixed_chat_agents.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
from semantic_kernel.contents.utils.author_role import AuthorRole
1111
from semantic_kernel.kernel import Kernel
1212

13-
# This sample shows how to use a bedrock agent in a group chat that includes multiple agents of different roles.
14-
# This sample uses the following main component(s):
15-
# - a Bedrock agent
16-
# - a ChatCompletionAgent
17-
# - an AgentGroupChat
18-
# You will learn how to create a new or connect to an existing Bedrock agent and put it in a group chat with
19-
# another agent.
13+
"""
14+
This sample shows how to use a bedrock agent in a group chat that includes multiple agents of different roles.
15+
This sample uses the following main component(s):
16+
- a Bedrock agent
17+
- a ChatCompletionAgent
18+
- an AgentGroupChat
19+
You will learn how to create a new or connect to an existing Bedrock agent and put it in a group chat with
20+
another agent.
21+
"""
2022

2123
# This will be a chat completion agent
2224
REVIEWER_NAME = "ArtDirector"

‎python/samples/concepts/agents/bedrock_agent/bedrock_mixed_chat_agents_streaming.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
from semantic_kernel.contents.utils.author_role import AuthorRole
1111
from semantic_kernel.kernel import Kernel
1212

13-
# This sample shows how to use a bedrock agent in a group chat that includes multiple agents of different roles.
14-
# This sample uses the following main component(s):
15-
# - a Bedrock agent
16-
# - a ChatCompletionAgent
17-
# - an AgentGroupChat
18-
# You will learn how to create a new or connect to an existing Bedrock agent and put it in a group chat with
19-
# another agent.
13+
"""
14+
This sample shows how to use a bedrock agent in a group chat that includes multiple agents of different roles.
15+
This sample uses the following main component(s):
16+
- a Bedrock agent
17+
- a ChatCompletionAgent
18+
- an AgentGroupChat
19+
You will learn how to create a new or connect to an existing Bedrock agent and put it in a group chat with
20+
another agent.
21+
"""
2022

2123
# This will be a chat completion agent
2224
REVIEWER_NAME = "ArtDirector"

0 commit comments

Comments
 (0)
Please sign in to comment.