Code below @Girijesh (with some added notes of mine):
# Before you start, please run the following code to set up your environment.
# This code will reset the environment (if needed) and prepare the resources for the lesson.
# It does this by quickly running through all the code from the previous lessons.
!sh ./ro_shared_data/reset.sh
%run ./ro_shared_data/lesson_2_prep.py lesson2
import os
agentId = os.environ['BEDROCK_AGENT_ID']
agentAliasId = os.environ['BEDROCK_AGENT_ALIAS_ID']
region_name = 'us-west-2'
lambda_function_arn = os.environ['LAMBDA_FUNCTION_ARN']
#################
import boto3
import uuid
from helper import *
sessionId = str(uuid.uuid4())
message = "My name is Mike, my mug is broken and I want a refund."
invoke_agent_and_print(
agentId=agentId,
agentAliasId=agentAliasId,
inputText=message,
sessionId=sessionId
)
# Define the function for the agent
create_agent_action_group_response = bedrock_agent.create_agent_action_group(
actionGroupName='customer-support-actions',
agentId=agentId,
actionGroupExecutor={
'lambda': lambda_function_arn #defined in the helper code
},
functionSchema={
'functions': [ #each of the functions/actions to follow through with
{
'name': 'customerId',
'description': 'Get a customer ID given available details. At least one parameter must be sent to the function. This is private information and must not be given to the user.', #natural language description of the function so the agent knows what it does
'parameters': {
'email': {
'description': 'Email address',
'required': False,
'type': 'string'
},
'name': {
'description': 'Customer name',
'required': False,
'type': 'string'
},
'phone': {
'description': 'Phone number',
'required': False,
'type': 'string'
},
}
},
{
'name': 'sendToSupport',
'description': 'Send a message to the support team, used for service escalation. ',
'parameters': {
'custId': {
'description': 'customer ID',
'required': True,
'type': 'string'
},
'supportSummary': {
'description': 'Summary of the support request',
'required': True,
'type': 'string'
}
}
}
]
},
agentVersion='DRAFT',
)
# call the function
create_agent_action_group_response
'''{'ResponseMetadata': {'RequestId': '1e9234ee-d77a-4384-9705-404db8945ef2',
'HTTPStatusCode': 200,
'HTTPHeaders': {'date': 'Fri, 01 Nov 2024 17:36:36 GMT',
'content-type': 'application/json',
'content-length': '1187',
'connection': 'keep-alive',
'x-amzn-requestid': '1e9234ee-d77a-4384-9705-404db8945ef2',
'x-amz-apigw-id': 'Ak-xtHHwPHcEAYw=',
'x-amzn-trace-id': 'Root=1-672511a4-6787586422cf9fd23c591f84'},
'RetryAttempts': 0},
'agentActionGroup': {'actionGroupExecutor': {'lambda': 'arn:aws:lambda:us-west-2:211125649762:function:dlai-support-agent-4REIH'},
'actionGroupId': 'OGESQU6MVY',
'actionGroupName': 'customer-support-actions',
'actionGroupState': 'ENABLED',
'agentId': 'FM0ZD5LAJ5',
'agentVersion': 'DRAFT',
'createdAt': datetime.datetime(2024, 11, 1, 17, 36, 36, 271704, tzinfo=tzlocal()),
'functionSchema': {'functions': [{'description': 'Get a customer ID given available details. At least one parameter must be sent to the function. This is private information and must not be given to the user.',
'name': 'customerId',
'parameters': {'email': {'description': 'Email address',
'required': False,
'type': 'string'},
'name': {'description': 'Customer name',
'required': False,
'type': 'string'},
'phone': {'description': 'Phone number',
'required': False,
'type': 'string'}},
'requireConfirmation': 'DISABLED'},
{'description': 'Send a message to the support team, used for service escalation. ',
'name': 'sendToSupport',
'parameters': {'custId': {'description': 'customer ID',
'required': True,
'type': 'string'},
'supportSummary': {'description': 'Summary of the support request',
'required': True,
'type': 'string'}},
'requireConfirmation': 'DISABLED'}]},
'updatedAt': datetime.datetime(2024, 11, 1, 17, 36, 36, 271704, tzinfo=tzlocal())}}'''
# Get the ActionID from the response to check the status of the action
actionGroupId = create_agent_action_group_response['agentActionGroup']['actionGroupId']
wait_for_action_group_status(
agentId=agentId,
actionGroupId=actionGroupId,
targetStatus='ENABLED'
)
# Prepare agent
bedrock_agent.prepare_agent(
agentId=agentId
)
wait_for_agent_status(
agentId=agentId,
targetStatus='PREPARED'
)
# Update the agent alias
bedrock_agent.update_agent_alias(
agentId=agentId,
agentAliasId=agentAliasId,
agentAliasName='MyAgentAlias',
)
wait_for_agent_alias_status(
agentId=agentId,
agentAliasId=agentAliasId,
targetStatus='PREPARED'
)
# This has now setup the agent with a few actions (functions) it is able to do in response to a user query.
# Below will test the agent to see if it starts using these functions to resolve the user query
sessionId = str(uuid.uuid4())
message = "My name is Mike (mike@mike.com), my mug is broken and I want a refund."
invoke_agent_and_print(
agentId=agentId,
agentAliasId=agentAliasId,
inputText=message,
sessionId=sessionId,
enableTrace=False
)
invoke_agent_and_print(
agentId=agentId,
agentAliasId=agentAliasId,
inputText=message,
sessionId=sessionId,
enableTrace=True
)