I need a help for convert code to java

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?

  • The code is:
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 :smiley: 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 :blush:

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
Screenshot_1

entries is:
requestOptions, tools, toolConfig

And i want enter this:

system_instruction="Your task is to create a complete story",
  1. if You want to code and be good dev you need to read
    start from here [ keep the eyes around the magick java in exaple url - it will help you fight kotlin :stuck_out_tongue: ]
    Google AI Client SDK  |  Android Developers
  2. you dont need to use the biggest in args constructor - use the one from my example
  3. about the system_instruction see the Content.Builder class
  4. and read read read that doeasn’t hurt :smiley: then you will get it by yourself !
1 Like

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 :smiley:
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();