Skip to content
Snippets Groups Projects
Commit 05de24ee authored by Shreyas Prabhu's avatar Shreyas Prabhu
Browse files

Implement Auth Test

parent 2e563863
Branches
No related tags found
No related merge requests found
...@@ -12,7 +12,11 @@ repositories { ...@@ -12,7 +12,11 @@ repositories {
dependencies { dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0') testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter' testImplementation 'org.junit.jupiter:junit-jupiter'
implementation 'com.google.cloud:google-cloud-vertexai:1.17.0' implementation 'com.google.cloud:google-cloud-vertexai:1.17.0' // Make sure this version is up to date. Check Maven Central for the latest version!
implementation 'com.google.auth:google-auth-library-oauth2-http:1.17.0' // Or the latest version. Check Maven Central!
implementation 'com.google.api:gax:2.33.0' // Or the latest version. Check Maven Central!
// IntelliJ dependencies
compileOnly 'org.jetbrains:annotations:24.0.1' // Use the latest version. Check Maven Central!
} }
test { test {
......
package com.example;
import com.google.auth.Credentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.Candidate;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.api.GenerationConfig;
import com.google.cloud.vertexai.generativeai.ContentMaker;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;
public class AuthTest {
private static final String PROJECT_ID = "niveus-llm"; // Replace with your actual project ID
private static final String LOCATION = "us-central1";
private static final String SERVICE_ACCOUNT_JSON_PATH = "./key.json"; // Use an *absolute* path!
public static void main(String[] args) {
VertexAI vertexAi = null; // Declare VertexAI outside try block for proper closing
try {
// Load credentials from the service account JSON file.
Credentials credentials = ServiceAccountCredentials.fromStream(new FileInputStream(SERVICE_ACCOUNT_JSON_PATH));
Objects.requireNonNull(credentials, "Failed to load credentials from " + SERVICE_ACCOUNT_JSON_PATH);
// Initialize Vertex AI with the credentials. Just initializing is enough to test authentication.
vertexAi = new VertexAI.Builder()
.setProjectId(PROJECT_ID)
.setLocation(LOCATION)
.setCredentials(credentials)
.build();
System.out.println(vertexAi);
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);
}
}
System.out.println("Successfully authenticated and initialized Vertex AI!");
} catch (IOException e) {
System.err.println("Error loading service account credentials: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.err.println("Error during Vertex AI initialization: " + e.getMessage());
e.printStackTrace(); // Print the full stack trace for debugging
} finally {
if (vertexAi != null) {
try {
vertexAi.close(); // Important: Close the VertexAI instance in a finally block
System.out.println("Vertex AI connection closed.");
} catch (Exception e) {
System.err.println("Error closing Vertex AI: " + e.getMessage());
e.printStackTrace();
}
}
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment