|
28 | 28 | import org.springframework.ai.chat.messages.Message; |
29 | 29 | import org.springframework.ai.chat.messages.ToolResponseMessage; |
30 | 30 | import org.springframework.ai.chat.metadata.ChatGenerationMetadata; |
| 31 | +import org.springframework.ai.chat.metadata.Usage; |
31 | 32 | import org.springframework.ai.chat.prompt.Prompt; |
32 | 33 | import org.springframework.ai.model.function.FunctionCallback; |
33 | 34 | import org.springframework.ai.model.function.FunctionCallbackContext; |
@@ -228,24 +229,25 @@ protected boolean isToolCall(ChatResponse chatResponse, Set<String> toolCallFini |
228 | 229 | return false; |
229 | 230 | } |
230 | 231 |
|
231 | | - return generations.stream().anyMatch(g -> isToolCall(g, toolCallFinishReasons)); |
| 232 | + return generations.stream().anyMatch(g -> isToolCall(g, toolCallFinishReasons, chatResponse)); |
232 | 233 | } |
233 | 234 |
|
234 | 235 | /** |
235 | 236 | * Check if the generation is a tool call. The tool call finish reasons are used to |
236 | 237 | * determine if the generation is a tool call. |
237 | 238 | * @param generation the generation to check. |
238 | 239 | * @param toolCallFinishReasons the tool call finish reasons to check. |
| 240 | + * @param chatResponse the chat response to check. |
239 | 241 | * @return true if the generation is a tool call, false otherwise. |
240 | 242 | */ |
241 | | - protected boolean isToolCall(Generation generation, Set<String> toolCallFinishReasons) { |
| 243 | + protected boolean isToolCall(Generation generation, Set<String> toolCallFinishReasons, ChatResponse chatResponse) { |
242 | 244 | var finishReason = (generation.getMetadata().getFinishReason() != null) |
243 | 245 | ? generation.getMetadata().getFinishReason() : ""; |
244 | | - ChatGenerationMetadata metadata = generation.getMetadata(); |
| 246 | + Usage usage = chatResponse.getMetadata().getUsage(); |
245 | 247 | return generation.getOutput().hasToolCalls() && (toolCallFinishReasons.stream() |
246 | 248 | .map(s -> s.toLowerCase()) |
247 | 249 | .toList() |
248 | | - .contains(finishReason.toLowerCase()) || metadata != null); |
| 250 | + .contains(finishReason.toLowerCase()) || usage != null); |
249 | 251 | } |
250 | 252 |
|
251 | 253 | /** |
|
0 commit comments