Skip to content

Call Gemini function even when text part is returned #3512

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 1 commit 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 @@ -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 @@ -587,35 +588,27 @@ protected List<Generation> responseCandidateToGeneration(Candidate candidate) {
.finishReason(candidateFinishReason.name())
.build();

boolean isFunctionCall = candidate.getContent().getPartsList().stream().allMatch(Part::hasFunctionCall);

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();
var assistantToolCalls = candidate.getContent()
.getPartsList()
.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 = new AssistantMessage("", messageMetadata, assistantToolCalls);
var text = candidate.getContent()
.getPartsList()
.stream()
.filter(Part::hasText)
.map(Part::getText)
.collect(Collectors.joining(System.lineSeparator()));

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

return generations;
}
return List.of(new Generation(new AssistantMessage(text, messageMetadata, assistantToolCalls),
chatGenerationMetadata));
}

private ChatResponseMetadata toChatResponseMetadata(Usage usage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,30 @@ public void functionCallExplicitOpenApiSchema() {
assertThat(response.getResult().getOutput().getText()).contains("30", "10", "15");
}

@Test
public void functionCallModelReturnsMixedTextAndFunctionCallParts() {
UserMessage userMessage = new UserMessage(
"What can you tell me about the temperature in San Francisco, Paris and in Tokyo? Return the temperature in Celsius. Expose your thinking process.");

List<Message> messages = new ArrayList<>(List.of(userMessage));

var promptOptions = VertexAiGeminiChatOptions.builder()
.model(VertexAiGeminiChatModel.ChatModel.GEMINI_2_0_FLASH)
.toolCallbacks(List.of(FunctionToolCallback.builder("get_current_weather", new MockWeatherService())
.description("Get the current weather in a given location.")
.inputType(MockWeatherService.Request.class)
.build()))
.build();

ChatResponse chatResponse = this.chatModel.call(new Prompt(messages, promptOptions));

assertThat(chatResponse.getResult().getOutput().getText()).contains("30", "10", "15");

assertThat(chatResponse.getMetadata()).isNotNull();
assertThat(chatResponse.getMetadata().getUsage()).isNotNull();
assertThat(chatResponse.getMetadata().getUsage().getTotalTokens()).isGreaterThan(150).isLessThan(550);
}

@Test
public void functionCallTestInferredOpenApiSchema() {

Expand Down