Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes for resolution of tool providers, improve tests #1192

Merged
merged 1 commit into from
Jan 6, 2025
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
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
Loading