Skip to content

Commit f0b6b78

Browse files
refactor(spring-ai-hunyuan): 重构代码并适配新的 API 认证方式
- 更新 API 调用方式,使用 secretId 和 secretKey 进行认证 - 修复测试配置中的 API 实例创建逻辑 -优化部分代码结构,提高可读性和维护性
1 parent a530918 commit f0b6b78

File tree

5 files changed

+29
-30
lines changed

5 files changed

+29
-30
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class HunYuanChatModel extends AbstractToolCallSupport implements ChatMod
8585
/**
8686
* Low-level access to the Hunyuan API.
8787
*/
88-
private final HunYuanApi moonshotApi;
88+
private final HunYuanApi hunYuanApi;
8989

9090
private final RetryTemplate retryTemplate;
9191

@@ -101,58 +101,58 @@ public class HunYuanChatModel extends AbstractToolCallSupport implements ChatMod
101101

102102
/**
103103
* Initializes a new instance of the HunyuanChatModel.
104-
* @param moonshotApi The Hunyuan instance to be used for interacting with the
104+
* @param hunYuanApi The Hunyuan instance to be used for interacting with the
105105
* Hunyuan Chat API.
106106
*/
107-
public HunYuanChatModel(HunYuanApi moonshotApi) {
108-
this(moonshotApi, HunYuanChatOptions.builder().model(HunYuanApi.DEFAULT_CHAT_MODEL).build());
107+
public HunYuanChatModel(HunYuanApi hunYuanApi) {
108+
this(hunYuanApi, HunYuanChatOptions.builder().model(HunYuanApi.DEFAULT_CHAT_MODEL).build());
109109
}
110110

111111
/**
112112
* Initializes a new instance of the HunyuanChatModel.
113-
* @param moonshotApi The Hunyuan instance to be used for interacting with the
113+
* @param hunYuanApi The Hunyuan instance to be used for interacting with the
114114
* Hunyuan Chat API.
115115
* @param options The HunyuanChatOptions to configure the chat client.
116116
*/
117-
public HunYuanChatModel(HunYuanApi moonshotApi, HunYuanChatOptions options) {
118-
this(moonshotApi, options, null, RetryUtils.DEFAULT_RETRY_TEMPLATE);
117+
public HunYuanChatModel(HunYuanApi hunYuanApi, HunYuanChatOptions options) {
118+
this(hunYuanApi, options, null, RetryUtils.DEFAULT_RETRY_TEMPLATE);
119119
}
120120

121121
/**
122122
* Initializes a new instance of the HunyuanChatModel.
123-
* @param moonshotApi The Hunyuan instance to be used for interacting with the
123+
* @param hunYuanApi The Hunyuan instance to be used for interacting with the
124124
* Hunyuan Chat API.
125125
* @param options The HunyuanChatOptions to configure the chat client.
126126
* @param functionCallbackResolver The function callback resolver to resolve the
127127
* function by its name.
128128
* @param retryTemplate The retry template.
129129
*/
130-
public HunYuanChatModel(HunYuanApi moonshotApi, HunYuanChatOptions options,
130+
public HunYuanChatModel(HunYuanApi hunYuanApi, HunYuanChatOptions options,
131131
FunctionCallbackResolver functionCallbackResolver, RetryTemplate retryTemplate) {
132-
this(moonshotApi, options, functionCallbackResolver, List.of(), retryTemplate, ObservationRegistry.NOOP);
132+
this(hunYuanApi, options, functionCallbackResolver, List.of(), retryTemplate, ObservationRegistry.NOOP);
133133
}
134134

135135
/**
136136
* Initializes a new instance of the HunyuanChatModel.
137-
* @param moonshotApi The Hunyuan instance to be used for interacting with the
137+
* @param hunYuanApi The Hunyuan instance to be used for interacting with the
138138
* Hunyuan Chat API.
139139
* @param options The HunyuanChatOptions to configure the chat client.
140140
* @param functionCallbackResolver resolves the function by its name.
141141
* @param toolFunctionCallbacks The tool function callbacks.
142142
* @param retryTemplate The retry template.
143143
* @param observationRegistry The ObservationRegistry used for instrumentation.
144144
*/
145-
public HunYuanChatModel(HunYuanApi moonshotApi, HunYuanChatOptions options,
145+
public HunYuanChatModel(HunYuanApi hunYuanApi, HunYuanChatOptions options,
146146
FunctionCallbackResolver functionCallbackResolver, List<FunctionCallback> toolFunctionCallbacks,
147147
RetryTemplate retryTemplate, ObservationRegistry observationRegistry) {
148148
super(functionCallbackResolver, options, toolFunctionCallbacks);
149-
Assert.notNull(moonshotApi, "HunyuanApi must not be null");
149+
Assert.notNull(hunYuanApi, "HunyuanApi must not be null");
150150
Assert.notNull(options, "Options must not be null");
151151
Assert.notNull(retryTemplate, "RetryTemplate must not be null");
152152
Assert.isTrue(CollectionUtils.isEmpty(options.getFunctionCallbacks()),
153153
"The default function callbacks must be set via the toolFunctionCallbacks constructor parameter");
154154
Assert.notNull(observationRegistry, "ObservationRegistry must not be null");
155-
this.moonshotApi = moonshotApi;
155+
this.hunYuanApi = hunYuanApi;
156156
this.defaultOptions = options;
157157
this.retryTemplate = retryTemplate;
158158
this.observationRegistry = observationRegistry;
@@ -188,7 +188,7 @@ public ChatResponse call(Prompt prompt) {
188188
this.observationRegistry)
189189
.observe(() -> {
190190
ResponseEntity<ChatCompletionResponse> completionEntity = this.retryTemplate
191-
.execute(ctx -> this.moonshotApi.chatCompletionEntity(request));
191+
.execute(ctx -> this.hunYuanApi.chatCompletionEntity(request));
192192

193193
var chatCompletion = completionEntity.getBody().response();
194194

@@ -243,7 +243,7 @@ public Flux<ChatResponse> stream(Prompt prompt) {
243243
ChatCompletionRequest request = createRequest(prompt, true);
244244

245245
Flux<ChatCompletionChunk> completionChunks = this.retryTemplate
246-
.execute(ctx -> this.moonshotApi.chatCompletionStream(request));
246+
.execute(ctx -> this.hunYuanApi.chatCompletionStream(request));
247247

248248
// For chunked responses, only the first chunk contains the choice role.
249249
// The rest of the chunks with same ID share the same role.

models/spring-ai-hunyuan/src/test/java/org/springframework/ai/hunyuan/HunYuanTestConfiguration.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,19 @@
2828
public class HunYuanTestConfiguration {
2929

3030
@Bean
31-
public HunYuanApi moonshotApi() {
32-
var apiKey = System.getenv("MOONSHOT_API_KEY");
33-
if (!StringUtils.hasText(apiKey)) {
31+
public HunYuanApi hunYuanApi() {
32+
var secretId = System.getenv("HUNYUAN_SECRET_ID");
33+
var secretKey = System.getenv("HUNYUAN_SECRET_KEY");
34+
if (!StringUtils.hasText(secretId) && !StringUtils.hasText(secretKey)) {
3435
throw new IllegalArgumentException(
35-
"Missing MOONSHOT_API_KEY environment variable. Please set it to your Moonshot API key.");
36+
"Missing HUNYUAN_SECRET_ID & HUNYUAN_SECRET_KEY environment variable. Please set it to your HUNYUAN API info.");
3637
}
37-
return new HunYuanApi(apiKey,apiKey);
38+
return new HunYuanApi(secretId,secretKey);
3839
}
3940

4041
@Bean
41-
public HunYuanChatModel moonshotChatModel(HunYuanApi moonshotApi) {
42-
return new HunYuanChatModel(moonshotApi);
43-
}
44-
45-
public void tst() {
42+
public HunYuanChatModel hunYuanChatModel(HunYuanApi hunYuanApi) {
43+
return new HunYuanChatModel(hunYuanApi);
4644
}
4745

4846
}

models/spring-ai-hunyuan/src/test/java/org/springframework/ai/hunyuan/aot/HunYuanRuntimeHintsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class HunYuanRuntimeHintsTests {
3737
@Test
3838
void registerHints() {
3939
RuntimeHints runtimeHints = new RuntimeHints();
40-
HunYuanRuntimeHints moonshotRuntimeHints = new HunYuanRuntimeHints();
41-
moonshotRuntimeHints.registerHints(runtimeHints, null);
40+
HunYuanRuntimeHints hunYuanRuntimeHints = new HunYuanRuntimeHints();
41+
hunYuanRuntimeHints.registerHints(runtimeHints, null);
4242

4343
Set<TypeReference> jsonAnnotatedClasses = findJsonAnnotatedClassesInPackage(HunYuanApi.class);
4444
for (TypeReference jsonAnnotatedClass : jsonAnnotatedClasses) {

models/spring-ai-hunyuan/src/test/java/org/springframework/ai/hunyuan/api/HunYuanApiToolFunctionCallIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private void toolFunctionCall(String userMessage, String cityName) {
122122
MockWeatherService.Response weatherResponse = this.weatherService.apply(weatherRequest);
123123

124124
// extend conversation with function response.
125-
messages.add(new ChatCompletionMessage("" + weatherResponse.temp() + weatherRequest.unit(), Role.tool,
125+
messages.add(new ChatCompletionMessage("温度为:" + weatherResponse.temp() + weatherRequest.unit(), Role.tool,
126126
null, toolCall.id(), null));
127127
}
128128
}

models/spring-ai-hunyuan/src/test/java/org/springframework/ai/hunyuan/chat/HunYuanChatModelIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
* @author Geng Rong
5353
*/
5454
@SpringBootTest(classes = HunYuanTestConfiguration.class)
55-
@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".+")
55+
@EnabledIfEnvironmentVariable(named = "HUNYUAN_SECRET_ID", matches = ".+")
56+
@EnabledIfEnvironmentVariable(named = "HUNYUAN_SECRET_KEY", matches = ".+")
5657
public class HunYuanChatModelIT {
5758

5859
private static final Logger logger = LoggerFactory.getLogger(HunYuanChatModelIT.class);

0 commit comments

Comments
 (0)