Create app.js
Browse files
app.js
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Import required packages for Hugging Face
|
2 |
+
import express from 'express';
|
3 |
+
import cors from 'cors';
|
4 |
+
import fetch from 'node-fetch';
|
5 |
+
|
6 |
+
const app = express();
|
7 |
+
const PORT = process.env.PORT || 7860; // Hugging Face uses port 7860 by default
|
8 |
+
|
9 |
+
// Target API URLs remain the same
|
10 |
+
const TARGET_API_URLS = [
|
11 |
+
"https://gpt4fc.deno.dev/zaiwen",
|
12 |
+
"https://gpt4fc2.deno.dev/zaiwen",
|
13 |
+
"https://gpt4fc3.deno.dev/zaiwen",
|
14 |
+
"https://gpt4fc4.deno.dev/zaiwen",
|
15 |
+
];
|
16 |
+
|
17 |
+
// Middleware setup
|
18 |
+
app.use(express.json());
|
19 |
+
app.use(cors({
|
20 |
+
origin: '*',
|
21 |
+
methods: ['POST', 'OPTIONS'],
|
22 |
+
allowedHeaders: ['Content-Type', 'Authorization', 'OpenAI-Organization', 'User-Agent']
|
23 |
+
}));
|
24 |
+
|
25 |
+
// Handler for completions endpoint
|
26 |
+
async function handleCompletions(req, res) {
|
27 |
+
try {
|
28 |
+
const requestBody = req.body;
|
29 |
+
|
30 |
+
// Iterate through the target API URLs
|
31 |
+
for (const apiUrl of TARGET_API_URLS) {
|
32 |
+
try {
|
33 |
+
// Forward the request to the target API
|
34 |
+
const headers = {
|
35 |
+
'Content-Type': 'application/json',
|
36 |
+
};
|
37 |
+
|
38 |
+
// Forward other relevant headers from the original request
|
39 |
+
['authorization', 'openai-organization', 'user-agent'].forEach(header => {
|
40 |
+
if (req.headers[header]) {
|
41 |
+
headers[header] = req.headers[header];
|
42 |
+
}
|
43 |
+
});
|
44 |
+
|
45 |
+
const response = await fetch(`${apiUrl}/v1/chat/completions`, {
|
46 |
+
method: 'POST',
|
47 |
+
headers: headers,
|
48 |
+
body: JSON.stringify(requestBody)
|
49 |
+
});
|
50 |
+
|
51 |
+
// Check if the target API returned an error
|
52 |
+
if (!response.ok) {
|
53 |
+
console.warn(
|
54 |
+
`Error from target API ${apiUrl}:`,
|
55 |
+
response.status,
|
56 |
+
response.statusText
|
57 |
+
);
|
58 |
+
continue; // Try the next API URL
|
59 |
+
}
|
60 |
+
|
61 |
+
// Get the response from the target API
|
62 |
+
const responseBody = await response.json();
|
63 |
+
|
64 |
+
// Check if the response has content and it's not empty
|
65 |
+
if (
|
66 |
+
responseBody &&
|
67 |
+
responseBody.choices &&
|
68 |
+
responseBody.choices.length > 0 &&
|
69 |
+
responseBody.choices[0].message &&
|
70 |
+
responseBody.choices[0].message.content &&
|
71 |
+
responseBody.choices[0].message.content.trim() !== ""
|
72 |
+
) {
|
73 |
+
// Return the response to the client
|
74 |
+
return res.status(response.status).json(responseBody);
|
75 |
+
} else {
|
76 |
+
console.warn(`API ${apiUrl} returned empty or invalid content. Trying next API.`);
|
77 |
+
continue; // Try the next API URL
|
78 |
+
}
|
79 |
+
} catch (error) {
|
80 |
+
console.warn(`Error calling API ${apiUrl}:`, error);
|
81 |
+
continue; // Try the next API URL
|
82 |
+
}
|
83 |
+
}
|
84 |
+
|
85 |
+
// If all APIs failed, return an error
|
86 |
+
return res.status(500).send('All APIs failed to return valid content.');
|
87 |
+
} catch (error) {
|
88 |
+
console.error('Error handling /v1/chat/completions:', error);
|
89 |
+
return res.status(500).send('Internal Server Error');
|
90 |
+
}
|
91 |
+
}
|
92 |
+
|
93 |
+
// Setup routes
|
94 |
+
app.post('/v1/chat/completions', handleCompletions);
|
95 |
+
|
96 |
+
// Default route
|
97 |
+
app.get('/', (req, res) => {
|
98 |
+
res.send('API is running');
|
99 |
+
});
|
100 |
+
|
101 |
+
// Start the server
|
102 |
+
app.listen(PORT, () => {
|
103 |
+
console.log(`Server is running on port ${PORT}`);
|
104 |
+
});
|
105 |
+
|
106 |
+
// For Hugging Face deployment
|
107 |
+
export default app;
|
108 |
+
|