如何通过Java代码为VertexAI设置GoogleCredentials
为VertexAI配置自定义GoogleCredentials令牌
我通过CLI执行gcloud auth login使用默认认证时,代码可以正常运行,但现在希望为VertexAI配置自定义的GoogleCredentials令牌(比如通过GoogleCredentials credentials = GoogleCredentials.create(token);创建的令牌)。我注意到VertexAI类只有getCredentials方法,没有setter,但VertexAI.Builder类提供了setCredentials方法,却不清楚具体用法。现有使用默认认证的代码如下,请帮忙修改为通过自定义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; } } }
解决方案
要使用自定义GoogleCredentials,你需要通过VertexAI.Builder构建VertexAI实例,而非直接调用构造方法。核心步骤是先创建好自定义的GoogleCredentials对象,再通过Builder的setCredentials方法传入,同时指定项目ID和区域,最后调用build()生成实例。
修改后的完整代码如下:
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; import com.google.auth.oauth2.GoogleCredentials; 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 token = "your-custom-auth-token"; GoogleCredentials credentials = GoogleCredentials.create(token); String output = simpleQuestion(projectId, location, modelName, credentials); System.out.println(output); } public static String simpleQuestion(String projectId, String location, String modelName, GoogleCredentials credentials) throws Exception { // 使用Builder构建VertexAI实例并传入自定义credentials try (VertexAI vertexAI = new VertexAI.Builder() .setProjectId(projectId) .setLocation(location) .setCredentials(credentials) .build()) { GenerativeModel model = new GenerativeModel(modelName, vertexAI); GenerateContentResponse response = model.generateContent("Why is the sky blue?"); return ResponseHandler.getText(response); } } }
关键修改说明
- 新增
GoogleCredentials参数传入业务方法,也可在方法内部直接创建该对象(根据你的令牌获取逻辑调整) - 用
VertexAI.Builder替代原有的直接构造,依次配置项目ID、区域、自定义凭证后构建实例 - 保留try-with-resources语法,确保VertexAI客户端资源能自动释放
内容的提问来源于stack exchange,提问作者Sarada Chelluboyena
相关产品推荐
相关产品推荐

