Hello, how are you, I hope you are well
-I am an Android application developer and use Java.
-I got kotlin code and couldn’t convert it to java code.
-Can you help me with this?
val model = GenerativeModel(
"gemini-1.5-pro",
BuildConfig.geminiApiKey,
generationConfig = generationConfig {
temperature = 1f
topK = 64
topP = 0.95f
maxOutputTokens = 8192
responseMimeType = "text/plain"
},
systemInstruction = content { text("Your task is to create a complete story") },
)
// MODEL CONFIG BUILDER
// google likes much a builder pattern to make the devs life bit harder …
GenerationConfig.Builder configBuilder = new GenerationConfig.Builder();
configBuilder.temperature = 1f;
configBuilder.topK = 64;
configBuilder.topP = 0.951f;
configBuilder.maxOutputTokens = 8192;
// SAFETY SETTINGS
// for sure not worth mention
List<SafetySetting> safetySettings = new ArrayList();
safetySettings.add(new SafetySetting(HarmCategory.HARASSMENT, BlockThreshold.MEDIUM_AND_ABOVE));
safetySettings.add(new SafetySetting(HarmCategory.HATE_SPEECH, BlockThreshold.MEDIUM_AND_ABOVE));
// MODEL
// OBJECT CREATION by use of CONSTRUCTOR with foolowing parameter types
// <>(String,String,GenerationConfig,List)
GenerativeModel gm = new GenerativeModel(
"gemini-1.5-pro",
BuildConfig.apiKey,
configBuilder.build(), // USE BUILDER AND BUILD CONFIG
safetySettings
);
// DISCLAIMERS:
/**
* I HATE WORD KOTLIN
* I DON'T KNOW KOTLIN
* I DON'T USE ANY THINGS THAT INVOLVES KOTLIN
* BUT THAT DOASN'T STOPED ME FROM CONVERTING ONE SYNTAX TO THE OTHER
* FOR THAT YOU NEED ONLY TO KNOW THAT KOTLIN IS MIX OF GROOVY AND JAVA
* IS NEITHER NECESSARY NOR USEFUL ONLY CREATES CONFUSION AROSE
* BECAUSE SOME PEOPLE ARE TOO BORED IN THEIR DAILY LIVES
*
*/
// HINT for JAVA DEVS if you see any code like
somename {
a = b
}
think of it just as ordinary java pure object
which task is to do a configure other object at the runtime
(mainly the outer object in which body it resides)
1 Like
Thank you very much, but there are still three entries that I don’t know how to enter
entries is:
requestOptions, tools, toolConfig
And i want enter this:
system_instruction="Your task is to create a complete story",
Are you know how can i enter System instructions in code
system_instruction="Your task is to create a complete story",
I don’t found it in documents or examples
already answered
// CREATE FUTURE’s FROM MODEL
GenerativeModelFutures model = GenerativeModelFutures.from(gm);
// CREATE CONTENT (aka your system_instruction) with use of such beloved google BUILDER PATTERN
Content content = new Content.Builder()
.addText("Your task is to create a complete story.")
.build();
// CREATE FUTURE TO CALL ON EXECUTOR
ListenableFuture<GenerateContentResponse> response = model.generateContent(content);
everything is in the tutorial in the provided link
I’m sorry for the annoying
But it’s found in GenerativeModel code
And what you explained is user input ?!!
// CREATE SYSTEM INSTRUCTION - its type is the same as prompt
Content systemInstruction = new Content.Builder()
.addTest("<INSTRUCTION HERE>")
.build();
// HERE IS YOUR REQUEST BUILDER
GenerateContentRequest.Builder requestBuilder = GenerateContentRequest.newBuilder();
requestBuilder.setModel(resourceName)
.addAllContents(contents)
.setGenerationConfig(generationConfig)
.addAllSafetySettings(safetySettings)
.addAllTools(tools);
/// HERE YOU SET THE INSTRUCTION TO BUILDER (which type is Content)
requestBuilder.setSystemInstruction(systemInstructions);
// THEN FINALLY YOU BUILD AND USE THE REQUEST
Thank you very much for your wonderful help, You meant to add SystemInstruction to Content like this
Content content = new Content.Builder()
.addText(getString(R.string.task))
.addText(": ")
.addText(updateStoryInputs())
.build();