概述

在这个实验中,我们提供了一个使用 Amazon Bedrock Agent 功能定义的 HR 代理示例。

在创建代理的操作组时,我们可以通过提供函数详细信息或传递 API 架构来定义操作。当提供函数详细信息时,我们可以简化操作组创建过程,并设置代理以引出我们定义的一组参数。然后,我们可以将这些参数传递给我们的应用程序,并自定义如何使用它们在我们自己的系统中执行操作。

代理连接到一个包含有关员工可用假期天数和计划休假的生成数据的内存 SQLite 数据库。创建的体系结构如下:

其中假期数据库具有以下模式:

该代理允许员工 get_available_vacations_daysreserve_vacation_time 根据员工的请求。

下面的代码显示了作为 JSON 对象列表定义的函数,这些对象通过 functionSchema 参数传递给代理的操作组。

    agent_functions = [
        {
            'name': 'get_available_vacations_days'
            'description': '获取某个员工的可用假期天数'
            'parameters': {
                "employee_id": {
                    "description": "获取可用假期的员工 ID"
                    "required": True
                    "type": "integer"
                }
            }
        }
        {
            'name': 'reserve_vacation_time'
            'description': '为特定员工预订假期时间'
            'parameters': {
                "employee_id": {
                    "description": "将为其预订时间的员工 ID"
                    "required": True
                    "type": "integer"
                }
                "start_date": {
                    "description": "假期开始日期"
                    "required": True
                    "type": "string"
                }
                "end_date": {
                    "description": "假期结束日期"
                    "required": True
                    "type": "string"
                }
            }
        }
    ]

函数定义作为 functionSchema 参数传递给 create_agent_action_group 函数

    agent_action_group_response = bedrock_agent_client.create_agent_action_group(
        agentId=agent_id
        agentVersion='DRAFT'
        actionGroupExecutor={
            'lambda': lambda_function['FunctionArn']
        }
        actionGroupName=agent_action_group_name
        functionSchema={
            'functions': agent_functions
        }
        description=agent_action_group_description
    )

这两个操作都作为 AWS Lambda 函数的一部分实现,该函数通过事件从代理接收输入。

事件具有以下结构:

{
    "messageVersion": "1.0" 
    "agent": {
        "alias": "<AGENT_ALIAS>" 
        "name": "hr-assistant-function-def" 
        "version": "<AGENT_VERSION>"
        "id": "<AGENT_ID>"
    } 
    "sessionId": "<SESSION_ID>" 
    "sessionAttributes": {
        "<ATTRIBUTE_NAME>": "# Session attributes to be addressed in example 06-prompt-and-session-attributes"
    } 
    "promptSessionAttributes": {
        "<PROMPT_NAME>": "# Session attributes to be addressed in example 06-prompt-and-session-attributes"
    } 
    "inputText": "<USER_INPUT_TEXT>" 
    "actionGroup": "VacationsActionGroup" 
    "function": "<FUNCTION_TRIGGERED_BY_USER_INPUT_TEXT>" 
    "parameters": [{
        "<PARAM_1>": "<PARAM_1_VAL>" 
        "<PARAM_2>": "<PARAM_2_VAL>" 
        "<PARAM_N>": "<PARAM_N_VAL>"
    }]
}

要处理代理正在调用的操作,我们需要从 event 中恢复操作组、函数和参数,并将响应作为包含 TEXT 键的 JSON 对象返回。为此,以下代码添加到 Lambda 函数中:

def lambda_handler(event context):
    action_group = event['actionGroup']
    function = event['function']
    parameters = event.get('parameters' [])
    
    # 设置预期的响应主体
    responseBody =  {
        "TEXT": {
            "body": "sample response"
        }
    }
    
    # 处理请求的操作的逻辑在此处
    ...
    
    # 最后,将响应返回给代理
    action_response = {
        'actionGroup': action_group
        'function': function
        'functionResponse': {
            'responseBody': responseBody
        }

    }

    function_response = {
        'response': action_response 
        'messageVersion': event['messageVersion']
    }
    return function_response

试一试

在 AWS 活动中

如果我们是通过 workshop studio 参加研讨会的,现在请转到 SageMaker Studio 中的 JupyterLab。

在 JupyterLab UI 中导航到 bedrock-agents-workshop/01-create-agent-with-function-definition/01-create-agent-with-function-definition.ipynb

自主学习

这里有一个笔记本,让我们可以尝试上述内容: 使用函数定义创建代理