This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new a6c4f3cbd020 CAMEL-28057: camel-langchain4j-embeddingstore: validate
before embedding (#31268)
a6c4f3cbd020 is described below
commit a6c4f3cbd020fbcd2cb6829cc3db6c1d29b24fb5
Author: Andrea Cosentino <[email protected]>
AuthorDate: Thu Dec 3 20:23:38 2026 +0100
CAMEL-28057: camel-langchain4j-embeddingstore: validate before embedding
(#31268)
Co-authored-by: Codex <[email protected]>
---
.../LangChain4jEmbeddingStoreProducer.java | 39 +++++----
.../LangChain4jEmbeddingStoreValidationTest.java | 95 ++++++++++++++++++++++
2 files changed, 116 insertions(+), 18 deletions(-)
diff --git
a/components/camel-ai/camel-langchain4j-embeddingstore/src/main/java/org/apache/camel/component/langchain4j/embeddingstore/LangChain4jEmbeddingStoreProducer.java
b/components/camel-ai/camel-langchain4j-embeddingstore/src/main/java/org/apache/camel/component/langchain4j/embeddingstore/LangChain4jEmbeddingStoreProducer.java
index 74da58a30091..c7ab7906f475 100644
---
a/components/camel-ai/camel-langchain4j-embeddingstore/src/main/java/org/apache/camel/component/langchain4j/embeddingstore/LangChain4jEmbeddingStoreProducer.java
+++
b/components/camel-ai/camel-langchain4j-embeddingstore/src/main/java/org/apache/camel/component/langchain4j/embeddingstore/LangChain4jEmbeddingStoreProducer.java
@@ -32,7 +32,6 @@ import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingMatch;
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
-import
dev.langchain4j.store.embedding.EmbeddingSearchRequest.EmbeddingSearchRequestBuilder;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.filter.Filter;
import org.apache.camel.CamelContext;
@@ -179,6 +178,8 @@ public class LangChain4jEmbeddingStoreProducer extends
DefaultProducer {
&& in.getHeader(LangChain4jEmbeddingsHeaders.TEXT_SEGMENT) ==
null
&& in.getBody() instanceof Collection<?> collection) {
List<TextSegment> textSegments = toTextSegments(collection);
+ List<String> callerIds =
in.getHeader(LangChain4jEmbeddingStoreHeaders.EMBEDDING_IDS, List.class);
+ validateCallerIds(callerIds, textSegments.size());
List<Embedding> embedded =
config.getEmbeddingModel().embedAll(textSegments).content();
addBatch(in, store, embedded, textSegments);
return;
@@ -247,11 +248,7 @@ public class LangChain4jEmbeddingStoreProducer extends
DefaultProducer {
Message in, EmbeddingStore<TextSegment> store, List<Embedding>
embeddings, List<TextSegment> textSegments) {
List<String> callerIds =
in.getHeader(LangChain4jEmbeddingStoreHeaders.EMBEDDING_IDS, List.class);
- // Validate sizes match when caller IDs are provided
- if (callerIds != null && callerIds.size() != embeddings.size()) {
- throw new IllegalArgumentException(
- "EMBEDDING_IDS size (" + callerIds.size() + ") must match
EMBEDDINGS size (" + embeddings.size() + ")");
- }
+ validateCallerIds(callerIds, embeddings.size());
if (textSegments != null && textSegments.size() != embeddings.size()) {
throw new IllegalArgumentException(
"TEXT_SEGMENTS size (" + textSegments.size() + ") must
match EMBEDDINGS size (" + embeddings.size() + ")");
@@ -276,6 +273,13 @@ public class LangChain4jEmbeddingStoreProducer extends
DefaultProducer {
in.setBody(ids);
}
+ private static void validateCallerIds(List<String> callerIds, int
expectedSize) {
+ if (callerIds != null && callerIds.size() != expectedSize) {
+ throw new IllegalArgumentException(
+ "EMBEDDING_IDS size (" + callerIds.size() + ") must match
EMBEDDINGS size (" + expectedSize + ")");
+ }
+ }
+
/**
* Removes embeddings from the store. Supports multiple removal strategies:
*
@@ -362,33 +366,32 @@ public class LangChain4jEmbeddingStoreProducer extends
DefaultProducer {
final Message in = exchange.getMessage();
LangChain4jEmbeddingStoreConfiguration config =
getEndpoint().getConfiguration();
- Embedding embedding = resolveEmbedding(exchange).embedding();
-
// Get maxResults from header, fallback to endpoint config
Integer maxResults =
in.getHeader(LangChain4jEmbeddingStoreHeaders.MAX_RESULTS, Integer.class);
if (maxResults == null) {
maxResults = config.getMaxResults();
}
- EmbeddingSearchRequestBuilder esrb = EmbeddingSearchRequest.builder()
- .queryEmbedding(embedding)
- .maxResults(maxResults);
+ if (maxResults != null && maxResults <= 0) {
+ throw new IllegalArgumentException("maxResults must be greater
than zero");
+ }
// Get minScore from header, fallback to endpoint config
Double minScore =
in.getHeader(LangChain4jEmbeddingStoreHeaders.MIN_SCORE, Double.class);
if (minScore == null) {
minScore = config.getMinScore();
}
- if (minScore != null) {
- esrb = esrb.minScore(minScore);
+ if (minScore != null && !(minScore >= 0 && minScore <= 1)) {
+ throw new IllegalArgumentException("minScore must be between 0 and
1");
}
Filter filter = in.getHeader(LangChain4jEmbeddingStoreHeaders.FILTER,
Filter.class);
- if (filter != null) {
- esrb = esrb.filter(filter);
- }
-
- EmbeddingSearchRequest embeddingSearchRequest = esrb.build();
+ EmbeddingSearchRequest embeddingSearchRequest =
EmbeddingSearchRequest.builder()
+ .queryEmbedding(resolveEmbedding(exchange).embedding())
+ .maxResults(maxResults)
+ .minScore(minScore)
+ .filter(filter)
+ .build();
List<EmbeddingMatch<TextSegment>> matches
=
config.getEmbeddingStore().search(embeddingSearchRequest).matches();
diff --git
a/components/camel-ai/camel-langchain4j-embeddingstore/src/test/java/org/apache/camel/component/langchain4j/embeddingstore/LangChain4jEmbeddingStoreValidationTest.java
b/components/camel-ai/camel-langchain4j-embeddingstore/src/test/java/org/apache/camel/component/langchain4j/embeddingstore/LangChain4jEmbeddingStoreValidationTest.java
new file mode 100644
index 000000000000..76908a40e25c
--- /dev/null
+++
b/components/camel-ai/camel-langchain4j-embeddingstore/src/test/java/org/apache/camel/component/langchain4j/embeddingstore/LangChain4jEmbeddingStoreValidationTest.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://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.apache.camel.component.langchain4j.embeddingstore;
+
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import dev.langchain4j.data.embedding.Embedding;
+import dev.langchain4j.data.segment.TextSegment;
+import dev.langchain4j.model.embedding.EmbeddingModel;
+import dev.langchain4j.model.output.Response;
+import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class LangChain4jEmbeddingStoreValidationTest extends CamelTestSupport {
+
+ private final AtomicInteger modelCalls = new AtomicInteger();
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ LangChain4jEmbeddingStoreComponent component
+ = context.getComponent(LangChain4jEmbeddingStore.SCHEME,
LangChain4jEmbeddingStoreComponent.class);
+ component.getConfiguration().setEmbeddingStore(new
InMemoryEmbeddingStore<>());
+ component.getConfiguration().setEmbeddingModel(new EmbeddingModel() {
+ @Override
+ public Response<Embedding> embed(TextSegment segment) {
+ modelCalls.incrementAndGet();
+ return Response.from(Embedding.from(new float[] { 1 }));
+ }
+
+ @Override
+ public Response<List<Embedding>> embedAll(List<TextSegment>
segments) {
+ modelCalls.incrementAndGet();
+ return Response.from(segments.stream().map(segment ->
Embedding.from(new float[] { 1 })).toList());
+ }
+ });
+ return context;
+ }
+
+ @Test
+ void rejectsMismatchedIdsBeforeEmbedding() {
+ Exchange result =
fluentTemplate.to("langchain4j-embeddingstore:test?action=ADD")
+ .withBody(List.of("one", "two"))
+ .withHeader(LangChain4jEmbeddingStoreHeaders.EMBEDDING_IDS,
List.of("one-id"))
+ .send();
+
+
assertThat(result.getException()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("EMBEDDING_IDS");
+ assertThat(modelCalls).hasValue(0);
+ }
+
+ @Test
+ void rejectsInvalidMaxResultsBeforeEmbedding() {
+ Exchange result =
fluentTemplate.to("langchain4j-embeddingstore:test?action=SEARCH")
+ .withBody("query")
+ .withHeader(LangChain4jEmbeddingStoreHeaders.MAX_RESULTS, 0)
+ .send();
+
+
assertThat(result.getException()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("maxResults");
+ assertThat(modelCalls).hasValue(0);
+ }
+
+ @ParameterizedTest
+ @ValueSource(doubles = { -0.1, 1.1, Double.NaN })
+ void rejectsInvalidMinScoreBeforeEmbedding(double minScore) {
+ Exchange result =
fluentTemplate.to("langchain4j-embeddingstore:test?action=SEARCH")
+ .withBody("query")
+ .withHeader(LangChain4jEmbeddingStoreHeaders.MIN_SCORE,
minScore)
+ .send();
+
+
assertThat(result.getException()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("minScore");
+ assertThat(modelCalls).hasValue(0);
+ }
+}