forked from spring-projects/spring-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enhanced support for functions as tools via FunctionToolCallback (deprecating the existing FunctionInvokingFunctionCallback). * Aligned JSON Schema generation and parsing logic between function-based and method-based tools. * Deprecated previous client-side function calling APIs. * Included AOT configuration for Tool-annotated methods in Spring beans. Relates to spring-projectsgh-2049 Signed-off-by: Thomas Vitale <[email protected]>
- Loading branch information
1 parent
b77e084
commit 29b9b1b
Showing
32 changed files
with
1,121 additions
and
51 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...ng-ai-core/src/main/java/org/springframework/ai/aot/ToolBeanRegistrationAotProcessor.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,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); | ||
} | ||
|
||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
spring-ai-core/src/main/java/org/springframework/ai/aot/package-info.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 @@ | ||
/* | ||
* 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; | ||
import org.springframework.lang.NonNullFields; |
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.