Update server.js
Browse files
server.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
-
const express = require('express');
|
| 2 |
const axios = require('axios');
|
| 3 |
const fs = require('fs');
|
| 4 |
const path = require('path');
|
| 5 |
-
const swaggerUi = require('swagger-ui-express');
|
| 6 |
-
const swaggerJsDoc = require('swagger-jsdoc');
|
| 7 |
|
| 8 |
const app = express();
|
| 9 |
app.set('json spaces', 2);
|
| 10 |
|
| 11 |
-
const geminiApiKey = '
|
| 12 |
const geminiApiUrl = 'https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent';
|
| 13 |
|
| 14 |
const geminiTmpDir = path.join(require('os').tmpdir(), 'gemini_sessions');
|
|
@@ -16,125 +14,76 @@ const deepseekTmpDir = path.join(require('os').tmpdir(), 'deepseek_sessions');
|
|
| 16 |
if (!fs.existsSync(geminiTmpDir)) fs.mkdirSync(geminiTmpDir);
|
| 17 |
if (!fs.existsSync(deepseekTmpDir)) fs.mkdirSync(deepseekTmpDir);
|
| 18 |
|
| 19 |
-
const swaggerOptions = {
|
| 20 |
-
swaggerDefinition: {
|
| 21 |
-
openapi: '3.0.0',
|
| 22 |
-
info: {
|
| 23 |
-
title: 'scnario AI',
|
| 24 |
-
version: '1.0.0',
|
| 25 |
-
description: 'Free AI.'
|
| 26 |
-
},
|
| 27 |
-
servers: [{ url: 'https://scnario-gemini.hf.space' }],
|
| 28 |
-
},
|
| 29 |
-
apis: [__filename],
|
| 30 |
-
};
|
| 31 |
-
|
| 32 |
-
const swaggerDocs = swaggerJsDoc(swaggerOptions);
|
| 33 |
-
app.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
|
| 34 |
-
|
| 35 |
function getSessionFile(sessionId, model) {
|
| 36 |
-
|
| 37 |
-
|
| 38 |
}
|
| 39 |
|
| 40 |
async function gemini(sessionId, prompt) {
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
const response = await axios.post(`${geminiApiUrl}?key=${geminiApiKey}`, {
|
| 46 |
-
contents: history.map(entry => ({
|
| 47 |
-
role: entry.role,
|
| 48 |
-
parts: entry.content.parts
|
| 49 |
-
}))
|
| 50 |
-
}, {
|
| 51 |
-
headers: { 'Content-Type': 'application/json' }
|
| 52 |
-
});
|
| 53 |
-
|
| 54 |
-
const reply = response.data.candidates?.[0]?.content?.parts?.[0]?.text || 'No response';
|
| 55 |
-
history.push({ role: 'assistant', content: { parts: [{ text: reply }] } });
|
| 56 |
-
fs.writeFileSync(sessionFile, JSON.stringify(history, null, 2));
|
| 57 |
-
return reply;
|
| 58 |
}
|
| 59 |
|
| 60 |
async function deepseek(sessionId, prompt) {
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
let { data } = await axios.post("https://ai.clauodflare.workers.dev/chat", {
|
| 66 |
-
"model": "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
|
| 67 |
-
"messages": history
|
| 68 |
-
}).catch(e => e.response);
|
| 69 |
-
|
| 70 |
-
if (!data.success) throw new Error(JSON.stringify(data, null, 2));
|
| 71 |
-
let response = data.data.response
|
| 72 |
-
history.push({ role: 'assistant', content: response });
|
| 73 |
-
fs.writeFileSync(sessionFile, JSON.stringify(history, null, 2));
|
| 74 |
-
return response;
|
| 75 |
}
|
| 76 |
|
| 77 |
-
/**
|
| 78 |
-
* @swagger
|
| 79 |
-
* /gemini:
|
| 80 |
-
* get:
|
| 81 |
-
* summary: Get response from Gemini AI
|
| 82 |
-
* parameters:
|
| 83 |
-
* - in: query
|
| 84 |
-
* name: question
|
| 85 |
-
* required: true
|
| 86 |
-
* schema:
|
| 87 |
-
* type: string
|
| 88 |
-
* - in: query
|
| 89 |
-
* name: sessionId
|
| 90 |
-
* required: true
|
| 91 |
-
* schema:
|
| 92 |
-
* type: string
|
| 93 |
-
* responses:
|
| 94 |
-
* 200:
|
| 95 |
-
* reply: Lorem ipsum dolor sit amet
|
| 96 |
-
*/
|
| 97 |
app.get('/gemini', async (req, res) => {
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
});
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
* schema:
|
| 118 |
-
* type: string
|
| 119 |
-
* - in: query
|
| 120 |
-
* name: sessionId
|
| 121 |
-
* required: true
|
| 122 |
-
* schema:
|
| 123 |
-
* type: string
|
| 124 |
-
* responses:
|
| 125 |
-
* 200:
|
| 126 |
-
* reply: Lorem ipsum dolor sit amet
|
| 127 |
-
* think: Lorem ipsum dolor sit amet
|
| 128 |
-
*/
|
| 129 |
-
app.get('/deepseek-ai/r1', async (req, res) => {
|
| 130 |
-
try {
|
| 131 |
-
const { question, sessionId } = req.query;
|
| 132 |
-
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
|
| 133 |
-
const reply = await deepseek(sessionId, question);
|
| 134 |
-
res.json({ reply });
|
| 135 |
-
} catch (error) {
|
| 136 |
-
res.status(500).json({ error: error.response?.data || error.message });
|
| 137 |
-
}
|
| 138 |
});
|
| 139 |
|
| 140 |
-
app.listen(7860
|
|
|
|
| 1 |
+
Pakai ini const express = require('express');
|
| 2 |
const axios = require('axios');
|
| 3 |
const fs = require('fs');
|
| 4 |
const path = require('path');
|
|
|
|
|
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
app.set('json spaces', 2);
|
| 8 |
|
| 9 |
+
const geminiApiKey = 'AIzaSyCRpX4SnNQl__BNLcVWqgzKdbIlt06vod0';
|
| 10 |
const geminiApiUrl = 'https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent';
|
| 11 |
|
| 12 |
const geminiTmpDir = path.join(require('os').tmpdir(), 'gemini_sessions');
|
|
|
|
| 14 |
if (!fs.existsSync(geminiTmpDir)) fs.mkdirSync(geminiTmpDir);
|
| 15 |
if (!fs.existsSync(deepseekTmpDir)) fs.mkdirSync(deepseekTmpDir);
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
function getSessionFile(sessionId, model) {
|
| 18 |
+
const dir = model === 'gemini' ? geminiTmpDir : deepseekTmpDir;
|
| 19 |
+
return path.join(dir, ${sessionId}.json);
|
| 20 |
}
|
| 21 |
|
| 22 |
async function gemini(sessionId, prompt) {
|
| 23 |
+
const sessionFile = getSessionFile(sessionId, 'gemini');
|
| 24 |
+
let history = [];
|
| 25 |
+
if (fs.existsSync(sessionFile)) {
|
| 26 |
+
history = JSON.parse(fs.readFileSync(sessionFile, 'utf8'));
|
| 27 |
+
}
|
| 28 |
+
history.push({ role: 'user', content: { parts: [{ text: prompt }] } });
|
| 29 |
+
|
| 30 |
+
const response = await axios.post(`${geminiApiUrl}?key=${geminiApiKey}`, {
|
| 31 |
+
contents: history.map(entry => ({
|
| 32 |
+
role: entry.role === 'assistant' ? 'model' : 'user',
|
| 33 |
+
parts: entry.content.parts
|
| 34 |
+
}))
|
| 35 |
+
}, {
|
| 36 |
+
headers: { 'Content-Type': 'application/json' }
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
const reply = response.data.candidates?.[0]?.content?.parts?.[0]?.text || 'No response';
|
| 40 |
+
history.push({ role: 'assistant', content: { parts: [{ text: reply }] } });
|
| 41 |
+
fs.writeFileSync(sessionFile, JSON.stringify(history, null, 2));
|
| 42 |
+
return reply;
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
async function deepseek(sessionId, prompt) {
|
| 47 |
+
const sessionFile = getSessionFile(sessionId, 'deepseek');
|
| 48 |
+
let history = [];
|
| 49 |
+
if (fs.existsSync(sessionFile)) {
|
| 50 |
+
history = JSON.parse(fs.readFileSync(sessionFile, 'utf8'));
|
| 51 |
+
}
|
| 52 |
+
history.push({ role: 'user', content: prompt });
|
| 53 |
+
|
| 54 |
+
let { data } = await axios.post("https://ai.clauodflare.workers.dev/chat", {
|
| 55 |
+
"model": "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
|
| 56 |
+
"messages": history
|
| 57 |
+
}).catch(e => e.response);
|
| 58 |
+
|
| 59 |
+
if (!data.success) throw new Error(JSON.stringify(data, null, 2));
|
| 60 |
+
let response = data.data.response
|
| 61 |
+
history.push({ role: 'assistant', content: response });
|
| 62 |
+
fs.writeFileSync(sessionFile, JSON.stringify(history, null, 2));
|
| 63 |
+
return response;
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
}
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
app.get('/gemini', async (req, res) => {
|
| 68 |
+
try {
|
| 69 |
+
const { question, sessionId } = req.query;
|
| 70 |
+
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
|
| 71 |
+
const reply = await gemini(sessionId, question);
|
| 72 |
+
res.json({ reply });
|
| 73 |
+
} catch (error) {
|
| 74 |
+
res.status(500).json({ error: error.response?.data || error.message });
|
| 75 |
+
}
|
| 76 |
});
|
| 77 |
|
| 78 |
+
app.get('/deepseek-ai/deepseek-r1-distill-qwen-32b', async (req, res) => {
|
| 79 |
+
try {
|
| 80 |
+
const { question, sessionId } = req.query;
|
| 81 |
+
if (!question || !sessionId) return res.status(400).json({ error: 'Missing parameters' });
|
| 82 |
+
const reply = await deepseek(sessionId, question);
|
| 83 |
+
res.json({ reply: reply.split("</think>")?.pop()?.trim() || reply.split("</think>")[1]?.trim(), think: reply.split('</think>')[0]?.trim() });
|
| 84 |
+
} catch (error) {
|
| 85 |
+
res.status(500).json({ error: error.response?.data || error.message });
|
| 86 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
});
|
| 88 |
|
| 89 |
+
app.listen(7860);
|