|
1 | 1 | package dev.langchain4j.service.spring.mode.automatic.withTools;
|
2 | 2 |
|
3 | 3 | import dev.langchain4j.service.spring.AiServicesAutoConfig;
|
| 4 | +import dev.langchain4j.service.spring.mode.automatic.withTools.aop.ToolObserverAspect; |
4 | 5 | import org.junit.jupiter.api.Test;
|
5 | 6 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
6 | 7 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
7 | 8 |
|
8 | 9 | import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
|
| 10 | +import static dev.langchain4j.service.spring.mode.automatic.withTools.AopEnhancedTools.TOOL_OBSERVER_KEY; |
| 11 | +import static dev.langchain4j.service.spring.mode.automatic.withTools.AopEnhancedTools.TOOL_OBSERVER_KEY_NAME_DESCRIPTION; |
| 12 | +import static dev.langchain4j.service.spring.mode.automatic.withTools.AopEnhancedTools.TOOL_OBSERVER_PACKAGE_NAME; |
| 13 | +import static dev.langchain4j.service.spring.mode.automatic.withTools.AopEnhancedTools.TOOL_OBSERVER_PACKAGE_NAME_DESCRIPTION; |
9 | 14 | import static dev.langchain4j.service.spring.mode.automatic.withTools.PackagePrivateTools.CURRENT_TIME;
|
10 | 15 | import static dev.langchain4j.service.spring.mode.automatic.withTools.PublicTools.CURRENT_DATE;
|
11 | 16 | import static org.assertj.core.api.Assertions.assertThat;
|
| 17 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
12 | 20 |
|
13 | 21 | class AiServicesAutoConfigIT {
|
14 | 22 |
|
@@ -61,6 +69,46 @@ void should_create_AI_service_with_tool_that_is_package_private_method_in_packag
|
61 | 69 | });
|
62 | 70 | }
|
63 | 71 |
|
| 72 | + @Test |
| 73 | + void should_create_AI_service_with_tool_which_is_enhanced_by_spring_aop() { |
| 74 | + contextRunner |
| 75 | + .withPropertyValues( |
| 76 | + "langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY, |
| 77 | + "langchain4j.open-ai.chat-model.temperature=0.0", |
| 78 | + "langchain4j.open-ai.chat-model.log-requests=true", |
| 79 | + "langchain4j.open-ai.chat-model.log-responses=true" |
| 80 | + ) |
| 81 | + .withUserConfiguration(AiServiceWithToolsApplication.class) |
| 82 | + .run(context -> { |
| 83 | + |
| 84 | + // given |
| 85 | + AiServiceWithTools aiService = context.getBean(AiServiceWithTools.class); |
| 86 | + |
| 87 | + // when |
| 88 | + String answer = aiService.chat("Which package is the @ToolObserver annotation located in? " + |
| 89 | + "And what is the key of the @ToolObserver annotation?" + |
| 90 | + "And What is the current time?"); |
| 91 | + |
| 92 | + System.out.println("Answer: " + answer); |
| 93 | + |
| 94 | + // then should use AopEnhancedTools.getAspectPackage() |
| 95 | + // & AopEnhancedTools.getToolObserverKey() |
| 96 | + // & PackagePrivateTools.getCurrentTime() |
| 97 | + assertThat(answer).contains(TOOL_OBSERVER_PACKAGE_NAME); |
| 98 | + assertThat(answer).contains(TOOL_OBSERVER_KEY); |
| 99 | + assertThat(answer).contains(String.valueOf(CURRENT_TIME.getMinute())); |
| 100 | + |
| 101 | + // and AOP aspect should be called |
| 102 | + // & only for getToolObserverKey() which is annotated with @ToolObserver |
| 103 | + ToolObserverAspect aspect = context.getBean(ToolObserverAspect.class); |
| 104 | + assertTrue(aspect.aspectHasBeenCalled()); |
| 105 | + |
| 106 | + assertEquals(1, aspect.getObservedTools().size()); |
| 107 | + assertTrue(aspect.getObservedTools().contains(TOOL_OBSERVER_KEY_NAME_DESCRIPTION)); |
| 108 | + assertFalse(aspect.getObservedTools().contains(TOOL_OBSERVER_PACKAGE_NAME_DESCRIPTION)); |
| 109 | + }); |
| 110 | + } |
| 111 | + |
64 | 112 | // TODO tools which are not @Beans?
|
65 | 113 | // TODO negative cases
|
66 | 114 | // TODO no @AiServices in app, just models
|
|
0 commit comments