Skip to content

Commit 9747dc5

Browse files
committed
Avoid using cache and tools in the same @RegisterAiService
1 parent c9f5d4c commit 9747dc5

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

core/deployment/src/main/java/io/quarkiverse/langchain4j/deployment/AiServicesProcessor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,13 @@ public void handleDeclarativeServices(AiServicesRecorder recorder,
447447
String aiCacheEmbeddingModelName = aiCacheBuildItem.getEmbeddingModelName();
448448
boolean enableCache = aiCacheBuildItem.isEnable();
449449

450+
// It is not possible to use the cache in combination with the tools.
451+
if (!toolClassNames.isEmpty() && enableCache
452+
&& declarativeAiServiceClassInfo.hasAnnotation(LangChain4jDotNames.CACHE_RESULT)) {
453+
throw new RuntimeException("The cache cannot be used in combination with the tools. Affected class: %s"
454+
.formatted(serviceClassName));
455+
}
456+
450457
SyntheticBeanBuildItem.ExtendedBeanConfigurator configurator = SyntheticBeanBuildItem
451458
.configure(QuarkusAiServiceContext.class)
452459
.forceApplicationClass()

core/runtime/src/main/java/io/quarkiverse/langchain4j/runtime/aiservice/AiServiceMethodImplementationSupport.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.util.Map;
1717
import java.util.Optional;
1818
import java.util.concurrent.Callable;
19-
import java.util.concurrent.ConcurrentHashMap;
2019
import java.util.concurrent.ExecutorService;
2120
import java.util.concurrent.Future;
2221
import java.util.function.Consumer;
@@ -62,7 +61,6 @@ public class AiServiceMethodImplementationSupport {
6261
private static final Logger log = Logger.getLogger(AiServiceMethodImplementationSupport.class);
6362
private static final int MAX_SEQUENTIAL_TOOL_EXECUTIONS = 10;
6463
private static final List<DefaultMemoryIdProvider> DEFAULT_MEMORY_ID_PROVIDERS;
65-
private static final Map<UserMessage, Embedding> CACHE = new ConcurrentHashMap<>();
6664

6765
static {
6866
var defaultMemoryIdProviders = ServiceHelper.loadFactories(
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.quarkiverse.langchain4j.bam.deployment;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.fail;
6+
7+
import org.jboss.shrinkwrap.api.ShrinkWrap;
8+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.RegisterExtension;
11+
12+
import dev.langchain4j.service.UserMessage;
13+
import io.quarkiverse.langchain4j.CacheResult;
14+
import io.quarkiverse.langchain4j.RegisterAiService;
15+
import io.quarkus.test.QuarkusUnitTest;
16+
17+
public class CacheWithToolTest {
18+
19+
@RegisterExtension
20+
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
21+
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class))
22+
.assertException(t -> {
23+
assertThat(t).isInstanceOf(RuntimeException.class);
24+
assertEquals("The cache cannot be used in combination with the tools. Affected class: %s"
25+
.formatted(AiService.class.getName()), t.getMessage());
26+
});
27+
28+
@RegisterAiService(tools = Object.class)
29+
public interface AiService {
30+
@CacheResult
31+
public String poem(@UserMessage("{text}") String text);
32+
}
33+
34+
@Test
35+
void test() {
36+
fail("Should not be called");
37+
}
38+
}

0 commit comments

Comments
 (0)