Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@ void methodGetWeatherToolContext() {
}

@Test
void methodGetWeatherToolContextButNonContextMethod() {
void methodGetWeatherWithContextMethodButMissingContext() {

TestFunctionClass targetObject = new TestFunctionClass();

// @formatter:off
var toolMethod = ReflectionUtils.findMethod(
TestFunctionClass.class, "getWeatherNonStatic", String.class, Unit.class);
TestFunctionClass.class, "getWeatherWithContext", String.class, Unit.class, ToolContext.class);

assertThatThrownBy(() -> ChatClient.create(this.chatModel).prompt()
.user("What's the weather like in San Francisco, Tokyo, and Paris? Use Celsius.")
Expand All @@ -209,11 +209,10 @@ void methodGetWeatherToolContextButNonContextMethod() {
.toolMethod(toolMethod)
.toolObject(targetObject)
.build())
.toolContext(Map.of("tool", "value"))
.call()
.content())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("ToolContext is not supported by the method as an argument");
.hasMessage("ToolContext is required by the method as an argument");
// @formatter:on
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ void methodGetWeatherToolContext() {
}

@Test
void methodGetWeatherToolContextButNonContextMethod() {
void methodGetWeatherToolContextButMissingContextArgument() {

TestFunctionClass targetObject = new TestFunctionClass();

var toolMethod = ReflectionUtils.findMethod(TestFunctionClass.class, "getWeatherNonStatic", String.class,
Unit.class);
var toolMethod = ReflectionUtils.findMethod(TestFunctionClass.class, "getWeatherWithContext", String.class,
Unit.class, ToolContext.class);

// @formatter:off
assertThatThrownBy(() -> ChatClient.create(this.chatModel).prompt()
Expand All @@ -181,12 +181,11 @@ void methodGetWeatherToolContextButNonContextMethod() {
.build())
.toolMethod(toolMethod)
.toolObject(targetObject)
.build())
.toolContext(Map.of("tool", "value"))
.build())
.call()
.content())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("ToolContext is not supported by the method as an argument");
.hasMessage("ToolContext is required by the method as an argument");
// @formatter:on
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ public String call(String toolInput, @Nullable ToolContext toolContext) {
}

private void validateToolContextSupport(@Nullable ToolContext toolContext) {
var isToolContextRequired = toolContext != null && !CollectionUtils.isEmpty(toolContext.getContext());
var isNonEmptyToolContextProvided = toolContext != null && !CollectionUtils.isEmpty(toolContext.getContext());
var isToolContextAcceptedByMethod = Stream.of(toolMethod.getParameterTypes())
.anyMatch(type -> ClassUtils.isAssignable(type, ToolContext.class));
if (isToolContextRequired && !isToolContextAcceptedByMethod) {
throw new IllegalArgumentException("ToolContext is not supported by the method as an argument");
if (isToolContextAcceptedByMethod && !isNonEmptyToolContextProvided) {
throw new IllegalArgumentException("ToolContext is required by the method as an argument");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,21 @@ void shouldHandleToolContextWhenSupported() {
}

@Test
void shouldThrowExceptionWhenToolContextNotSupported() {
Method toolMethod = getMethod("publicMethod", PublicTools.class);
void shouldThrowExceptionWhenToolContextArgumentIsMissing() {
Method toolMethod = getMethod("methodWithToolContext", ToolContextTools.class);
MethodToolCallback callback = MethodToolCallback.builder()
.toolDefinition(ToolDefinition.from(toolMethod))
.toolMetadata(ToolMetadata.from(toolMethod))
.toolMethod(toolMethod)
.toolObject(new PublicTools())
.build();

ToolContext toolContext = new ToolContext(Map.of("key", "value"));

assertThatThrownBy(() -> callback.call("""
{
"input": "test"
}
""", toolContext)).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("ToolContext is not supported");
""")).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("ToolContext is required by the method as an argument");
}

@Test
Expand Down