In this post we show creation of Agent in AWS bedrock and a simple text chat application using python boto3 library.
Create Agent in AWS Console
First open the AWS console, navigate to the AWS bedrock service, and click on Agents.
Click on Create Agent, enter a name for it, and click Create.
For our demo, we switch to the cheapest available model:
Fill in instructions for the agent:
Click on Prepare to create a draft of the agent:
We can now test the agent is working as expected, or else update the agent instructions.
Now we create an alias for the agent, which is a published version of the agent.
Chat using python boto3
Use the following code to run a simple chat with the agent.
import uuid
import boto3
REGION = "us-east-1"
AGENT_ID = "AB5BQ7PVAL"
AGENT_ALIAS_ID = "9BII1XBJP9"
client = boto3.client('bedrock-agent-runtime', region_name=REGION)
session_id = str(uuid.uuid4())
print("Chat Starting")
while True:
user_input = input("Enter prompt:")
if user_input.lower() in ['exit', 'quit']:
print("Bye")
break
response = client.invoke_agent(
agentId=AGENT_ID,
agentAliasId=AGENT_ALIAS_ID,
sessionId=session_id,
inputText=user_input,
)
response_body = response['completion']
print("Answer:", end=" ", flush=True)
for chunk in response_body:
if 'chunk' in chunk:
print(chunk['chunk']['bytes'].decode("utf-8"), end="", flush=True)
print()
An example of this chat is below.
Final Note
This is a very simple example of the AWS bedrock agent activation. Other agent properties include guardrails, agent memory across sessions, and multi-agent configuration.
Best practices for agents creation can be found here.
No comments:
Post a Comment