Skip to content

Commit 64c4567

Browse files
committed
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
1 parent b3bb541 commit 64c4567

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/function-callback.adoc

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
= FunctionCallback
22

3-
== Overview
4-
53
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.
64

7-
== FunctionCallback Interface
8-
9-
The main interface defines several key methods:
5+
The `FunctionCallback` interface defines several key methods:
106

117
* `getName()`: Returns the unique function name within the AI model context
128
* `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:
1612
1713
== Builder Pattern
1814

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.
2116
This is particularly useful for defining function callbacks that you can register, pragmatically, on the fly, with your `ChatClient` or `ChatModel` model calls.
2217

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:
2420

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
2627

2728
Converts any `java.util.function.Function`, `BiFunction`, `Supplier` or `Consumer` into a `FunctionCallback` that can be called by the AI model.
2829

2930
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)`.
3031

31-
==== Function<I, O>
32+
=== Function<I, O>
3233

3334
[source,java]
3435
----
@@ -39,7 +40,7 @@ FunctionCallback callback = FunctionCallback.builder()
3940
.build();
4041
----
4142

42-
==== BiFunction<I, ToolContext, O>
43+
=== BiFunction<I, ToolContext, O>
4344

4445
Using Function with input type <I> and additional xref:api/functions.adoc#Tool-Context[ToolContext] parameter:
4546

@@ -53,7 +54,7 @@ FunctionCallback callback = FunctionCallback.builder()
5354
.build();
5455
----
5556

56-
==== Supplier<O>
57+
=== Supplier<O>
5758

5859
Use `java.util.Supplier<O>` or `java.util.function.Function<Void, O>` to define functions that don't take any input:
5960

@@ -66,7 +67,7 @@ FunctionCallback.builder()
6667
.build();
6768
----
6869

69-
==== Consumer<I>
70+
=== Consumer<I>
7071

7172
Use `java.util.Consumer<I>` or `java.util.function.Function<I, Void>` to define functions that don't produce output:
7273

@@ -83,7 +84,7 @@ FunctionCallback.builder()
8384
.build();
8485
----
8586

86-
==== Generics Input Type
87+
=== Generics Input Type
8788

8889
Use the `ParameterizedTypeReference` to define functions with generic input types:
8990

@@ -105,7 +106,8 @@ FunctionCallback.builder()
105106
.build();
106107
----
107108

108-
=== Method Invoking Approach
109+
[[Method-Invoking]]
110+
== Method Invoking Approach
109111

110112
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.
111113

@@ -117,7 +119,7 @@ The method invoking implements the `FunctionCallback` interface and provides:
117119
- Any parameter/return types (primitives, objects, collections)
118120
- Special handling for xref:api/functions.adoc#Tool-Context[ToolContext] parameters
119121

120-
==== Static Method Invocation
122+
=== Static Method Invocation
121123

122124
You can refer to a static method in a class by providing the method name, parameter types, and the target class.
123125

@@ -136,7 +138,7 @@ FunctionCallback callback = FunctionCallback.builder()
136138
.build();
137139
----
138140

139-
==== Object instance Method Invocation
141+
=== Object instance Method Invocation
140142

141143
You can refer to an instance method in a class by providing the method name, parameter types, and the target object instance.
142144

@@ -236,20 +238,19 @@ FunctionCallback.builder()
236238
* 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.
237239
* Use `BiFunction<I, ToolContext, O>` to access the ToolContext in the function invocation approach and add `ToolContext` parameter in the method invoking approach.
238240

239-
240-
== Notes on Schema Generation
241+
=== Notes on Schema Generation
241242

242243
* The framework automatically generates JSON schemas from Java types
243244
* 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.
244245
* Generated schemas respect Jackson annotations on model classes
245246
* You can bypass the automatic generation by providing custom schemas using `inputTypeSchema()`
246247

247-
== Common Pitfalls to Avoid
248+
=== Common Pitfalls to Avoid
248249

249-
=== Lack of Description
250+
==== Lack of Description
250251
* Always provide explicit descriptions instead of relying on auto-generated ones
251252
* Clear descriptions improve model's function selection accuracy
252253

253-
=== Schema Mismatches
254+
==== Schema Mismatches
254255
* Ensure input types match the Function's input parameter types.
255256
* Use `ParameterizedTypeReference` for generic types.

0 commit comments

Comments
 (0)