Skip to content

Commit

Permalink
Producer function can be provided by the LLMConfig to be used fir lazy (
Browse files Browse the repository at this point in the history
#78)

creation  instead of providing already created bean.

Signed-off-by: Emmanuel Hugonnet <[email protected]>
  • Loading branch information
ehsavoie authored and Buhake Sindi committed Dec 19, 2024
1 parent 18d4262 commit 7bb2e37
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Set<String> getBeanNames() {

@Override
public <T> T getBeanPropertyValue(String beanName, String propertyName, Class<T> type) {
if (VALUE.equals(propertyName)) {
if (PRODUCER.equals(propertyName)) {
return null;
}
T value = config.getOptionalValue(getBeanPropertyName(beanName, propertyName), type).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public interface LLMConfig {

String PREFIX = "smallrye.llm.plugin";
String VALUE = "defined_bean_value";
String PRODUCER = "defined_bean_producer";

void init();

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <R> the result.
*/
@FunctionalInterface
public interface ProducerFunction<R> {
/**
* 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<R> lookup, String beanName);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -62,10 +63,12 @@ public static void createAllLLMBeans(LLMConfig llmConfig, Consumer<BeanData> bea
}
Class<? extends Annotation> scopeClass = (Class<? extends Annotation>) loadClass(scopeClassName);
Class<?> targetClass = loadClass(className);
Object bean = llmConfig.getBeanPropertyValue(beanName, VALUE, targetClass);
if (bean != null) {
ProducerFunction<Object> producer = llmConfig.getBeanPropertyValue(beanName, PRODUCER,
ProducerFunction.class);
if (producer != null) {
beanBuilder.accept(
new BeanData(targetClass, null, scopeClass, beanName, (Instance<Object> creationalContext) -> bean));
new BeanData(targetClass, null, scopeClass, beanName,
(Instance<Object> creationalContext) -> producer.produce(creationalContext, beanName)));
} else {
// test if there is an inner static class Builder
Class<?> builderCLass = Arrays.stream(targetClass.getDeclaredClasses())
Expand Down

0 comments on commit 7bb2e37

Please sign in to comment.