File size: 3,197 Bytes
ab05c0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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;