-
Notifications
You must be signed in to change notification settings - Fork 963
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
Advancing Tool Support - Part 2 #2085
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2023-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.aot; | ||
|
||
import org.springframework.ai.tool.annotation.Tool; | ||
import org.springframework.aot.generate.GenerationContext; | ||
import org.springframework.aot.hint.MemberCategory; | ||
import org.springframework.aot.hint.ReflectionHints; | ||
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; | ||
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor; | ||
import org.springframework.beans.factory.aot.BeanRegistrationCode; | ||
import org.springframework.beans.factory.support.RegisteredBean; | ||
import org.springframework.core.annotation.MergedAnnotations; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.TYPE_HIERARCHY; | ||
|
||
/** | ||
* AOT {@code BeanRegistrationAotProcessor} that detects the presence of the {@link Tool} | ||
* annotation on methods and creates the required reflection hints. | ||
* | ||
* @author Thomas Vitale | ||
* @since 1.0.0 | ||
*/ | ||
class ToolBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor { | ||
|
||
@Override | ||
@Nullable | ||
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { | ||
Class<?> beanClass = registeredBean.getBeanClass(); | ||
MergedAnnotations.Search search = MergedAnnotations.search(TYPE_HIERARCHY); | ||
|
||
boolean hasAnyToolAnnotatedMethods = Stream.of(ReflectionUtils.getDeclaredMethods(beanClass)) | ||
.anyMatch(method -> search.from(method).isPresent(Tool.class)); | ||
|
||
if (hasAnyToolAnnotatedMethods) { | ||
return new AotContribution(beanClass); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static class AotContribution implements BeanRegistrationAotContribution { | ||
|
||
private final MemberCategory[] memberCategories = new MemberCategory[] { MemberCategory.INVOKE_DECLARED_METHODS, | ||
MemberCategory.INVOKE_PUBLIC_METHODS }; | ||
|
||
private final Class<?> toolClass; | ||
|
||
public AotContribution(Class<?> toolClass) { | ||
this.toolClass = toolClass; | ||
} | ||
|
||
@Override | ||
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) { | ||
ReflectionHints reflectionHints = generationContext.getRuntimeHints().reflection(); | ||
reflectionHints.registerType(toolClass, memberCategories); | ||
} | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2023-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
@NonNullApi | ||
@NonNullFields | ||
package org.springframework.ai.aot; | ||
|
||
import org.springframework.lang.NonNullApi; | ||
This comment was marked as resolved.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spring Framework is migrating to JSpecify in the new generation (version 7). When that happens, I imagine the other Spring projects (including Spring AI) will follow. The current generation is still Spring Framework 6, where JSpecify is not adopted. |
||
import org.springframework.lang.NonNullFields; |
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.
@sdeleuze I followed your tips and implemented this
BeanRegistrationAotProcessor
to process Spring beans which contain@Tool
-annotated methods. I included unit tests and also tested it in a GraalVM-based app here: https://github.com/ThomasVitale/llm-apps-java-spring-ai/tree/main/patterns/tool-calling/tool-calling-openai