CoolFace
Apppublic

yrtyry/vertex

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
App README

OpenAI to Gemini Adapter

This service provides an OpenAI-compatible API that translates requests to Google's Vertex AI Gemini models, allowing you to use Gemini models with tools expecting an OpenAI interface.

Features

  • OpenAI-compatible API endpoints (/v1/chat/completions, /v1/models).
  • Supports Google Cloud credentials via GOOGLE_CREDENTIALS_JSON secret (recommended for Spaces) or local file methods.
  • Supports credential rotation when using local files.
  • Handles streaming and non-streaming responses.
  • Configured for easy deployment on Hugging Face Spaces using Docker (port 7860) or locally via Docker Compose (port 8050).

Hugging Face Spaces Deployment (Recommended)

This application is ready for deployment on Hugging Face Spaces using Docker.

  1. 1.Create a new Space: Go to Hugging Face Spaces and create a new Space, choosing "Docker" as the Space SDK.
  2. 2.Upload Files: Upload the app/ directory, Dockerfile, and app/requirements.txt to your Space repository. You can do this via the web interface or using Git.
  3. 3.Configure Secrets: In your Space settings, navigate to the Secrets section and add the following secrets:
  4. 4.API_KEY: Your desired API key for authenticating requests to this adapter service. If not set, it defaults to 123456.
  5. 5.GOOGLE_CREDENTIALS_JSON: The entire content of your Google Cloud service account JSON key file. Copy and paste the JSON content directly into the secret value field. This is the required method for providing credentials on Hugging Face.
  6. 6.Deployment: Hugging Face will automatically build and deploy the Docker container. The application will run on port 7860 as defined in the Dockerfile and this README's metadata.

Your adapter service will be available at the URL provided by your Hugging Face Space (e.g., https://your-user-name-your-space-name.hf.space).

Local Docker Setup (for Development/Testing)

Prerequisites

  • Docker and Docker Compose
  • Google Cloud service account credentials with Vertex AI access

Credential Setup (Local Docker)

  1. 1.Create a credentials directory in the project root:
bash
    mkdir -p credentials
  1. 1.Add your service account JSON files to the credentials directory:
bash
    # Example with multiple credential files
    cp /path/to/your/service-account1.json credentials/service-account1.json
    cp /path/to/your/service-account2.json credentials/service-account2.json

The service will automatically detect and rotate through all .json files in this directory if the GOOGLE_CREDENTIALS_JSON environment variable is not set.

  1. 1.Alternatively, set the GOOGLE_APPLICATION_CREDENTIALS environment variable in your local environment or `docker-compose.yml` to the path of a single credential file (used as a fallback if the other methods fail).

Running Locally

Start the service using Docker Compose:

bash
docker-compose up -d

The service will be available at http://localhost:8050 (as defined in docker-compose.yml).

API Usage

The service implements OpenAI-compatible endpoints:

  • GET /v1/models - List available models
  • POST /v1/chat/completions - Create a chat completion
  • GET /health - Health check endpoint (includes credential status)

All endpoints require authentication using an API key in the Authorization header.

Authentication

The service requires an API key for authentication.

To authenticate, include the API key in the Authorization header using the Bearer token format:

Authorization: Bearer YOUR_API_KEY

Replace YOUR_API_KEY with the key you configured (either via the API_KEY secret/environment variable or the default 123456).

Example Requests

(Replace `YOUR_ADAPTER_URL` with your Hugging Face Space URL or `http://localhost:8050` if running locally)

Basic Request
bash
curl -X POST YOUR_ADAPTER_URL/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gemini-1.5-pro",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Hello, how are you?"}
    ],
    "temperature": 0.7
  }'
Grounded Search Request
bash
curl -X POST YOUR_ADAPTER_URL/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gemini-2.5-pro-exp-03-25-search",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant with access to the latest information."},
      {"role": "user", "content": "What are the latest developments in quantum computing?"}
    ],
    "temperature": 0.2
  }'

Supported Models

The API supports the following Vertex AI Gemini models:

Model IDDescription
gemini-2.5-pro-exp-03-25Gemini 2.5 Pro Experimental (March 25)
gemini-2.5-pro-exp-03-25-searchGemini 2.5 Pro with Google Search grounding
gemini-2.0-flashGemini 2.0 Flash
gemini-2.0-flash-searchGemini 2.0 Flash with Google Search grounding
gemini-2.0-flash-liteGemini 2.0 Flash Lite
gemini-2.0-flash-lite-searchGemini 2.0 Flash Lite with Google Search grounding
gemini-2.0-pro-exp-02-05Gemini 2.0 Pro Experimental (February 5)
gemini-1.5-flashGemini 1.5 Flash
gemini-1.5-flash-8bGemini 1.5 Flash 8B
gemini-1.5-proGemini 1.5 Pro
gemini-1.0-pro-002Gemini 1.0 Pro
gemini-1.0-pro-vision-001Gemini 1.0 Pro Vision
gemini-embedding-expGemini Embedding Experimental

Models with the -search suffix enable grounding with Google Search using dynamic retrieval.

Supported Parameters

The API supports common OpenAI-compatible parameters, mapping them to Vertex AI where possible:

OpenAI ParameterVertex AI ParameterDescription
temperaturetemperatureControls randomness (0.0 to 1.0)
max_tokensmax_output_tokensMaximum number of tokens to generate
top_ptop_pNucleus sampling parameter (0.0 to 1.0)
top_ktop_kTop-k sampling parameter
stopstop_sequencesList of strings that stop generation when encountered
presence_penaltypresence_penaltyPenalizes repeated tokens
frequency_penaltyfrequency_penaltyPenalizes frequent tokens
seedseedRandom seed for deterministic generation
logprobslogprobsNumber of log probabilities to return
ncandidate_countNumber of completions to generate

Credential Handling Priority

The application loads Google Cloud credentials in the following order:

  1. 1.`GOOGLE_CREDENTIALS_JSON` Environment Variable / Secret: Checks for the JSON content directly in this variable (Required for Hugging Face).
  2. 2.`credentials/` Directory (Local Only): Looks for .json files in the directory specified by CREDENTIALS_DIR (Default: /app/credentials inside the container). Rotates through found files. Used if GOOGLE_CREDENTIALS_JSON is not set.
  3. 3.`GOOGLE_APPLICATION_CREDENTIALS` Environment Variable (Local Only): Checks for a file path specified by this variable. Used as a fallback if the above methods fail.

Environment Variables / Secrets

  • API_KEY: API key for authentication (Default: 123456). Required as Secret on Hugging Face.
  • GOOGLE_CREDENTIALS_JSON: (Required Secret on Hugging Face) The full JSON content of your service account key. Takes priority over other methods.
  • CREDENTIALS_DIR (Local Only): Directory containing credential files (Default: /app/credentials in the container). Used if GOOGLE_CREDENTIALS_JSON is not set.
  • GOOGLE_APPLICATION_CREDENTIALS (Local Only): Path to a specific credential file. Used as a fallback if the above methods fail.
  • PORT: Not needed for CMD config (uses 7860). Hugging Face provides this automatically, docker-compose.yml maps 8050 locally.

Health Check

You can check the status of the service using the health endpoint:

bash
curl YOUR_ADAPTER_URL/health -H "Authorization: Bearer YOUR_API_KEY"

This returns information about the credential status:

json
{
  "status": "ok",
  "credentials": {
    "available": 1, // Example: 1 if loaded via JSON secret, or count if loaded from files
    "files": [], // Lists files only if using CREDENTIALS_DIR method
    "current_index": 0
  }
}

License

This project is licensed under the MIT License.