Configure Lambda functions to send information that an Amazon Bedrock agent elicits from the user (original) (raw)

You can define a Lambda function to program the business logic for an action group. After a Amazon Bedrock agent determines the API operation that it needs to invoke in an action group, it sends information from the API schema alongside relevant metadata as an input event to the Lambda function. To write your function, you must understand the following components of the Lambda function:

You write your Lambda function to define how to handle an action group and to customize how you want the API response to be returned. You use the variables from the input event to define your functions and return a response to the agent.

Note

An action group can contain up to 11 API operations, but you can only write one Lambda function. Because the Lambda function can only receive an input event and return a response for one API operation at a time, you should write the function considering the different API operations that may be invoked.

For your agent to use a Lambda function, you must attach a resource-based policy to the function to provide permissions for the agent. For more information, follow the steps at Resource-based policy to allow Amazon Bedrock to invoke an action group Lambda function. For more information about resource-based policies in Lambda, see Using resource-based policies for Lambda in the AWS Lambda Developer Guide.

To learn how to define a function while creating the action group, see Add an action group to your agent in Amazon Bedrock.

Topics

Lambda input event from Amazon Bedrock

When an action group using a Lambda function is invoked, Amazon Bedrock sends a Lambda input event of the following general format. You can define your Lambda function to use any of the input event fields to manipulate the business logic within the function to successfully perform the action. For more information about Lambda functions, see Event-driven invocation in the AWS Lambda Developer Guide.

The input event format depends on whether you defined the action group with an API schema or with function details:

{  
    "messageVersion": "1.0",  
    "agent": {  
        "name": "string",  
        "id": "string",  
        "alias": "string",  
        "version": "string"  
    },  
    "inputText": "string",  
    "sessionId": "string",  
    "actionGroup": "string",  
    "apiPath": "string",  
    "httpMethod": "string",  
    "parameters": [  
        {  
            "name": "string",  
            "type": "string",  
            "value": "string"  
        },  
    ...  
    ],  
    "requestBody": {  
        "content": {  
            "<content_type>": {  
                "properties": [  
                   {  
                       "name": "string",  
                       "type": "string",  
                       "value": "string"  
                    },  
                            ...  
                ]  
            }  
        }  
    },  
    "sessionAttributes": {  
        "string": "string",  
    },  
    "promptSessionAttributes": {  
        "string": "string"  
    }  
}  
{  
    "messageVersion": "1.0",  
    "agent": {  
        "name": "string",  
        "id": "string",  
        "alias": "string",  
        "version": "string"  
    },  
    "inputText": "string",  
    "sessionId": "string",  
    "actionGroup": "string",  
    "function": "string",  
    "parameters": [  
        {  
            "name": "string",  
            "type": "string",  
            "value": "string"  
        },  
    ...  
    ],  
    "sessionAttributes": {  
        "string": "string",  
    },  
    "promptSessionAttributes": {  
        "string": "string"  
    }  
}  

The following list describes the input event fields;

Lambda response event to Amazon Bedrock

Amazon Bedrock expects a response from your Lambda function that matches the following format. The response consists of parameters returned from the API operation. The agent can use the response from the Lambda function for further orchestration or to help it return a response to the customer.

Note

The maximum payload response size matches the maximum size of a synchronous response from the Lambda function. For more information, see the invocation payload resource quota in the AWS Lambda Developer Guide.

The input event format depends on whether you defined the action group with an API schema or with function details:

{  
    "messageVersion": "1.0",  
    "response": {  
        "actionGroup": "string",  
        "apiPath": "string",  
        "httpMethod": "string",  
        "httpStatusCode": number,  
        "responseBody": {  
            "<contentType>": {  
                "body": "JSON-formatted string"  
            }  
        }  
    },  
    "sessionAttributes": {  
        "string": "string",  
        ...  
    },  
    "promptSessionAttributes": {  
        "string": "string",  
        ...  
    },  
    "knowledgeBasesConfiguration": [  
        {  
            "knowledgeBaseId": "string",  
            "retrievalConfiguration": {  
                "vectorSearchConfiguration": {  
                    "numberOfResults": int,  
                    "overrideSearchType": "HYBRID | SEMANTIC",  
                    "filter": RetrievalFilter object  
                }  
            }  
        },  
        ...  
    ]  
}  
{  
    "messageVersion": "1.0",  
    "response": {  
        "actionGroup": "string",  
        "function": "string",  
        "functionResponse": {  
            "responseState": "FAILURE | REPROMPT",  
            "responseBody": {  
                "<functionContentType>": {  
                    "body": "JSON-formatted string"  
                }  
            }  
        }  
    },  
    "sessionAttributes": {  
        "string": "string",  
    },  
    "promptSessionAttributes": {  
        "string": "string"  
    },  
    "knowledgeBasesConfiguration": [  
        {  
            "knowledgeBaseId": "string",  
            "retrievalConfiguration": {  
                "vectorSearchConfiguration": {  
                    "numberOfResults": int,  
                    "filter": {  
                        RetrievalFilter object  
                    }  
                }  
            }  
        },  
        ...  
    ]  
}  

The following list describes the response fields:

Action group Lambda function example

The following is an minimal example of how the Lambda function can be defined in Python. Select the tab corresponding to whether you defined the action group with an OpenAPI schema or with function details:

OpenAPI schema

def lambda_handler(event, context):

    agent = event['agent']
    actionGroup = event['actionGroup']
    api_path = event['apiPath']
    # get parameters
    get_parameters = event.get('parameters', [])
    # post parameters
    post_parameters = event['requestBody']['content']['application/json']['properties']

    response_body = {
        'application/json': {
            'body': "sample response"
        }
    }
    
    action_response = {
        'actionGroup': event['actionGroup'],
        'apiPath': event['apiPath'],
        'httpMethod': event['httpMethod'],
        'httpStatusCode': 200,
        'responseBody': response_body
    }
    
    session_attributes = event['sessionAttributes']
    prompt_session_attributes = event['promptSessionAttributes']
    
    api_response = {
        'messageVersion': '1.0', 
        'response': action_response,
        'sessionAttributes': session_attributes,
        'promptSessionAttributes': prompt_session_attributes
    }
        
    return api_response

Function details

def lambda_handler(event, context):

    agent = event['agent']
    actionGroup = event['actionGroup']
    function = event['function']
    parameters = event.get('parameters', [])

    response_body = {
        'TEXT': {
            'body': "sample response"
        }
    }
    
    function_response = {
        'actionGroup': event['actionGroup'],
        'function': event['function'],
        'functionResponse': {
            'responseBody': response_body
        }
    }
    
    session_attributes = event['sessionAttributes']
    prompt_session_attributes = event['promptSessionAttributes']
    
    action_response = {
        'messageVersion': '1.0', 
        'response': function_response,
        'sessionAttributes': session_attributes,
        'promptSessionAttributes': prompt_session_attributes
    }
        
    return action_response