From 7bb2e379edd1fa7d344c19d7c1c9018944e65921 Mon Sep 17 00:00:00 2001 From: Emmanuel Hugonnet Date: Thu, 19 Dec 2024 11:09:44 +0100 Subject: [PATCH] Producer function can be provided by the LLMConfig to be used fir lazy (#78) creation instead of providing already created bean. Signed-off-by: Emmanuel Hugonnet --- ...hain4JPluginsBuildCompatibleExtension.java | 3 ++- .../mpconfig/LLMConfigMPConfig.java | 2 +- .../core/config/spi/LLMConfig.java | 2 +- .../core/config/spi/ProducerFunction.java | 20 +++++++++++++++++++ .../llm/plugin/CommonLLMPluginCreator.java | 11 ++++++---- 5 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/core/langchain4j/core/config/spi/ProducerFunction.java diff --git a/smallrye-llm-langchain4j-buildcompatible-extension/src/main/java/io/smallrye/llm/plugin/LangChain4JPluginsBuildCompatibleExtension.java b/smallrye-llm-langchain4j-buildcompatible-extension/src/main/java/io/smallrye/llm/plugin/LangChain4JPluginsBuildCompatibleExtension.java index f43b1bf..664f3e8 100644 --- a/smallrye-llm-langchain4j-buildcompatible-extension/src/main/java/io/smallrye/llm/plugin/LangChain4JPluginsBuildCompatibleExtension.java +++ b/smallrye-llm-langchain4j-buildcompatible-extension/src/main/java/io/smallrye/llm/plugin/LangChain4JPluginsBuildCompatibleExtension.java @@ -23,8 +23,9 @@ public class LangChain4JPluginsBuildCompatibleExtension implements BuildCompatib @SuppressWarnings({ "unused", "unchecked" }) @Synthesis public void createSynthetics(SyntheticComponents syntheticComponents) throws ClassNotFoundException { - if (llmConfig == null) + if (llmConfig == null) { llmConfig = LLMConfigProvider.getLlmConfig(); + } LOGGER.info("CDI BCE Langchain4j plugin"); /* diff --git a/smallrye-llm-langchain4j-config-mpconfig/src/main/java/io/smallrye/llm/core/langchain4j/mpconfig/LLMConfigMPConfig.java b/smallrye-llm-langchain4j-config-mpconfig/src/main/java/io/smallrye/llm/core/langchain4j/mpconfig/LLMConfigMPConfig.java index 089840f..5d7eee4 100644 --- a/smallrye-llm-langchain4j-config-mpconfig/src/main/java/io/smallrye/llm/core/langchain4j/mpconfig/LLMConfigMPConfig.java +++ b/smallrye-llm-langchain4j-config-mpconfig/src/main/java/io/smallrye/llm/core/langchain4j/mpconfig/LLMConfigMPConfig.java @@ -42,7 +42,7 @@ public Set getBeanNames() { @Override public T getBeanPropertyValue(String beanName, String propertyName, Class type) { - if (VALUE.equals(propertyName)) { + if (PRODUCER.equals(propertyName)) { return null; } T value = config.getOptionalValue(getBeanPropertyName(beanName, propertyName), type).orElse(null); diff --git a/smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/core/langchain4j/core/config/spi/LLMConfig.java b/smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/core/langchain4j/core/config/spi/LLMConfig.java index 4a90597..fdb8f68 100644 --- a/smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/core/langchain4j/core/config/spi/LLMConfig.java +++ b/smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/core/langchain4j/core/config/spi/LLMConfig.java @@ -15,7 +15,7 @@ public interface LLMConfig { String PREFIX = "smallrye.llm.plugin"; - String VALUE = "defined_bean_value"; + String PRODUCER = "defined_bean_producer"; void init(); diff --git a/smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/core/langchain4j/core/config/spi/ProducerFunction.java b/smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/core/langchain4j/core/config/spi/ProducerFunction.java new file mode 100644 index 0000000..df78725 --- /dev/null +++ b/smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/core/langchain4j/core/config/spi/ProducerFunction.java @@ -0,0 +1,20 @@ +package io.smallrye.llm.core.langchain4j.core.config.spi; + +import jakarta.enterprise.inject.Instance; + +/** + * Simple function to produce synthetics beans via BeanData + * + * @param the result. + */ +@FunctionalInterface +public interface ProducerFunction { + /** + * Produces a bean using its name and a lookup context. + * + * @param lookup: lookup context. + * @param beanName: the name of the bean. + * @return the created bean. + */ + R produce(Instance lookup, String beanName); +} diff --git a/smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/plugin/CommonLLMPluginCreator.java b/smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/plugin/CommonLLMPluginCreator.java index 9236b56..64d8785 100644 --- a/smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/plugin/CommonLLMPluginCreator.java +++ b/smallrye-llm-langchain4j-core/src/main/java/io/smallrye/llm/plugin/CommonLLMPluginCreator.java @@ -1,6 +1,6 @@ package io.smallrye.llm.plugin; -import static io.smallrye.llm.core.langchain4j.core.config.spi.LLMConfig.VALUE; +import static io.smallrye.llm.core.langchain4j.core.config.spi.LLMConfig.PRODUCER; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; @@ -29,6 +29,7 @@ import dev.langchain4j.store.embedding.EmbeddingStore; import io.smallrye.llm.core.langchain4j.core.config.spi.LLMConfig; import io.smallrye.llm.core.langchain4j.core.config.spi.LLMConfigProvider; +import io.smallrye.llm.core.langchain4j.core.config.spi.ProducerFunction; /* smallrye.llm.plugin.content-retriever.class=dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever @@ -62,10 +63,12 @@ public static void createAllLLMBeans(LLMConfig llmConfig, Consumer bea } Class scopeClass = (Class) loadClass(scopeClassName); Class targetClass = loadClass(className); - Object bean = llmConfig.getBeanPropertyValue(beanName, VALUE, targetClass); - if (bean != null) { + ProducerFunction producer = llmConfig.getBeanPropertyValue(beanName, PRODUCER, + ProducerFunction.class); + if (producer != null) { beanBuilder.accept( - new BeanData(targetClass, null, scopeClass, beanName, (Instance creationalContext) -> bean)); + new BeanData(targetClass, null, scopeClass, beanName, + (Instance creationalContext) -> producer.produce(creationalContext, beanName))); } else { // test if there is an inner static class Builder Class builderCLass = Arrays.stream(targetClass.getDeclaredClasses())