2222import com .google .cloud .vertexai .api .Citation ;
2323import com .google .cloud .vertexai .api .CitationMetadata ;
2424import com .google .cloud .vertexai .api .Content ;
25+ import com .google .cloud .vertexai .api .FunctionCall ;
2526import com .google .cloud .vertexai .api .GenerateContentResponse ;
2627import com .google .cloud .vertexai .api .Part ;
28+ import com .google .common .collect .ImmutableList ;
2729import java .util .ArrayList ;
2830import java .util .HashMap ;
2931import java .util .List ;
3335public class ResponseHandler {
3436
3537 /**
36- * Get the text message in a GenerateContentResponse.
38+ * Gets the text message in a GenerateContentResponse.
3739 *
3840 * @param response a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance
3941 * @return a String that aggregates all the text parts in the response
4042 * @throws IllegalArgumentException if the response has 0 or more than 1 candidates, or if the
4143 * response is blocked by safety reason or unauthorized citations
4244 */
4345 public static String getText (GenerateContentResponse response ) {
44- FinishReason finishReason = getFinishReason (response );
45- if (finishReason == FinishReason .SAFETY ) {
46- throw new IllegalArgumentException ("The response is blocked due to safety reason." );
47- } else if (finishReason == FinishReason .RECITATION ) {
48- throw new IllegalArgumentException ("The response is blocked due to unauthorized citations." );
49- }
46+ checkFinishReason (getFinishReason (response ));
5047
5148 String text = "" ;
5249 List <Part > parts = response .getCandidates (0 ).getContent ().getPartsList ();
@@ -58,26 +55,40 @@ public static String getText(GenerateContentResponse response) {
5855 }
5956
6057 /**
61- * Get the content in a GenerateContentResponse.
58+ * Gets the list of function calls in a GenerateContentResponse.
59+ *
60+ * @param response a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance
61+ * @return a list of {@link com.google.cloud.vertexai.api.FunctionCall} in the response
62+ * @throws IllegalArgumentException if the response has 0 or more than 1 candidates, or if the
63+ * response is blocked by safety reason or unauthorized citations
64+ */
65+ public static ImmutableList <FunctionCall > getFunctionCalls (GenerateContentResponse response ) {
66+ checkFinishReason (getFinishReason (response ));
67+ if (response .getCandidatesCount () == 0 ) {
68+ return ImmutableList .of ();
69+ }
70+ return response .getCandidates (0 ).getContent ().getPartsList ().stream ()
71+ .filter ((part ) -> part .hasFunctionCall ())
72+ .map ((part ) -> part .getFunctionCall ())
73+ .collect (ImmutableList .toImmutableList ());
74+ }
75+
76+ /**
77+ * Gets the content in a GenerateContentResponse.
6278 *
6379 * @param response a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance
6480 * @return the {@link com.google.cloud.vertexai.api.Content} in the response
6581 * @throws IllegalArgumentException if the response has 0 or more than 1 candidates, or if the
6682 * response is blocked by safety reason or unauthorized citations
6783 */
6884 public static Content getContent (GenerateContentResponse response ) {
69- FinishReason finishReason = getFinishReason (response );
70- if (finishReason == FinishReason .SAFETY ) {
71- throw new IllegalArgumentException ("The response is blocked due to safety reason." );
72- } else if (finishReason == FinishReason .RECITATION ) {
73- throw new IllegalArgumentException ("The response is blocked due to unauthorized citations." );
74- }
85+ checkFinishReason (getFinishReason (response ));
7586
7687 return response .getCandidates (0 ).getContent ();
7788 }
7889
7990 /**
80- * Get the finish reason in a GenerateContentResponse.
91+ * Gets the finish reason in a GenerateContentResponse.
8192 *
8293 * @param response a {@link com.google.cloud.vertexai.api.GenerateContentResponse} instance
8394 * @return the {@link com.google.cloud.vertexai.api.FinishReason} in the response
@@ -93,7 +104,7 @@ public static FinishReason getFinishReason(GenerateContentResponse response) {
93104 return response .getCandidates (0 ).getFinishReason ();
94105 }
95106
96- /** Aggregate a stream of responses into a single GenerateContentResponse. */
107+ /** Aggregates a stream of responses into a single GenerateContentResponse. */
97108 static GenerateContentResponse aggregateStreamIntoResponse (
98109 ResponseStream <GenerateContentResponse > responseStream ) {
99110 GenerateContentResponse res = GenerateContentResponse .getDefaultInstance ();
@@ -170,4 +181,12 @@ static GenerateContentResponse aggregateStreamIntoResponse(
170181
171182 return res ;
172183 }
184+
185+ private static void checkFinishReason (FinishReason finishReason ) {
186+ if (finishReason == FinishReason .SAFETY ) {
187+ throw new IllegalArgumentException ("The response is blocked due to safety reason." );
188+ } else if (finishReason == FinishReason .RECITATION ) {
189+ throw new IllegalArgumentException ("The response is blocked due to unauthorized citations." );
190+ }
191+ }
173192}
0 commit comments