Spaces:
Running
Running
Update app.js
Browse files
app.js
CHANGED
@@ -1,10 +1,12 @@
|
|
1 |
-
// app.js
|
2 |
const express = require('express');
|
3 |
const fetch = require('node-fetch');
|
|
|
|
|
4 |
const app = express();
|
5 |
-
|
6 |
|
7 |
const MODEL = 'claude-3-5-sonnet@20240620';
|
|
|
8 |
const PROJECT_ID = process.env.PROJECT_ID;
|
9 |
const CLIENT_ID = process.env.CLIENT_ID;
|
10 |
const CLIENT_SECRET = process.env.CLIENT_SECRET;
|
@@ -47,7 +49,6 @@ async function getAccessToken() {
|
|
47 |
});
|
48 |
|
49 |
const data = await response.json();
|
50 |
-
|
51 |
tokenCache.accessToken = data.access_token;
|
52 |
tokenCache.expiry = now + data.expires_in;
|
53 |
} finally {
|
@@ -68,13 +69,11 @@ function constructApiUrl(location) {
|
|
68 |
return `https://${location}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${location}/publishers/anthropic/models/${MODEL}:streamRawPredict`;
|
69 |
}
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
});
|
76 |
|
77 |
-
app.post('/ai/v1/messages', async (req, res) => {
|
78 |
const apiKey = req.headers['x-api-key'];
|
79 |
if (apiKey !== API_KEY) {
|
80 |
return res.status(403).json({
|
@@ -91,35 +90,47 @@ app.post('/ai/v1/messages', async (req, res) => {
|
|
91 |
const apiUrl = constructApiUrl(location);
|
92 |
|
93 |
let requestBody = req.body;
|
94 |
-
|
95 |
if (requestBody.anthropic_version) {
|
96 |
delete requestBody.anthropic_version;
|
97 |
}
|
98 |
-
|
99 |
-
if (requestBody.model
|
100 |
-
requestBody.model
|
101 |
}
|
102 |
-
|
103 |
requestBody.anthropic_version = "vertex-2023-10-16";
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
'Authorization': `Bearer ${accessToken}`,
|
110 |
-
'Content-Type': 'application/json; charset=utf-8'
|
111 |
-
},
|
112 |
-
body: JSON.stringify(requestBody)
|
113 |
-
});
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
}
|
121 |
-
});
|
122 |
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
const express = require('express');
|
2 |
const fetch = require('node-fetch');
|
3 |
+
const bodyParser = require('body-parser');
|
4 |
+
|
5 |
const app = express();
|
6 |
+
app.use(bodyParser.json());
|
7 |
|
8 |
const MODEL = 'claude-3-5-sonnet@20240620';
|
9 |
+
|
10 |
const PROJECT_ID = process.env.PROJECT_ID;
|
11 |
const CLIENT_ID = process.env.CLIENT_ID;
|
12 |
const CLIENT_SECRET = process.env.CLIENT_SECRET;
|
|
|
49 |
});
|
50 |
|
51 |
const data = await response.json();
|
|
|
52 |
tokenCache.accessToken = data.access_token;
|
53 |
tokenCache.expiry = now + data.expires_in;
|
54 |
} finally {
|
|
|
69 |
return `https://${location}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${location}/publishers/anthropic/models/${MODEL}:streamRawPredict`;
|
70 |
}
|
71 |
|
72 |
+
async function handleRequest(req, res) {
|
73 |
+
if (req.method === 'OPTIONS') {
|
74 |
+
return handleOptions(res);
|
75 |
+
}
|
|
|
76 |
|
|
|
77 |
const apiKey = req.headers['x-api-key'];
|
78 |
if (apiKey !== API_KEY) {
|
79 |
return res.status(403).json({
|
|
|
90 |
const apiUrl = constructApiUrl(location);
|
91 |
|
92 |
let requestBody = req.body;
|
93 |
+
|
94 |
if (requestBody.anthropic_version) {
|
95 |
delete requestBody.anthropic_version;
|
96 |
}
|
97 |
+
|
98 |
+
if (requestBody.model) {
|
99 |
+
delete requestBody.model;
|
100 |
}
|
101 |
+
|
102 |
requestBody.anthropic_version = "vertex-2023-10-16";
|
103 |
|
104 |
+
const modifiedHeaders = {
|
105 |
+
'Authorization': `Bearer ${accessToken}`,
|
106 |
+
'Content-Type': 'application/json; charset=utf-8'
|
107 |
+
};
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
+
const response = await fetch(apiUrl, {
|
110 |
+
headers: modifiedHeaders,
|
111 |
+
method: 'POST',
|
112 |
+
body: JSON.stringify(requestBody)
|
113 |
+
});
|
|
|
|
|
114 |
|
115 |
+
const responseBody = await response.text();
|
116 |
+
res.status(response.status).set(response.headers).send(responseBody);
|
117 |
+
}
|
118 |
+
|
119 |
+
function handleOptions(res) {
|
120 |
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
121 |
+
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
|
122 |
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, x-api-key, anthropic-version, model');
|
123 |
+
res.sendStatus(204);
|
124 |
+
}
|
125 |
+
|
126 |
+
app.post('/ai/v1/messages', handleRequest);
|
127 |
+
app.options('/ai/v1/messages', handleOptions);
|
128 |
+
|
129 |
+
app.get('/', (req, res) => {
|
130 |
+
res.status(200).send('Vertex Claude API Proxy');
|
131 |
});
|
132 |
+
|
133 |
+
const PORT = process.env.PORT || 8080;
|
134 |
+
app.listen(PORT, () => {
|
135 |
+
console.log(`Server is running on port ${PORT}`);
|
136 |
+
});
|