orpiske commented on code in PR #21704:
URL: https://github.com/apache/camel/pull/21704#discussion_r2899492376
##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?>
inputStreamToAiAgentBody(InputStream inputStream, E
}
}
+ /**
+ * Converts a {@link String} to an {@link AiAgentBody} with the
appropriate {@link Content} type.
+ * <p>
+ * This converter is useful for the text components that return Text Body.
+ * </p>
+ *
+ * @param text
+ * @param exchange
+ * @return an AiAgentBody with the appropriate Content type
+ */
+ @Converter
+ public static AiAgentBody<?> textToAiAgentBody(String text, Exchange
exchange) {
+ String userMessage = exchange.getIn().getHeader(USER_MESSAGE,
String.class);
+ String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE,
String.class);
+ Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+ TextContent content = TextContent.from(text);
+
+ AiAgentBody<TextContent> body = new AiAgentBody<>();
+ body.setUserMessage(userMessage != null ? userMessage : text);
+ body.setSystemMessage(systemMessage);
+ body.setMemoryId(memoryId);
+ body.setContent(content);
+ return body;
+ }
+
+ /**
+ * Converts a {@link String} to an {@link AiAgentBody} with the
appropriate {@link Content} type.
+ * <p>
+ * This converter is useful for the text components that return Text Body.
+ * </p>
+ *
+ * @param bufferedImage
+ * @param exchange
+ * @return an AiAgentBody with the appropriate Content type
+ */
+ @Converter
+ public static AiAgentBody<?> bufferImageToAiAgentBody(BufferedImage
bufferedImage, Exchange exchange) {
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ ImageIO.write(bufferedImage, "png", baos);
Review Comment:
Are we positively sure the image format is always PNG?
##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?>
inputStreamToAiAgentBody(InputStream inputStream, E
}
}
+ /**
+ * Converts a {@link String} to an {@link AiAgentBody} with the
appropriate {@link Content} type.
+ * <p>
+ * This converter is useful for the text components that return Text Body.
+ * </p>
+ *
+ * @param text
+ * @param exchange
+ * @return an AiAgentBody with the appropriate Content type
+ */
+ @Converter
+ public static AiAgentBody<?> textToAiAgentBody(String text, Exchange
exchange) {
+ String userMessage = exchange.getIn().getHeader(USER_MESSAGE,
String.class);
+ String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE,
String.class);
+ Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+ TextContent content = TextContent.from(text);
+
+ AiAgentBody<TextContent> body = new AiAgentBody<>();
+ body.setUserMessage(userMessage != null ? userMessage : text);
+ body.setSystemMessage(systemMessage);
+ body.setMemoryId(memoryId);
+ body.setContent(content);
+ return body;
+ }
+
+ /**
+ * Converts a {@link String} to an {@link AiAgentBody} with the
appropriate {@link Content} type.
+ * <p>
+ * This converter is useful for the text components that return Text Body.
+ * </p>
+ *
+ * @param bufferedImage
+ * @param exchange
+ * @return an AiAgentBody with the appropriate Content type
+ */
+ @Converter
+ public static AiAgentBody<?> bufferImageToAiAgentBody(BufferedImage
bufferedImage, Exchange exchange) {
+ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+ ImageIO.write(bufferedImage, "png", baos);
+ byte[] bytes = baos.toByteArray();
+ String base64 = Base64.getEncoder().encodeToString(bytes);
+ Image img =
Image.builder().base64Data(base64).mimeType("image/png").build();
Review Comment:
As with the format, we may need to find a way to make this configurable or
auto-detectable.
##########
components/camel-ai/camel-langchain4j-agent/src/main/java/org/apache/camel/component/langchain4j/agent/LangChain4jAgentConverter.java:
##########
@@ -191,10 +197,79 @@ public static AiAgentBody<?>
inputStreamToAiAgentBody(InputStream inputStream, E
}
}
+ /**
+ * Converts a {@link String} to an {@link AiAgentBody} with the
appropriate {@link Content} type.
+ * <p>
+ * This converter is useful for the text components that return Text Body.
+ * </p>
+ *
+ * @param text
+ * @param exchange
+ * @return an AiAgentBody with the appropriate Content type
+ */
+ @Converter
+ public static AiAgentBody<?> textToAiAgentBody(String text, Exchange
exchange) {
+ String userMessage = exchange.getIn().getHeader(USER_MESSAGE,
String.class);
+ String systemMessage = exchange.getIn().getHeader(SYSTEM_MESSAGE,
String.class);
+ Object memoryId = exchange.getIn().getHeader(MEMORY_ID);
+
+ TextContent content = TextContent.from(text);
+
+ AiAgentBody<TextContent> body = new AiAgentBody<>();
+ body.setUserMessage(userMessage != null ? userMessage : text);
+ body.setSystemMessage(systemMessage);
+ body.setMemoryId(memoryId);
+ body.setContent(content);
+ return body;
+ }
+
+ /**
+ * Converts a {@link String} to an {@link AiAgentBody} with the
appropriate {@link Content} type.
Review Comment:
Depending on the details below, regarding the image format, we may need to
document the limitations here and in the component docs as well.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]