-
Notifications
You must be signed in to change notification settings - Fork 579
feat(bedrock): add guardrail_last_turn_only option #1224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Related: #999 |
|
@aiancheruk Thank you for raising this PR! Could you refine the helper function a bit? Once that's done, I'll be happy to merge it. |
cb166a6 to
8cda99f
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…en feature flag is true
src/strands/models/bedrock.py
Outdated
| last_user_idx = -1 | ||
| if guardrail_last_turn_only: | ||
| for i in range(len(messages) - 1, -1, -1): | ||
| if messages[i]["role"] == "user": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will also apply to any tool result content blocks. Is the intention here to only apply the guardrail to the latest message sent by the user, or the latest message with role of "user" (includes toolResult blocks)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good callout, we should avoid toolReuslt.
| guardrail_version: Version of the guardrail to apply | ||
| guardrail_stream_processing_mode: The guardrail processing mode | ||
| guardrail_redact_input: Flag to redact input if a guardrail is triggered. Defaults to True. | ||
| guardrail_redact_input_message: If a Bedrock Input guardrail triggers, replace the input with this message. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this change work with redaction? It looks like the logic in this pull request will search back for the latest user message to apply guardrailContent to, but redaction only works on the latest message in the messages array.
We may need to simplify the logic in this pull request to only check if the latest message in the messages array has role "user", and only contains a content block of type "text"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are absolutely right, but we should allow multimodal content, so the only case is to avoid toolResult.
Then after we find last_user_idx we check if there is 'text' field.
c75596b to
38ff327
Compare
Description
This PR adds a new guardrail_last_turn_only parameter to the BedrockModel that allows users to send only the last conversation turn (the most recent user message and assistant response) to AWS Bedrock Guardrails for evaluation, instead of the entire conversation history.
Key changes:
guardrail_last_turn_only: bool = Falseparameter toBedrockConfig_get_last_turn_messages()helper method to extract the last turn from conversation history_format_request()to use filtered messages whenguardrail_last_turn_only=TrueBenefits:
Related Issues
#999
Documentation PR
strands-agents/docs#340
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.