-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(bedrock): add multimodal support for documents, images and videos #1888
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
Closed
tzolov
wants to merge
3
commits into
spring-projects:main
from
tzolov:bedrock-converse-imporve-multimodality-support
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,10 +37,6 @@ | |
| <developerConnection>[email protected]:spring-projects/spring-ai.git</developerConnection> | ||
| </scm> | ||
|
|
||
| <properties> | ||
| <aws.sdk.version>2.29.3</aws.sdk.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
|
|
||
| <!-- production dependencies --> | ||
|
|
@@ -59,7 +55,7 @@ | |
| <dependency> | ||
| <groupId>software.amazon.awssdk</groupId> | ||
| <artifactId>bedrockruntime</artifactId> | ||
| <version>${aws.sdk.version}</version> | ||
| <version>${awssdk.version}</version> | ||
| <exclusions> | ||
| <exclusion> | ||
| <groupId>commons-logging</groupId> | ||
|
|
@@ -71,13 +67,13 @@ | |
| <dependency> | ||
| <groupId>software.amazon.awssdk</groupId> | ||
| <artifactId>sts</artifactId> | ||
| <version>${aws.sdk.version}</version> | ||
| <version>${awssdk.version}</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>software.amazon.awssdk</groupId> | ||
| <artifactId>netty-nio-client</artifactId> | ||
| <version>${aws.sdk.version}</version> | ||
| <version>${awssdk.version}</version> | ||
| </dependency> | ||
|
|
||
| <!-- test dependencies --> | ||
|
|
||
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
133 changes: 133 additions & 0 deletions
133
...onverse/src/main/java/org/springframework/ai/bedrock/converse/api/BedrockMediaFormat.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| * Copyright 2024 - 2024 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.springframework.ai.bedrock.converse.api; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| import software.amazon.awssdk.services.bedrockruntime.model.DocumentFormat; | ||
| import software.amazon.awssdk.services.bedrockruntime.model.ImageFormat; | ||
| import software.amazon.awssdk.services.bedrockruntime.model.VideoFormat; | ||
|
|
||
| import org.springframework.ai.model.Media; | ||
| import org.springframework.util.MimeType; | ||
|
|
||
| /** | ||
| * The BedrockMediaFormat class provides mappings between MIME types and their | ||
| * corresponding Bedrock media formats for documents, images, and videos. It supports | ||
| * conversion of MIME types to specific formats used by the Bedrock runtime. | ||
| * | ||
| * <p> | ||
| * Supported document formats include PDF, CSV, DOC, DOCX, XLS, XLSX, HTML, TXT, and MD. | ||
| * Supported image formats include JPEG, PNG, GIF, and WEBP. Supported video formats | ||
| * include MKV, MOV, MP4, WEBM, FLV, MPEG, MPG, WMV, and 3GP. | ||
| * </p> | ||
| * | ||
| * <p> | ||
| * Usage example: | ||
| * </p> | ||
| * <pre> | ||
| * String format = BedrockMediaFormat.getFormatAsString(Media.Format.DOC_PDF); | ||
| * </pre> | ||
| * | ||
| * <p> | ||
| * Throws IllegalArgumentException if the MIME type is unsupported. | ||
| * </p> | ||
| * | ||
| * @author Christian Tzolov | ||
| * @since 1.0.0 | ||
| */ | ||
| public class BedrockMediaFormat { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make abstract |
||
|
|
||
| // @formatter:off | ||
| public static final Map<MimeType, DocumentFormat> DOCUMENT_MAP = Map.of( | ||
| Media.Format.DOC_PDF, DocumentFormat.PDF, | ||
| Media.Format.DOC_CSV, DocumentFormat.CSV, | ||
| Media.Format.DOC_DOC, DocumentFormat.DOC, | ||
| Media.Format.DOC_DOCX, DocumentFormat.DOCX, | ||
| Media.Format.DOC_XLS, DocumentFormat.XLS, | ||
| Media.Format.DOC_XLSX, DocumentFormat.XLSX, | ||
| Media.Format.DOC_HTML, DocumentFormat.HTML, | ||
| Media.Format.DOC_TXT, DocumentFormat.TXT, | ||
| Media.Format.DOC_MD, DocumentFormat.MD); | ||
| // @formatter:on | ||
|
|
||
| // @formatter:off | ||
| public static final Map<MimeType, ImageFormat> IMAGE_MAP = Map.of( | ||
| Media.Format.IMAGE_JPEG, ImageFormat.JPEG, | ||
| Media.Format.IMAGE_PNG, ImageFormat.PNG, | ||
| Media.Format.IMAGE_GIF, ImageFormat.GIF, | ||
| Media.Format.IMAGE_WEBP, ImageFormat.WEBP); | ||
| // @formatter:on | ||
|
|
||
| // @formatter:off | ||
| public static final Map<MimeType, VideoFormat> VIDEO_MAP = Map.of( | ||
| Media.Format.VIDEO_MKV, VideoFormat.MKV, | ||
| Media.Format.VIDEO_MOV, VideoFormat.MOV, | ||
| Media.Format.VIDEO_MP4, VideoFormat.MP4, | ||
| Media.Format.VIDEO_WEBM, VideoFormat.WEBM, | ||
| Media.Format.VIDEO_FLV, VideoFormat.FLV, | ||
| Media.Format.VIDEO_MPEG, VideoFormat.MPEG, | ||
| Media.Format.VIDEO_WMV, VideoFormat.WMV, | ||
| Media.Format.VIDEO_THREE_GP, VideoFormat.THREE_GP); | ||
| // @formatter:on | ||
|
|
||
| public static String getFormatAsString(MimeType mimeType) { | ||
| if (isSupportedDocumentFormat(mimeType)) { | ||
| return DOCUMENT_MAP.get(mimeType).toString(); | ||
| } | ||
| else if (isSupportedImageFormat(mimeType)) { | ||
| return IMAGE_MAP.get(mimeType).toString(); | ||
| } | ||
| else if (isSupportedVideoFormat(mimeType)) { | ||
| return VIDEO_MAP.get(mimeType).toString(); | ||
| } | ||
| throw new IllegalArgumentException("Unsupported media format: " + mimeType); | ||
| } | ||
|
|
||
| public static Boolean isSupportedDocumentFormat(MimeType mimeType) { | ||
| return DOCUMENT_MAP.containsKey(mimeType); | ||
| } | ||
|
|
||
| public static DocumentFormat getDocumentFormat(MimeType mimeType) { | ||
| if (!isSupportedDocumentFormat(mimeType)) { | ||
| throw new IllegalArgumentException("Unsupported document format: " + mimeType); | ||
| } | ||
| return DOCUMENT_MAP.get(mimeType); | ||
| } | ||
|
|
||
| public static Boolean isSupportedImageFormat(MimeType mimeType) { | ||
| return IMAGE_MAP.containsKey(mimeType); | ||
| } | ||
|
|
||
| public static ImageFormat getImageFormat(MimeType mimeType) { | ||
| if (!isSupportedImageFormat(mimeType)) { | ||
| throw new IllegalArgumentException("Unsupported image format: " + mimeType); | ||
| } | ||
| return IMAGE_MAP.get(mimeType); | ||
| } | ||
|
|
||
| public static Boolean isSupportedVideoFormat(MimeType mimeType) { | ||
| return VIDEO_MAP.containsKey(mimeType); | ||
| } | ||
|
|
||
| public static VideoFormat getVideoFormat(MimeType mimeType) { | ||
| if (!isSupportedVideoFormat(mimeType)) { | ||
| throw new IllegalArgumentException("Unsupported video format: " + mimeType); | ||
| } | ||
| return VIDEO_MAP.get(mimeType); | ||
| } | ||
|
|
||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
should look to break up this multi-screen function into if/else structure with additional helper methods.