Skip to content

Commit 8aaf132

Browse files
committed
UT for IsToolExecutionRequiredWithAttempts
Signed-off-by: lambochen <[email protected]>
1 parent 0ec9490 commit 8aaf132

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

spring-ai-model/src/main/java/org/springframework/ai/model/tool/ToolExecutionEligibilityPredicate.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ default boolean isToolExecutionRequired(ChatOptions promptOptions, ChatResponse
4343
return test(promptOptions, chatResponse);
4444
}
4545

46+
/**
47+
* Determines if tool execution should be performed based on the prompt options and chat response and the number of attempts.
48+
* @param promptOptions The options from the prompt
49+
* @param chatResponse The response from the chat model
50+
* @param attempts The number of attempts
51+
* @return true if tool execution should be performed, false otherwise
52+
* @see ToolCallingChatOptions#getInternalToolExecutionMaxAttempts()
53+
* @see #isToolExecutionRequired(ChatOptions, ChatResponse)
54+
*/
4655
default boolean isToolExecutionRequired(ChatOptions promptOptions, ChatResponse chatResponse, int attempts) {
4756
boolean isToolExecutionRequired = isToolExecutionRequired(promptOptions, chatResponse);
4857
if (!isToolExecutionRequired) {

spring-ai-model/src/test/java/org/springframework/ai/model/tool/ToolExecutionEligibilityPredicateTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ void whenIsToolExecutionRequiredWithNullPromptOptions() {
4545
.hasMessageContaining("promptOptions cannot be null");
4646
}
4747

48+
@Test
49+
void whenIsToolExecutionRequiredWithAttempts() {
50+
ToolExecutionEligibilityPredicate predicate = new TestToolExecutionEligibilityPredicate();
51+
ToolCallingChatOptions promptOptions = ToolCallingChatOptions.builder().build();
52+
ChatResponse chatResponse = new ChatResponse(List.of(new Generation(new AssistantMessage("test"))));
53+
promptOptions.setInternalToolExecutionMaxAttempts(2);
54+
55+
assertThat(predicate.isToolExecutionRequired(promptOptions, chatResponse, 1)).isTrue();
56+
assertThat(predicate.isToolExecutionRequired(promptOptions, chatResponse, 2)).isTrue();
57+
58+
// attempts value is oversize
59+
assertThat(predicate.isToolExecutionRequired(promptOptions, chatResponse, 3)).isFalse();
60+
}
61+
4862
@Test
4963
void whenIsToolExecutionRequiredWithNullChatResponse() {
5064
ToolExecutionEligibilityPredicate predicate = new TestToolExecutionEligibilityPredicate();

0 commit comments

Comments
 (0)