Skip to content

Python: Move to a thinner wrap for Bedrock Agent in Python #10740

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
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions python/samples/concepts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
- [Azure AI Agent Streaming](./agents/azure_ai_agent/azure_ai_agent_streaming.py)

#### [Bedrock Agent](../../semantic_kernel/agents/bedrock/bedrock_agent.py)

- [Bedrock Agent Simple Chat Streaming](./agents/bedrock_agent/bedrock_agent_simple_chat_streaming.py)
- [Bedrock Agent Simple Chat](./agents/bedrock_agent/bedrock_agent_simple_chat.py)
- [Bedrock Agent Update Agent](./agents/bedrock_agent/bedrock_agent_update_agent.py)
- [Bedrock Agent Use Existing](./agents/bedrock_agent/bedrock_agent_use_existing.py)
- [Bedrock Agent With Code Interpreter Streaming](./agents/bedrock_agent/bedrock_agent_with_code_interpreter_streaming.py)
- [Bedrock Agent With Code Interpreter](./agents/bedrock_agent/bedrock_agent_with_code_interpreter.py)
- [Bedrock Agent With Kernel Function Simple](./agents/bedrock_agent/bedrock_agent_with_kernel_function_simple.py)
- [Bedrock Agent With Kernel Function Streaming](./agents/bedrock_agent/bedrock_agent_with_kernel_function_streaming.py)
- [Bedrock Agent With Kernel Function](./agents/bedrock_agent/bedrock_agent_with_kernel_function.py)
- [Bedrock Agent Mixed Chat Agents Streaming](./agents/bedrock_agent/bedrock_mixed_chat_agents_streaming.py)
Expand Down
2 changes: 0 additions & 2 deletions python/samples/concepts/agents/bedrock_agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
| [bedrock_agent_with_code_interpreter_streaming.py](bedrock_agent_with_code_interpreter_streaming.py) | Example of using the Bedrock agent with a code interpreter and streaming. |
| [bedrock_mixed_chat_agents.py](bedrock_mixed_chat_agents.py) | Example of using multiple chat agents in a single script. |
| [bedrock_mixed_chat_agents_streaming.py](bedrock_mixed_chat_agents_streaming.py) | Example of using multiple chat agents in a single script with streaming. |
| [bedrock_agent_update_agent.py](bedrock_agent_update_agent.py) | Example of updating an agent. |
| [bedrock_agent_use_existing.py](bedrock_agent_use_existing.py) | Example of using an existing agent. |

## Before running the samples

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


async def main():
bedrock_agent = await BedrockAgent.create(AGENT_NAME, instructions=INSTRUCTION)
bedrock_agent = await BedrockAgent.create_and_prepare_agent(AGENT_NAME, instructions=INSTRUCTION)
session_id = BedrockAgent.create_session_id()

try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


async def main():
bedrock_agent = await BedrockAgent.create(AGENT_NAME, instructions=INSTRUCTION)
bedrock_agent = await BedrockAgent.create_and_prepare_agent(AGENT_NAME, instructions=INSTRUCTION)
session_id = BedrockAgent.create_session_id()

try:
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@


async def main():
bedrock_agent = await BedrockAgent.create(
AGENT_NAME,
instructions=INSTRUCTION,
enable_code_interpreter=True,
)
bedrock_agent = await BedrockAgent.create_and_prepare_agent(AGENT_NAME, instructions=INSTRUCTION)
await bedrock_agent.create_code_interpreter_action_group()

session_id = BedrockAgent.create_session_id()

Expand All @@ -48,7 +45,8 @@ async def main():
):
print(f"Response:\n{response}")
assert isinstance(response, ChatMessageContent) # nosec
binary_item = next(item for item in response.items if isinstance(item, BinaryContent))
if not binary_item:
binary_item = next((item for item in response.items if isinstance(item, BinaryContent)), None)
finally:
# Delete the agent
await bedrock_agent.delete_agent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@


async def main():
bedrock_agent = await BedrockAgent.create(
AGENT_NAME,
instructions=INSTRUCTION,
enable_code_interpreter=True,
)
bedrock_agent = await BedrockAgent.create_and_prepare_agent(AGENT_NAME, instructions=INSTRUCTION)
await bedrock_agent.create_code_interpreter_action_group()

session_id = BedrockAgent.create_session_id()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ async def main():
# Create a kernel
kernel = get_kernel()

bedrock_agent = await BedrockAgent.create(
bedrock_agent = await BedrockAgent.create_and_prepare_agent(
AGENT_NAME,
instructions=INSTRUCTION,
INSTRUCTION,
kernel=kernel,
enable_kernel_function=True,
)
# Note: We still need to create the kernel function action group on the service side.
await bedrock_agent.create_kernel_function_action_group()

session_id = BedrockAgent.create_session_id()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (c) Microsoft. All rights reserved.

import asyncio
from typing import Annotated

from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent
from semantic_kernel.functions.kernel_function_decorator import kernel_function

# This sample shows how to interact with a Bedrock agent that is capable of using kernel functions.
# Instead of creating a kernel and adding plugins to it, you can directly pass the plugins to the
# agent when creating it.
# This sample uses the following main component(s):
# - a Bedrock agent
# - a kernel function
# - a kernel
# You will learn how to create a new Bedrock agent and ask it a question that requires a kernel function to answer.

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


class WeatherPlugin:
"""Mock weather plugin."""

@kernel_function(description="Get real-time weather information.")
def current(self, location: Annotated[str, "The location to get the weather"]) -> str:
"""Returns the current weather."""
return f"The weather in {location} is sunny."


async def main():
bedrock_agent = await BedrockAgent.create_and_prepare_agent(
AGENT_NAME,
INSTRUCTION,
plugins=[WeatherPlugin()],
)
# Note: We still need to create the kernel function action group on the service side.
await bedrock_agent.create_kernel_function_action_group()

session_id = BedrockAgent.create_session_id()

try:
# Invoke the agent
async for response in bedrock_agent.invoke(
session_id=session_id,
input_text="What is the weather in Seattle?",
):
print(f"Response:\n{response}")
finally:
# Delete the agent
await bedrock_agent.delete_agent()

# Sample output (using anthropic.claude-3-haiku-20240307-v1:0):
# Response:
# The current weather in Seattle is sunny.


if __name__ == "__main__":
asyncio.run(main())
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ async def main():
# Create a kernel
kernel = get_kernel()

bedrock_agent = await BedrockAgent.create(
bedrock_agent = await BedrockAgent.create_and_prepare_agent(
AGENT_NAME,
instructions=INSTRUCTION,
INSTRUCTION,
kernel=kernel,
enable_kernel_function=True,
)
# Note: We still need to create the kernel function action group on the service side.
await bedrock_agent.create_kernel_function_action_group()

session_id = BedrockAgent.create_session_id()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def main():
instructions=REVIEWER_INSTRUCTIONS,
)

agent_writer = await BedrockAgent.create(
agent_writer = await BedrockAgent.create_and_prepare_agent(
COPYWRITER_NAME,
instructions=COPYWRITER_INSTRUCTIONS,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def main():
instructions=REVIEWER_INSTRUCTIONS,
)

agent_writer = await BedrockAgent.create(
agent_writer = await BedrockAgent.create_and_prepare_agent(
COPYWRITER_NAME,
instructions=COPYWRITER_INSTRUCTIONS,
)
Expand Down
Loading
Loading