Skip to content
Snippets Groups Projects
Select Git revision
  • 2e563863779d9024edc35cdb78ed1608d1926f63
  • master default protected
2 results

Main.java

Blame
  • Main.java 2.43 KiB
    package com.example;
    
    import com.google.cloud.vertexai.VertexAI;
    import com.google.cloud.vertexai.api.GenerationConfig;
    import com.google.cloud.vertexai.generativeai.ContentMaker;
    import com.google.cloud.vertexai.generativeai.GenerativeModel;
    import com.google.cloud.vertexai.api.GenerateContentResponse;
    import com.google.cloud.vertexai.api.Candidate;
    import java.io.IOException;
    
    public class Main {
        private static final String PROJECT_ID = "niveus-ncode";
        private static final String LOCATION = "us-central1";
    
        public static void main(String[] args) throws IOException {
            try (VertexAI vertexAi = new VertexAI(PROJECT_ID, LOCATION);) {
                GenerationConfig generationConfig = GenerationConfig.newBuilder()
                        .setTemperature(0.3f)
                        .setMaxOutputTokens(300)
                        .setTopP(1.0f)
                        .setTopK(40)
                        .build();
    
                GenerativeModel model = new GenerativeModel.Builder()
                         .setModelName("gemini-pro")
    //                    .setModelName("code-gecko@latest")
                        .setVertexAi(vertexAi)
                        .setGenerationConfig(generationConfig)
                        .setSystemInstruction(
                                ContentMaker.fromString(
                                        "You are an AI specialized in inline code completion.Your task is to generate only the missing code snippet that correctly completes the given input.Do not repeat or modify any existing code.Ensure the completion integrates seamlessly with the provided context.Do not add extra brackets, class definitions, or method signatures if they are already present in the context."))
    
                        .build();
                GenerateContentResponse response = model.generateContent("public void actionPerformed(@NotNull AnActionEvent e) {\n" +
                        "            final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);\n" +
                        "            final CaretModel caretModel = editor.getCaretModel();");
                // Extract and print the generated text
                for (Candidate candidate : response.getCandidatesList()) {
                    if (candidate.hasContent() && !candidate.getContent().getPartsList().isEmpty()) {
                        String text = candidate.getContent().getParts(0).getText();
                        System.out.println("Generated Response: " + text);
                    }
                }
            }
            }
        }