Skip to content

Commit bb52db5

Browse files
committed
Fix OpenAiChatClientMethodInvokingFunctionCallbackIT
- Update test to check if the exception is thrown when using the method without toolcontext as method argument but with explicit toolcontext values - Add test to verify that exception is not thrown when toolcontext is not set even with the callback method with the toolcontext argument Signed-off-by: Ilayaperumal Gopinathan <ilayaperumal.gopinathan@broadcom.com>
1 parent 2ca1be2 commit bb52db5

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/client/OpenAiChatClientMethodInvokingFunctionCallbackIT.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.util.ReflectionUtils;
3838

3939
import static org.assertj.core.api.Assertions.assertThat;
40+
import static org.assertj.core.api.Assertions.assertThatCode;
4041
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
4142

4243
@SpringBootTest(classes = OpenAiTestConfiguration.class)
@@ -165,12 +166,12 @@ void methodGetWeatherToolContext() {
165166
}
166167

167168
@Test
168-
void methodGetWeatherToolContextButMissingContextArgument() {
169+
void methodGetWeatherNonStaticButWithToolContext() {
169170

170171
TestFunctionClass targetObject = new TestFunctionClass();
171172

172-
var toolMethod = ReflectionUtils.findMethod(TestFunctionClass.class, "getWeatherWithContext", String.class,
173-
Unit.class, ToolContext.class);
173+
var toolMethod = ReflectionUtils.findMethod(TestFunctionClass.class, "getWeatherNonStatic", String.class,
174+
Unit.class);
174175

175176
// @formatter:off
176177
assertThatThrownBy(() -> ChatClient.create(this.chatModel).prompt()
@@ -182,13 +183,37 @@ void methodGetWeatherToolContextButMissingContextArgument() {
182183
.toolMethod(toolMethod)
183184
.toolObject(targetObject)
184185
.build())
186+
.toolContext(Map.of("tool-context", "value"))
185187
.call()
186188
.content())
187189
.isInstanceOf(IllegalArgumentException.class)
188190
.hasMessage("ToolContext is required by the method as an argument");
189191
// @formatter:on
190192
}
191193

194+
@Test
195+
void methodGetWeatherToolContextButWithoutToolContext() {
196+
197+
TestFunctionClass targetObject = new TestFunctionClass();
198+
199+
var toolMethod = ReflectionUtils.findMethod(TestFunctionClass.class, "getWeatherWithContext", String.class,
200+
Unit.class, ToolContext.class);
201+
202+
// @formatter:off
203+
assertThatCode(() ->ChatClient.create(this.chatModel).prompt()
204+
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius.")
205+
.toolCallbacks(MethodToolCallback.builder()
206+
.toolDefinition(ToolDefinitions.builder(toolMethod)
207+
.description("Get the weather in location")
208+
.build())
209+
.toolMethod(toolMethod)
210+
.toolObject(targetObject)
211+
.build())
212+
.call()
213+
.content()).doesNotThrowAnyException();
214+
// @formatter:on
215+
}
216+
192217
@Test
193218
void methodNoParameters() {
194219

0 commit comments

Comments
 (0)