Mike067/vertex
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_JSONsecret (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.
- Create a new Space: Go to Hugging Face Spaces and create a new Space, choosing "Docker" as the Space SDK.
- Upload Files: Upload the
app/directory,Dockerfile, andapp/requirements.txtto your Space repository. You can do this via the web interface or using Git. - Configure Secrets: In your Space settings, navigate to the Secrets section and add the following secrets:
API_KEY: Your desired API key for authenticating requests to this adapter service. If not set, it defaults to123456.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.- Deployment: Hugging Face will automatically build and deploy the Docker container. The application will run on port 7860 as defined in the
Dockerfileand 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)
- Create a
credentialsdirectory in the project root:
mkdir -p credentials- Add your service account JSON files to the
credentialsdirectory:
# 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.
- Alternatively, set the
GOOGLE_APPLICATION_CREDENTIALSenvironment 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:
docker-compose up -dThe 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 modelsPOST /v1/chat/completions- Create a chat completionGET /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_KEYReplace 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
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
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:
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:
Credential Handling Priority
The application loads Google Cloud credentials in the following order:
- `GOOGLE_CREDENTIALS_JSON` Environment Variable / Secret: Checks for the JSON content directly in this variable (Required for Hugging Face).
- `credentials/` Directory (Local Only): Looks for
.jsonfiles in the directory specified byCREDENTIALS_DIR(Default:/app/credentialsinside the container). Rotates through found files. Used ifGOOGLE_CREDENTIALS_JSONis not set. - `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/credentialsin the container). Used ifGOOGLE_CREDENTIALS_JSONis 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 forCMDconfig (uses 7860). Hugging Face provides this automatically,docker-compose.ymlmaps 8050 locally.
Health Check
You can check the status of the service using the health endpoint:
curl YOUR_ADAPTER_URL/health -H "Authorization: Bearer YOUR_API_KEY"This returns information about the credential status:
{
"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.
