From 13a724b1ee156e0b1b9b9dd88245d0c968f7272d Mon Sep 17 00:00:00 2001 From: "copybara-service[bot]" <56741989+copybara-service[bot]@users.noreply.github.com> Date: Mon, 3 Jun 2024 16:35:12 -0700 Subject: [PATCH] fix: [vertexai] modify ResponseHandler.getFunctionCalls to not throw for finish reasons (#10909) PiperOrigin-RevId: 639135803 Co-authored-by: Jaycee Li --- .../cloud/vertexai/generativeai/ResponseHandler.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ResponseHandler.java b/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ResponseHandler.java index 44cca9bd4925..129c80d6b6e1 100644 --- a/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ResponseHandler.java +++ b/java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ResponseHandler.java @@ -17,7 +17,6 @@ package com.google.cloud.vertexai.generativeai; import com.google.cloud.vertexai.api.Candidate; -import com.google.cloud.vertexai.api.Candidate.Builder; import com.google.cloud.vertexai.api.Candidate.FinishReason; import com.google.cloud.vertexai.api.Citation; import com.google.cloud.vertexai.api.CitationMetadata; @@ -59,14 +58,10 @@ public static String getText(GenerateContentResponse response) { * * @param response a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance * @return a list of {@link com.google.cloud.vertexai.api.FunctionCall} in the response - * @throws IllegalArgumentException if the response has 0 or more than 1 candidates, or if the - * response is blocked by safety reason or unauthorized citations + * @throws IllegalArgumentException if the response has 0 or more than 1 candidates */ public static ImmutableList getFunctionCalls(GenerateContentResponse response) { - checkFinishReason(getFinishReason(response)); - if (response.getCandidatesCount() == 0) { - return ImmutableList.of(); - } + getFinishReason(response); return response.getCandidates(0).getContent().getPartsList().stream() .filter((part) -> part.hasFunctionCall()) .map((part) -> part.getFunctionCall()) @@ -162,7 +157,7 @@ static GenerateContentResponse aggregateStreamIntoResponse( List aggregatedCandidates = new ArrayList<>(); candidates.forEach( (index, candidate) -> { - Builder candidateBuilder = candidate.toBuilder(); + Candidate.Builder candidateBuilder = candidate.toBuilder(); if (aggregatedContentParts.containsKey(index)) { candidateBuilder.setContent( Content.newBuilder()