Skip to content
Closed
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 @@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
Expand Down Expand Up @@ -600,39 +601,27 @@ protected List<Generation> responseCandidateToGeneration(Candidate candidate) {
.finishReason(candidateFinishReason.name())
.build();

boolean isFunctionCall = candidate.getContent().getPartsList().stream().allMatch(Part::hasFunctionCall);
List<Part> parts = candidate.getContent().getPartsList();

if (isFunctionCall) {
List<AssistantMessage.ToolCall> assistantToolCalls = candidate.getContent()
.getPartsList()
.stream()
.filter(part -> part.hasFunctionCall())
.map(part -> {
FunctionCall functionCall = part.getFunctionCall();
var functionName = functionCall.getName();
String functionArguments = structToJson(functionCall.getArgs());
return new AssistantMessage.ToolCall("", "function", functionName, functionArguments);
})
.toList();
List<AssistantMessage.ToolCall> assistantToolCalls = parts.stream().filter(Part::hasFunctionCall).map(part -> {
FunctionCall functionCall = part.getFunctionCall();
var functionName = functionCall.getName();
String functionArguments = structToJson(functionCall.getArgs());
return new AssistantMessage.ToolCall("", "function", functionName, functionArguments);
}).toList();

AssistantMessage assistantMessage = AssistantMessage.builder()
.content("")
.properties(messageMetadata)
.toolCalls(assistantToolCalls)
.build();
String text = parts.stream()
.filter(part -> part.hasText() && !part.getText().isEmpty())
.map(Part::getText)
.collect(Collectors.joining(" "));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't make sense to me to be turning one candidate into multiple generations, but perhaps I'm misunderstanding?

Previous behaviour collected all parts containing function tools and combined into one AssistantMessage; idea here was to take the same approach here if multiple parts contain text.


return List.of(new Generation(assistantMessage, chatGenerationMetadata));
}
else {
List<Generation> generations = candidate.getContent()
.getPartsList()
.stream()
.map(part -> AssistantMessage.builder().content(part.getText()).properties(messageMetadata).build())
.map(assistantMessage -> new Generation(assistantMessage, chatGenerationMetadata))
.toList();
AssistantMessage assistantMessage = AssistantMessage.builder()
.content(text)
.properties(messageMetadata)
.toolCalls(assistantToolCalls)
.build();

return generations;
}
return List.of(new Generation(assistantMessage, chatGenerationMetadata));
}

private ChatResponseMetadata toChatResponseMetadata(Usage usage) {
Expand Down