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..1f9a74f2 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 which 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 which 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..fa2c56ed 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 which 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 which joins the parts into a + // markdown compatible text representation String resultText = result.getText(); System.out.println(resultText); }