Update server.js
Browse files
server.js
CHANGED
|
@@ -15,11 +15,14 @@ const geminiApiUrl = 'https://generativelanguage.googleapis.com/v1/models/gemini
|
|
| 15 |
|
| 16 |
const geminiTmpDir = path.join(require('os').tmpdir(), 'gemini_sessions');
|
| 17 |
const deepseekTmpDir = path.join(require('os').tmpdir(), 'deepseek_sessions');
|
|
|
|
|
|
|
| 18 |
if (!fs.existsSync(geminiTmpDir)) fs.mkdirSync(geminiTmpDir);
|
| 19 |
if (!fs.existsSync(deepseekTmpDir)) fs.mkdirSync(deepseekTmpDir);
|
|
|
|
| 20 |
|
| 21 |
function getSessionFile(sessionId, model) {
|
| 22 |
-
const dir = model === 'gemini' ? geminiTmpDir : deepseekTmpDir;
|
| 23 |
return path.join(dir, `${sessionId}.json`);
|
| 24 |
}
|
| 25 |
|
|
@@ -42,7 +45,7 @@ async function gemini(sessionId, prompt) {
|
|
| 42 |
|
| 43 |
const reply = response.data.candidates?.[0]?.content?.parts?.[0]?.text || 'No response';
|
| 44 |
history.push({ role: 'assistant', content: { parts: [{ text: reply }] } });
|
| 45 |
-
fs.writeFileSync(sessionFile,
|
| 46 |
return reply;
|
| 47 |
}
|
| 48 |
|
|
@@ -62,7 +65,27 @@ async function deepseek(sessionId, prompt) {
|
|
| 62 |
if (!data.success) throw new Error(JSON.stringify(data, null, 2));
|
| 63 |
let response = data.data.response
|
| 64 |
history.push({ role: 'assistant', content: response.split("</think>")[1]?.trim() });
|
| 65 |
-
fs.writeFileSync(sessionFile,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
return response;
|
| 67 |
}
|
| 68 |
|
|
@@ -82,7 +105,7 @@ app.all('/', (_, res) => {
|
|
| 82 |
status['memoryUsage'] =
|
| 83 |
`${utils.formatSize(totalmem - freemem)} / ${utils.formatSize(totalmem)}`
|
| 84 |
res.json({
|
| 85 |
-
router: ['/google/gemini-1.5-pro', '/deepseek-ai/deepseek-r1-distill-qwen-32b'],
|
| 86 |
uptime: new Date(process.uptime() * 1000).toUTCString().split(' ')[4],
|
| 87 |
status
|
| 88 |
})
|
|
@@ -110,4 +133,15 @@ app.get('/deepseek-ai/deepseek-r1-distill-qwen-32b', async (req, res) => {
|
|
| 110 |
}
|
| 111 |
});
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
app.listen(7860);
|
|
|
|
| 15 |
|
| 16 |
const geminiTmpDir = path.join(require('os').tmpdir(), 'gemini_sessions');
|
| 17 |
const deepseekTmpDir = path.join(require('os').tmpdir(), 'deepseek_sessions');
|
| 18 |
+
const qwenTmpDir = path.join(require('os').tmpdir(), 'qwen_sessions');
|
| 19 |
+
|
| 20 |
if (!fs.existsSync(geminiTmpDir)) fs.mkdirSync(geminiTmpDir);
|
| 21 |
if (!fs.existsSync(deepseekTmpDir)) fs.mkdirSync(deepseekTmpDir);
|
| 22 |
+
if (!fs.existsSync(qwenTmpDir)) fs.mkdirSync(qwenTmpDir);
|
| 23 |
|
| 24 |
function getSessionFile(sessionId, model) {
|
| 25 |
+
const dir = model === 'gemini' ? geminiTmpDir : deepseekTmpDir && model === 'qwen' ? qwenTmpDir : '';
|
| 26 |
return path.join(dir, `${sessionId}.json`);
|
| 27 |
}
|
| 28 |
|
|
|
|
| 45 |
|
| 46 |
const reply = response.data.candidates?.[0]?.content?.parts?.[0]?.text || 'No response';
|
| 47 |
history.push({ role: 'assistant', content: { parts: [{ text: reply }] } });
|
| 48 |
+
fs.writeFileSync(sessionFile, history);
|
| 49 |
return reply;
|
| 50 |
}
|
| 51 |
|
|
|
|
| 65 |
if (!data.success) throw new Error(JSON.stringify(data, null, 2));
|
| 66 |
let response = data.data.response
|
| 67 |
history.push({ role: 'assistant', content: response.split("</think>")[1]?.trim() });
|
| 68 |
+
fs.writeFileSync(sessionFile, history);
|
| 69 |
+
return response;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
async function qwen(sessionId, prompt) {
|
| 73 |
+
const sessionFile = getSessionFile(sessionId, 'qwen');
|
| 74 |
+
let history = [];
|
| 75 |
+
if (fs.existsSync(sessionFile)) {
|
| 76 |
+
history = JSON.parse(fs.readFileSync(sessionFile, 'utf8'));
|
| 77 |
+
}
|
| 78 |
+
history.push({ role: 'user', content: prompt });
|
| 79 |
+
|
| 80 |
+
let { data } = await axios.post("https://ai.clauodflare.workers.dev/chat", {
|
| 81 |
+
"model": "@cf/qwen/qwen1.5-0.5b-chat",
|
| 82 |
+
"messages": history
|
| 83 |
+
}).catch(e => e.response);
|
| 84 |
+
|
| 85 |
+
if (!data.success) throw new Error(JSON.stringify(data, null, 2));
|
| 86 |
+
let response = data.data.response
|
| 87 |
+
history.push({ role: 'assistant', content: response });
|
| 88 |
+
fs.writeFileSync(sessionFile, history);
|
| 89 |
return response;
|
| 90 |
}
|
| 91 |
|
|
|
|
| 105 |
status['memoryUsage'] =
|
| 106 |
`${utils.formatSize(totalmem - freemem)} / ${utils.formatSize(totalmem)}`
|
| 107 |
res.json({
|
| 108 |
+
router: ['/google/gemini-1.5-pro', '/deepseek-ai/deepseek-r1-distill-qwen-32b', '/qwen/qwen1.5-0.5b-chat'],
|
| 109 |
uptime: new Date(process.uptime() * 1000).toUTCString().split(' ')[4],
|
| 110 |
status
|
| 111 |
})
|
|
|
|
| 133 |
}
|
| 134 |
});
|
| 135 |
|
| 136 |
+
app.get('/qwen/qwen1.5-0.5b-chat', async (req, res) => {
|
| 137 |
+
try {
|
| 138 |
+
const { question, sessionId } = req.query;
|
| 139 |
+
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
|
| 140 |
+
const reply = await qwen(sessionId, question);
|
| 141 |
+
res.json({ reply });
|
| 142 |
+
} catch (error) {
|
| 143 |
+
res.status(500).json({ error: error.response?.data || error.message });
|
| 144 |
+
}
|
| 145 |
+
});
|
| 146 |
+
|
| 147 |
app.listen(7860);
|