Skip to content

Commit bc24b4c

Browse files
committed
docs: Clarify function calling concepts and usage patterns
Improves function calling documentation with clearer organization and examples: - Reorganizes content into server-side and client-side registration sections - Adds detailed examples for both function-invoking and method-invoking approaches - Enhances tool context documentation with diagrams and usage examples - Updates function-calling diagrams to reflect current implementation
1 parent 018257a commit bc24b4c

File tree

5 files changed

+71
-26
lines changed

5 files changed

+71
-26
lines changed

spring-ai-core/src/test/java/org/springframework/ai/model/function/MethodFunctionCallbackTests.java renamed to spring-ai-core/src/test/java/org/springframework/ai/model/function/MethodInvokingFunctionCallbackTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @author Christian Tzolov
3131
* @since 1.0.0
3232
*/
33-
public class MethodFunctionCallbackTests {
33+
public class MethodInvokingFunctionCallbackTests {
3434

3535
private static final Map<String, Object> arguments = new ConcurrentHashMap<>();
3636

Loading
Loading

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

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ In general, the custom functions need to provide a function `name`, `descriptio
2828

2929
As a developer, you need to implement a function that takes the function call arguments sent from the AI model, and responds with the result back to the model. Your function can in turn invoke other 3rd party services to provide the results.
3030

31-
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ChatClient`.
31+
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ChatClient` or registering the function dynamically in your prompt request.
3232

3333
Under the hood, Spring wraps your POJO (the function) with the appropriate adapter code that enables interaction with the AI Model, saving you from writing tedious boilerplate code.
3434
The basis of the underlying infrastructure is the link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallback.java[FunctionCallback.java] interface and the companion Builder utility class to simplify the implementation and registration of Java callback functions.
@@ -39,16 +39,20 @@ Suppose we want the AI model to respond with information that it does not have,
3939

4040
We can provide the AI model with metadata about our own functions that it can use to retrieve that information as it processes your prompt.
4141

42-
For example, if during the processing of a prompt, the AI Model determines that it needs additional information about the temperature in a given location, it will start a server-side generated request/response interaction. The AI Model invokes a client side function.
43-
The AI Model provides method invocation details as JSON, and it is the responsibility of the client to execute that function and return the response.
42+
For example, if during the processing of a prompt, the AI Model determines that it needs additional information about the temperature in a given location, it will start a server-side generated request/response interaction.
43+
Instead of returning the final response message, the AI Model returns at special Tool Call request, providing the function name and arguments (as JSON).
44+
It is the responsibility of the client to process this message and execute the named function and return the response
45+
as Tool Response message back to the AI Model.
4446

4547
Spring AI greatly simplifies the code you need to write to support function invocation.
4648
It brokers the function invocation conversation for you.
47-
You can simply provide your function definition as a `@Bean` and then provide the bean name of the function in your prompt options.
49+
You can simply provide your function definition as a `@Bean` and then provide the bean name of the function in your prompt options or pass the function directly as a parameter in your prompt request options.
50+
4851
You can also reference multiple function bean names in your prompt.
4952

50-
== Quick Start
53+
=== Example Use Case
5154

55+
Lets define a simple use case that we can use as an example to explain how function invocation works.
5256
Let's create a chatbot that answer questions by calling our own function.
5357
To support the response of the chatbot, we will register our own function that takes a location and returns the current weather in that location.
5458

@@ -91,7 +95,9 @@ data class Response(val temp: Double, val unit: Unit) {}
9195
======
9296
--
9397

94-
=== Registering Functions as Beans
98+
== Server-Side Registration
99+
100+
=== Functions as Beans
95101

96102
Spring AI provides multiple ways to register custom functions as beans in the Spring context.
97103

@@ -266,18 +272,23 @@ Here is the current weather for the requested cities:
266272

267273
The link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/openai/tool/FunctionCallbackWithPlainFunctionBeanIT.java[FunctionCallbackWithPlainFunctionBeanIT.java] test demo this approach.
268274

269-
=== Register functions: On the fly
275+
== Client-Side Registration
276+
277+
In addition to the auto-configuration, you can register callback functions, dynamically.
278+
You can use either the function invoking or method invoking approaches to register functions with your `ChatClient` or `ChatModel` requests.
270279

271-
In addition to the auto-configuration, you can register callback functions, dynamically:
280+
The client-side registration enables you to register functions by default.
281+
282+
=== Function Invoking
272283

273284
[source,java]
274285
----
275286
ChatClient chatClient = ...
276-
287+
277288
ChatResponse response = this.chatClient.prompt("What's the weather like in San Francisco, Tokyo, and Paris?")
278289
.functions(FunctionCallback.builder()
279290
.description("Get the weather in location") // (2) function description
280-
.function("CurrentWeather", new MockWeatherService()) // (1) function name and instance
291+
.function("currentWeather", (Request request) -> new Response(30.0, Unit.C)) // (1) function name and instance
281292
.inputType(MockWeatherService.Request.class) // (3) input type to build the JSON schema
282293
.build())
283294
.call()
@@ -290,29 +301,28 @@ This approach allows to choose dynamically different functions to be called base
290301

291302
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/openai/tool/FunctionCallbackInPromptIT.java[FunctionCallbackInPromptIT.java] integration test provides a complete example of how to register a function with the `ChatClient` and use it in a prompt request.
292303

293-
=== Register functions: Method Invoking FunctionCallback
304+
=== Method Invoking
294305

295-
The `MethodFunctionCallback` enables method invocation through reflection while automatically handling JSON schema generation and parameter conversion.
306+
The `MethodInvokingFunctionCallback` enables method invocation through reflection while automatically handling JSON schema generation and parameter conversion.
296307
It's particularly useful for integrating Java methods as callable functions within AI model interactions.
297308

298-
The `MethodFunctionCallback` implements the `FunctionCallback` interface and provides:
309+
The `MethodInvokingFunctionCallback` implements the `FunctionCallback` interface and provides:
299310

300311
- Automatic JSON schema generation for method parameters
301312
- Support for both static and instance methods
302313
- Any number of parameters (including none) and return values (including void)
303314
- Any parameter/return types (primitives, objects, collections)
304315
- Special handling for `ToolContext` parameters
305316

306-
You need the `FunctionCallback.Builder` to create `MethodFunctionCallback` like this:
317+
You need the `FunctionCallback.Builder` to create `MethodInvokingFunctionCallback` like this:
307318

308319
[source,java]
309320
----
310321
// Create using builder pattern
311-
FunctionCallback callback = FunctionCallback.builder()
312-
.description("Method description") // Required: Helps AI understand the function
313-
.objectMapper(objectMapper) // Optional: Custom ObjectMapper
314-
.method("MethodName", Class<?>...argumentTypes) // Required: The method to invoke and its argument types
315-
.targetObject(targetObject) // Required only for instance methods
322+
FunctionCallback methodInvokingCallback = FunctionCallback.builder()
323+
.description("Function calling description") // Hints the AI to know when to call this method
324+
.method("MethodName", Class<?>...argumentTypes) // The method to invoke and its argument types
325+
.targetObject(targetObject) // Required instance methods for static methods use targetClass
316326
.build();
317327
----
318328

@@ -365,16 +375,25 @@ String response = ChatClient.create(chatModel).prompt()
365375
366376
======
367377

368-
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/client/OpenAiChatClientMethodFunctionCallbackIT.java[OpenAiChatClientMethodFunctionCallbackIT]
378+
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/client/OpenAiChatClientMethodInvokingFunctionCallbackIT.java[OpenAiChatClientMethodInvokingFunctionCallbackIT]
369379
integration test provides additional examples of how to use the FunctionCallback.Builder to create method invocation FunctionCallbacks.
370380

371-
=== Tool Context
381+
== Tool Context
382+
383+
Spring AI now supports passing additional contextual information to function callbacks through a tool context.
384+
This feature allows you to provide extra, user provided, data that can be used within the function execution along with the function arguments passed by the AI model.
385+
386+
image::function-calling-tool-context.jpg[Function calling with Tool Context, width=700, align="center"]
372387

373-
Spring AI now supports passing additional contextual information to function callbacks through a tool context. This feature allows you to provide extra data that can be used within the function execution, enhancing the flexibility and power of function calling.
388+
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/model/ToolContext.java[ToolContext] class provides a way to pass additional context information.
374389

375-
The context information that is passed in as the second argument of a `java.util.BiFunction`. The `ToolContext` contains as an immutable `Map<String,Object>` allowing you to access key-value pairs.
390+
=== Using Tool Context
376391

377-
==== How to Use Tool Context
392+
In case of function-invoking, the context information that is passed in as the second argument of a `java.util.BiFunction`.
393+
394+
For method-invoking, the context information is passed as a method argument of type `ToolContext`.
395+
396+
==== Function Invoking
378397

379398
You can set the tool context when building your chat options and use a BiFunction for your callback:
380399

@@ -415,3 +434,29 @@ ChatResponse response = chatClient.prompt("What's the weather like in San Franci
415434
In this example, the `weatherFunction` is defined as a BiFunction that takes both the request and the tool context as parameters. This allows you to access the context directly within the function logic.
416435

417436
This approach allows you to pass session-specific or user-specific information to your functions, enabling more contextual and personalized responses.
437+
438+
==== Method Invoking
439+
440+
[source,java]
441+
----
442+
public class DeviceController {
443+
public void setDeviceState(String deviceId, boolean state, ToolContext context) {
444+
Map<String, Object> contextData = context.getContext();
445+
// Implementation using context data
446+
}
447+
}
448+
449+
// Usage
450+
DeviceController controller = new DeviceController();
451+
452+
String response = ChatClient.create(chatModel).prompt()
453+
.user("Turn on the living room lights")
454+
.functions(FunctionCallback.builder()
455+
.description("Control device state")
456+
.method("setDeviceState", String.class,boolean.class,ToolContext.class)
457+
.targetObject(controller)
458+
.build())
459+
.toolContext(Map.of("location", "home"))
460+
.call()
461+
.content();
462+
----

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<suppress files="BaseOllamaIT.java" checks="HideUtilityClassConstructor"/>
3232
<suppress files="BaseOCIGenAITest.java" checks="HideUtilityClassConstructor"/>
3333
<suppress files="OpenAiChatModelResponseFormatIT.java" checks="RegexpSinglelineJava"/>
34-
<suppress files="MethodFunctionCallbackTests.java" checks="RegexpSinglelineJava"/>
34+
<suppress files="MethodInvokingFunctionCallbackTests.java" checks="RegexpSinglelineJava"/>
3535
<suppress files="ClientIT.java" checks="RegexpSinglelineJava"/>
3636

3737

0 commit comments

Comments
 (0)