Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 122 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ Interacting with Google's Gemini models through Vertex AI requires complex authe
### Features

* **Simplified Authentication:** Replaces complex Vertex AI authentication with a single, permanent user secret.
* **Model Flexibility:** Supports accessing various models, with a default setting of `gemini-pro` (v1.0). Users can switch to `gemini-1.5-pro-preview-0409` for the latest version.
* **Model Flexibility:** Supports accessing various models, with a default setting of `gemini-pro` (v1.0). Users can switch to `gemini-1.5-pro` for the latest version.
* **API Proxying:** Acts as a middleman for Vertex AI API requests, handling authentication and communication seamlessly.
* **Credential Rotation:** Automatically rotates between multiple Vertex AI accounts to distribute load and increase reliability.
* **Request Format Adapter:** Automatically converts various request formats (including OpenAI-style) to the Vertex AI format, improving compatibility.

### Setting Up Vertex AI API JSON API Key Authentication

To use Google Cloud Vertex AI, you need to set up a service account and obtain a JSON API key. Follow these steps to create a service account and obtain the necessary credentials:
To use Google Cloud Vertex AI, you need to set up one or more service accounts and obtain JSON API keys. Follow these steps to create service accounts and obtain the necessary credentials:

#### 1. Create a Service Account:

Expand All @@ -35,10 +37,33 @@ To use Google Cloud Vertex AI, you need to set up a service account and obtain a

### Configuration

#### Single Credential Setup (Legacy)
1. **Vertex AI Credentials:** Place your Vertex AI JSON API key within the `config/vertex-ai.json` file.
2. **User Secret:** Define the user secret within the `users` array of the `config/default.json` file.
3. **Project & Location:** Specify your Google Cloud Project ID and location in the `vertex` field of `config/default.json`.

#### Multiple Credential Setup (Recommended)
1. **Vertex AI Credentials:** Place your multiple Vertex AI JSON API keys in the config directory (e.g., `config/vertex-ai-1.json`, `config/vertex-ai-2.json`, etc.)
2. **Configuration:** Define each credential in the `vertexCredentials` section of the `config/default.json` file:

```json
"vertexCredentials": {
"credentials": [
{
"location": "us-central1",
"project": "your-project-id-1",
"credentialsFile": "config/vertex-ai-1.json"
},
{
"location": "us-central1",
"project": "your-project-id-2",
"credentialsFile": "config/vertex-ai-2.json"
}
]
}
```

The proxy will automatically rotate between these credentials with each request, distributing the load across multiple accounts.

### Deployment

