Update genisi.js
Browse files
genisi.js
CHANGED
|
@@ -8,24 +8,20 @@ import { fileURLToPath } from 'url';
|
|
| 8 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 9 |
const app = express();
|
| 10 |
|
| 11 |
-
// إعدادات البيئة
|
| 12 |
app.use(cors());
|
| 13 |
app.use(express.json());
|
| 14 |
app.use(express.static(__dirname));
|
| 15 |
|
| 16 |
-
// تعريف الموديلات المستخدمة
|
| 17 |
const MODELS = {
|
| 18 |
-
chat: "kimi-k2.5-fw", //
|
| 19 |
-
code: "gemma-4-31b",
|
| 20 |
-
image: "flux.1-dev"
|
| 21 |
};
|
| 22 |
|
| 23 |
-
/
|
| 24 |
-
* دالة توليد الصور عبر NVIDIA API
|
| 25 |
-
*/
|
| 26 |
async function generateImage(prompt) {
|
| 27 |
const NVIDIA_KEY = process.env.NVIDIA_API_KEY;
|
| 28 |
-
if (!NVIDIA_KEY) throw new Error("
|
| 29 |
|
| 30 |
const url = "https://ai.api.nvidia.com/v1/genai/black-forest-labs/flux.1-dev";
|
| 31 |
const res = await fetch(url, {
|
|
@@ -35,64 +31,62 @@ async function generateImage(prompt) {
|
|
| 35 |
"Content-Type": "application/json"
|
| 36 |
},
|
| 37 |
body: JSON.stringify({
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
})
|
| 43 |
});
|
| 44 |
|
| 45 |
const result = await res.json();
|
| 46 |
-
if (!res.ok) throw new Error(result.detail || "NVIDIA
|
| 47 |
|
| 48 |
-
|
| 49 |
-
if (result && result.result && result.result.image) {
|
| 50 |
-
return result.result.image;
|
| 51 |
-
} else if (result.image) {
|
| 52 |
-
return result.image;
|
| 53 |
-
}
|
| 54 |
-
throw new Error("لم يتم العثور على بيانات الصورة في الرد");
|
| 55 |
}
|
| 56 |
|
| 57 |
/**
|
| 58 |
-
*
|
|
|
|
| 59 |
*/
|
| 60 |
app.post('/api/genisi', async (req, res) => {
|
| 61 |
-
const { action, message } = req.body;
|
| 62 |
const POE_KEY = process.env.POE_API_KEY;
|
| 63 |
|
| 64 |
if (!POE_KEY && action !== 'image') {
|
| 65 |
-
return res.status(500).json({ error: "POE_API_KEY
|
| 66 |
}
|
| 67 |
|
| 68 |
try {
|
|
|
|
| 69 |
if (action === 'image') {
|
| 70 |
-
// معالجة الصور (بدون ستريم لأنها تعود كملف واحد)
|
| 71 |
const imageData = await generateImage(message);
|
|
|
|
| 72 |
return res.json({ success: true, type: 'image', data: imageData });
|
| 73 |
}
|
| 74 |
|
| 75 |
-
// إعداد
|
| 76 |
res.setHeader('Content-Type', 'text/event-stream');
|
| 77 |
res.setHeader('Cache-Control', 'no-cache');
|
| 78 |
res.setHeader('Connection', 'keep-alive');
|
|
|
|
| 79 |
|
| 80 |
const client = new OpenAI({
|
| 81 |
apiKey: POE_KEY,
|
| 82 |
baseURL: "https://api.poe.com/v1"
|
| 83 |
});
|
| 84 |
|
| 85 |
-
//
|
| 86 |
-
|
| 87 |
model: action === 'code' ? MODELS.code : MODELS.chat,
|
| 88 |
messages: [{ role: "user", content: message }],
|
| 89 |
stream: true,
|
|
|
|
| 90 |
};
|
| 91 |
|
| 92 |
-
// تفعيل
|
| 93 |
-
if (action === 'chat') {
|
| 94 |
requestOptions.extra_body = {
|
| 95 |
-
|
| 96 |
};
|
| 97 |
}
|
| 98 |
|
|
@@ -101,7 +95,6 @@ app.post('/api/genisi', async (req, res) => {
|
|
| 101 |
for await (const chunk of stream) {
|
| 102 |
const content = chunk.choices[0]?.delta?.content || "";
|
| 103 |
if (content) {
|
| 104 |
-
// إرسال البيانات للمتصفح فوراً
|
| 105 |
res.write(`data: ${JSON.stringify({ content })}\n\n`);
|
| 106 |
}
|
| 107 |
}
|
|
@@ -110,7 +103,7 @@ app.post('/api/genisi', async (req, res) => {
|
|
| 110 |
res.end();
|
| 111 |
|
| 112 |
} catch (err) {
|
| 113 |
-
console.error("
|
| 114 |
if (!res.headersSent) {
|
| 115 |
res.status(500).json({ success: false, error: err.message });
|
| 116 |
} else {
|
|
@@ -120,17 +113,15 @@ app.post('/api/genisi', async (req, res) => {
|
|
| 120 |
}
|
| 121 |
});
|
| 122 |
|
| 123 |
-
// تشغيل الصفحة الرئيسية
|
| 124 |
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'index.html')));
|
| 125 |
|
| 126 |
-
const PORT = 7860;
|
| 127 |
app.listen(PORT, () => {
|
| 128 |
console.log(`
|
| 129 |
-
🚀 Genisi Server
|
| 130 |
-------------------------
|
| 131 |
Port: ${PORT}
|
| 132 |
-
|
| 133 |
-
Reasoning: Kimi (Active)
|
| 134 |
Developer: AnesNT
|
| 135 |
-------------------------
|
| 136 |
`);
|
|
|
|
| 8 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 9 |
const app = express();
|
| 10 |
|
|
|
|
| 11 |
app.use(cors());
|
| 12 |
app.use(express.json());
|
| 13 |
app.use(express.static(__dirname));
|
| 14 |
|
|
|
|
| 15 |
const MODELS = {
|
| 16 |
+
chat: "kimi-k2.5-fw", // يدعم Reasoning
|
| 17 |
+
code: "gemma-4-31b", // سريع، بدون reasoning
|
| 18 |
+
image: "flux.1-dev"
|
| 19 |
};
|
| 20 |
|
| 21 |
+
// توليد الصور (بدون تغيير كبير)
|
|
|
|
|
|
|
| 22 |
async function generateImage(prompt) {
|
| 23 |
const NVIDIA_KEY = process.env.NVIDIA_API_KEY;
|
| 24 |
+
if (!NVIDIA_KEY) throw new Error("مفتاح NVIDIA API غير موجود!");
|
| 25 |
|
| 26 |
const url = "https://ai.api.nvidia.com/v1/genai/black-forest-labs/flux.1-dev";
|
| 27 |
const res = await fetch(url, {
|
|
|
|
| 31 |
"Content-Type": "application/json"
|
| 32 |
},
|
| 33 |
body: JSON.stringify({
|
| 34 |
+
prompt,
|
| 35 |
+
mode: "base",
|
| 36 |
+
cfg_scale: 3.5,
|
| 37 |
+
aspect_ratio: "1:1"
|
| 38 |
})
|
| 39 |
});
|
| 40 |
|
| 41 |
const result = await res.json();
|
| 42 |
+
if (!res.ok) throw new Error(result.detail || "خطأ في NVIDIA API");
|
| 43 |
|
| 44 |
+
return result.result?.image || result.image || null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
+
* معالجة طلبات Genisi
|
| 49 |
+
* body: { action, message, reasoning? }
|
| 50 |
*/
|
| 51 |
app.post('/api/genisi', async (req, res) => {
|
| 52 |
+
const { action, message, reasoning = false } = req.body;
|
| 53 |
const POE_KEY = process.env.POE_API_KEY;
|
| 54 |
|
| 55 |
if (!POE_KEY && action !== 'image') {
|
| 56 |
+
return res.status(500).json({ error: "مفتاح POE_API_KEY غير موجود" });
|
| 57 |
}
|
| 58 |
|
| 59 |
try {
|
| 60 |
+
// معالجة الصور (بدون stream)
|
| 61 |
if (action === 'image') {
|
|
|
|
| 62 |
const imageData = await generateImage(message);
|
| 63 |
+
if (!imageData) throw new Error("لم يتم استلام بيانات الصورة");
|
| 64 |
return res.json({ success: true, type: 'image', data: imageData });
|
| 65 |
}
|
| 66 |
|
| 67 |
+
// إعداد Streaming للنصوص
|
| 68 |
res.setHeader('Content-Type', 'text/event-stream');
|
| 69 |
res.setHeader('Cache-Control', 'no-cache');
|
| 70 |
res.setHeader('Connection', 'keep-alive');
|
| 71 |
+
res.setHeader('X-Accel-Buffering', 'no'); // منع buffering في nginx
|
| 72 |
|
| 73 |
const client = new OpenAI({
|
| 74 |
apiKey: POE_KEY,
|
| 75 |
baseURL: "https://api.poe.com/v1"
|
| 76 |
});
|
| 77 |
|
| 78 |
+
// بناء الخيارات الأساسية
|
| 79 |
+
const requestOptions = {
|
| 80 |
model: action === 'code' ? MODELS.code : MODELS.chat,
|
| 81 |
messages: [{ role: "user", content: message }],
|
| 82 |
stream: true,
|
| 83 |
+
// gemma يعمل بشكل افتراضي بدون reasoning
|
| 84 |
};
|
| 85 |
|
| 86 |
+
// تفعيل Reasoning فقط لـ Kimi وعند الطلب الصريح
|
| 87 |
+
if (action === 'chat' && reasoning) {
|
| 88 |
requestOptions.extra_body = {
|
| 89 |
+
reasoning_effort: "high" // أو "medium" حسب الرغبة
|
| 90 |
};
|
| 91 |
}
|
| 92 |
|
|
|
|
| 95 |
for await (const chunk of stream) {
|
| 96 |
const content = chunk.choices[0]?.delta?.content || "";
|
| 97 |
if (content) {
|
|
|
|
| 98 |
res.write(`data: ${JSON.stringify({ content })}\n\n`);
|
| 99 |
}
|
| 100 |
}
|
|
|
|
| 103 |
res.end();
|
| 104 |
|
| 105 |
} catch (err) {
|
| 106 |
+
console.error("Server Error:", err);
|
| 107 |
if (!res.headersSent) {
|
| 108 |
res.status(500).json({ success: false, error: err.message });
|
| 109 |
} else {
|
|
|
|
| 113 |
}
|
| 114 |
});
|
| 115 |
|
|
|
|
| 116 |
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'index.html')));
|
| 117 |
|
| 118 |
+
const PORT = process.env.PORT || 7860;
|
| 119 |
app.listen(PORT, () => {
|
| 120 |
console.log(`
|
| 121 |
+
🚀 Genisi Server Running
|
| 122 |
-------------------------
|
| 123 |
Port: ${PORT}
|
| 124 |
+
Reasoning: Kimi (On Demand) | Gemma (Fast)
|
|
|
|
| 125 |
Developer: AnesNT
|
| 126 |
-------------------------
|
| 127 |
`);
|