Commit
·
f16ae1a
1
Parent(s):
1760c30
added project files
Browse files
README.md
CHANGED
@@ -1,11 +1,203 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: docker
|
7 |
-
|
8 |
-
license: mit
|
9 |
---
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
title: OpenAI to Gemini Adapter
|
3 |
+
emoji: 🔄☁️
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: green
|
6 |
sdk: docker
|
7 |
+
app_port: 7860
|
|
|
8 |
---
|
9 |
|
10 |
+
# OpenAI to Gemini Adapter
|
11 |
+
|
12 |
+
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.
|
13 |
+
|
14 |
+
## Features
|
15 |
+
|
16 |
+
- OpenAI-compatible API endpoints (`/v1/chat/completions`, `/v1/models`).
|
17 |
+
- Supports Google Cloud credentials via `GOOGLE_CREDENTIALS_JSON` secret (recommended for Spaces) or local file methods.
|
18 |
+
- Supports credential rotation when using local files.
|
19 |
+
- Handles streaming and non-streaming responses.
|
20 |
+
- Configured for easy deployment on Hugging Face Spaces using Docker (port 7860) or locally via Docker Compose (port 8050).
|
21 |
+
|
22 |
+
## Hugging Face Spaces Deployment (Recommended)
|
23 |
+
|
24 |
+
This application is ready for deployment on Hugging Face Spaces using Docker.
|
25 |
+
|
26 |
+
1. **Create a new Space:** Go to Hugging Face Spaces and create a new Space, choosing "Docker" as the Space SDK.
|
27 |
+
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.
|
28 |
+
3. **Configure Secrets:** In your Space settings, navigate to the **Secrets** section and add the following secrets:
|
29 |
+
* `API_KEY`: Your desired API key for authenticating requests to this adapter service. If not set, it defaults to `123456`.
|
30 |
+
* `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.**
|
31 |
+
4. **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.
|
32 |
+
|
33 |
+
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`).
|
34 |
+
|
35 |
+
## Local Docker Setup (for Development/Testing)
|
36 |
+
|
37 |
+
### Prerequisites
|
38 |
+
|
39 |
+
- Docker and Docker Compose
|
40 |
+
- Google Cloud service account credentials with Vertex AI access
|
41 |
+
|
42 |
+
### Credential Setup (Local Docker)
|
43 |
+
|
44 |
+
1. Create a `credentials` directory in the project root:
|
45 |
+
```bash
|
46 |
+
mkdir -p credentials
|
47 |
+
```
|
48 |
+
2. Add your service account JSON files to the `credentials` directory:
|
49 |
+
```bash
|
50 |
+
# Example with multiple credential files
|
51 |
+
cp /path/to/your/service-account1.json credentials/service-account1.json
|
52 |
+
cp /path/to/your/service-account2.json credentials/service-account2.json
|
53 |
+
```
|
54 |
+
The service will automatically detect and rotate through all `.json` files in this directory if the `GOOGLE_CREDENTIALS_JSON` environment variable is *not* set.
|
55 |
+
3. 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).
|
56 |
+
|
57 |
+
### Running Locally
|
58 |
+
|
59 |
+
Start the service using Docker Compose:
|
60 |
+
|
61 |
+
```bash
|
62 |
+
docker-compose up -d
|
63 |
+
```
|
64 |
+
|
65 |
+
The service will be available at `http://localhost:8050` (as defined in `docker-compose.yml`).
|
66 |
+
|
67 |
+
## API Usage
|
68 |
+
|
69 |
+
The service implements OpenAI-compatible endpoints:
|
70 |
+
|
71 |
+
- `GET /v1/models` - List available models
|
72 |
+
- `POST /v1/chat/completions` - Create a chat completion
|
73 |
+
- `GET /health` - Health check endpoint (includes credential status)
|
74 |
+
|
75 |
+
All endpoints require authentication using an API key in the Authorization header.
|
76 |
+
|
77 |
+
### Authentication
|
78 |
+
|
79 |
+
The service requires an API key for authentication.
|
80 |
+
|
81 |
+
To authenticate, include the API key in the `Authorization` header using the `Bearer` token format:
|
82 |
+
|
83 |
+
```
|
84 |
+
Authorization: Bearer YOUR_API_KEY
|
85 |
+
```
|
86 |
+
|
87 |
+
Replace `YOUR_API_KEY` with the key you configured (either via the `API_KEY` secret/environment variable or the default `123456`).
|
88 |
+
|
89 |
+
### Example Requests
|
90 |
+
|
91 |
+
*(Replace `YOUR_ADAPTER_URL` with your Hugging Face Space URL or `http://localhost:8050` if running locally)*
|
92 |
+
|
93 |
+
#### Basic Request
|
94 |
+
|
95 |
+
```bash
|
96 |
+
curl -X POST YOUR_ADAPTER_URL/v1/chat/completions \
|
97 |
+
-H "Content-Type: application/json" \
|
98 |
+
-H "Authorization: Bearer YOUR_API_KEY" \
|
99 |
+
-d '{
|
100 |
+
"model": "gemini-1.5-pro",
|
101 |
+
"messages": [
|
102 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
103 |
+
{"role": "user", "content": "Hello, how are you?"}
|
104 |
+
],
|
105 |
+
"temperature": 0.7
|
106 |
+
}'
|
107 |
+
```
|
108 |
+
|
109 |
+
#### Grounded Search Request
|
110 |
+
|
111 |
+
```bash
|
112 |
+
curl -X POST YOUR_ADAPTER_URL/v1/chat/completions \
|
113 |
+
-H "Content-Type: application/json" \
|
114 |
+
-H "Authorization: Bearer YOUR_API_KEY" \
|
115 |
+
-d '{
|
116 |
+
"model": "gemini-2.5-pro-exp-03-25-search",
|
117 |
+
"messages": [
|
118 |
+
{"role": "system", "content": "You are a helpful assistant with access to the latest information."},
|
119 |
+
{"role": "user", "content": "What are the latest developments in quantum computing?"}
|
120 |
+
],
|
121 |
+
"temperature": 0.2
|
122 |
+
}'
|
123 |
+
```
|
124 |
+
|
125 |
+
### Supported Models
|
126 |
+
|
127 |
+
The API supports the following Vertex AI Gemini models:
|
128 |
+
|
129 |
+
| Model ID | Description |
|
130 |
+
| ------------------------------ | ---------------------------------------------- |
|
131 |
+
| `gemini-2.5-pro-exp-03-25` | Gemini 2.5 Pro Experimental (March 25) |
|
132 |
+
| `gemini-2.5-pro-exp-03-25-search` | Gemini 2.5 Pro with Google Search grounding |
|
133 |
+
| `gemini-2.0-flash` | Gemini 2.0 Flash |
|
134 |
+
| `gemini-2.0-flash-search` | Gemini 2.0 Flash with Google Search grounding |
|
135 |
+
| `gemini-2.0-flash-lite` | Gemini 2.0 Flash Lite |
|
136 |
+
| `gemini-2.0-flash-lite-search` | Gemini 2.0 Flash Lite with Google Search grounding |
|
137 |
+
| `gemini-2.0-pro-exp-02-05` | Gemini 2.0 Pro Experimental (February 5) |
|
138 |
+
| `gemini-1.5-flash` | Gemini 1.5 Flash |
|
139 |
+
| `gemini-1.5-flash-8b` | Gemini 1.5 Flash 8B |
|
140 |
+
| `gemini-1.5-pro` | Gemini 1.5 Pro |
|
141 |
+
| `gemini-1.0-pro-002` | Gemini 1.0 Pro |
|
142 |
+
| `gemini-1.0-pro-vision-001` | Gemini 1.0 Pro Vision |
|
143 |
+
| `gemini-embedding-exp` | Gemini Embedding Experimental |
|
144 |
+
|
145 |
+
Models with the `-search` suffix enable grounding with Google Search using dynamic retrieval.
|
146 |
+
|
147 |
+
### Supported Parameters
|
148 |
+
|
149 |
+
The API supports common OpenAI-compatible parameters, mapping them to Vertex AI where possible:
|
150 |
+
|
151 |
+
| OpenAI Parameter | Vertex AI Parameter | Description |
|
152 |
+
| ------------------- | --------------------- | ------------------------------------------------- |
|
153 |
+
| `temperature` | `temperature` | Controls randomness (0.0 to 1.0) |
|
154 |
+
| `max_tokens` | `max_output_tokens` | Maximum number of tokens to generate |
|
155 |
+
| `top_p` | `top_p` | Nucleus sampling parameter (0.0 to 1.0) |
|
156 |
+
| `top_k` | `top_k` | Top-k sampling parameter |
|
157 |
+
| `stop` | `stop_sequences` | List of strings that stop generation when encountered |
|
158 |
+
| `presence_penalty` | `presence_penalty` | Penalizes repeated tokens |
|
159 |
+
| `frequency_penalty` | `frequency_penalty` | Penalizes frequent tokens |
|
160 |
+
| `seed` | `seed` | Random seed for deterministic generation |
|
161 |
+
| `logprobs` | `logprobs` | Number of log probabilities to return |
|
162 |
+
| `n` | `candidate_count` | Number of completions to generate |
|
163 |
+
|
164 |
+
## Credential Handling Priority
|
165 |
+
|
166 |
+
The application loads Google Cloud credentials in the following order:
|
167 |
+
|
168 |
+
1. **`GOOGLE_CREDENTIALS_JSON` Environment Variable / Secret:** Checks for the JSON *content* directly in this variable (Required for Hugging Face).
|
169 |
+
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.
|
170 |
+
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.
|
171 |
+
|
172 |
+
## Environment Variables / Secrets
|
173 |
+
|
174 |
+
- `API_KEY`: API key for authentication (Default: `123456`). **Required as Secret on Hugging Face.**
|
175 |
+
- `GOOGLE_CREDENTIALS_JSON`: **(Required Secret on Hugging Face)** The full JSON content of your service account key. Takes priority over other methods.
|
176 |
+
- `CREDENTIALS_DIR` (Local Only): Directory containing credential files (Default: `/app/credentials` in the container). Used if `GOOGLE_CREDENTIALS_JSON` is not set.
|
177 |
+
- `GOOGLE_APPLICATION_CREDENTIALS` (Local Only): Path to a *specific* credential file. Used as a fallback if the above methods fail.
|
178 |
+
- `PORT`: Not needed for `CMD` config (uses 7860). Hugging Face provides this automatically, `docker-compose.yml` maps 8050 locally.
|
179 |
+
|
180 |
+
## Health Check
|
181 |
+
|
182 |
+
You can check the status of the service using the health endpoint:
|
183 |
+
|
184 |
+
```bash
|
185 |
+
curl YOUR_ADAPTER_URL/health -H "Authorization: Bearer YOUR_API_KEY"
|
186 |
+
```
|
187 |
+
|
188 |
+
This returns information about the credential status:
|
189 |
+
|
190 |
+
```json
|
191 |
+
{
|
192 |
+
"status": "ok",
|
193 |
+
"credentials": {
|
194 |
+
"available": 1, // Example: 1 if loaded via JSON secret, or count if loaded from files
|
195 |
+
"files": [], // Lists files only if using CREDENTIALS_DIR method
|
196 |
+
"current_index": 0
|
197 |
+
}
|
198 |
+
}
|
199 |
+
```
|
200 |
+
|
201 |
+
## License
|
202 |
+
|
203 |
+
This project is licensed under the MIT License.
|