File size: 12,172 Bytes
ae85e64 da0287c 1c1f7da a661e3d 52b826a 1f34ec9 1401140 1c1f7da da0287c c4c8bf4 8bfcbfe 1401140 8bfcbfe f5aa385 bc298bd f5aa385 8bfcbfe f41f433 bc298bd 8bfcbfe bc298bd 1906116 1c1f7da 1401140 1c1f7da b74e526 ca77827 b74e526 c4c8bf4 b74e526 c4c8bf4 b74e526 c4c8bf4 b74e526 c4c8bf4 b74e526 2af4778 b74e526 1c1f7da 1401140 d86fd7d b74e526 ca77827 b74e526 c4c8bf4 b74e526 d86fd7d c4c8bf4 b74e526 c4c8bf4 b74e526 d86fd7d 2af4778 f5aa385 e81adfd 1eeec96 bc298bd f5aa385 ca77827 f5aa385 e81adfd f5aa385 2af4778 b74e526 8bfcbfe bc298bd a661e3d 1f34ec9 a661e3d fb93ce5 a661e3d 3c547a5 1eeec96 4bc0d8d b74e526 1c1f7da 1401140 1eeec96 d86fd7d c4c8bf4 b74e526 8bfcbfe d86fd7d 1eeec96 d86fd7d e81adfd f5aa385 e81adfd bc298bd c4c8bf4 |
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
const express = require('express');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const os = require('os');
const bytes = require('bytes');
const cp = require('child_process');
//const swaggerUi = require('swagger-ui-express');
const app = express();
app.set('json spaces', 2);
const geminiApiKey = 'AIzaSyCRpX4SnNQl__BNLcVWqgzKdbIlt06vod0';
const geminiApiUrl = 'https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent';
const geminiTmpDir = path.join(require('os').tmpdir(), 'gemini_sessions');
const deepseekTmpDir = path.join(require('os').tmpdir(), 'deepseek_sessions');
const qwenTmpDir = path.join(require('os').tmpdir(), 'qwen_sessions');
const mistralTmpDir = path.join(require('os').tmpdir(), 'mistral_sessions');
if (!fs.existsSync(geminiTmpDir)) fs.mkdirSync(geminiTmpDir);
if (!fs.existsSync(deepseekTmpDir)) fs.mkdirSync(deepseekTmpDir);
if (!fs.existsSync(qwenTmpDir)) fs.mkdirSync(qwenTmpDir);
if (!fs.existsSync(mistralTmpDir)) fs.mkdirSync(mistralTmpDir);
function getSessionFile(sessionId, model) {
const dir = model === 'gemini' ? geminiTmpDir : deepseekTmpDir || model === 'qwen' ? qwenTmpDir : mistralTmpDir;
return path.join(dir, `${sessionId}.json`);
}
async function gemini(sessionId, prompt) {
const sessionFile = getSessionFile(sessionId, 'gemini');
let history = [];
if (fs.existsSync(sessionFile)) {
history = JSON.parse(fs.readFileSync(sessionFile, 'utf8'));
}
history.push({ role: 'user', content: { parts: [{ text: prompt }] } });
const response = await axios.post(`${geminiApiUrl}?key=${geminiApiKey}`, {
contents: history.map(entry => ({
role: entry.role === 'assistant' ? 'model' : 'user',
parts: entry.content.parts
}))
}, {
headers: { 'Content-Type': 'application/json' }
});
const reply = response.data.candidates?.[0]?.content?.parts?.[0]?.text || 'No response';
history.push({ role: 'assistant', content: { parts: [{ text: reply }] } });
fs.writeFileSync(sessionFile, json.stringify(history));
return reply;
}
async function deepseek(sessionId, prompt, r1 = true) {
const sessionFile = getSessionFile(sessionId, 'deepseek');
let history = [];
if (fs.existsSync(sessionFile)) {
history = JSON.parse(fs.readFileSync(sessionId, 'utf8'));
}
history.push({ role: 'user', content: prompt });
let { data } = await axios.post("https://ai.clauodflare.workers.dev/chat", {
"model": r1 ? "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b" : "@cf/deepseek-ai/deepseek-math-7b-instruct",
"messages": history
}).catch(e => e.response);
if (!data.success) throw new Error(JSON.stringify(data, null, 2));
let response = data.data.response
history.push({ role: 'assistant', content: r1 ? response.split("</think>")[1]?.trim() : response });
fs.writeFileSync(sessionFile, JSON.stringify(history));
return response;
}
async function qwen(sessionId, prompt, selModel = 0) {
const model = ["@cf/qwen/qwen1.5-0.5b-chat", "@cf/qwen/qwen1.5-1.8b-chat", "@cf/qwen/qwen1.5-14b-chat-awq", "@cf/qwen/qwen1.5-7b-chat-awq"]
const sessionFile = getSessionFile(sessionId, 'qwen');
let history = [];
if (fs.existsSync(sessionFile)) {
history = JSON.parse(fs.readFileSync(sessionFile, 'utf8'));
}
history.push({ role: 'user', content: prompt });
let { data } = await axios.post("https://ai.clauodflare.workers.dev/chat", {
"model": model[selModel],
"messages": history
}).catch(e => e.response);
if (!data.success) throw new Error(JSON.stringify(data, null, 2));
let response = data.data.response
history.push({ role: 'assistant', content: response });
fs.writeFileSync(sessionFile, JSON.stringify(history));
return response;
}
async function google(sessionId, prompt, selModel = 0) {
const model = ["@hf/google/gemma-7b-it", "@cf/google/gemma-7b-it-lora", "@cf/google/gemma-2b-it-lora"]
const sessionFile = getSessionFile(sessionId, 'qwen');
let history = [];
if (fs.existsSync(sessionFile)) {
history = JSON.parse(fs.readFileSync(sessionFile, 'utf8'));
}
history.push({ role: 'user', content: prompt });
let { data } = await axios.post("https://ai.clauodflare.workers.dev/chat", {
"model": model[selModel],
"messages": history
}).catch(e => e.response);
if (!data.success) throw new Error(JSON.stringify(data, null, 2));
let response = data.data.response
history.push({ role: 'assistant', content: response });
fs.writeFileSync(sessionFile, JSON.stringify(history));
return response;
}
async function mistral(sessionId, prompt, selModel = 0) {
const model = ["@hf/mistral/mistral-7b-instruct-v0.2", "@cf/mistral/mistral-7b-instruct-v0.2-lora", "@cf/mistral/mistral-7b-instruct-v0.1"]
const sessionFile = getSessionFile(sessionId, 'mistral');
let history = [];
if (fs.existsSync(sessionFile)) {
history = JSON.parse(fs.readFileSync(sessionFile, 'utf8'));
}
history.push({ role: 'user', content: prompt });
let { data } = await axios.post("https://ai.clauodflare.workers.dev/chat", {
"model": model[selModel],
"messages": history
}).catch(e => e.response);
if (!data.success) throw new Error(JSON.stringify(data, null, 2));
let response = data.data.response
history.push({ role: 'assistant', content: response });
fs.writeFileSync(sessionFile, JSON.stringify(history));
return response;
}
const utils = {
formatSize: (n) => bytes(+n, { unitSeparator: ' ' })
}
app.all('/', (_, res) => {
const status = {}
status['diskUsage'] = cp.execSync('du -sh').toString().split('M')[0] + ' MB'
const used = process.memoryUsage()
for (let x in used) status[x] = utils.formatSize(used[x])
const totalmem = os.totalmem()
const freemem = os.freemem()
status['memoryUsage'] =
`${utils.formatSize(totalmem - freemem)} / ${utils.formatSize(totalmem)}`
res.json({
ai: [
{
'google': [
'gemini-1.5-pro',
'gemma-7b-it',
'gemma-7b-it-lora',
'gemma-2b-it-lora'
]
},
{
'deepseek-ai': [
'deepseek-r1-distill-qwen-32b',
'deepseek-math-7b-instruct'
]
},
{
'qwen': [
'qwen1.5-0.5b-chat',
'qwen1.5-1.8b-chat',
'qwen1.5-14b-chat-awq',
'qwen1.5-7b-chat-awq'
]
},
{
'mistral': [
'mistral-7b-instruct-v0.2',
'mistral-7b-instruct-v0.2-lora',
'mistral-7b-instruct-v0.1'
]
}
],
uptime: new Date(process.uptime() * 1000).toUTCString().split(' ')[4],
status
})
})
/* Google */
app.get('/google/gemini-1.5-pro', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await gemini(sessionId, question);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
app.get('/google/gemma-7b-it', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await google(sessionId, question);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
app.get('/google/gemma-7b-it-lora', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await gemini(sessionId, question, 1);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
app.get('/google/gemma-2b-it-lora', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await gemini(sessionId, question, 2);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
/* DeepSeek-AI */
app.get('/deepseek-ai/deepseek-r1-distill-qwen-32b', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await deepseek(sessionId, question);
res.json({ reply: reply.split("</think>")?.pop()?.trim() || reply.split("</think>")[1]?.trim(), think: reply.split('</think>')[0]?.trim() });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
app.get('/depseek-ai/deepseek-math-7b-instruct', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await deepseek(sessionId, question, false);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
/* QWEN */
app.get('/qwen/qwen1.5-0.5b-chat', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await qwen(sessionId, question);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
app.get('/qwen/qwen1.5-1.8b-chat', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await qwen(sessionId, question, 1);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
app.get('/qwen/qwen1.5-14b-chat-awq', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await qwen(sessionId, question, 2);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
app.get('/qwen/qwen1.5-7b-chat-awq', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await qwen(sessionId, question, 3);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
/* Mistral */
app.get('/mistral/mistral-7b-instruct-v0.2', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await mistral(sessionId, question);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
app.get('/mistral/mistral-7b-instruct-v0.2-lora', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await mistral(sessionId, question, 1);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
app.get('/mistral/mistral-7b-instruct-v0.1', async (req, res) => {
try {
const { question, sessionId } = req.query;
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
const reply = await mistral(sessionId, question, 2);
res.json({ reply });
} catch (error) {
res.status(500).json({ error: error.response?.data || error.message });
}
});
app.listen(7860); |