Spaces:
Paused
Paused
Update hf.js
Browse files
hf.js
CHANGED
|
@@ -164,38 +164,37 @@ app.get('/hf/v1/models', (req, res) => {
|
|
| 164 |
});
|
| 165 |
|
| 166 |
// 聊天完成度代理
|
| 167 |
-
app.
|
| 168 |
-
|
| 169 |
-
|
|
|
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
},
|
| 178 |
-
onProxyReq: (proxyReq, req) => {
|
| 179 |
-
// 设置 checksum header
|
| 180 |
-
proxyReq.setHeader('x-cursor-checksum', checksum);
|
| 181 |
-
|
| 182 |
-
// 如果有请求体,重写请求体
|
| 183 |
-
if (req.body) {
|
| 184 |
-
const bodyData = JSON.stringify(req.body);
|
| 185 |
-
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
|
| 186 |
-
proxyReq.write(bodyData);
|
| 187 |
}
|
| 188 |
-
}
|
| 189 |
-
onError: (err, req, res) => {
|
| 190 |
-
console.error('Proxy Error:', err);
|
| 191 |
-
res.status(500).json({ error: 'Proxy error', details: err.message });
|
| 192 |
-
}
|
| 193 |
-
});
|
| 194 |
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
});
|
| 197 |
|
| 198 |
|
|
|
|
| 199 |
// 主页路由
|
| 200 |
app.get('/', (req, res) => {
|
| 201 |
const htmlContent = `
|
|
|
|
| 164 |
});
|
| 165 |
|
| 166 |
// 聊天完成度代理
|
| 167 |
+
app.post('/hf/v1/chat/completions', async (req, res) => {
|
| 168 |
+
try {
|
| 169 |
+
// 生成 checksum
|
| 170 |
+
const checksum = `zo${getRandomIDPro({ dictType: 'max', size: 6 })}${getRandomIDPro({ dictType: 'max', size: 64 })}/${getRandomIDPro({ dictType: 'max', size: 64 })}`;
|
| 171 |
|
| 172 |
+
// 转发请求到目标服务器
|
| 173 |
+
const response = await axios.post('http://localhost:3010/v1/chat/completions', req.body, {
|
| 174 |
+
headers: {
|
| 175 |
+
...req.headers,
|
| 176 |
+
'x-cursor-checksum': checksum,
|
| 177 |
+
'host': 'localhost:3010'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
}
|
| 179 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
+
// 返回响应
|
| 182 |
+
res.status(response.status).json(response.data);
|
| 183 |
+
} catch (error) {
|
| 184 |
+
console.error('Error in chat completions:', error);
|
| 185 |
+
if (error.response) {
|
| 186 |
+
res.status(error.response.status).json(error.response.data);
|
| 187 |
+
} else {
|
| 188 |
+
res.status(500).json({
|
| 189 |
+
error: 'Internal server error',
|
| 190 |
+
message: error.message
|
| 191 |
+
});
|
| 192 |
+
}
|
| 193 |
+
}
|
| 194 |
});
|
| 195 |
|
| 196 |
|
| 197 |
+
|
| 198 |
// 主页路由
|
| 199 |
app.get('/', (req, res) => {
|
| 200 |
const htmlContent = `
|