Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.ai.anthropic.api.AnthropicApi.ChatCompletionRequest;
import org.springframework.ai.anthropic.api.AnthropicApi.ChatCompletionResponse;
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlock;
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlock.Source;
import org.springframework.ai.anthropic.api.AnthropicApi.ContentBlock.Type;
import org.springframework.ai.anthropic.api.AnthropicApi.Role;
import org.springframework.ai.anthropic.metadata.AnthropicUsage;
Expand All @@ -58,6 +59,7 @@
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.ChatOptionsBuilder;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.Media;
import org.springframework.ai.model.ModelOptionsUtils;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.ai.model.function.FunctionCallbackContext;
Expand Down Expand Up @@ -355,6 +357,16 @@ else if (mediaData instanceof String text) {

}

private Type getContentBlockTypeByMedia(Media media) {
if (media.getMimeType().toString().startsWith("image")) {
return Type.IMAGE;
}
else if (media.getMimeType().toString().contains("pdf")) {
return Type.DOCUMENT;
}
return Type.IMAGE;
Copy link
Member

Choose a reason for hiding this comment

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

if it wasn't classified as image, prob better to throw an exception

}

ChatCompletionRequest createRequest(Prompt prompt, boolean stream) {

Set<String> functionsForThisRequest = new HashSet<>();
Expand All @@ -367,11 +379,12 @@ ChatCompletionRequest createRequest(Prompt prompt, boolean stream) {
List<ContentBlock> contents = new ArrayList<>(List.of(new ContentBlock(message.getContent())));
if (message instanceof UserMessage userMessage) {
if (!CollectionUtils.isEmpty(userMessage.getMedia())) {
List<ContentBlock> mediaContent = userMessage.getMedia()
.stream()
.map(media -> new ContentBlock(media.getMimeType().toString(),
this.fromMediaData(media.getData())))
.toList();
List<ContentBlock> mediaContent = userMessage.getMedia().stream().map(media -> {
Type contentBlockType = getContentBlockTypeByMedia(media);
var source = new Source(media.getMimeType().toString(),
this.fromMediaData(media.getData()));
return new ContentBlock(contentBlockType, source);
}).toList();
contents.addAll(mediaContent);
}
}
Expand Down
Loading