Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions docs/modules/ROOT/pages/reference/extensions/ai-tool.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,59 @@ from("ai-tool:greet?description=Greet a user"
.setBody(simple("Hello, ${header.name}!"));
----

[id="extensions-ai-tool-usage-langchain4j-integration"]
=== LangChain4j integration

When both `camel-quarkus-ai-tool` and a Quarkus LangChain4j extension (`quarkus-langchain4j-ollama`, `quarkus-langchain4j-openai`, etc.) are on the classpath, a `ToolProvider` CDI bean is registered automatically at build time.
All routes registered via `ai-tool:` endpoints become available to `@RegisterAiService` AI services without explicit wiring.

You must also add `camel-langchain4j-agent` to your project dependencies. This artifact provides the `AiToolSpec` to `ToolSpecification` conversion and is not brought in transitively.

[source,xml]
----
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-langchain4j-agent</artifactId>
</dependency>
----

With both dependencies in place, a `@RegisterAiService` interface can call Camel tools directly:

[source,java]
----
@RegisterAiService
public interface WeatherAiService {
String chat(@UserMessage String question);
}
----

Any `ai-tool:` route is now callable by the LLM behind `WeatherAiService`. No `toolProvider` attribute or manual bean reference is needed.

[id="extensions-ai-tool-usage-tag-filtering-with-camelaitools"]
==== Tag filtering with `@CamelAiTools`

By default, all registered `ai-tool:` routes are visible to every AI service. When you need different AI services to see different subsets of tools, annotate the service interface with `@CamelAiTools` and specify a tag value that matches the `tags` parameter on your `ai-tool:` routes.

[source,java]
----
@RegisterAiService
@CamelAiTools("weather")
public interface WeatherAgent {
String chat(@UserMessage String question);
}

@RegisterAiService
@CamelAiTools("support")
public interface SupportAgent {
String chat(@UserMessage String question);
}
----

With this setup:

* `WeatherAgent` sees only tools tagged with `weather` (plus any untagged tools from the default pool).
* `SupportAgent` sees only tools tagged with `support` (plus any untagged tools from the default pool).
* A `@RegisterAiService` without `@CamelAiTools` sees all registered tools regardless of tags.

NOTE: The `@CamelAiTools` value must match a tag declared in the `ai-tool:` endpoint URI (e.g. `from("ai-tool:getWeather?tags=weather&...")`). If the annotation value is blank or does not match any route tag, the AI service will only see untagged tools.

5 changes: 5 additions & 0 deletions extensions-support/langchain4j/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-support-langchain4j</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ai-tool</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.deployment;

import java.util.function.BooleanSupplier;

public class AiToolPresent implements BooleanSupplier {
private static final String AI_TOOL_REGISTRY_CLASS = "org.apache.camel.component.ai.tool.AiToolRegistry";
private static final String AI_TOOL_SPEC_CONVERTER_CLASS = "org.apache.camel.component.langchain4j.agent.AiToolSpecToLangChain4j";

@Override
public boolean getAsBoolean() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
cl.loadClass(AI_TOOL_REGISTRY_CLASS);
cl.loadClass(AI_TOOL_SPEC_CONVERTER_CLASS);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@
*/
package org.apache.camel.quarkus.component.support.langchain4j.deployment;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import dev.langchain4j.guardrail.Guardrail;
import dev.langchain4j.guardrail.InputGuardrail;
import dev.langchain4j.guardrail.OutputGuardrail;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.GeneratedBeanBuildItem;
import io.quarkus.arc.deployment.GeneratedBeanGizmoAdaptor;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
Expand All @@ -34,7 +40,14 @@
import io.quarkus.deployment.builditem.SystemPropertyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.MethodCreator;
import io.quarkus.gizmo.MethodDescriptor;
import io.quarkus.gizmo.ResultHandle;
import jakarta.inject.Singleton;
import org.apache.camel.quarkus.component.support.langchain4j.AiToolSpecConverter;
import org.apache.camel.quarkus.component.support.langchain4j.CamelAiToolProvider;
import org.apache.camel.quarkus.component.support.langchain4j.CamelAiToolsInterceptor;
import org.apache.camel.quarkus.component.support.langchain4j.QuarkusLangchain4jRecorder;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
Expand All @@ -53,6 +66,8 @@ class SupportQuarkusLangchain4jProcessor {

public static final DotName REGISTER_AI_SERVICES_DOTNAME = DotName
.createSimple("io.quarkiverse.langchain4j.RegisterAiService");
private static final DotName CAMEL_AI_TOOLS_DOTNAME = DotName
.createSimple("org.apache.camel.quarkus.component.support.langchain4j.CamelAiTools");

private static final Logger LOG = Logger.getLogger(SupportQuarkusLangchain4jProcessor.class);

Expand Down Expand Up @@ -127,10 +142,104 @@ void registerQuarkusLangchain4jNativeSupport(
.build());
}

@BuildStep(onlyIf = AiToolPresent.class)
AdditionalBeanBuildItem registerCamelAiToolProvider() {
LOG.info("Camel AI Tool detected - registering CamelAiToolProvider as CDI bean for ToolProvider auto-discovery");
return AdditionalBeanBuildItem.unremovableOf(CamelAiToolProvider.class);
}

@BuildStep(onlyIf = AiToolPresent.class)
void generateAiToolSpecConverter(BuildProducer<GeneratedBeanBuildItem> generatedBeans,
BuildProducer<UnremovableBeanBuildItem> unremovableBeans) {

String generatedClassName = "org.apache.camel.quarkus.component.support.langchain4j.AiToolSpecConverterImpl";

try (ClassCreator cc = ClassCreator.builder()
.classOutput(new GeneratedBeanGizmoAdaptor(generatedBeans))
.className(generatedClassName)
.interfaces(AiToolSpecConverter.class)
.build()) {

cc.addAnnotation(Singleton.class);

String toolSpecClass = "dev.langchain4j.agent.tool.ToolSpecification";
String aiToolSpecClass = "org.apache.camel.component.ai.tool.AiToolSpec";

try (MethodCreator mc = cc.getMethodCreator("toToolSpecification",
toolSpecClass, aiToolSpecClass)) {

ResultHandle spec = mc.getMethodParam(0);
ResultHandle result = mc.invokeStaticMethod(
MethodDescriptor.ofMethod(
"org.apache.camel.component.langchain4j.agent.AiToolSpecToLangChain4j",
"toToolSpecification",
toolSpecClass,
aiToolSpecClass),
spec);
mc.returnValue(result);
}
}

unremovableBeans.produce(beanClassNames(generatedClassName));
}

@BuildStep(onlyIf = AiToolPresent.class)
@Record(ExecutionTime.STATIC_INIT)
void configureCamelAiToolTags(
CombinedIndexBuildItem combinedIndex,
BuildProducer<AdditionalBeanBuildItem> additionalBeans,
QuarkusLangchain4jRecorder recorder) {

IndexView index = combinedIndex.getIndex();
Map<String, String> tagMap = new HashMap<>();
for (AnnotationInstance annotation : index.getAnnotations(CAMEL_AI_TOOLS_DOTNAME)) {
if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) {
String className = annotation.target().asClass().name().toString();
if (annotation.value() == null) {
LOG.warnf("@CamelAiTools on %s has no value — skipping", className);
continue;
}
String tagValue = annotation.value().asString();
if (tagValue.isBlank()) {
LOG.warnf("@CamelAiTools on %s has blank value — skipping", className);
continue;
}
tagMap.put(className, tagValue);
LOG.infof("Discovered @CamelAiTools(\"%s\") on %s", tagValue, className);
}
}

if (tagMap.isEmpty()) {
return;
}

recorder.setCamelAiToolTagMap(tagMap);

additionalBeans.produce(AdditionalBeanBuildItem.builder()
.addBeanClasses(CamelAiToolsInterceptor.class)
.setUnremovable()
.build());
}

@BuildStep
void markAiServicesAsUnremovable(
void validateAndRegisterAiServices(
CombinedIndexBuildItem indexBuildItem,
BuildProducer<UnremovableBeanBuildItem> unremovableBeans) {

if (!new AiToolPresent().getAsBoolean()) {
Collection<AnnotationInstance> aiToolsAnnotations = indexBuildItem.getIndex()
.getAnnotations(CAMEL_AI_TOOLS_DOTNAME);
if (!aiToolsAnnotations.isEmpty()) {
LOG.warnf("@CamelAiTools annotations found but camel-langchain4j-agent is not on the classpath. "
+ "Add camel-langchain4j-agent dependency to enable the Camel AI tool bridge. "
+ "Affected classes: %s",
aiToolsAnnotations.stream()
.filter(a -> a.target().kind() == AnnotationTarget.Kind.CLASS)
.map(a -> a.target().asClass().name().toString())
.collect(Collectors.joining(", ")));
}
}

LOG.debug("Discovering classes annotated with @RegisterAiService to mark implementation beans as unremovable");

for (AnnotationInstance instance : indexBuildItem.getIndex().getAnnotations(REGISTER_AI_SERVICES_DOTNAME)) {
Expand Down
10 changes: 10 additions & 0 deletions extensions-support/langchain4j/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-embeddings</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ai-tool</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-langchain4j-agent</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 dev.langchain4j.agent.tool.ToolSpecification;
import org.apache.camel.component.ai.tool.AiToolSpec;

/**
* Converts a Camel {@link AiToolSpec} to a langchain4j {@link ToolSpecification}.
* The implementation is generated at build time via Gizmo to avoid a compile-time
* dependency on {@code camel-langchain4j-agent}.
*/
public interface AiToolSpecConverter {
ToolSpecification toToolSpecification(AiToolSpec spec);
}
Loading
Loading