Skip to content

Commit a766c01

Browse files
docs(api): 添加 HunYuan AI 聊天模型文档
- 新增 HunYuan AI聊天模型的使用说明和配置指南 - 添加 HunYuan函数调用的示例和实现细节 - 更新导航栏,增加 HunYuan AI 相关链接 - 修正 HunYuan API 客户端的注释- 更新 README 文件中的文档链接
1 parent 5d40435 commit a766c01

File tree

5 files changed

+468
-3
lines changed

5 files changed

+468
-3
lines changed

models/spring-ai-hunyuan/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[Hunyuan Chat Documentation](https://docs.spring.io/spring-ai/reference/api/chat/moonshot-chat.html)
1+
[Hunyuan Chat Documentation](https://docs.spring.io/spring-ai/reference/api/chat/hunyuan-chat.html)

models/spring-ai-hunyuan/src/main/java/org/springframework/ai/hunyuan/api/HunYuanApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ public record Usage(
323323
* output is reviewed in real-time, and segments that fail the review will have their
324324
* FinishReason set to sensitive. If false, the entire output is reviewed before being
325325
* returned. If real-time text display is required in your application, you should
326-
* handle the case where FinishReason is sensitive by撤回已显示的内容 and providing a custom
327-
* message. Example: false
326+
* handle the case where FinishReason is sensitive and providing a custom message.
327+
* Example: false
328328
* @param tools A list of tools the model may call. Currently, only functions are
329329
* supported as a tool.
330330
* @param toolChoice Controls which (if any) function is called by the model. Possible

spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
***** xref:api/chat/functions/vertexai-gemini-chat-functions.adoc[Gemini Function Calling]
2424
*** xref:api/chat/groq-chat.adoc[Groq]
2525
*** xref:api/chat/huggingface.adoc[Hugging Face]
26+
*** xref:api/chat/hunyuan-chat.adoc[HunYuan AI]
27+
**** xref:api/chat/functions/hunyuan-chat-functions.adoc[HunYuanFunction Calling]
2628
*** xref:api/chat/mistralai-chat.adoc[Mistral AI]
2729
**** xref:api/chat/functions/mistralai-chat-functions.adoc[Mistral Function Calling]
2830
*** xref:api/chat/minimax-chat.adoc[MiniMax]
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
= Function Calling
2+
3+
You can register custom Java functions with the `HunYuanChatModel` and have the HunYuan model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
4+
This allows you to connect the LLM capabilities with external tools and APIs.
5+
The HunYuan models are trained to detect when a function should be called and to respond with JSON that adheres to the function signature.
6+
7+
The HunYuan API does not call the function directly; instead, the model generates JSON that you can use to call the function in your code and return the result back to the model to complete the conversation.
8+
9+
Spring AI provides flexible and user-friendly ways to register and call custom functions.
10+
In general, the custom functions need to provide a function `name`, `description`, and the function call `signature` (as JSON schema) to let the model know what arguments the function expects. The `description` helps the model to understand when to call the function.
11+
12+
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.
13+
14+
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 `ChatModel`.
15+
16+
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.
17+
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.
18+
19+
// Additionally, the Auto-Configuration provides a way to auto-register any Function<I, O> beans definition as function calling candidates in the `ChatModel`.
20+
21+
22+
== How it works
23+
24+
Suppose we want the AI model to respond with information that it does not have, for example the current temperature at a given location.
25+
26+
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.
27+
28+
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.
29+
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.
30+
31+
The model-client interaction is illustrated in the <<spring-ai-function-calling-flow>> diagram.
32+
33+
Spring AI greatly simplifies code you need to write to support function invocation.
34+
It brokers the function invocation conversation for you.
35+
You can simply provide your function definition as a `@Bean` and then provide the bean name of the function in your prompt options.
36+
You can also reference multiple function bean names in your prompt.
37+
38+
== Quick Start
39+
40+
Let's create a chatbot that answer questions by calling our own function.
41+
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.
42+
43+
When the response to the prompt to the model needs to answer a question such as `"What’s the weather like in Boston?"` the AI model will invoke the client providing the location value as an argument to be passed to the function. This RPC-like data is passed as JSON.
44+
45+
Our function calls some SaaS based weather service API and returns the weather response back to the model to complete the conversation. In this example we will use a simple implementation named `MockWeatherService` that hard codes the temperature for various locations.
46+
47+
The following `MockWeatherService.java` represents the weather service API:
48+
49+
[source,java]
50+
----
51+
public class MockWeatherService implements Function<Request, Response> {
52+
53+
public enum Unit { C, F }
54+
public record Request(String location, Unit unit) {}
55+
public record Response(double temp, Unit unit) {}
56+
57+
public Response apply(Request request) {
58+
return new Response(30.0, Unit.C);
59+
}
60+
}
61+
----
62+
63+
=== Registering Functions as Beans
64+
65+
With the link:../hunyuan-chat.html#_auto_configuration[HunYuanChatModel Auto-Configuration] you have multiple ways to register custom functions as beans in the Spring context.
66+
67+
We start with describing the most POJO friendly options.
68+
69+
70+
==== Plain Java Functions
71+
72+
In this approach you define `@Beans` in your application context as you would any other Spring managed object.
73+
74+
Internally, Spring AI `ChatModel` will create an instance of a `FunctionCallback` instance that adds the logic for it being invoked via the AI model.
75+
The name of the `@Bean` is passed as a `ChatOption`.
76+
77+
78+
[source,java]
79+
----
80+
@Configuration
81+
static class Config {
82+
83+
@Bean
84+
@Description("Get the weather in location") // function description
85+
public Function<MockWeatherService.Request, MockWeatherService.Response> weatherFunction1() {
86+
return new MockWeatherService();
87+
}
88+
...
89+
}
90+
----
91+
92+
The `@Description` annotation is optional and provides a function description (2) that helps the model to understand when to call the function. It is an important property to set to help the AI model determine what client side function to invoke.
93+
94+
Another option to provide the description of the function is to the `@JacksonDescription` annotation on the `MockWeatherService.Request` to provide the function description:
95+
96+
[source,java]
97+
----
98+
99+
@Configuration
100+
static class Config {
101+
102+
@Bean
103+
public Function<Request, Response> currentWeather3() { // (1) bean name as function name.
104+
return new MockWeatherService();
105+
}
106+
...
107+
}
108+
109+
@JsonClassDescription("Get the weather in location") // (2) function description
110+
public record Request(String location, Unit unit) {}
111+
----
112+
113+
It is a best practice to annotate the request object with information such that the generates JSON schema of that function is as descriptive as possible to help the AI model pick the correct function to invoke.
114+
115+
The link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/hunyuan/tool/FunctionCallbackWithPlainFunctionBeanIT.java[FunctionCallbackWithPlainFunctionBeanIT.java] demonstrates this approach.
116+
117+
118+
==== FunctionCallback Wrapper
119+
120+
Another way register a function is to create `FunctionCallback` instance like this:
121+
122+
[source,java]
123+
----
124+
@Configuration
125+
static class Config {
126+
127+
@Bean
128+
public FunctionCallback weatherFunctionInfo() {
129+
130+
return FunctionCallback.builder()
131+
.function("CurrentWeather", new MockWeatherService()) // (1) function name and instance
132+
.description("Get the weather in location") // (2) function description
133+
.inputType(MockWeatherService.Request.class) // (3) function signature
134+
.build();
135+
}
136+
...
137+
}
138+
----
139+
140+
It wraps the 3rd party, `MockWeatherService` function and registers it as a `CurrentWeather` function with the `HunYuanChatModel`.
141+
It also provides a description (2) and the function signature (3) to let the model know what arguments the function expects.
142+
143+
NOTE: By default, the response converter does a JSON serialization of the Response object.
144+
145+
NOTE: The `FunctionCallback` internally resolves the function call signature based on the `MockWeatherService.Request` class.
146+
147+
=== Specifying functions in Chat Options
148+
149+
To let the model know and call your `CurrentWeather` function you need to enable it in your prompt requests:
150+
151+
[source,java]
152+
----
153+
HunYuanChatModel chatModel = ...
154+
155+
UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
156+
157+
ChatResponse response = this.chatModel.call(new Prompt(List.of(this.userMessage),
158+
HunYuanChatOptions.builder().function("CurrentWeather").build())); // (1) Enable the function
159+
160+
logger.info("Response: {}", response);
161+
----
162+
163+
// NOTE: You can can have multiple functions registered in your `ChatModel` but only those enabled in the prompt request will be considered for the function calling.
164+
165+
Above user question will trigger 3 calls to `CurrentWeather` function (one for each city) and the final response will be something like this:
166+
167+
----
168+
Here is the current weather for the requested cities:
169+
- San Francisco, CA: 30.0°C
170+
- Tokyo, Japan: 10.0°C
171+
- Paris, France: 15.0°C
172+
----
173+
174+
The link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/hunyuan/tool/HunYuanFunctionCallbackIT.java[HunYuanFunctionCallbackIT.java] test demo this approach.
175+
176+
177+
=== Register/Call Functions with Prompt Options
178+
179+
In addition to the auto-configuration you can register callback functions, dynamically, with your Prompt requests:
180+
181+
[source,java]
182+
----
183+
HunYuanChatModel chatModel = ...
184+
185+
UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
186+
187+
var promptOptions = HunYuanChatOptions.builder()
188+
.functionCallbacks(List.of(FunctionCallback.builder()
189+
.function("CurrentWeather", new MockWeatherService()) // (1) function name
190+
.description("Get the weather in location") // (2) function description
191+
.inputType(MockWeatherService.Request.class) // (3) function signature
192+
.build())) // function code
193+
.build();
194+
195+
ChatResponse response = this.chatModel.call(new Prompt(List.of(this.userMessage), this.promptOptions));
196+
----
197+
198+
NOTE: The in-prompt registered functions are enabled by default for the duration of this request.
199+
200+
This approach allows to dynamically chose different functions to be called based on the user input.
201+
202+
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/hunyuan/tool/FunctionCallbackInPromptIT.java[FunctionCallbackInPromptIT.java] integration test provides a complete example of how to register a function with the `HunYuanChatModel` and use it in a prompt request.

0 commit comments

Comments
 (0)