-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #1606 AiServicesAutoConfig is unable to detect AiService in the p…
…ackage specified by @componentscan (#35) AiServicesAutoConfig is unable to detect AiService in the package specified by @componentscan Changes Made: - Replaced the original use of Reflections with a combination of `BeanDefinitionRegistryPostProcessor` and `ClassPathBeanDefinitionScanner` to register interfaces. - Scanned all the packages specified by @componentscan and those obtained from AutoConfigurationPackages as basePackages. Relevant Links: - langchain4j/langchain4j#1606 - langchain4j/langchain4j#1593
- Loading branch information
Showing
8 changed files
with
205 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...-boot-starter/src/main/java/dev/langchain4j/service/spring/AiServiceScannerProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package dev.langchain4j.service.spring; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; | ||
import org.springframework.boot.autoconfigure.AutoConfigurationPackages; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Component | ||
public class AiServiceScannerProcessor implements BeanDefinitionRegistryPostProcessor { | ||
|
||
@Override | ||
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { | ||
ClassPathAiServiceScanner classPathInterfaceScanner = new ClassPathAiServiceScanner(registry, false); | ||
classPathInterfaceScanner.registerFilters(); | ||
Set<String> basePackages = getBasePackages((ConfigurableListableBeanFactory) registry); | ||
for (String basePackage : basePackages) { | ||
classPathInterfaceScanner.scan(basePackage); | ||
} | ||
} | ||
|
||
private Set<String> getBasePackages(ConfigurableListableBeanFactory beanFactory) { | ||
Set<String> basePackages = new LinkedHashSet<>(); | ||
|
||
List<String> autoConfigPackages = AutoConfigurationPackages.get(beanFactory); | ||
basePackages.addAll(autoConfigPackages); | ||
|
||
String[] beanNames = beanFactory.getBeanNamesForAnnotation(ComponentScan.class); | ||
for (String beanName : beanNames) { | ||
Class<?> beanClass = beanFactory.getType(beanName); | ||
if (beanClass != null) { | ||
ComponentScan componentScan = beanClass.getAnnotation(ComponentScan.class); | ||
if (componentScan != null) { | ||
Collections.addAll(basePackages, componentScan.value()); | ||
Collections.addAll(basePackages, componentScan.basePackages()); | ||
for (Class<?> basePackageClass : componentScan.basePackageClasses()) { | ||
basePackages.add(basePackageClass.getPackage().getName()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return basePackages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...-boot-starter/src/main/java/dev/langchain4j/service/spring/ClassPathAiServiceScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dev.langchain4j.service.spring; | ||
|
||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; | ||
import org.springframework.core.type.filter.AnnotationTypeFilter; | ||
|
||
public class ClassPathAiServiceScanner extends ClassPathBeanDefinitionScanner { | ||
|
||
public ClassPathAiServiceScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) { | ||
super(registry, useDefaultFilters); | ||
} | ||
|
||
@Override | ||
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { | ||
return beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent(); | ||
} | ||
|
||
public void registerFilters() { | ||
addIncludeFilter(new AnnotationTypeFilter(AiService.class)); | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
...ain4j-spring-boot-starter/src/main/java/dev/langchain4j/spring/LangChain4jAutoConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
package dev.langchain4j.spring; | ||
|
||
import dev.langchain4j.rag.spring.RagAutoConfig; | ||
import dev.langchain4j.service.spring.AiServiceScannerProcessor; | ||
import dev.langchain4j.service.spring.AiServicesAutoConfig; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.context.annotation.Import; | ||
|
||
@AutoConfiguration | ||
@Import({ | ||
AiServicesAutoConfig.class, | ||
RagAutoConfig.class | ||
RagAutoConfig.class, | ||
AiServiceScannerProcessor.class | ||
}) | ||
public class LangChain4jAutoConfig { | ||
} |
12 changes: 12 additions & 0 deletions
12
...spring/mode/automatic/differentPackage/package1/DifferentPackageAiServiceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package dev.langchain4j.service.spring.mode.automatic.differentPackage.package1; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class DifferentPackageAiServiceApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DifferentPackageAiServiceApplication.class, args); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
.../service/spring/mode/automatic/differentPackage/package1/DifferentPackageAiServiceIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package dev.langchain4j.service.spring.mode.automatic.differentPackage.package1; | ||
|
||
import dev.langchain4j.service.spring.AiServicesAutoConfig; | ||
import dev.langchain4j.service.spring.mode.automatic.differentPackage.package2.DifferentPackageAiService; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.context.annotation.ComponentScan; | ||
|
||
import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class DifferentPackageAiServiceIT { | ||
|
||
ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class)); | ||
|
||
@ComponentScan(value = "dev.langchain4j.service.spring.mode.automatic.differentPackage.package2") | ||
static class ComponentScanWithValue { | ||
} | ||
|
||
@ComponentScan(basePackages = "dev.langchain4j.service.spring.mode.automatic.differentPackage.package2") | ||
static class ComponentScanWithBasePackages { | ||
} | ||
|
||
@ComponentScan(basePackageClasses = dev.langchain4j.service.spring.mode.automatic.differentPackage.package2.DifferentPackageAiService.class) | ||
static class ComponentScanWithBasePackageClasses { | ||
} | ||
|
||
@Test | ||
void should_create_AI_service_that_use_componentScan_value() { | ||
|
||
contextRunner | ||
.withPropertyValues( | ||
"langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY, | ||
"langchain4j.open-ai.chat-model.max-tokens=20", | ||
"langchain4j.open-ai.chat-model.temperature=0.0" | ||
) | ||
.withUserConfiguration(DifferentPackageAiServiceApplication.class) | ||
.withUserConfiguration(ComponentScanWithValue.class) | ||
.run(context -> { | ||
|
||
// given | ||
DifferentPackageAiService aiService = context.getBean(DifferentPackageAiService.class); | ||
|
||
// when | ||
String answer = aiService.chat("What is the capital of Germany?"); | ||
|
||
// then | ||
assertThat(answer).containsIgnoringCase("Berlin"); | ||
}); | ||
} | ||
|
||
@Test | ||
void should_create_AI_service_that_use_componentScan_basePackages() { | ||
|
||
contextRunner | ||
.withPropertyValues( | ||
"langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY, | ||
"langchain4j.open-ai.chat-model.max-tokens=20", | ||
"langchain4j.open-ai.chat-model.temperature=0.0" | ||
) | ||
.withUserConfiguration(DifferentPackageAiServiceApplication.class) | ||
.withUserConfiguration(ComponentScanWithBasePackages.class) | ||
.run(context -> { | ||
|
||
// given | ||
DifferentPackageAiService aiService = context.getBean(DifferentPackageAiService.class); | ||
|
||
// when | ||
String answer = aiService.chat("What is the capital of Germany?"); | ||
|
||
// then | ||
assertThat(answer).containsIgnoringCase("Berlin"); | ||
}); | ||
} | ||
|
||
@Test | ||
void should_create_AI_service_that_use_componentScan_basePackageClasses() { | ||
|
||
contextRunner | ||
.withPropertyValues( | ||
"langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY, | ||
"langchain4j.open-ai.chat-model.max-tokens=20", | ||
"langchain4j.open-ai.chat-model.temperature=0.0" | ||
) | ||
.withUserConfiguration(DifferentPackageAiServiceApplication.class) | ||
.withUserConfiguration(ComponentScanWithBasePackageClasses.class) | ||
.run(context -> { | ||
|
||
// given | ||
DifferentPackageAiService aiService = context.getBean(DifferentPackageAiService.class); | ||
|
||
// when | ||
String answer = aiService.chat("What is the capital of Germany?"); | ||
|
||
// then | ||
assertThat(answer).containsIgnoringCase("Berlin"); | ||
}); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...4j/service/spring/mode/automatic/differentPackage/package2/DifferentPackageAiService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.langchain4j.service.spring.mode.automatic.differentPackage.package2; | ||
|
||
import dev.langchain4j.service.spring.AiService; | ||
|
||
@AiService | ||
public interface DifferentPackageAiService { | ||
|
||
String chat(String userMessage); | ||
} |