Skip to content

Commit

Permalink
Improve code executions snippets (google-gemini#208)
Browse files Browse the repository at this point in the history
Show that content parts contain the executableCode and executableResult
as separate parts

---------

Co-authored-by: Daymon <[email protected]>
  • Loading branch information
2 people authored and PatilShreyas committed Sep 21, 2024
1 parent a345232 commit 66ec18c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand All @@ -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]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +68,15 @@ void codeExecutionBasic() {
new FutureCallback<GenerateContentResponse>() {
@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);
}
Expand Down Expand Up @@ -108,6 +119,15 @@ void codeExecutionChat() {
new FutureCallback<GenerateContentResponse>() {
@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);
}
Expand Down

0 comments on commit 66ec18c

Please sign in to comment.