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

Producer function can be provided by the LLMConfig to be used for lazy #78

Merged
merged 1 commit into from
Dec 19, 2024
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 @@ -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 All @@ -21,6 +21,7 @@

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 All @@ -47,10 +48,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
Loading