JiriOndrusek commented on code in PR #8970:
URL: https://github.com/apache/camel-quarkus/pull/8970#discussion_r3736350188
##########
extensions-support/langchain4j/deployment/src/main/java/org/apache/camel/quarkus/component/support/langchain4j/deployment/SupportQuarkusLangchain4jProcessor.java:
##########
@@ -256,4 +265,94 @@ void validateAndRegisterAiServices(
}
}
}
+
+ /**
+ * Produces {@link RetrievalAugmentor} CDI beans that bridge Camel
ingestion routes with
+ * {@code @RegisterAiService} RAG.
+ *
+ * <p>
+ * Two modes:
+ * <ul>
+ * <li><b>Explicit config</b> — each entry under {@code
quarkus.camel.langchain4j.rag.augmentors.<name>}
+ * produces a {@code @Named("<name>")} RetrievalAugmentor backed by the
configured store.
+ * If only one entry exists, it is also marked as {@code defaultBean()}
for auto-discovery.</li>
+ * <li><b>Auto-detection</b> — when no config entries exist, at least one
EmbeddingStore and one
+ * EmbeddingModel are present, and no RetrievalAugmentor exists yet, a
default one is produced
+ * backed by the {@code @Default} CDI bean.</li>
+ * </ul>
+ */
+ @BuildStep
+ @Record(ExecutionTime.RUNTIME_INIT)
+ void registerDefaultRetrievalAugmentor(
+ BeanDiscoveryFinishedBuildItem beanDiscovery,
+ RagBridgeConfig ragBridgeConfig,
+ QuarkusLangchain4jRecorder recorder,
+ BuildProducer<SyntheticBeanBuildItem> syntheticBeans) {
+
+ DotName embeddingStoreDN =
DotName.createSimple(EmbeddingStore.class.getName());
+ DotName embeddingModelDN =
DotName.createSimple(EmbeddingModel.class.getName());
+ DotName retrievalAugmentorDN =
DotName.createSimple(RetrievalAugmentor.class.getName());
+
+ int embeddingStoreCount = 0;
+ int embeddingModelCount = 0;
+ boolean hasRetrievalAugmentor = false;
+
+ for (BeanInfo bean :
beanDiscovery.beanStream().collect(Collectors.toList())) {
+ for (org.jboss.jandex.Type type : bean.getTypes()) {
+ DotName typeName = type.name();
+ if (typeName.equals(embeddingStoreDN)) {
+ embeddingStoreCount++;
+ } else if (typeName.equals(embeddingModelDN)) {
+ embeddingModelCount++;
+ } else if (typeName.equals(retrievalAugmentorDN)) {
+ hasRetrievalAugmentor = true;
+ }
+ }
+ }
+
+ Map<String, AugmentorConfig> augmentors = ragBridgeConfig.augmentors();
+
+ if (!augmentors.isEmpty()) {
+ for (Map.Entry<String, AugmentorConfig> entry :
augmentors.entrySet()) {
+ String name = entry.getKey();
+ AugmentorConfig cfg = entry.getValue();
+
+ LOG.infof("Registering named RetrievalAugmentor '%s' backed by
EmbeddingStore '%s'",
+ name, cfg.embeddingStoreName());
Review Comment:
Done.
##########
extensions-support/langchain4j/deployment/src/main/java/org/apache/camel/quarkus/component/support/langchain4j/deployment/SupportQuarkusLangchain4jProcessor.java:
##########
@@ -256,4 +265,94 @@ void validateAndRegisterAiServices(
}
}
}
+
+ /**
+ * Produces {@link RetrievalAugmentor} CDI beans that bridge Camel
ingestion routes with
+ * {@code @RegisterAiService} RAG.
+ *
+ * <p>
+ * Two modes:
+ * <ul>
+ * <li><b>Explicit config</b> — each entry under {@code
quarkus.camel.langchain4j.rag.augmentors.<name>}
+ * produces a {@code @Named("<name>")} RetrievalAugmentor backed by the
configured store.
+ * If only one entry exists, it is also marked as {@code defaultBean()}
for auto-discovery.</li>
+ * <li><b>Auto-detection</b> — when no config entries exist, at least one
EmbeddingStore and one
+ * EmbeddingModel are present, and no RetrievalAugmentor exists yet, a
default one is produced
+ * backed by the {@code @Default} CDI bean.</li>
+ * </ul>
+ */
+ @BuildStep
+ @Record(ExecutionTime.RUNTIME_INIT)
+ void registerDefaultRetrievalAugmentor(
+ BeanDiscoveryFinishedBuildItem beanDiscovery,
+ RagBridgeConfig ragBridgeConfig,
+ QuarkusLangchain4jRecorder recorder,
+ BuildProducer<SyntheticBeanBuildItem> syntheticBeans) {
+
+ DotName embeddingStoreDN =
DotName.createSimple(EmbeddingStore.class.getName());
+ DotName embeddingModelDN =
DotName.createSimple(EmbeddingModel.class.getName());
+ DotName retrievalAugmentorDN =
DotName.createSimple(RetrievalAugmentor.class.getName());
+
+ int embeddingStoreCount = 0;
+ int embeddingModelCount = 0;
+ boolean hasRetrievalAugmentor = false;
+
+ for (BeanInfo bean :
beanDiscovery.beanStream().collect(Collectors.toList())) {
+ for (org.jboss.jandex.Type type : bean.getTypes()) {
+ DotName typeName = type.name();
+ if (typeName.equals(embeddingStoreDN)) {
+ embeddingStoreCount++;
+ } else if (typeName.equals(embeddingModelDN)) {
+ embeddingModelCount++;
+ } else if (typeName.equals(retrievalAugmentorDN)) {
+ hasRetrievalAugmentor = true;
+ }
+ }
+ }
+
+ Map<String, AugmentorConfig> augmentors = ragBridgeConfig.augmentors();
+
+ if (!augmentors.isEmpty()) {
+ for (Map.Entry<String, AugmentorConfig> entry :
augmentors.entrySet()) {
+ String name = entry.getKey();
+ AugmentorConfig cfg = entry.getValue();
+
+ LOG.infof("Registering named RetrievalAugmentor '%s' backed by
EmbeddingStore '%s'",
+ name, cfg.embeddingStoreName());
+
+ SyntheticBeanBuildItem.ExtendedBeanConfigurator configurator =
SyntheticBeanBuildItem
+ .configure(RetrievalAugmentor.class)
+ .scope(ApplicationScoped.class)
+
.addQualifier().annotation(Named.class).addValue("value", name).done()
+ .setRuntimeInit()
+
.supplier(recorder.createDefaultRetrievalAugmentorSupplier(
+ cfg.embeddingStoreName(),
cfg.embeddingModelName().orElse(null)));
+
+ // Single augmentor configured and no user/Easy-RAG augmentor
present:
+ // mark as defaultBean() so @RegisterAiService discovers it
without a qualifier
+ if (augmentors.size() == 1 && !hasRetrievalAugmentor) {
+ configurator.defaultBean();
+ }
+
+ syntheticBeans.produce(configurator.done());
+ }
+ return;
+ }
+
+ if (hasRetrievalAugmentor) {
+ return;
+ }
+
+ if (embeddingStoreCount >= 1 && embeddingModelCount >= 1) {
+ LOG.info("EmbeddingStore and EmbeddingModel CDI beans detected"
+ + " - registering default RetrievalAugmentor backed by
@Default store");
Review Comment:
Done.
##########
extensions-support/langchain4j/runtime/src/main/java/org/apache/camel/quarkus/component/support/langchain4j/DefaultRetrievalAugmentorSupplier.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.quarkus.component.support.langchain4j;
+
+import java.util.function.Supplier;
+
+import dev.langchain4j.data.segment.TextSegment;
+import dev.langchain4j.model.embedding.EmbeddingModel;
+import dev.langchain4j.rag.DefaultRetrievalAugmentor;
+import dev.langchain4j.rag.RetrievalAugmentor;
+import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
+import dev.langchain4j.store.embedding.EmbeddingStore;
+import io.quarkiverse.langchain4j.EmbeddingStoreName;
+import io.quarkus.arc.Arc;
+import jakarta.enterprise.inject.literal.NamedLiteral;
+import jakarta.enterprise.util.TypeLiteral;
+import org.jboss.logging.Logger;
+
+/**
+ * Lazily creates a {@link RetrievalAugmentor} backed by the CDI {@link
EmbeddingStore}
+ * and {@link EmbeddingModel} beans. Used as a supplier for the synthetic bean
produced
+ * by the deployment processor.
+ */
+public class DefaultRetrievalAugmentorSupplier implements
Supplier<RetrievalAugmentor> {
+
+ private static final Logger LOG =
Logger.getLogger(DefaultRetrievalAugmentorSupplier.class);
+
+ private static final TypeLiteral<EmbeddingStore<TextSegment>>
EMBEDDING_STORE_TYPE = new TypeLiteral<>() {
+ };
+
+ private final String embeddingStoreName;
+ private final String embeddingModelName;
+
+ public DefaultRetrievalAugmentorSupplier() {
+ this(null, null);
+ }
+
+ public DefaultRetrievalAugmentorSupplier(String embeddingStoreName, String
embeddingModelName) {
+ this.embeddingStoreName = embeddingStoreName;
+ this.embeddingModelName = embeddingModelName;
+ }
+
+ @Override
+ public RetrievalAugmentor get() {
+ EmbeddingStore<TextSegment> store;
+ if (embeddingStoreName != null) {
+ store = Arc.container()
+ .instance(EMBEDDING_STORE_TYPE,
EmbeddingStoreName.Literal.of(embeddingStoreName)).get();
+ if (store == null) {
+ throw new IllegalStateException(
+ "No EmbeddingStore CDI bean found with
@EmbeddingStoreName(\"" + embeddingStoreName + "\")");
+ }
+ } else {
+ store = Arc.container().instance(EMBEDDING_STORE_TYPE).get();
+ if (store == null) {
+ throw new IllegalStateException("No default EmbeddingStore CDI
bean found");
+ }
+ }
+
+ EmbeddingModel model;
+ if (embeddingModelName != null) {
+ model = Arc.container()
+ .instance(EmbeddingModel.class,
NamedLiteral.of(embeddingModelName)).get();
+ if (model == null) {
+ throw new IllegalStateException(
+ "No EmbeddingModel CDI bean found with @Named(\"" +
embeddingModelName + "\")");
+ }
+ } else {
+ model = Arc.container().instance(EmbeddingModel.class).get();
+ if (model == null) {
+ throw new IllegalStateException("No default EmbeddingModel CDI
bean found");
+ }
+ }
+
+ LOG.infof("Creating default RetrievalAugmentor bridging Camel
ingestion with @RegisterAiService RAG"
+ + " (store=%s, model=%s)", embeddingStoreName != null ?
embeddingStoreName : "@Default",
+ embeddingModelName != null ? embeddingModelName : "@Default");
Review Comment:
Done.
--
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]