bibibi12345 commited on
Commit
ebf9765
·
1 Parent(s): 4118a69

updated readme

Browse files
Files changed (1) hide show
  1. README.md +63 -121
README.md CHANGED
@@ -4,55 +4,73 @@ 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
 
@@ -61,7 +79,6 @@ Start the service using Docker Compose:
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
@@ -70,34 +87,27 @@ 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?"}
@@ -106,97 +116,29 @@ curl -X POST YOUR_ADAPTER_URL/v1/chat/completions \
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
 
 
4
  colorFrom: blue
5
  colorTo: green
6
  sdk: docker
7
+ app_port: 7860 # Port exposed by Dockerfile, used by Hugging Face Spaces
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. The codebase has been refactored for modularity and improved maintainability.
13
 
14
  ## Features
15
 
16
  - OpenAI-compatible API endpoints (`/v1/chat/completions`, `/v1/models`).
17
+ - Modular codebase located within the `app/` directory.
18
+ - Centralized environment variable management in `app/config.py`.
19
+ - Supports Google Cloud credentials via:
20
+ - `GOOGLE_CREDENTIALS_JSON` environment variable (containing the JSON key content).
21
+ - Service account JSON files placed in a specified directory (defaults to `credentials/` in the project root, mapped to `/app/credentials` in the container).
22
+ - Supports credential rotation when using multiple local credential files.
23
  - Handles streaming and non-streaming responses.
24
  - Configured for easy deployment on Hugging Face Spaces using Docker (port 7860) or locally via Docker Compose (port 8050).
25
+ - Support for Vertex AI Express Mode via `VERTEX_EXPRESS_API_KEY` environment variable.
26
 
27
  ## Hugging Face Spaces Deployment (Recommended)
28
 
29
  This application is ready for deployment on Hugging Face Spaces using Docker.
30
 
31
  1. **Create a new Space:** Go to Hugging Face Spaces and create a new Space, choosing "Docker" as the Space SDK.
32
+ 2. **Upload Files:** Add all project files (including the `app/` directory, `.gitignore`, `Dockerfile`, `docker-compose.yml`, and `requirements.txt`) to your Space repository. You can do this via the web interface or using Git.
33
+ 3. **Configure Secrets:** In your Space settings, navigate to the **Secrets** section and add the following:
34
+ * `API_KEY`: Your desired API key for authenticating requests to this adapter service. (Default: `123456` if not set, as per `app/config.py`).
35
+ * `GOOGLE_CREDENTIALS_JSON`: The **entire content** of your Google Cloud service account JSON key file. This is the primary method for providing credentials on Hugging Face.
36
+ * `VERTEX_EXPRESS_API_KEY` (Optional): If you have a Vertex AI Express API key and want to use eligible models in Express Mode.
37
+ * Other environment variables (see "Environment Variables" section below) can also be set as secrets if you need to override defaults (e.g., `FAKE_STREAMING`).
38
+ 4. **Deployment:** Hugging Face will automatically build and deploy the Docker container. The application will run on port 7860.
39
 
40
+ Your adapter service will be available at the URL provided by your Hugging Face Space.
41
 
42
  ## Local Docker Setup (for Development/Testing)
43
 
44
  ### Prerequisites
45
 
46
  - Docker and Docker Compose
47
+ - Google Cloud service account credentials with Vertex AI access (if not using Vertex Express exclusively).
48
 
49
  ### Credential Setup (Local Docker)
50
 
51
+ The application uses `app/config.py` to manage environment variables. You can set these in a `.env` file at the project root (which is ignored by git) or directly in your `docker-compose.yml` for local development.
52
+
53
+ 1. **Method 1: JSON Content via Environment Variable (Recommended for consistency with Spaces)**
54
+ * Set the `GOOGLE_CREDENTIALS_JSON` environment variable to the full JSON content of your service account key.
55
+ 2. **Method 2: Credential Files in a Directory**
56
+ * If `GOOGLE_CREDENTIALS_JSON` is *not* set, the adapter will look for service account JSON files in the directory specified by the `CREDENTIALS_DIR` environment variable.
57
+ * The default `CREDENTIALS_DIR` is `/app/credentials` inside the container.
58
+ * Create a `credentials` directory in your project root: `mkdir -p credentials`
59
+ * Place your service account JSON key files (e.g., `my-project-creds.json`) into this `credentials/` directory. The `docker-compose.yml` mounts this local directory to `/app/credentials` in the container.
60
+ * The service will automatically detect and rotate through all `.json` files in this directory.
61
+
62
+ ### Environment Variables for Local Docker (`.env` file or `docker-compose.yml`)
63
+
64
+ Create a `.env` file in the project root or modify your `docker-compose.override.yml` (if you use one) or `docker-compose.yml` to set these:
65
+
66
+ ```env
67
+ API_KEY="your_secure_api_key_here" # Replace with your actual key or leave for default
68
+ # GOOGLE_CREDENTIALS_JSON='{"type": "service_account", ...}' # Option 1: Paste JSON content
69
+ # CREDENTIALS_DIR="/app/credentials" # Option 2: (Default path if GOOGLE_CREDENTIALS_JSON is not set)
70
+ # VERTEX_EXPRESS_API_KEY="your_vertex_express_key" # Optional
71
+ # FAKE_STREAMING="false" # Optional, for debugging
72
+ # FAKE_STREAMING_INTERVAL="1.0" # Optional, for debugging
73
+ ```
74
 
