Refactor AWS Bedrock Provider for Multi-modal Support & Correct Token Limits#3714
Merged
timothycarambat merged 14 commits intoMintplex-Labs:masterfrom May 6, 2025
Merged
Conversation
added 4 commits
April 24, 2025 12:59
…ropic Claude Sonnet models: - Context Window defaults to 8192 maximum, which isn't correct - Multimodal stopped working when removing langchain, which was transparently handling image_url to a format sonnet expects.
move utils for AWS specific functionality to subfile add token output max to ENV so setting persits
cabwds
pushed a commit
to cabwds/anything-llm
that referenced
this pull request
Jul 3, 2025
… Limits (Mintplex-Labs#3714) * Fixed two primary issues discovered while using AWS Bedrock with Anthropic Claude Sonnet models: - Context Window defaults to 8192 maximum, which isn't correct - Multimodal stopped working when removing langchain, which was transparently handling image_url to a format sonnet expects. * Ran `yarn lint` * Updated .env.example to have aws bedrock examples too * Refactor for readability move utils for AWS specific functionality to subfile add token output max to ENV so setting persits --------- Co-authored-by: Tristan Stahnke <tristan.stahnke+gpsec@guidepointsecurity.com> Co-authored-by: Timothy Carambat <rambat1010@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Type
What is in this change?
This PR significantly refactors the AWSBedrockLLM provider (server/utils/AiProviders/bedrock/index.js) to:
Additional Information
The previous version of the Bedrock provider had two major issues:
No Multi-modal Support: The #generateContent function responsible for handling message attachments was unimplemented or commented out. This prevented users from sending images to multi-modal Bedrock models (like Claude 3).
Incorrect maxTokens Usage: The promptWindowLimit() function (reading AWS_BEDROCK_LLM_MODEL_TOKEN_LIMIT) was used to set the inferenceConfig.maxTokens parameter in API calls. This value represents the total context window, which is often much larger than the maximum number of tokens a model can generate in a single output. This led to ValidationException errors when the configured context window exceeded the model's specific output limit (e.g., sending 131k when the model's output limit was 8192).
Detailed Changes:
Multi-modal Input Implementation (#generateContent & Helpers)
Implemented #generateContent:
Added getImageFormatFromMime Helper:
Added Base64 Data URI Stripping:
Added base64ToUint8Array Helper:
Output Token Limit Correction (getMaxOutputTokens & API Calls)
Introduced DEFAULT_MAX_OUTPUT_TOKENS:
Added getMaxOutputTokens Method:
Updated API Calls (getChatCompletion, streamGetChatCompletion):
Prompt Construction (constructPrompt)
API Call Structure
Code Quality and Refinements
Introduced constants for better maintainability (SUPPORTED_BEDROCK_IMAGE_FORMATS, DEFAULT_MAX_OUTPUT_TOKENS, DEFAULT_CONTEXT_WINDOW_TOKENS).
Developer Validations
yarn lintfrom the root of the repo & committed changes