-
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.
- Loading branch information
1 parent
439dc62
commit 4740657
Showing
17 changed files
with
816 additions
and
378 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
AGENT_ROLE_AMAZON_RESOURCE_NAME=[YOUR_AGENT_ROLE_AMAZON_RESOURCE_NAME] | ||
FOUNDATION_MODEL=[YOUR_FOUNDATION_MODEL] | ||
BEDROCK_AGENT_AGENT_RESOURCE_ROLE_ARN=[YOUR_AGENT_ROLE_AMAZON_RESOURCE_NAME] | ||
BEDROCK_AGENT_FOUNDATION_MODEL=[YOUR_FOUNDATION_MODEL] |
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
57 changes: 57 additions & 0 deletions
57
python/samples/concepts/agents/bedrock_agent/bedrock_agent_simple_chat_streaming.py
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,57 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
import asyncio | ||
import uuid | ||
|
||
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent | ||
|
||
# This sample shows how to interact with a Bedrock agent via streaming in the simplest way. | ||
# This sample uses the following main component(s): | ||
# - a Bedrock agent | ||
# You will learn how to create a new Bedrock agent and talk to it. | ||
|
||
AGENT_NAME = "semantic-kernel-bedrock-agent" | ||
INSTRUCTION = "You are a friendly assistant. You help people find information." | ||
|
||
|
||
async def main(): | ||
bedrock_agent = await BedrockAgent.create(AGENT_NAME, instructions=INSTRUCTION) | ||
|
||
# Use a uiud as the session id | ||
new_session_id = str(uuid.uuid4()) | ||
|
||
try: | ||
while True: | ||
user_input = input("User:> ") | ||
if user_input == "exit": | ||
print("\n\nExiting chat...") | ||
break | ||
|
||
# Invoke the agent | ||
# The chat history is maintained in the session | ||
print("Bedrock agent: ", end="") | ||
async for response in bedrock_agent.invoke_stream( | ||
session_id=new_session_id, | ||
input_text=user_input, | ||
): | ||
print(response, end="") | ||
print() | ||
except KeyboardInterrupt: | ||
print("\n\nExiting chat...") | ||
return False | ||
except EOFError: | ||
print("\n\nExiting chat...") | ||
return False | ||
finally: | ||
# Delete the agent | ||
await bedrock_agent.delete_agent() | ||
|
||
# Sample output (using anthropic.claude-3-haiku-20240307-v1:0): | ||
# User:> Hi, my name is John. | ||
# Bedrock agent: Hello John. How can I help you? | ||
# User:> What is my name? | ||
# Bedrock agent: Your name is John. | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
91 changes: 0 additions & 91 deletions
91
...n/samples/concepts/agents/bedrock_agent/bedrock_agent_simple_chat_with_kernel_function.py
This file was deleted.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
python/samples/concepts/agents/bedrock_agent/bedrock_agent_with_code_interpreter.py
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,79 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
import asyncio | ||
import os | ||
import uuid | ||
|
||
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent | ||
from semantic_kernel.contents.binary_content import BinaryContent | ||
from semantic_kernel.contents.chat_message_content import ChatMessageContent | ||
|
||
# This sample shows how to interact with a Bedrock agent that is capable of writing and executing code. | ||
# This sample uses the following main component(s): | ||
# - a Bedrock agent | ||
# You will learn how to create a new Bedrock agent and ask it a question that requires coding to answer. | ||
# After running this sample, a bar chart will be generated and saved to a file in the same directory | ||
# as this script. | ||
|
||
AGENT_NAME = "semantic-kernel-bedrock-agent" | ||
INSTRUCTION = "You are a friendly assistant. You help people find information." | ||
|
||
|
||
ASK = """ | ||
Create a bar chart for the following data: | ||
Panda 5 | ||
Tiger 8 | ||
Lion 3 | ||
Monkey 6 | ||
Dolpin 2 | ||
""" | ||
|
||
|
||
async def main(): | ||
bedrock_agent = await BedrockAgent.create( | ||
AGENT_NAME, | ||
instructions=INSTRUCTION, | ||
enable_code_interpreter=True, | ||
) | ||
|
||
# Use a uiud as the session id | ||
new_session_id = str(uuid.uuid4()) | ||
|
||
# Placeholder for the file generated by the code interpreter | ||
binary_item: BinaryContent | None = None | ||
|
||
try: | ||
# Invoke the agent | ||
async for response in bedrock_agent.invoke( | ||
session_id=new_session_id, | ||
input_text=ASK, | ||
): | ||
print(f"Response:\n{response}") | ||
assert isinstance(response, ChatMessageContent) # nosec | ||
binary_item = next(item for item in response.items if isinstance(item, BinaryContent)) | ||
finally: | ||
# Delete the agent | ||
await bedrock_agent.delete_agent() | ||
|
||
# Save the chart to a file | ||
if not binary_item: | ||
raise RuntimeError("No chart generated") | ||
|
||
file_path = os.path.join(os.path.dirname(__file__), binary_item.metadata["name"]) | ||
binary_item.write_to_file(os.path.join(os.path.dirname(__file__), binary_item.metadata["name"])) | ||
print(f"Chart saved to {file_path}") | ||
|
||
# Sample output (using anthropic.claude-3-haiku-20240307-v1:0): | ||
# Response: | ||
# Here is the bar chart for the given data: | ||
# [A bar chart showing the following data: | ||
# Panda 5 | ||
# Tiger 8 | ||
# Lion 3 | ||
# Monkey 6 | ||
# Dolpin 2] | ||
# Chart saved to ... | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
Oops, something went wrong.