zaiwen2 / app.js
yuoop's picture
Create app.js
ab05c0e verified
raw
history blame
3.2 kB
// Import required packages for Hugging Face
import express from 'express';
import cors from 'cors';
import fetch from 'node-fetch';
const app = express();
const PORT = process.env.PORT || 7860; // Hugging Face uses port 7860 by default
// Target API URLs remain the same
const TARGET_API_URLS = [
"https://gpt4fc.deno.dev/zaiwen",
"https://gpt4fc2.deno.dev/zaiwen",
"https://gpt4fc3.deno.dev/zaiwen",
"https://gpt4fc4.deno.dev/zaiwen",
];
// Middleware setup
app.use(express.json());
app.use(cors({
origin: '*',
methods: ['POST', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'OpenAI-Organization', 'User-Agent']
}));
// Handler for completions endpoint
async function handleCompletions(req, res) {
try {
const requestBody = req.body;
// Iterate through the target API URLs
for (const apiUrl of TARGET_API_URLS) {
try {
// Forward the request to the target API
const headers = {
'Content-Type': 'application/json',
};
// Forward other relevant headers from the original request
['authorization', 'openai-organization', 'user-agent'].forEach(header => {
if (req.headers[header]) {
headers[header] = req.headers[header];
}
});
const response = await fetch(`${apiUrl}/v1/chat/completions`, {
method: 'POST',
headers: headers,
body: JSON.stringify(requestBody)
});
// Check if the target API returned an error
if (!response.ok) {
console.warn(
`Error from target API ${apiUrl}:`,
response.status,
response.statusText
);
continue; // Try the next API URL
}
// Get the response from the target API
const responseBody = await response.json();
// Check if the response has content and it's not empty
if (
responseBody &&
responseBody.choices &&
responseBody.choices.length > 0 &&
responseBody.choices[0].message &&
responseBody.choices[0].message.content &&
responseBody.choices[0].message.content.trim() !== ""
) {
// Return the response to the client
return res.status(response.status).json(responseBody);
} else {
console.warn(`API ${apiUrl} returned empty or invalid content. Trying next API.`);
continue; // Try the next API URL
}
} catch (error) {
console.warn(`Error calling API ${apiUrl}:`, error);
continue; // Try the next API URL
}
}
// If all APIs failed, return an error
return res.status(500).send('All APIs failed to return valid content.');
} catch (error) {
console.error('Error handling /v1/chat/completions:', error);
return res.status(500).send('Internal Server Error');
}
}
// Setup routes
app.post('/v1/chat/completions', handleCompletions);
// Default route
app.get('/', (req, res) => {
res.send('API is running');
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// For Hugging Face deployment
export default app;