davsclaus commented on code in PR #29108:
URL: https://github.com/apache/camel/pull/29108#discussion_r4457573240
##########
components/camel-spring-parent/camel-spring-ai/camel-spring-ai-embeddings/src/main/java/org/apache/camel/component/springai/embeddings/SpringAiEmbeddingsProducer.java:
##########
@@ -109,4 +112,44 @@ public void process(Exchange exchange) throws Exception {
message.setHeader(SpringAiEmbeddingsHeaders.EMBEDDING_METADATA,
response.getMetadata());
}
}
+
+ /**
+ * The text to embed of a body, or of an element of a List body: a Spring
AI Document as its text, and text, bytes,
+ * a stream, a Reader or a file read as text. Any other value, such as a
Map or a POJO, fails as an
+ * InvalidPayloadException instead of being embedded as its toString().
+ *
+ * @param index the index of the element in a List body, or -1 for the
body itself
+ */
+ private static String toText(Exchange exchange, Object value, int index)
+ throws InvalidPayloadException, NoTypeConversionAvailableException
{
+ String what = index < 0 ? "" : "the element at index " + index + " of
the List body is " + typeOf(value) + "; ";
+ if (value instanceof Document document) {
+ if (document.getText() == null) {
+ throw new InvalidPayloadException(
+ exchange, String.class, exchange.getMessage(),
+ what + "spring-ai-embeddings embeds the text of a
Document, but the Document has no text");
+ }
+ return document.getText();
+ }
+ if (!isReadableAsText(value)) {
+ throw new InvalidPayloadException(
+ exchange, String.class, exchange.getMessage(),
+ what + "spring-ai-embeddings embeds text, bytes, a stream,
a Reader, a file or a Spring AI Document;"
+ + " convert
the body to text before the endpoint, such"
+ + " as with
convertBodyTo(String) or a marshal to JSON");
+ }
+ return
exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class,
exchange, value);
+ }
+
+ /** A value Camel converts to its text, rather than to the toString() of
an object. */
+ private static boolean isReadableAsText(Object value) {
Review Comment:
Thanks for the table. The list stays as in camel-langchain4j-embeddings for
this PR. A follow-up JIRA covers both components (Claus files it), so the two
keep the same rule. In 426d88326c56 the upgrade guide entry now names a
`char[]`, a Netty `ByteBuf` (netty-http with `disableStreamCache=true`), a
custom type with a converter to `String`, a `List` with such an element, and a
`Set` or an array of texts, and says to add `convertBodyTo(String.class)`
before the endpoint. A new test sends a `char[]` (fails) and then the same body
through `convertBodyTo(String.class)` (embeds `hello`).
_Claude Code on behalf of Claus Ibsen (davsclaus)_
##########
components/camel-spring-parent/camel-spring-ai/camel-spring-ai-embeddings/src/main/java/org/apache/camel/component/springai/embeddings/SpringAiEmbeddingsEndpoint.java:
##########
@@ -37,10 +37,11 @@
syntax = "spring-ai-embeddings:embeddingId", producerOnly = true,
category = {
Category.AI
}, headersClass = SpringAiEmbeddingsHeaders.class)
-@BodyTypes(producerIn = @BodyType(value = Carrier.ANY,
- description = "The text to embed, or a List
of texts for a batch. Another body, and"
- + " each element of a List, is
converted to text (a Map or a POJO as its"
- + " toString()); no body, or a
null element in a List, fails"),
+@BodyTypes(producerIn = @BodyType(value = { Carrier.TEXT, Carrier.BYTES,
Carrier.STREAM, Carrier.FILE, Carrier.LIST },
Review Comment:
Done in 426d88326c56: the producerIn description now says "a java.util.List
of them for a batch (a Set or an array of them fails)". Catalog JSON
regenerated; no camel-yaml-dsl-validator test refers to this component. New
tests check that a `Set` and a `String[]` body fail. `Carrier.POJO` for the
`Document` is left out, as in camel-langchain4j-embeddings for `TextSegment`:
the description names the Document, and declaring POJO would suggest that any
POJO is fine.
_Claude Code on behalf of Claus Ibsen (davsclaus)_
##########
components/camel-spring-parent/camel-spring-ai/camel-spring-ai-embeddings/src/main/java/org/apache/camel/component/springai/embeddings/SpringAiEmbeddingsProducer.java:
##########
@@ -109,4 +112,44 @@ public void process(Exchange exchange) throws Exception {
message.setHeader(SpringAiEmbeddingsHeaders.EMBEDDING_METADATA,
response.getMetadata());
}
}
+
+ /**
+ * The text to embed of a body, or of an element of a List body: a Spring
AI Document as its text, and text, bytes,
+ * a stream, a Reader or a file read as text. Any other value, such as a
Map or a POJO, fails as an
+ * InvalidPayloadException instead of being embedded as its toString().
+ *
+ * @param index the index of the element in a List body, or -1 for the
body itself
+ */
+ private static String toText(Exchange exchange, Object value, int index)
+ throws InvalidPayloadException, NoTypeConversionAvailableException
{
+ String what = index < 0 ? "" : "the element at index " + index + " of
the List body is " + typeOf(value) + "; ";
Review Comment:
Done in 426d88326c56: the index and type text is now built by a small helper
that is only called in the two throw branches, as in
camel-langchain4j-embeddings.
_Claude Code on behalf of Claus Ibsen (davsclaus)_
--
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]