a.t.
a.t.

Reputation: 2798

Using (Bearer) Api token on Gemini Pro version in Java

Question

How can I ask "Can you say hello?" to the Gemini Pro model in Java, using a (Bearer) API token as only means of authentication?

Example

This answer contains an example that shows how one is able to use an API token to interact with the Vertex AI. For example, it shows how one can log on device A in using one's Google account and then use that authentication to obtain a (Bearer) API token. I assume this API token can then be used on device B to ask: "Can you say hello world?" to the Gemini Pro model of Google's Vertex AI, without login into one's Google account and password on device B.

Alternatively, one can directly ask an API token for the Gemini Pro model of Google's Vertex AI at: https://console.cloud.google.com/apis/credentials?project=<your_project_name>. However, I have not yet found out how to use that API token in Java only code to ask the model: "Can you say hello world?".

Note

For completeness I ensured I can use the Gemini Pro model with normal authentication, using this example:

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.");
    }
  }
}

The Bearer Api token can then be found using:

gcloud auth application-default print-access-token

Upvotes: 0

Views: 1153

Answers (1)

John Hanley
John Hanley

Reputation: 81336

Your Requirements

Using (Bearer) Api token on Gemini Pro version in Java.

without login into one's Google account and password on device B.

Summary

You cannot use API Keys with the Vertex AI. You must switch the type of credentials or the API. The Vertex AI supports service accounts that do not require user logins.

Recommendation

I recommend that you switch to the Google AI Gemini API because of support for mobile devices and API Keys. Later you can switch the credential type from API Keys to User OAuth if private data needs to be managed.

Notes

There is no such item as an API token in Google Cloud. Using that wording will create confusion. See below for more details.

You mention device B without defining what type of device. I will assume a mobile device such as Android or iPhone. The Vertex AI is not designed for use on mobile devices and doing so will present security challenges.

Details

The Gemini service provides two APIs and corresponding SDKs:

The first API is based on a simpler design and supports mobile devices. The second API is more complex and feature rich. It is designed to integrate with Google Cloud IAM and other Google Cloud services. This document has more details comparing the two APIs.

There are two types of credentials used by those APIs for authorization:

  • API Keys
  • OAuth Access Tokens

There are four types of credentials supported by Google Cloud:

  • API Keys
  • JWT Assertions
  • OAuth Access Tokens
  • OIDC Identity Tokens

JWT Assertions, OAuth, and OIDC tokens can be used for Bearer tokens. API Keys cannot be used as Bearer tokens in Google Cloud.

The Google AI Gemini API supports API Keys and User OAuth Access Tokens. The Google Cloud Vertex AI Gemini API requires OAuth access tokens and does not support API Keys.

Your example code is using the Google Cloud Vertex AI Gemini API which does not support API Keys.

You have two options:

  1. Continue using the Vertex SDK and switch to OAuth for authorization which supports user and service accounts.
  2. Switch to the Google AI Gemini API. This document provides details and example code in Java for Android. I am not aware of a Java SDK for backend (server) code. The HTTP REST API is easy to work with and can be easily written in Java.

Generate Content Example

The HTTP REST API includes a curl example that you can port to Java.

I have improved the documentation example.

curl https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent \
  -H 'Content-Type: application/json' \
  -H 'x-goog-api-key: MY_API_KEY' \
  -X POST \
  -d '{
        "contents": [{
          "parts":[{
            "text": "Write a story about a magic backpack."
          }]
         }]
       }'

Suggestion

Do not include the API Key as part of the HTTP request query parameter. That can be a security vulnerability in some environments. Instead, pass the API Key using the HTTP header x-goog-api-key. Example:

x-goog-api-key: MY_API_KEY

Upvotes: 6

Related Questions