From 66fd3ce830feafb2d98bd86c076f600e4337eabf Mon Sep 17 00:00:00 2001 From: Rodrigo Lazo Paz Date: Wed, 17 Jul 2024 15:14:12 -0400 Subject: [PATCH] Improve code executions snippets Show that content parts contain the executableCode and executableResult as separate parts --- .../generative/samples/code_execution.kt | 10 ++++++++++ .../samples/java/code_execution.java | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/samples/src/main/java/com/google/ai/client/generative/samples/code_execution.kt b/samples/src/main/java/com/google/ai/client/generative/samples/code_execution.kt index 7fcbe1e0..7de599e4 100644 --- a/samples/src/main/java/com/google/ai/client/generative/samples/code_execution.kt +++ b/samples/src/main/java/com/google/ai/client/generative/samples/code_execution.kt @@ -39,6 +39,11 @@ suspend fun codeExecutionBasic() { val response = model.generateContent("What is the sum of the first 50 prime numbers?") + // Each `part` either contains `text`, `executable_code` or an `execution_result` + println(response.candidates[0].content.parts.joinToString("\n")) + + // Alternatively, you can use the `text` accessor joins the parts into a markdown compatible + // text representation println(response.text) // [END code_execution_basic] } @@ -58,6 +63,11 @@ suspend fun codeExecutionChat() { val response = chat.sendMessage("What is the sum of the first 50 prime numbers?") + // Each `part` either contains `text`, `executable_code` or an `execution_result` + println(response.candidates[0].content.parts.joinToString("\n")) + + // Alternatively, you can use the `text` accessor joins the parts into a markdown compatible + // text representation println(response.text) // [END code_execution_chat] } \ No newline at end of file diff --git a/samples/src/main/java/com/google/ai/client/generative/samples/java/code_execution.java b/samples/src/main/java/com/google/ai/client/generative/samples/java/code_execution.java index 637f3d83..ef517631 100644 --- a/samples/src/main/java/com/google/ai/client/generative/samples/java/code_execution.java +++ b/samples/src/main/java/com/google/ai/client/generative/samples/java/code_execution.java @@ -18,8 +18,10 @@ import com.google.ai.client.generativeai.GenerativeModel; import com.google.ai.client.generativeai.java.ChatFutures; import com.google.ai.client.generativeai.java.GenerativeModelFutures; +import com.google.ai.client.generativeai.type.Candidate; import com.google.ai.client.generativeai.type.Content; import com.google.ai.client.generativeai.type.GenerateContentResponse; +import com.google.ai.client.generativeai.type.Part; import com.google.ai.client.generativeai.type.RequestOptions; import com.google.ai.client.generativeai.type.Tool; import com.google.common.util.concurrent.FutureCallback; @@ -66,6 +68,15 @@ void codeExecutionBasic() { new FutureCallback() { @Override public void onSuccess(GenerateContentResponse result) { + // Each `part` either contains `text`, `executable_code` or an + // `execution_result` + Candidate candidate = result.getCandidates().get(0); + for (Part part : candidate.getContent().getParts()) { + System.out.println(part); + } + + // Alternatively, you can use the `text` accessor joins the parts into a + // markdown compatible text representation String resultText = result.getText(); System.out.println(resultText); } @@ -108,6 +119,15 @@ void codeExecutionChat() { new FutureCallback() { @Override public void onSuccess(GenerateContentResponse result) { + // Each `part` either contains `text`, `executable_code` or an + // `execution_result` + Candidate candidate = result.getCandidates().get(0); + for (Part part : candidate.getContent().getParts()) { + System.out.println(part); + } + + // Alternatively, you can use the `text` accessor joins the parts into a + // markdown compatible text representation String resultText = result.getText(); System.out.println(resultText); }