在 Java 中的 Gemini Pro 版本上使用 Bearer Api 令牌

问题描述 投票:0回答:1

背景

在尝试在 java 项目中使用 API 令牌(而不是登录)向 Gemini Pro 模型提问时,我在使用不记名令牌(原始 API 令牌)访问模型时遇到了一些困难。基于this答案,我尝试过:

public static void callViaBearerApiToken(
      String projectId, String location, String modelName, String bearerToken)
      throws ClientProtocolException, IOException {
    // TO get the endPointId type: gcloud ai endpoints list --region=${LOCATION} --uri in cli.
    String endpointId = "https://us-central1-aiplatform.googleapis.com/";
    // Define the endpoint URL
    String endpointUrl =
        "https://"
            + location
            + "-aiplatform.googleapis.com/v1/projects/"
            + projectId
            + "/locations/"
            + location
            // + "/endpoints/"
            // + endpointId
            + ":predict";
    System.out.println("endpointUrl=\n" + endpointUrl);
    String someQuestion = "Can you say hello world?";

    // Define the request body
    JsonObject requestBody = new JsonObject();
    JsonObject instance = new JsonObject();
    instance.addProperty("content", someQuestion);
    requestBody.add("instances", instance);

    // Convert the request body to JSON
    String requestBodyJson = new Gson().toJson(requestBody);

    // Create an HTTP client and request
    HttpClient client = HttpClients.createDefault();
    HttpPost request = new HttpPost(endpointUrl);
    StringEntity requestEntity = new StringEntity(requestBodyJson);
    requestEntity.setContentType("application/json");
    request.setEntity(requestEntity);

    // Set the authentication header
    request.setHeader(HttpHeaders.AUTHORIZATION, bearerToken);

    // Send the request and get the response
    HttpResponse response = client.execute(request);
    HttpEntity responseEntity = response.getEntity();
    String responseBodyJson = EntityUtils.toString(responseEntity);

    // Get response headers
    org.apache.http.Header[] headers = response.getAllHeaders();
    for (org.apache.http.Header header : headers) {
        System.out.println(header.getName() + ": " + header.getValue());
    }
  
    // Get the status code
    int statusCode = response.getStatusLine().getStatusCode();
    System.out.println("Status Code: " + statusCode);


    // Deserialize the response body from JSON
    JsonObject responseBody = new Gson().fromJson(responseBodyJson, JsonObject.class);

    // Process the response
    System.out.println("response=\n" + responseBody);
    System.out.println("response again=\n" + responseBody.getAsString());
  }

然而,这会产生:

endpointUrl=
https://us-central1-aiplatform.googleapis.com/v1/projects/someProjectName/locations/us-central1:predict

Content-Type: text/html
Date: Sun, 11 Feb 2024 18:04:50 GMT
Server: scaffolding on HTTPServer2
Content-Length: 0
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

Status Code: 404

response=
null

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.google.gson.JsonObject.getAsString()" because "responseBody" is null
    at com.doctestbot.prepare_calls.CallGoogle.callViaBearerApiToken(CallGoogle.java:81)
    at com.doctestbot.Main.main(Main.java:53)

使用以下命令生成 Bearer API 令牌后:

gcloud auth application-default print-access-token

问题

我怎样才能问“你能打个招呼吗?”对于 Java 中的 Gemini Pro 模型,使用 Bearer API 令牌作为唯一身份验证方式?

疑问

我认为我应该从谷歌云设置中的某个地方获取端点ID,因为大多数其他问题提到这会屏蔽它以保护隐私,然后输入:

gcloud ai endpoints list --region=us-central1 --uri

输出:

Using endpoint [https://us-central1-aiplatform.googleapis.com/]
Listed 0 items.

但是,在下面的示例中似乎没有必要手动指定端点。

注意

为了完整起见,我确保我可以使用具有正常身份验证的 Gemini Pro 模型,使用 this 示例:

import com.google.cloud.vertexai.VertexAI;
import com.google.cloud.vertexai.api.GenerateContentResponse;
import com.google.cloud.vertexai.generativeai.preview.ChatSession;
import com.google.cloud.vertexai.generativeai.preview.GenerativeModel;
import com.google.cloud.vertexai.generativeai.preview.ResponseHandler;
import java.io.IOException;

public class ChatDiscussion {

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

    chatDiscussion(projectId, location, modelName);
  }

  // Ask interrelated questions in a row using a ChatSession object.
  public static void chatDiscussion(String projectId, String location, String modelName)
      throws IOException {
    // 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)) {
      GenerateContentResponse response;

      GenerativeModel model = new GenerativeModel(modelName, vertexAI);
      // Create a chat session to be used for interactive conversation.
      ChatSession chatSession = new ChatSession(model);

      response = chatSession.sendMessage("Hello.");
      System.out.println(ResponseHandler.getText(response));

      response = chatSession.sendMessage("What are all the colors in a rainbow?");
      System.out.println(ResponseHandler.getText(response));

      response = chatSession.sendMessage("Why does it appear when it rains?");
      System.out.println(ResponseHandler.getText(response));
      System.out.println("Chat Ended.");
    }
  }
}
java bearer-token google-cloud-vertex-ai
1个回答
0
投票

如果您想手动构建并发送 HTTP 请求(而不是让 Vertex AI SDK 为您完成),则选择 REST 选项卡将为您提供所需的所有信息。特别是,您将看到端点是:

https://<LOCATION>-aiplatform.googleapis.com/v1/projects/<PROJECT_ID>/locations/<LOCATION>/publishers/google/models/<MODEL_ID>:streamGenerateContent

它与您尝试过的不一样(这就是您得到 404 的原因)。

另请注意请求正文:

{
  "contents": {
    "role": <ROLE>,
    "parts": { "text": <TEXT> }
  },
  "safety_settings": {
    "category": <SAFETY_CATEGORY>,
    "threshold": <THRESHOLD>
  },
  "generation_config": {
    "temperature": <TEMPERATURE>,
    "topP": <TOP_P>,
    "topK": <TOP_K>,
    "candidateCount": 1,
    "maxOutputTokens": <MAX_OUTPUT_TOKENS>,
    "stopSequences": <STOP_SEQUENCES>
  }
}

它也与你尝试过的不同。

© www.soinside.com 2019 - 2024. All rights reserved.