You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: reorganize and enhance FunctionCallback documentation
* Remove redundant section headers
* Improve builder pattern documentation with hierarchical structure overview
* Add cross-references between related sections
* Restructure function and method invoking sections for better organization
* Adjust heading levels for better document hierarchy
* Move schema generation and common pitfalls under appropriate sections
Copy file name to clipboardExpand all lines: spring-ai-docs/src/main/antora/modules/ROOT/pages/api/function-callback.adoc
+23-22Lines changed: 23 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,8 @@
1
1
= FunctionCallback
2
2
3
-
== Overview
4
-
5
3
The `FunctionCallback` interface in Spring AI provides a standardized way to implement Large Language Model (LLM) function calling capabilities. It allows developers to register custom functions that can be called by AI models when specific conditions or intents are detected in the prompts.
6
4
7
-
== FunctionCallback Interface
8
-
9
-
The main interface defines several key methods:
5
+
The `FunctionCallback` interface defines several key methods:
10
6
11
7
* `getName()`: Returns the unique function name within the AI model context
12
8
* `getDescription()`: Provides a description that helps the model decide when to invoke the function
@@ -16,19 +12,24 @@ The main interface defines several key methods:
16
12
17
13
== Builder Pattern
18
14
19
-
Spring AI provides a fluent builder API for creating `FunctionCallback` implementations.
20
-
15
+
Spring AI provides a fluent builder API for creating `FunctionCallback` implementations.
21
16
This is particularly useful for defining function callbacks that you can register, pragmatically, on the fly, with your `ChatClient` or `ChatModel` model calls.
22
17
23
-
The builders helps with complex configurations, such as custom response handling, schema types (e.g. JSONSchema or OpenAPI), and object mapping.
18
+
Use the `FunctionCallback.builder()` method to create a new builder instance and chain the configuration methods to set the function name, description, input type, and other properties.
19
+
The `FunctionCallback.Builder` is a hierarchical with the following structure:
24
20
25
-
=== Function-Invoking Approach
21
+
- FunctionCallback.Builder - The root builder interface used for configuring the xref:_common_configurations[shared properties].
22
+
- FunctionInvokingSpec - The xref:Function-Invoking[function invoking] builder interface.
23
+
- MethodInvokingSpec - The xref:Method-Invoking[method invoking] builder interface.
24
+
25
+
[[Function-Invoking]]
26
+
== Function-Invoking Approach
26
27
27
28
Converts any `java.util.function.Function`, `BiFunction`, `Supplier` or `Consumer` into a `FunctionCallback` that can be called by the AI model.
28
29
29
30
NOTE: You can use lambda expressions or method references to define the function logic but you must provide the input type of the function using the `inputType(TYPE)`.
Use `java.util.Supplier<O>` or `java.util.function.Function<Void, O>` to define functions that don't take any input:
59
60
@@ -66,7 +67,7 @@ FunctionCallback.builder()
66
67
.build();
67
68
----
68
69
69
-
==== Consumer<I>
70
+
=== Consumer<I>
70
71
71
72
Use `java.util.Consumer<I>` or `java.util.function.Function<I, Void>` to define functions that don't produce output:
72
73
@@ -83,7 +84,7 @@ FunctionCallback.builder()
83
84
.build();
84
85
----
85
86
86
-
==== Generics Input Type
87
+
=== Generics Input Type
87
88
88
89
Use the `ParameterizedTypeReference` to define functions with generic input types:
89
90
@@ -105,7 +106,8 @@ FunctionCallback.builder()
105
106
.build();
106
107
----
107
108
108
-
=== Method Invoking Approach
109
+
[[Method-Invoking]]
110
+
== Method Invoking Approach
109
111
110
112
Enables method invocation through reflection while automatically handling JSON schema generation and parameter conversion. It’s particularly useful for integrating Java methods as callable functions within AI model interactions.
111
113
@@ -117,7 +119,7 @@ The method invoking implements the `FunctionCallback` interface and provides:
117
119
- Any parameter/return types (primitives, objects, collections)
118
120
- Special handling for xref:api/functions.adoc#Tool-Context[ToolContext] parameters
119
121
120
-
==== Static Method Invocation
122
+
=== Static Method Invocation
121
123
122
124
You can refer to a static method in a class by providing the method name, parameter types, and the target class.
You can refer to an instance method in a class by providing the method name, parameter types, and the target object instance.
142
144
@@ -236,20 +238,19 @@ FunctionCallback.builder()
236
238
* Use xref:api/functions.adoc#Tool-Context[ToolContext] when additional state or context is required that is provided from the User and not part of the function input generated by the AI model.
237
239
* Use `BiFunction<I, ToolContext, O>` to access the ToolContext in the function invocation approach and add `ToolContext` parameter in the method invoking approach.
238
240
239
-
240
-
== Notes on Schema Generation
241
+
=== Notes on Schema Generation
241
242
242
243
* The framework automatically generates JSON schemas from Java types
243
244
* For function invoking, the schema is generated based on the input type for the function that needs to be set using `inputType(TYPE)`. Use `ParameterizedTypeReference` for generic types.
244
245
* Generated schemas respect Jackson annotations on model classes
245
246
* You can bypass the automatic generation by providing custom schemas using `inputTypeSchema()`
246
247
247
-
== Common Pitfalls to Avoid
248
+
=== Common Pitfalls to Avoid
248
249
249
-
=== Lack of Description
250
+
==== Lack of Description
250
251
* Always provide explicit descriptions instead of relying on auto-generated ones
251
252
* Clear descriptions improve model's function selection accuracy
252
253
253
-
=== Schema Mismatches
254
+
==== Schema Mismatches
254
255
* Ensure input types match the Function's input parameter types.
255
256
* Use `ParameterizedTypeReference` for generic types.
0 commit comments