-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(models): add support for Amazon Bedrock native API #1115
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
feat(models): add support for Amazon Bedrock native API #1115
Conversation
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.
Awesome contribution! Really happy to see this new model supported!
I think you already tested it locally and the model calls work as expected: do you know if some additional test could be implemented?
Please find some comments/suggestions below to align your implementation better with the rest of the models.
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
Hi @albertvillanova, thanks for the super quick review, I really appreciate it! I'm off work today until 4PM WEST, but I hope to respond to all comments today. |
No hurry at all, @leandrodamascena! Let me know if you need any help from my side. |
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.
Hey @albertvillanova! Thanks for all the feedback, I addressed all of them and it's probably read to another review.
I attached a quick video showing this working and I can create others with specific settings if you want to see this working.
Screen.Recording.2025-04-02.at.16.12.39.mov
My bad here! I didn't saw that projects support python 3.10+ and I'm using |
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.
Thanks a lot! Just some minor comments/suggestions below and we are ready to merge!
# The Bedrock API does not support the `type` key in requests. | ||
# This block of code modifies the object to meet Bedrock's requirements. | ||
for message in messages.get("messages", []): | ||
for content in message.get("content", []): |
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.
I think you should add an if condition here to apply this modification only if message["content"]
is an instance of list. Note that it could also be an instance of str.
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.
Can you help me here? In what situations can it be an instance of str
? I tried many scenarios here and couldn't get an instance of str.
In Bedrock, it should always be an instance of list. And since I am just passing the array to bedrock, I expect it to always be a list. If it can be a str
, then I need to do some logic to transform it.
client.converse(
modelId='string',
messages=[
{
'role': 'user'|'assistant',
'content': [ ... ]
}
}
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.
We implemented the flatten_messages_as_text
parameter to handle this behavior: depending on the provider, some models do require a str content, others require a list content and others support both formats.
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.
Hmmm so in Bedrock I need to set the default to False always?
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.
Yes, if no model can be passed a str content...
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.
Yes! This is not so much about the model, but more about the bedrock-runtime API, and the API is the same for all models available in Bedrock. So I removed this field from the AmazonBedrockServerModel
class and I am passing False to ApiModel
.
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.
I have already sent a commit.
Hi @albertvillanova! Thanks for your super detailed review! I've addressed most of the comments and I think the one about |
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.
Great work! Thanks a lot for your effort and for submitting such a high-quality PR. I appreciate your contribution and really enjoyed reviewing it.
Just a few nits below and we are good to merge!
Thank you so much for your patience and review @albertvillanova! Not knowing the codebase and all the features is a challenge, but you led me to the success. I just made one suggested change and commented on the other. |
Thanks again for your awesome contribution! 🤗 |
Thanks for all the support and please fell free to reach me out if I can help with any bug/feature for this new integration. |
@leandrodamascena I'm reworking the cleaning of messages in https://github.com/huggingface/smolagents/pull/1002/files, and I wonder about one of your comments about bedrock chat template: we've got two possible templates
The idea of the PR https://github.com/huggingface/smolagents/pull/1002/files is to flatten by default messages to format 2. |
Hey @aymeric-roucher let me see what you're doing and will reply this comment today. |
Hey @aymeric-roucher, neither of those payloads work with Bedrock because, when working with text, the Bedrock API only supports payloads in the following format: messages = [
{
'role': 'user',
'content': [
{
'text': "..."
}
]
}
] You can see that the The Can I help with something in the PR #1002 ? |
Closes #1092
Summary
This pull request introduces the new
AmazonBedrockServerModel
class that provides direct access to Amazon Bedrock's foundation models via the Bedrock API. The implementation leverages the Bedrockconverse
API endpoint, allowing users to interact with all available Bedrock models through a consistent interface while maintaining the flexibility to configure model-specific parameters.Implementation Details
AmazonBedrockServerModel
class that extendsApiModel
converse
APIDocumentation Changes
AmazonBedrockServerModel
Testing
Additional Notes
I've added detailed comments throughout the code to explain the implementation choices and help future contributors understand the design patterns. The implementation follows the project's established patterns while accommodating the requirements of the Bedrock API.
User experience
Basic Usage
Advanced Configuration
Let me know if you'd like any additional changes or have questions about the implementation.