-
Notifications
You must be signed in to change notification settings - Fork 100
Issues3073:support ToolProvider automatically wired into the AI Service if available in the application contex #131
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
Open
zzx1314
wants to merge
3
commits into
langchain4j:main
Choose a base branch
from
zzx1314:issues3073
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
8 changes: 8 additions & 0 deletions
8
...va/dev/langchain4j/service/spring/mode/automatic/issue3073/AiServiceWithToolProvider.java
This file contains hidden or 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,8 @@ | ||
| package dev.langchain4j.service.spring.mode.automatic.issue3073; | ||
|
|
||
| import dev.langchain4j.service.spring.AiService; | ||
|
|
||
| @AiService | ||
| public interface AiServiceWithToolProvider { | ||
| String chat(String userMessage); | ||
| } |
32 changes: 32 additions & 0 deletions
32
...service/spring/mode/automatic/issue3073/TestAutowireAiServiceToolProviderApplication.java
This file contains hidden or 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,32 @@ | ||
| package dev.langchain4j.service.spring.mode.automatic.issue3073; | ||
|
|
||
| import dev.langchain4j.model.chat.ChatModel; | ||
| import dev.langchain4j.model.openai.OpenAiChatModel; | ||
| import dev.langchain4j.service.tool.ToolProvider; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| import org.springframework.context.annotation.Bean; | ||
|
|
||
| import static dev.langchain4j.model.openai.OpenAiChatModelName.GPT_4_O_MINI; | ||
|
|
||
|
|
||
| @SpringBootApplication | ||
| public class TestAutowireAiServiceToolProviderApplication { | ||
|
|
||
| @Bean | ||
| ChatModel chatModel() { | ||
| return OpenAiChatModel.builder() | ||
| .apiKey(System.getenv("OPENAI_API_KEY")) | ||
| .modelName(GPT_4_O_MINI) | ||
| .build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public ToolProvider toolProvider() { | ||
| return new TestMcpToolProvider(); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication.run(TestAutowireAiServiceToolProviderApplication.class, args); | ||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
...ava/dev/langchain4j/service/spring/mode/automatic/issue3073/TestAutowrieToolProvider.java
This file contains hidden or 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,54 @@ | ||
| package dev.langchain4j.service.spring.mode.automatic.issue3073; | ||
|
|
||
| import dev.langchain4j.service.AiServiceContext; | ||
| import dev.langchain4j.service.spring.AiServicesAutoConfig; | ||
| import dev.langchain4j.service.tool.ToolProvider; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.InvocationHandler; | ||
| import java.lang.reflect.Proxy; | ||
|
|
||
| public class TestAutowrieToolProvider { | ||
|
|
||
| ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
| .withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class)); | ||
|
|
||
| @Test | ||
| void should_fail_to_create_AI_service_when_conflicting_chat_models_are_found() { | ||
| contextRunner | ||
| .withUserConfiguration(TestAutowireAiServiceToolProviderApplication.class) | ||
| .run(context -> { | ||
| ToolProvider toolProviderBean = context.getBean(ToolProvider.class); | ||
| Assertions.assertNotNull(toolProviderBean, "ToolProvider bean should be present in the context."); | ||
|
|
||
| AiServiceWithToolProvider aiServiceProxy = context.getBean(AiServiceWithToolProvider.class); | ||
| Assertions.assertNotNull(aiServiceProxy, "AiServiceWithToolProvider should be created successfully."); | ||
|
|
||
| InvocationHandler handler = Proxy.getInvocationHandler(aiServiceProxy); | ||
| Assertions.assertNotNull(handler, "InvocationHandler should be present."); | ||
| Field this$0 = handler.getClass().getDeclaredField("this$0"); | ||
| this$0.setAccessible(true); | ||
| Object aiServices = this$0.get(handler); | ||
|
|
||
| Field contextField = aiServices.getClass().getSuperclass().getDeclaredField("context"); | ||
| contextField.setAccessible(true); | ||
| AiServiceContext aiServiceContext = (AiServiceContext) contextField.get(aiServices); | ||
| Assertions.assertNotNull(aiServiceContext, "AiServiceContext should be present."); | ||
|
|
||
| Field aiServiceField = aiServiceContext.getClass().getDeclaredField("toolService"); | ||
| aiServiceField.setAccessible(true); | ||
| Object toolService = aiServiceField.get(aiServiceContext); | ||
|
|
||
| Field toolProvider = toolService.getClass().getDeclaredField("toolProvider"); | ||
| toolProvider.setAccessible(true); | ||
| Object toolProviderObj = toolProvider.get(toolService); | ||
| Assertions.assertInstanceOf(TestMcpToolProvider.class, toolProviderObj, "ToolProvider should be TestMcpToolProvider."); | ||
|
|
||
|
|
||
| }); | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
...est/java/dev/langchain4j/service/spring/mode/automatic/issue3073/TestMcpToolProvider.java
This file contains hidden or 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.issue3073; | ||
|
|
||
| import dev.langchain4j.service.tool.ToolProvider; | ||
| import dev.langchain4j.service.tool.ToolProviderRequest; | ||
| import dev.langchain4j.service.tool.ToolProviderResult; | ||
|
|
||
| public class TestMcpToolProvider implements ToolProvider { | ||
| @Override | ||
| public ToolProviderResult provideTools(ToolProviderRequest toolProviderRequest) { | ||
| return null; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very brittle test and it also has a wrong name.
Please change the test to not use reflection, but use a real tool instead.
For example,
TestMcpToolProvidercould provide an actual tool which does somehting (e.g., returns an amount by a unique transaction ID). When asked AI Service to provide an amout for this specific transaction, check that LLM returns correct (hardcoded) value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zzx1314 please follow @dliubarskyi , wait for your feature.