Skip to content

Conversation

rubenfonseca
Copy link
Contributor

@rubenfonseca rubenfonseca commented Nov 2, 2023

Issue number: #3261

Summary

Changes

Please provide a summary of what's being changed

This PR adds support for a new type of event handler: Bedrock Agents.

User experience

Please share what the user experience looks like before and after this change

Before this PR, this would be the code necessary to build a Bedrock Agent (taken from here):

def open_claims():
    ...

def identify_missing_documents(parameters):
    ...

def send_reminders():
    ...

def lambda_handler(event, context):
    response_code = ...
    action = event['actionGroup']
    api_path = event['apiPath']

    if api_path == '/claims':
        body = open_claims()
    elif api_path == '/claims/{claimId}/identify-missing-documents':
        parameters = event['parameters']
        body = identify_missing_documents(parameters)
    elif api_path == '/send-reminders':
        body =  send_reminders()
    else:
        body = {"{}::{} is not a valid api, try another one.".format(action, api_path)}

    response_body = {
        'application/json': {
            'body': str(body)
        }
    }

    return {
        'actionGroup': event['actionGroup'],
        'apiPath': event['apiPath'],
        'httpMethod': event['httpMethod'],
        'httpStatusCode': response_code,
        'responseBody': response_body
    }

After this PR, this is that the code above looks like:

from aws_lambda_powertools.event_handler import BedrockAgentResolver

app = BedrockAgentResolver()


@app.get("/claims")
def open_claims():
    ...


@app.get("/claims/<claim_id>/identify-missing-documents")
def identify_missing_documents(claim_id: str):
    ...


@app.get("/send-reminders")
def send_reminders():
    ...


def lambda_handler(event, context):
    return app.resolve(event, context)

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 2, 2023
@rubenfonseca rubenfonseca marked this pull request as ready for review November 7, 2023 08:15
@rubenfonseca rubenfonseca requested a review from a team November 7, 2023 08:15
@rubenfonseca
Copy link
Contributor Author

@heitorlessa @leandrodamascena opening up this for early code review, while I finish the documentation this morning

@rubenfonseca rubenfonseca linked an issue Nov 7, 2023 that may be closed by this pull request
2 tasks
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NICE!!! That's gonna be a real life saver for so many people.. and even more when Lambda supports streaming responses for Python one day perhaps.

one important question about response builder into a public attr as it isn't clear to me why that is.

The cast also makes me think we can do better but I only had 20 seconds to look before a meeting.

@rubenfonseca rubenfonseca force-pushed the rf/bedrock-event-handler2 branch from 275e7e7 to 9a288aa Compare November 7, 2023 10:30
@codecov-commenter
Copy link

codecov-commenter commented Nov 7, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ad9e40f) 95.26% compared to head (fd7c974) 95.32%.
Report is 7 commits behind head on develop.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3285      +/-   ##
===========================================
+ Coverage    95.26%   95.32%   +0.06%     
===========================================
  Files          207      208       +1     
  Lines         9581     9607      +26     
  Branches      1756     1759       +3     
===========================================
+ Hits          9127     9158      +31     
+ Misses         337      333       -4     
+ Partials       117      116       -1     
Files Coverage Δ
aws_lambda_powertools/event_handler/__init__.py 100.00% <100.00%> (ø)
aws_lambda_powertools/event_handler/api_gateway.py 98.14% <100.00%> (+0.48%) ⬆️
...s_lambda_powertools/event_handler/bedrock_agent.py 100.00% <100.00%> (ø)
...ools/utilities/data_classes/bedrock_agent_event.py 98.57% <100.00%> (+0.06%) ⬆️

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code to delete after your latest improvement ;)

heitorlessa
heitorlessa previously approved these changes Nov 7, 2023
@rubenfonseca
Copy link
Contributor Author

@heitorlessa ready for another review: I've removed dead code and added more tests, which uncovered a small bug :) yeah!

heitorlessa
heitorlessa previously approved these changes Nov 7, 2023
@heitorlessa
Copy link
Contributor

heitorlessa commented Nov 7, 2023

@rubenfonseca failing on pydantic

FAILED tests/functional/event_handler/test_bedrock_agent.py::test_bedrock_agent_event_with_validation_error
assert 'value is not a valid dict' in '{"detail": [{"type": "dict_type", "loc": ["response"], "msg": "Input should be a valid dictionary", "input": "oh no, this is not a dict", "url": "https://errors.pydantic.dev/2.4/v/dict_type"}]}'

rubenfonseca and others added 2 commits November 8, 2023 09:26
Co-authored-by: Heitor Lessa <lessa@amazon.com>
Signed-off-by: Ruben Fonseca <fonseka@gmail.com>
@rubenfonseca rubenfonseca force-pushed the rf/bedrock-event-handler2 branch from db718bb to fd7c974 Compare November 8, 2023 08:26
Copy link

sonarqubecloud bot commented Nov 8, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 4 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@rubenfonseca rubenfonseca merged commit ed3f07b into develop Nov 8, 2023
@rubenfonseca rubenfonseca deleted the rf/bedrock-event-handler2 branch November 8, 2023 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commons event_handlers size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Add support for the Bedrock Agent event and response formats
3 participants