GH-3403: Add caching for JSON schema generation in function calling#3862
Open
Seol-JY wants to merge 2 commits intospring-projects:mainfrom
Open
GH-3403: Add caching for JSON schema generation in function calling#3862Seol-JY wants to merge 2 commits intospring-projects:mainfrom
Seol-JY wants to merge 2 commits intospring-projects:mainfrom
Conversation
…ction calling Fixes spring-projectsGH-3403 (spring-projects#3403) Add ConcurrentHashMap-based caching to eliminate repeated schema generation for identical method signatures in tool invocations. * Cache tool definitions by Method instance in ToolDefinitions * Cache JSON schemas with method signature + options as key * Include comprehensive test coverage for caching behavior Performance improvement: Eliminates CPU overhead from repeated schema generation in function calling applications. Signed-off-by: Seol-JY <wlsdud5654@gmail.com>
Fix formatting violations detected by spring-javaformat-maven-plugin Signed-off-by: Seol-JY <wlsdud5654@gmail.com>
Contributor
|
Can we also cache Such as: public final class MethodToolCallbackProvider implements ToolCallbackProvider {
@Override
public ToolCallback[] getToolCallbacks() {
if (this.cachedToolCallbacks == null) {
synchronized (this) {
if (this.cachedToolCallbacks == null) {
var toolCallbacks = this.toolObjects.stream()
.map(toolObject -> Stream
.of(ReflectionUtils.getDeclaredMethods(AopUtils.isAopProxy(toolObject)
? AopUtils.getTargetClass(toolObject) : toolObject.getClass()))
.filter(this::isToolAnnotatedMethod)
.filter(toolMethod -> !isFunctionalType(toolMethod))
.filter(ReflectionUtils.USER_DECLARED_METHODS::matches)
.map(toolMethod -> MethodToolCallback.builder()
.toolDefinition(ToolDefinitions.from(toolMethod))
.toolMetadata(ToolMetadata.from(toolMethod))
.toolMethod(toolMethod)
.toolObject(toolObject)
.toolCallResultConverter(ToolUtils.getToolCallResultConverter(toolMethod))
.build())
.toArray(ToolCallback[]::new))
.flatMap(Stream::of)
.toArray(ToolCallback[]::new);
validateToolCallbacks(toolCallbacks);
this.cachedToolCallbacks = toolCallbacks;
}
}
}
return this.cachedToolCallbacks;
}
} |
Contributor
Author
|
Hi @YunKuiLu, |
Contributor
Author
|
Hi @ilayaperumalg , when you have a moment, could you please review my PR? |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes GH-3403
Add ConcurrentHashMap-based caching to eliminate repeated schema generation for identical method signatures in tool invocations.
Performance improvement: Eliminates CPU overhead from repeated schema generation in function calling applications.