Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code executions snippets #208

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading