|
| 1 | +package com.javaaidev.chatagent.googlegenai; |
| 2 | + |
| 3 | +import com.google.genai.types.Content; |
| 4 | +import com.google.genai.types.Part; |
| 5 | +import com.javaaidev.chatagent.model.Attachment; |
| 6 | +import com.javaaidev.chatagent.model.ChatAgentRequest; |
| 7 | +import com.javaaidev.chatagent.model.ChatAgentResponse; |
| 8 | +import com.javaaidev.chatagent.model.FileMessagePart; |
| 9 | +import com.javaaidev.chatagent.model.ImageMessagePart; |
| 10 | +import com.javaaidev.chatagent.model.ReasoningMessagePart; |
| 11 | +import com.javaaidev.chatagent.model.TextMessagePart; |
| 12 | +import com.javaaidev.chatagent.model.ThreadAssistantMessage; |
| 13 | +import com.javaaidev.chatagent.model.ThreadAssistantMessagePart; |
| 14 | +import com.javaaidev.chatagent.model.ThreadUserMessage; |
| 15 | +import com.javaaidev.chatagent.model.ThreadUserMessagePart; |
| 16 | +import io.reactivex.rxjava3.core.Flowable; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Base64; |
| 20 | +import org.springframework.http.codec.ServerSentEvent; |
| 21 | + |
| 22 | +/** |
| 23 | + * Convert models between chat agent and Google GenAI |
| 24 | + */ |
| 25 | +public class ModelAdapter { |
| 26 | + |
| 27 | + private ModelAdapter() { |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Convert a {@linkplain ChatAgentRequest} to a {@linkplain Content} |
| 32 | + * |
| 33 | + * @param request {@linkplain ChatAgentRequest} of chat agent |
| 34 | + * @return {@linkplain Content} |
| 35 | + */ |
| 36 | + public static Content fromRequest(ChatAgentRequest request) { |
| 37 | + var parts = new ArrayList<Part>(); |
| 38 | + for (var message : request.messages()) { |
| 39 | + if (message instanceof ThreadUserMessage userMessage) { |
| 40 | + var textBuilder = new StringBuilder(); |
| 41 | + if (userMessage.content() != null && !userMessage.content().isEmpty()) { |
| 42 | + for (ThreadUserMessagePart part : userMessage.content()) { |
| 43 | + if (part instanceof TextMessagePart textContentPart) { |
| 44 | + textBuilder.append(textContentPart.text()); |
| 45 | + } else if (part instanceof FileMessagePart fileMessagePart) { |
| 46 | + parts.add(toPart(fileMessagePart.mimeType(), fileMessagePart.data())); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + if (userMessage.attachments() != null && !userMessage.attachments().isEmpty()) { |
| 51 | + for (Attachment attachment : userMessage.attachments()) { |
| 52 | + var mimeType = attachment.contentType(); |
| 53 | + if (attachment.content() != null && !attachment.content().isEmpty()) { |
| 54 | + for (ThreadUserMessagePart part : attachment.content()) { |
| 55 | + if (part instanceof ImageMessagePart imageMessagePart) { |
| 56 | + parts.add(toPart(mimeType, imageMessagePart.image())); |
| 57 | + } else if (part instanceof FileMessagePart fileMessagePart) { |
| 58 | + parts.add(toPart(mimeType, fileMessagePart.data())); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + parts.add(Part.fromText(textBuilder.toString())); |
| 65 | + } else if (message instanceof ThreadAssistantMessage assistantMessage) { |
| 66 | + var textBuilder = new StringBuilder(); |
| 67 | + for (ThreadAssistantMessagePart part : assistantMessage.content()) { |
| 68 | + if (part instanceof TextMessagePart textContentPart) { |
| 69 | + textBuilder.append(textContentPart.text()); |
| 70 | + } |
| 71 | + } |
| 72 | + parts.add(Part.fromText(textBuilder.toString())); |
| 73 | + } |
| 74 | + } |
| 75 | + return Content.fromParts(parts.toArray(new Part[0])); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Convert a {@linkplain Content} to {@linkplain ChatAgentResponse} |
| 80 | + * |
| 81 | + * @param content {@linkplain Content} |
| 82 | + * @return {@linkplain ChatAgentResponse} of chat agent |
| 83 | + */ |
| 84 | + public static ChatAgentResponse toResponse(Content content) { |
| 85 | + var messageParts = new ArrayList<ThreadAssistantMessagePart>(); |
| 86 | + if (content.parts().isPresent()) { |
| 87 | + for (Part part : content.parts().get()) { |
| 88 | + if (part.thought().isPresent() && part.thought().get()) { |
| 89 | + part.text().ifPresent(text -> messageParts.add(new ReasoningMessagePart(text))); |
| 90 | + } else { |
| 91 | + part.text().ifPresent(text -> messageParts.add(new TextMessagePart(text))); |
| 92 | + } |
| 93 | + if (part.inlineData().isPresent()) { |
| 94 | + var blob = part.inlineData().get(); |
| 95 | + if (blob.mimeType().isPresent() && blob.data().isPresent()) { |
| 96 | + var mimeType = blob.mimeType().get(); |
| 97 | + var data = blob.data().get(); |
| 98 | + if (isImage(mimeType)) { |
| 99 | + messageParts.add(new ImageMessagePart(fromMediaData(mimeType, data))); |
| 100 | + } else { |
| 101 | + messageParts.add(new FileMessagePart(fromMediaData(mimeType, data), mimeType)); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + return new ChatAgentResponse(messageParts); |
| 108 | + } |
| 109 | + |
| 110 | + private static Part toPart(String mimeType, String data) { |
| 111 | + return Part.fromBytes(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)), |
| 112 | + mimeType); |
| 113 | + } |
| 114 | + |
| 115 | + private static boolean isImage(String mimeType) { |
| 116 | + return mimeType.startsWith("image/"); |
| 117 | + } |
| 118 | + |
| 119 | + private static String fromMediaData(String mimeType, byte[] data) { |
| 120 | + if (isImage(mimeType)) { |
| 121 | + return String.format("data:%s;base64,%s", mimeType, |
| 122 | + Base64.getEncoder().encodeToString(data)); |
| 123 | + } else { |
| 124 | + return Base64.getEncoder().encodeToString(data); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Convert a stream of {@linkplain Content} to a stream of {@linkplain ChatAgentResponse} using |
| 130 | + * Server-sent Events |
| 131 | + * |
| 132 | + * @param content Stream of {@linkplain Content} |
| 133 | + * @return Stream of {@linkplain ChatAgentResponse} |
| 134 | + */ |
| 135 | + public static Flowable<ServerSentEvent<ChatAgentResponse>> toStreamingResponse( |
| 136 | + Flowable<Content> content) { |
| 137 | + return content.map(ModelAdapter::toResponse) |
| 138 | + .filter(ChatAgentResponse::hasContent) |
| 139 | + .map(response -> ServerSentEvent.<ChatAgentResponse>builder() |
| 140 | + .data(response) |
| 141 | + .build()); |
| 142 | + } |
| 143 | +} |
0 commit comments