Skip to content

Commit

Permalink
Code execution sample for java
Browse files Browse the repository at this point in the history
  • Loading branch information
rlazo committed Jul 16, 2024
1 parent 37a3eb5 commit 85339f9
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,22 @@ import org.json.JSONObject
*/
class FunctionType<T>(val name: String, val parse: (String?) -> T?) {
companion object {
@JvmField
val STRING = FunctionType<String>("STRING") { it }
@JvmField
val INTEGER = FunctionType<Int>("INTEGER") { it?.toIntOrNull() }
@JvmField
val LONG = FunctionType<Long>("INTEGER") { it?.toLongOrNull() }
@JvmField
val NUMBER = FunctionType<Double>("NUMBER") { it?.toDoubleOrNull() }
@JvmField
val BOOLEAN = FunctionType<Boolean>("BOOLEAN") { it?.toBoolean() }
@JvmField
val ARRAY =
FunctionType<List<String>>("ARRAY") { it ->
it?.let { Json.parseToJsonElement(it).jsonArray.map { element -> element.toString() } }
}
@JvmField
val OBJECT = FunctionType<JSONObject>("OBJECT") { it?.let { JSONObject(it) } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ suspend fun json_controlled_generation() {

suspend fun json_no_schema() {
// [START json_no_schema]

val generativeModel =
GenerativeModel(
// Specify a Gemini model appropriate for your use case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void codeExecutionBasic() {
/* generationConfig */ null,
/* safetySettings */ null,
/* requestOptions */ new RequestOptions(),
/* tools */ Collections.singletonList(Tool.Companion.getCODE_EXECUTION()));
/* tools */ Collections.singletonList(Tool.CODE_EXECUTION));
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Content inputContent =
Expand Down Expand Up @@ -91,7 +91,7 @@ void codeExecutionChat() {
/* generationConfig */ null,
/* safetySettings */ null,
/* requestOptions */ new RequestOptions(),
/* tools */ Collections.singletonList(Tool.Companion.getCODE_EXECUTION()));
/* tools */ Collections.singletonList(Tool.CODE_EXECUTION));
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Content inputContent =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,141 @@
// the "Set up your API Key section" in the [Gemini API
// quickstart](https://ai.google.dev/gemini-api/docs/quickstart?lang=android#set-up-api-key).

import com.google.ai.client.generativeai.GenerativeModel;
import com.google.ai.client.generativeai.java.GenerativeModelFutures;
import com.google.ai.client.generativeai.type.Content;
import com.google.ai.client.generativeai.type.FunctionType;
import com.google.ai.client.generativeai.type.GenerateContentResponse;
import com.google.ai.client.generativeai.type.GenerationConfig;
import com.google.ai.client.generativeai.type.Schema;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

class ControlledGeneration {
// TODO
void jsonControlledGeneration() {
// [START json_controlled_generation]

Schema<List<String>> schema =
new Schema(
/* name */ "recipes",
/* description */ "List of recipes",
/* format */ null,
/* nullable */ false,
/* list */ null,
/* properties */ null,
/* required */ null,
/* items */ new Schema(
/* name */ "recipe",
/* description */ "A recipe",
/* format */ null,
/* nullable */ false,
/* list */ null,
/* properties */ Map.of(
"recipeName",
new Schema(
/* name */ "recipeName",
/* description */ "Name of the recipe",
/* format */ null,
/* nullable */ false,
/* list */ null,
/* properties */ null,
/* required */ null,
/* items */ null,
/* type */ FunctionType.STRING)),
/* required */ null,
/* items */ null,
/* type */ FunctionType.OBJECT),
/* type */ FunctionType.ARRAY);

GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.responseMimeType = "application/json";
configBuilder.responseSchema = schema;

GenerationConfig generationConfig = configBuilder.build();

// Specify a Gemini model appropriate for your use case
GenerativeModel gm =
new GenerativeModel(
/* modelName */ "gemini-1.5-pro",
// Access your API key as a Build Configuration variable (see "Set up your API key"
// above)
/* apiKey */ BuildConfig.apiKey,
/* generationConfig */ generationConfig);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Content content = new Content.Builder().addText("List a few popular cookie recipes.").build();

// For illustrative purposes only. You should use an executor that fits your needs.
Executor executor = Executors.newSingleThreadExecutor();

ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(
response,
new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}

@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
},
executor);
// [END json_controlled_generation]
}

void json_no_schema() {
// [START json_no_schema]
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.responseMimeType = "application/json";

GenerationConfig generationConfig = configBuilder.build();

// Specify a Gemini model appropriate for your use case
GenerativeModel gm =
new GenerativeModel(
/* modelName */ "gemini-1.5-flash",
// Access your API key as a Build Configuration variable (see "Set up your API key"
// above)
/* apiKey */ BuildConfig.apiKey,
/* generationConfig */ generationConfig);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Content content =
new Content.Builder()
.addText(
"List a few popular cookie recipes using this JSON schema:\n"
+ "Recipe = {'recipeName': string}\n"
+ "Return: Array<Recipe>")
.build();

// For illustrative purposes only. You should use an executor that fits your needs.
Executor executor = Executors.newSingleThreadExecutor();

ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
Futures.addCallback(
response,
new FutureCallback<GenerateContentResponse>() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}

@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
},
executor);
// [END json_no_schema]
}
}

0 comments on commit 85339f9

Please sign in to comment.