最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google cloud vertex ai - How to set GoogleCredentials to VertexAI programmatically - Stack Overflow

programmeradmin7浏览0评论

Using Default Auth i.e. gcloud auth login using CLI. I can execute the code. But I want to add GoogleCredentails token to VertexAI.

GoogleCredentials credentials = GoogleCredentials.create(token); 

I observed that VertexAI have only getCredentails not the setter. I can see VertexAI.Builder class contains setCredentails but not sure how.

The below code works fine with gcloud auth login but can anyone help me how to execute the below code with setting GoogleCredentials.

import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import com.google.cloud.vertexai.generativeai.ResponseHandler;

public class QuestionAnswer {

  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-google-cloud-project-id";
    String location = "us-central1";
    String modelName = "gemini-1.5-flash-001";

    String output = simpleQuestion(projectId, location, modelName);
    System.out.println(output);
  }

  // Asks a question to the specified Vertex AI Gemini model and returns the generated answer.
  public static String simpleQuestion(String projectId, String location, String modelName)
      throws Exception {
    // Initialize client that will be used to send requests.
    // This client only needs to be created once, and can be reused for multiple requests.
    try (VertexAI vertexAI = new VertexAI(projectId, location)) {
      String output;
      GenerativeModel model = new GenerativeModel(modelName, vertexAI);
      // Send the question to the model for processing.
      GenerateContentResponse response = model.generateContent("Why is the sky blue?");
      // Extract the generated text from the model's response.
      output = ResponseHandler.getText(response);
      return output;
    }
  }
}

Using Default Auth i.e. gcloud auth login using CLI. I can execute the code. But I want to add GoogleCredentails token to VertexAI.

GoogleCredentials credentials = GoogleCredentials.create(token); 

I observed that VertexAI have only getCredentails not the setter. I can see VertexAI.Builder class contains setCredentails but not sure how.

The below code works fine with gcloud auth login but can anyone help me how to execute the below code with setting GoogleCredentials.

import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.generativeai.GenerativeModel;
import com.google.cloud.vertexai.generativeai.ResponseHandler;

public class QuestionAnswer {

  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-google-cloud-project-id";
    String location = "us-central1";
    String modelName = "gemini-1.5-flash-001";

    String output = simpleQuestion(projectId, location, modelName);
    System.out.println(output);
  }

  // Asks a question to the specified Vertex AI Gemini model and returns the generated answer.
  public static String simpleQuestion(String projectId, String location, String modelName)
      throws Exception {
    // Initialize client that will be used to send requests.
    // This client only needs to be created once, and can be reused for multiple requests.
    try (VertexAI vertexAI = new VertexAI(projectId, location)) {
      String output;
      GenerativeModel model = new GenerativeModel(modelName, vertexAI);
      // Send the question to the model for processing.
      GenerateContentResponse response = model.generateContent("Why is the sky blue?");
      // Extract the generated text from the model's response.
      output = ResponseHandler.getText(response);
      return output;
    }
  }
}
Share Improve this question asked Feb 5 at 0:37 Sarada ChelluboyenaSarada Chelluboyena 4223 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Generate token first - gcloud auth print-access-token

    String oauthToken = "Your token";
    Date tokenExpiry = new Date(System.currentTimeMillis() + 3600 * 1000); // Token expiry 1 hour from now
    
    // Create the AccessToken
    AccessToken token = new AccessToken(oauthToken, tokenExpiry);
    
    // Pass the AccessToken to GoogleCredentials
    GoogleCredentials credentials = GoogleCredentials.create(token);
    VertexAI vertexAi = new VertexAI.Builder() 
                        .setProjectId("projectID")
                        .setLocation("australia-southeast1")
                        .setCredentials(credentials)
                        .build();
发布评论

评论列表(0)

  1. 暂无评论