Skip to content

Commit 4f4c2d0

Browse files
committed
Move the tests into MoonshotChatModelFunctionCallingIT
1 parent b61309f commit 4f4c2d0

File tree

3 files changed

+77
-177
lines changed

3 files changed

+77
-177
lines changed

models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/MoonshotChatModel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,10 @@ private ChatResponseMetadata from(ChatCompletion result) {
345345
private ChatResponseMetadata from(ChatCompletion result, Usage usage) {
346346
Assert.notNull(result, "Moonshot ChatCompletionResult must not be null");
347347
return ChatResponseMetadata.builder()
348-
.withId(result.id() != null ? result.id() : "")
349-
.withUsage(usage)
350-
.withModel(result.model() != null ? result.model() : "")
351-
.withKeyValue("created", result.created() != null ? result.created() : 0L)
348+
.id(result.id() != null ? result.id() : "")
349+
.usage(usage)
350+
.model(result.model() != null ? result.model() : "")
351+
.keyValue("created", result.created() != null ? result.created() : 0L)
352352
.build();
353353
}
354354

models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonShotChatModelIT.java

Lines changed: 0 additions & 172 deletions
This file was deleted.

models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/chat/MoonshotChatModelFunctionCallingIT.java

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.ai.moonshot.chat;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.List;
2122
import java.util.Objects;
2223
import java.util.stream.Collectors;
@@ -53,6 +54,33 @@ class MoonshotChatModelFunctionCallingIT {
5354
@Autowired
5455
ChatModel chatModel;
5556

57+
private static final MoonshotApi.FunctionTool FUNCTION_TOOL = new MoonshotApi.FunctionTool(
58+
MoonshotApi.FunctionTool.Type.FUNCTION, new MoonshotApi.FunctionTool.Function(
59+
"Get the weather in location. Return temperature in 30°F or 30°C format.", "getCurrentWeather", """
60+
{
61+
"type": "object",
62+
"properties": {
63+
"location": {
64+
"type": "string",
65+
"description": "The city and state e.g. San Francisco, CA"
66+
},
67+
"lat": {
68+
"type": "number",
69+
"description": "The city latitude"
70+
},
71+
"lon": {
72+
"type": "number",
73+
"description": "The city longitude"
74+
},
75+
"unit": {
76+
"type": "string",
77+
"enum": ["C", "F"]
78+
}
79+
},
80+
"required": ["location", "lat", "lon", "unit"]
81+
}
82+
"""));
83+
5684
@Test
5785
void functionCallTest() {
5886

@@ -89,6 +117,7 @@ void streamFunctionCallTest() {
89117
.functionCallbacks(List.of(FunctionCallback.builder()
90118
.function("getCurrentWeather", new MockWeatherService())
91119
.description("Get the weather in location")
120+
.inputType(MockWeatherService.Request.class)
92121
.build()))
93122
.build();
94123

@@ -108,4 +137,47 @@ void streamFunctionCallTest() {
108137
assertThat(content).contains("30", "10", "15");
109138
}
110139

140+
@Test
141+
public void toolFunctionCallWithUsage() {
142+
var promptOptions = MoonshotChatOptions.builder()
143+
.model(MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue())
144+
.tools(Arrays.asList(FUNCTION_TOOL))
145+
.functionCallbacks(List.of(FunctionCallback.builder()
146+
.function("getCurrentWeather", new MockWeatherService())
147+
.description("Get the weather in location. Return temperature in 36°F or 36°C format.")
148+
.inputType(MockWeatherService.Request.class)
149+
.build()))
150+
.build();
151+
Prompt prompt = new Prompt("What's the weather like in San Francisco? Return the temperature in Celsius.",
152+
promptOptions);
153+
154+
ChatResponse chatResponse = this.chatModel.call(prompt);
155+
assertThat(chatResponse).isNotNull();
156+
assertThat(chatResponse.getResult().getOutput());
157+
assertThat(chatResponse.getResult().getOutput().getText()).contains("San Francisco");
158+
assertThat(chatResponse.getResult().getOutput().getText()).contains("30.0");
159+
assertThat(chatResponse.getMetadata().getUsage().getTotalTokens()).isLessThan(450).isGreaterThan(280);
160+
}
161+
162+
@Test
163+
public void testStreamFunctionCallUsage() {
164+
var promptOptions = MoonshotChatOptions.builder()
165+
.model(MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue())
166+
.tools(Arrays.asList(FUNCTION_TOOL))
167+
.functionCallbacks(List.of(FunctionCallback.builder()
168+
.function("getCurrentWeather", new MockWeatherService())
169+
.description("Get the weather in location. Return temperature in 36°F or 36°C format.")
170+
.inputType(MockWeatherService.Request.class)
171+
.build()))
172+
.build();
173+
Prompt prompt = new Prompt("What's the weather like in San Francisco? Return the temperature in Celsius.",
174+
promptOptions);
175+
176+
ChatResponse chatResponse = this.chatModel.stream(prompt).blockLast();
177+
assertThat(chatResponse).isNotNull();
178+
assertThat(chatResponse.getMetadata()).isNotNull();
179+
assertThat(chatResponse.getMetadata().getUsage()).isNotNull();
180+
assertThat(chatResponse.getMetadata().getUsage().getTotalTokens()).isLessThan(450).isGreaterThan(280);
181+
}
182+
111183
}

0 commit comments

Comments
 (0)