Skip to content

Commit

Permalink
Some fixes for resolution of tool providers, improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartisk committed Jan 6, 2025
1 parent c1ac62f commit a6c0b14
Show file tree
Hide file tree
Showing 14 changed files with 419 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ public void handleDeclarativeServices(AiServicesRecorder recorder,
boolean needsAuditServiceBean = false;
boolean needsModerationModelBean = false;
boolean needsImageModelBean = false;
boolean needsToolProviderBean = false;
Set<DotName> allToolNames = new HashSet<>();
Set<DotName> allToolProviders = new HashSet<>();

Expand Down Expand Up @@ -761,7 +762,8 @@ public void handleDeclarativeServices(AiServicesRecorder recorder,
.equals(toolProviderSupplierClassName)) {
configurator.addInjectionPoint(ParameterizedType.create(DotNames.CDI_INSTANCE,
new Type[] { ClassType.create(LangChain4jDotNames.TOOL_PROVIDER) }, null));
} else if (!RegisterAiService.BeanIfExistsToolProviderSupplier.class.getName()
needsToolProviderBean = true;
} else if (!RegisterAiService.NoToolProviderSupplier.class.getName()
.equals(toolProviderSupplierClassName) && toolProviderSupplierClassName != null) {
DotName toolProvider = DotName.createSimple(toolProviderSupplierClassName);
configurator.addInjectionPoint(ClassType.create(toolProvider));
Expand Down Expand Up @@ -800,6 +802,9 @@ public void handleDeclarativeServices(AiServicesRecorder recorder,
if (needsImageModelBean) {
unremovableProducer.produce(UnremovableBeanBuildItem.beanTypes(LangChain4jDotNames.IMAGE_MODEL));
}
if (needsToolProviderBean) {
unremovableProducer.produce(UnremovableBeanBuildItem.beanTypes(LangChain4jDotNames.TOOL_PROVIDER));
}
if (!allToolProviders.isEmpty()) {
unremovableProducer.produce(UnremovableBeanBuildItem.beanTypes(allToolProviders));
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.quarkiverse.langchain4j.test.toolresolution;

import static org.junit.jupiter.api.Assertions.assertEquals;

import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.UserMessage;
import io.quarkiverse.langchain4j.RegisterAiService;
import io.quarkus.test.QuarkusUnitTest;

/**
* If the AI service does not explicitly specify tools nor a tool provider
* and there is a bean that implements ToolProvider, that bean should be used.
*/
public class AutomaticToolProviderTest {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(TestAiSupplier.class,
TestAiModel.class,
ServiceWithDefaultToolProviderConfig.class,
MyCustomToolProvider.class));

@RegisterAiService(chatLanguageModelSupplier = TestAiSupplier.class)
interface ServiceWithDefaultToolProviderConfig {
String chat(@UserMessage String msg, @MemoryId Object id);
}

@Inject
ServiceWithDefaultToolProviderConfig service;

@Test
@ActivateRequestContext
void testCall() {
String answer = service.chat("hello", 1);
assertEquals("TOOL1", answer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.quarkiverse.langchain4j.test.toolresolution;

import static org.junit.jupiter.api.Assertions.assertEquals;

import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.UserMessage;
import io.quarkiverse.langchain4j.RegisterAiService;
import io.quarkus.test.QuarkusUnitTest;

/**
* If an AI service specifies an explicit tool provider (and no specific tools),
* that tool provider should be used.
*/
public class ExplicitToolProviderSupplierTest {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(TestAiSupplier.class,
TestAiModel.class,
ServiceWithExplicitToolProviderSupplier.class,
MyCustomToolProviderSupplier.class,
MyCustomToolProvider.class));

@RegisterAiService(toolProviderSupplier = MyCustomToolProviderSupplier.class, chatLanguageModelSupplier = TestAiSupplier.class)
interface ServiceWithExplicitToolProviderSupplier {

String chat(@UserMessage String msg, @MemoryId Object id);

}

@Inject
ServiceWithExplicitToolProviderSupplier service;

@Test
@ActivateRequestContext
void testCall() {
String answer = service.chat("hello", 1);
assertEquals("TOOL1", answer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.quarkiverse.langchain4j.test.toolresolution;

import static org.junit.jupiter.api.Assertions.assertEquals;

import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.UserMessage;
import io.quarkiverse.langchain4j.RegisterAiService;
import io.quarkus.test.QuarkusUnitTest;

/**
* If the AI service explicitly specifies tools, and there is a bean that implements ToolProvider,
* but the service also declares a NoToolProviderSupplier, the explicit tools should be used.
*/
public class ExplicitToolsAndNoBeanToolProviderTest {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(TestAiSupplier.class,
TestAiModel.class,
ServiceWithExplicitToolsAndNoToolProviderSupplier.class,
MyCustomToolProvider.class,
ToolsClass.class));

@RegisterAiService(chatLanguageModelSupplier = TestAiSupplier.class, tools = ToolsClass.class, toolProviderSupplier = RegisterAiService.NoToolProviderSupplier.class)
interface ServiceWithExplicitToolsAndNoToolProviderSupplier {
String chat(@UserMessage String msg, @MemoryId Object id);
}

@Inject
ServiceWithExplicitToolsAndNoToolProviderSupplier service;

@Test
@ActivateRequestContext
void testCall() {
String answer = service.chat("hello", 1);
assertEquals("\"EXPLICIT TOOL\"", answer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.quarkiverse.langchain4j.test.toolresolution;

import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;

import org.assertj.core.api.Assertions;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.UserMessage;
import io.quarkiverse.langchain4j.RegisterAiService;
import io.quarkus.test.QuarkusUnitTest;

/**
*
*/
public class ExplicitToolsAndProviderSupplierTest {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(TestAiSupplier.class,
TestAiModel.class,
ServiceWithToolClash.class,
MyCustomToolProviderSupplier.class,
MyCustomToolProvider.class,
ToolsClass.class));

@RegisterAiService(toolProviderSupplier = MyCustomToolProviderSupplier.class, tools = ToolsClass.class, chatLanguageModelSupplier = TestAiSupplier.class)
interface ServiceWithToolClash {

String chat(@UserMessage String msg, @MemoryId Object id);

}

@Inject
ServiceWithToolClash service;

@Test
@ActivateRequestContext
void testCall() {
try {
String answer = service.chat("hello", 1);
Assertions.fail("Exception expected");
} catch (Exception e) {
Assertions.assertThat(e.getMessage()).contains(" Cannot use a tool provider when explicit tools are provided");
}
}

}
Loading

0 comments on commit a6c0b14

Please sign in to comment.