Skip to content

Attach S3 file to Bedrock Agent session during an Action Group invocation

0

Is it possible to attach a file to a Bedrock Agent's session for chat or code interpretation via the handling an Action Group Lambda function invocation?

I have an action that generates an S3 file, the contents of which are too large to return directly because they exceed the 25 kB response size limit. But the file is well within the attachment size limit. I understand how how to attach the file while invoking the agent from the API, and that works well. But I don't see how to attach the file dynamically as part of the action handling. Ideally, the agent should not need to be invoke a second time after the action group usage identifies the file.

So the desired flow is:

  1. User prompt leads to Lambda invocation
  2. Lambda invocation generates S3 file and attaches to session
  3. Agent uses S3 file in the same way that it would if it were attached during the initial invocation

Is this possible?

asked 2 years ago1.2K views

2 Answers
0

Yes, it is possible to attach a file to a Bedrock Agent's session for chat or code interpretation during an Action Group Lambda function invocation, although the process is not as straightforward as attaching a file during the initial agent invocation.

When an action group using a Lambda function is invoked, the function receives an input event from Amazon Bedrock. This input event contains various fields that can be used to manipulate the business logic within the function. The Lambda function can then return a response event to Amazon Bedrock.

To achieve your desired flow, you could modify your Lambda function to do the following:

  1. Generate the S3 file as you're currently doing.

  2. Instead of trying to attach the file directly (which isn't directly supported in the Lambda response), you can include information about the generated file in the session attributes or prompt session attributes of the Lambda response.

  3. In your Lambda function's response, include the S3 file information (such as the S3 bucket and key) in the sessionAttributes or promptSessionAttributes. For example:

    response = {
        "messageVersion": "1.0",
        "response": {
            # Your regular response data here
        },
        "sessionAttributes": {
            "generatedFileS3Bucket": "your-bucket-name",
            "generatedFileS3Key": "path/to/your/file.txt"
        }
    }
  4. The agent will receive this information as part of its session state.

  5. You can then configure your agent to check for these session attributes in subsequent turns of the conversation. If it finds the attributes, it can use them to retrieve and attach the file for chat or code interpretation.

This approach allows you to indirectly "attach" the file to the session by providing the necessary information for the agent to retrieve and use the file in subsequent interactions, without needing to invoke the agent a second time.

Keep in mind that this method requires careful design of your agent's prompts and potentially some custom logic to handle the file attachment based on the session attributes. The agent would need to be instructed to look for these attributes and know how to use them to retrieve and attach the file when needed.

While this solution isn't as direct as attaching a file during the initial invocation, it provides a way to dynamically include files generated during action handling in the same conversation session.
Sources
Configure Lambda functions to send information that an Amazon Bedrock agent elicits from the user - Amazon Bedrock
SessionState - Amazon Bedrock

answered 2 years ago

AWS
SUPPORT ENGINEER

reviewed 2 years ago

  • Please expand on "You can then configure your agent to check for these session attributes in subsequent turns of the conversation. If it finds the attributes, it can use them to retrieve and attach the file for chat or code interpretation." Does subsequent turns mean subsequent invocations in the same session, meaning I would need some logic in the code that's initiating the invocations to invoke the agent agent with the attachment? Or can the custom sessionAttributes be used to immediately use the file after getting the response from the Lambda function in the course of a single invocation.

    Edit: I guess the bot doesn't respond to followups? In any case, I can't tell if this is accurate and I think it may not be.

0

The answer provided by the re:Post agent is not true. Returning a URI in the session attributes doesn’t make it available during the execution of an action group or an orchestration. It seems (only for Bedrock agents) that the only way to add files is in the initial request or by invoking it again with the same session ID and the new files OR by having an action that receives the s3 uri and you have a custom agent to do that work for you.
And it is quite disappointing

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.