Expand All @@ -63,15 +88,97 @@ services:
- ./config/:/usr/src/app/config/
```


### Usage

1. Start the proxy server.
2. Make requests to the following endpoint:
- `${host}/v1/{model}`
- Replace "{model}" with the desired model name. The default model is `gemini-pro`.
3. Include the user secret in the `Authorization` header as a Bearer token.
4. Request body stays the same as the Vertex AI API.
4. Format your request body as described below.

### Request Format

#### Standard Vertex AI Format (Recommended)
```json
{
"contents": [
{
"role": "user",
"parts": [
{
"text": "Your prompt text goes here"
}
]
}
],
"temperature": 0.7,
"maxOutputTokens": 1024,
"topP": 0.95
}
```

#### Multi-turn Conversation Format
```json
{
"contents": [
{
"role": "user",
"parts": [{ "text": "Hello, how are you?" }]
},
{
"role": "model",
"parts": [{ "text": "I'm doing well, thank you for asking! How can I help you today?" }]
},
{
"role": "user",
"parts": [{ "text": "Tell me about the weather today" }]
}
]
}
```

#### Legacy and Alternative Formats (Automatically Adapted)
The proxy includes a request adapter that automatically converts various request formats to the Vertex AI format. These include:

**OpenAI-style Format**
```json
{
"prompt": "Hello, world!",
"max_tokens": 100
}
```

**OpenAI Chat Format**
```json
{
"messages": [
{"role": "user", "content": "Hello, how are you?"},
{"role": "assistant", "content": "I'm doing well, thank you!"},
{"role": "user", "content": "Tell me about the weather today"}
]
}
```

### Example cURL Request

```bash
curl -X POST http://localhost:3030/v1/gemini-pro \
-H "Authorization: Bearer YOUR_SECRET_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"role": "user",
"parts": [
{
"text": "How are you doing today?"
}
]
}
]
}'
```

### Recommended Chatbot Client

Expand All @@ -80,9 +187,18 @@ services:
### Development

1. **Install Dependencies**: Run `npm install` to install the required dependencies.
2. **Start the Server**: Run `npm start` to start the server.
2. **Start the Server**: Run `npm run dev` for development mode or `npm start` for production mode.

This project implements Google Vertex AI inside a [FeathersJS](https://feathersjs.com/) server. If you want to add more features, you can refer to the FeathersJS documentation. Or you can use the [proxy middleware](./app/src/vertex/vertex-ai-proxy.ts) in your own project.

### Available Gemini Models

This project simply implement Google Vertex AI inside a [FeathersJS](https://feathersjs.com/) server. If you want to add more features, you can refer to the FeathersJS documentation. Or you can use the [proxy middleware](./app/src/vertex/vertex-ai-proxy.ts) in your own project.
| Model Name | Description |
|------------|-------------|
| gemini-pro | Gemini Pro 1.0 - Text-only model |
| gemini-pro-vision | Gemini Pro Vision - Supports images and text |
| gemini-1.5-pro | Gemini 1.5 Pro - Latest version with enhanced capabilities |
| gemini-1.5-flash | Gemini 1.5 Flash - Faster, more efficient model |

### Security Considerations

Expand Down
14 changes: 14 additions & 0 deletions app/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,19 @@
"vertex":{
"location": "",
"project": ""
},
"vertexCredentials": {
"credentials": [
{
"location": "",
"project": "",
"credentialsFile": "config/vertex-ai-1.json"
},
{
"location": "",
"project": "",
"credentialsFile": "config/vertex-ai-2.json"
}
]
}
}
191 changes: 188 additions & 3 deletions app/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,197 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vertex AI Proxy</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
line-height: 1.6;
color: #333;
max-width: 900px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #2c3e50;
border-bottom: 2px solid #3498db;
padding-bottom: 10px;
}
h2 {
color: #2980b9;
margin-top: 30px;
}
code {
background-color: #f4f6f8;
padding: 2px 6px;
border-radius: 3px;
font-family: 'Courier New', Courier, monospace;
}
pre {
background-color: #f4f6f8;
padding: 15px;
border-radius: 5px;
overflow-x: auto;
}
.container {
margin: 40px auto;
}
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 10px;
text-align: left;
}
th {
background-color: #f4f6f8;
}
.example-block {
background-color: #fff8dc;
border-left: 4px solid #f0db4f;
padding: 1rem;
margin: 1rem 0;
}
.warning-block {
background-color: #ffebee;
border-left: 4px solid #f44336;
padding: 1rem;
margin: 1rem 0;
}
.success-block {
background-color: #e8f5e9;
border-left: 4px solid #4caf50;
padding: 1rem;
margin: 1rem 0;
}
</style>
</head>
<body>
<div id="root">
<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);">
<h1>It works!</h1>
<div class="container">
<h1>Vertex AI Proxy</h1>
<p>This proxy server is running successfully. It provides a simplified API for accessing Google Cloud Vertex AI models like Gemini.</p>

<div class="success-block">
<strong>Status: </strong> Server is operational and ready to handle requests.
</div>

<h2>API Usage</h2>
<p>To use this API, send POST requests to:</p>
<pre><code>POST /v1/{model}</code></pre>
<p>Where <code>{model}</code> is the Gemini model you want to use (defaults to <code>gemini-pro</code> if not specified).</p>

<h2>Authentication</h2>
<p>Include your authentication token in the request header:</p>
<pre><code>Authorization: Bearer YOUR_SECRET_KEY</code></pre>

<h2>Request Format</h2>
<div class="warning-block">
<strong>Important: </strong>
The Vertex AI API requires a specific request format. The example below shows the correct format.
Common errors include using incorrect request structures that worked with older versions or other APIs.
</div>

<p>The correct request body format is:</p>
<pre><code>{
"contents": [
{
"role": "user",
"parts": [
{
"text": "Your prompt text goes here"
}
]
}
],
"temperature": 0.7,
"maxOutputTokens": 1024,
"topP": 0.95
}</code></pre>

<p>For multi-turn conversations, you need to include the conversation history:</p>
<pre><code>{
"contents": [
{
"role": "user",
"parts": [{ "text": "Hello, how are you?" }]
},
{
"role": "model",
"parts": [{ "text": "I'm doing well, thank you for asking! How can I help you today?" }]
},
{
"role": "user",
"parts": [{ "text": "Tell me about the weather today" }]
}
]
}</code></pre>

<h2>Legacy Format Support</h2>
<p>The proxy now includes an adapter that tries to convert various request formats to the format expected by Vertex AI. These include:</p>

<h3>OpenAI-Style Format</h3>
<pre><code>{
"prompt": "Hello, world!",
"max_tokens": 100
}</code></pre>

<h3>OpenAI Chat Format</h3>
<pre><code>{
"messages": [
{"role": "user", "content": "Hello, how are you?"},
{"role": "assistant", "content": "I'm doing well, thank you!"},
{"role": "user", "content": "Tell me about the weather today"}
]
}</code></pre>

<h2>Example cURL Request</h2>
<pre><code>curl -X POST http://localhost:3030/v1/gemini-pro \
-H "Authorization: Bearer YOUR_SECRET_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [
{
"role": "user",
"parts": [
{
"text": "How are you doing today?"
}
]
}
]
}'</code></pre>

<h2>Available Models</h2>
<table>
<tr>
<th>Model Name</th>
<th>Description</th>
</tr>
<tr>
<td>gemini-pro</td>
<td>Gemini Pro 1.0 - Text-only model</td>
</tr>
<tr>
<td>gemini-pro-vision</td>
<td>Gemini Pro Vision - Supports images and text</td>
</tr>
<tr>
<td>gemini-1.5-pro</td>
<td>Gemini 1.5 Pro - Latest version with enhanced capabilities</td>
</tr>
<tr>
<td>gemini-1.5-flash</td>
<td>Gemini 1.5 Flash - Faster, more efficient model</td>
</tr>
</table>

<footer style="margin-top: 50px; color: #7f8c8d; font-size: 0.9em; text-align: center; border-top: 1px solid #ecf0f1; padding-top: 20px;">
<p>Vertex AI Proxy - For more information, see the <a href="https://github.com/ShinChven/vertex-ai-proxy">GitHub repository</a>.</p>
</footer>
</div>
</body>
</html>
Loading