75
  ### Running Locally
76
 
 
79
  ```bash
80
  docker-compose up -d
81
  ```
 
82
  The service will be available at `http://localhost:8050` (as defined in `docker-compose.yml`).
83
 
84
  ## API Usage
 
87
 
88
  - `GET /v1/models` - List available models
89
  - `POST /v1/chat/completions` - Create a chat completion
90
+ - `GET /` - Basic status endpoint
91
 
92
+ All API endpoints require authentication using an API key in the Authorization header.
93
 
94
  ### Authentication
95
 
96
+ Include the API key in the `Authorization` header using the `Bearer` token format:
97
+ `Authorization: Bearer YOUR_API_KEY`
98
+ Replace `YOUR_API_KEY` with the key configured via the `API_KEY` environment variable (or the default).
 
 
 
 
 
 
99
 
100
  ### Example Requests
101
 
102
  *(Replace `YOUR_ADAPTER_URL` with your Hugging Face Space URL or `http://localhost:8050` if running locally)*
103
 
104
  #### Basic Request
 
105
  ```bash
106
  curl -X POST YOUR_ADAPTER_URL/v1/chat/completions \
107
  -H "Content-Type: application/json" \
108
  -H "Authorization: Bearer YOUR_API_KEY" \
109
  -d '{
110
+ "model": "gemini-1.5-pro", # Or any other supported model
111
  "messages": [
112
  {"role": "system", "content": "You are a helpful assistant."},
113
  {"role": "user", "content": "Hello, how are you?"}
 
116
  }'
117
  ```
118
 
119
+ ### Supported Models & Parameters
120
+ (Refer to the `list_models` endpoint output and original documentation for the most up-to-date list of supported models and parameters. The adapter aims to map common OpenAI parameters to their Vertex AI equivalents.)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  ## Credential Handling Priority
123
 
124
+ The application (via `app/config.py` and helper modules) prioritizes credentials as follows:
 
 
 
 
125
 
126
+ 1. **Vertex AI Express Mode (`VERTEX_EXPRESS_API_KEY` env var):** If this key is set and the requested model is eligible for Express Mode, this will be used.
127
+ 2. **Service Account Credentials (Rotated):** If Express Mode is not used/applicable:
128
+ * **`GOOGLE_CREDENTIALS_JSON` Environment Variable:** If set, its JSON content is parsed. Multiple JSON objects (comma-separated) or a single JSON object are supported. These are loaded into the `CredentialManager`.
129
+ * **Files in `CREDENTIALS_DIR`:** The `CredentialManager` scans the directory specified by `CREDENTIALS_DIR` (default is `credentials/` mapped to `/app/credentials` in Docker) for `.json` Mkey files.
130
+ * The `CredentialManager` then rotates through all successfully loaded service account credentials (from `GOOGLE_CREDENTIALS_JSON` and files in `CREDENTIALS_DIR`) for each request.
131
 
132
+ ## Key Environment Variables
 
 
 
 
133
 
134
+ These are sourced by `app/config.py`:
135
 
136
+ - `API_KEY`: API key for authenticating to this adapter service. (Default: `123456`)
137
+ - `GOOGLE_CREDENTIALS_JSON`: (Takes priority for SA creds) Full JSON content of your service account key(s).
138
+ - `CREDENTIALS_DIR`: Directory for service account JSON files if `GOOGLE_CREDENTIALS_JSON` is not set. (Default: `/app/credentials` within container context)
139
+ - `VERTEX_EXPRESS_API_KEY`: Optional API key for using Vertex AI Express Mode with compatible models.
140
+ - `FAKE_STREAMING`: Set to `"true"` to enable simulated streaming for non-streaming models (for testing). (Default: `"false"`)
141
+ - `FAKE_STREAMING_INTERVAL`: Interval in seconds for sending keep-alive messages during fake streaming. (Default: `1.0`)
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
  ## License
144