-
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
a26d902
commit 40bc545
Showing
6 changed files
with
136 additions
and
17 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
56 changes: 56 additions & 0 deletions
56
python/samples/concepts/agents/bedrock_agent/bedrock_agent_update_agent.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,56 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
import asyncio | ||
import uuid | ||
|
||
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent | ||
|
||
# This sample shows how to update an existing Bedrock agent. | ||
# This sample uses the following main component(s): | ||
# - a Bedrock agent | ||
# You will learn how to connect to a Bedrock agent and update its properties. | ||
|
||
|
||
# Make sure to replace AGENT_NAME and AGENT_ID with the correct values | ||
AGENT_NAME = "semantic-kernel-bedrock-agent" | ||
INSTRUCTION = "You are a friendly assistant but you don't know anything about AI." | ||
NEW_INSTRUCTION = "You are a friendly assistant and you know a lot about AI." | ||
|
||
|
||
async def main(): | ||
bedrock_agent = await BedrockAgent.create(AGENT_NAME, instructions=INSTRUCTION) | ||
|
||
async def ask_about_ai(): | ||
new_session_id = str(uuid.uuid4()) | ||
async for response in bedrock_agent.invoke( | ||
session_id=new_session_id, | ||
input_text="What is AI in one sentence?", | ||
): | ||
print(response) | ||
|
||
try: | ||
print("Before updating the agent:") | ||
await ask_about_ai() | ||
|
||
await bedrock_agent.update_agent(instruction=NEW_INSTRUCTION) | ||
|
||
print("After updating the agent:") | ||
await ask_about_ai() | ||
finally: | ||
# Delete the agent | ||
await bedrock_agent.delete_agent() | ||
|
||
# Sample output (using anthropic.claude-3-haiku-20240307-v1:0): | ||
# Before updating the agent: | ||
# I apologize, but I do not have any information about AI or the ability to define it. | ||
# As I mentioned, I am a friendly assistant without any knowledge about AI. I cannot | ||
# provide a definition for AI in one sentence. If you have a different question I can | ||
# try to assist with, please let me know. | ||
# After updating the agent: | ||
# AI is the field of computer science that aims to create systems and machines that can | ||
# perform tasks that typically require human intelligence, such as learning, problem-solving, | ||
# perception, and decision-making. | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
54 changes: 54 additions & 0 deletions
54
python/samples/concepts/agents/bedrock_agent/bedrock_agent_use_existing.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,54 @@ | ||
# 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 in the simplest way. | ||
# This sample uses the following main component(s): | ||
# - a Bedrock agent that has already been created | ||
# You will learn how to connect to an existing Bedrock agent and talk to it. | ||
|
||
|
||
# Make sure to replace AGENT_NAME and AGENT_ID with the correct values | ||
AGENT_NAME = "semantic-kernel-bedrock-agent" | ||
AGENT_ID = "..." | ||
|
||
|
||
async def main(): | ||
bedrock_agent = await BedrockAgent.retrieve(AGENT_ID, AGENT_NAME) | ||
|
||
# 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 | ||
async for response in bedrock_agent.invoke( | ||
session_id=new_session_id, | ||
input_text=user_input, | ||
): | ||
print(f"Bedrock agent: {response}") | ||
except KeyboardInterrupt: | ||
print("\n\nExiting chat...") | ||
return False | ||
except EOFError: | ||
print("\n\nExiting chat...") | ||
return False | ||
|
||
# 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()) |
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
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
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