Skip to content

Adapt tool_calls and support xinghuo large model #1734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -360,8 +360,8 @@ public Flux<ChatResponse> stream(Prompt prompt) {
* @return true if the generation is a tool call
*/
@Override
protected boolean isToolCall(Generation generation, Set<String> toolCallFinishReasons) {
if (!super.isToolCall(generation, toolCallFinishReasons)) {
protected boolean isToolCall(Generation generation, Set<String> toolCallFinishReasons, ChatResponse chatResponse) {
if (!super.isToolCall(generation, toolCallFinishReasons, chatResponse)) {
return false;
}
return generation.getOutput()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,12 @@ else if (message.getMessageType() == MessageType.ASSISTANT) {
else if (message.getMessageType() == MessageType.TOOL) {
ToolResponseMessage toolMessage = (ToolResponseMessage) message;

toolMessage.getResponses()
.forEach(response -> Assert.isTrue(response.id() != null, "ToolResponseMessage must have an id"));
if (toolMessage.getResponses().size() > 1) {
toolMessage.getResponses()
.forEach(response -> Assert.isTrue(response.id() != null,
"ToolResponseMessage must have an id"));
}

return toolMessage.getResponses()
.stream()
.map(tr -> new ChatCompletionMessage(tr.responseData(), ChatCompletionMessage.Role.TOOL, tr.name(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.ToolResponseMessage;
import org.springframework.ai.chat.metadata.ChatGenerationMetadata;
import org.springframework.ai.chat.metadata.Usage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.function.FunctionCallback;
import org.springframework.ai.model.function.FunctionCallbackContext;
Expand Down Expand Up @@ -227,23 +229,25 @@ protected boolean isToolCall(ChatResponse chatResponse, Set<String> toolCallFini
return false;
}

return generations.stream().anyMatch(g -> isToolCall(g, toolCallFinishReasons));
return generations.stream().anyMatch(g -> isToolCall(g, toolCallFinishReasons, chatResponse));
}

/**
* Check if the generation is a tool call. The tool call finish reasons are used to
* determine if the generation is a tool call.
* @param generation the generation to check.
* @param toolCallFinishReasons the tool call finish reasons to check.
* @param chatResponse the chat response to check.
* @return true if the generation is a tool call, false otherwise.
*/
protected boolean isToolCall(Generation generation, Set<String> toolCallFinishReasons) {
protected boolean isToolCall(Generation generation, Set<String> toolCallFinishReasons, ChatResponse chatResponse) {
var finishReason = (generation.getMetadata().getFinishReason() != null)
? generation.getMetadata().getFinishReason() : "";
return generation.getOutput().hasToolCalls() && toolCallFinishReasons.stream()
Usage usage = chatResponse.getMetadata().getUsage();
return generation.getOutput().hasToolCalls() && (toolCallFinishReasons.stream()
.map(s -> s.toLowerCase())
.toList()
.contains(finishReason.toLowerCase());
.contains(finishReason.toLowerCase()) || usage != null);
}

/**
Expand Down