Update genisi.js
Browse files
genisi.js
CHANGED
|
@@ -13,12 +13,57 @@ app.use(express.json());
|
|
| 13 |
app.use(express.static(__dirname));
|
| 14 |
|
| 15 |
const MODELS = {
|
| 16 |
-
chat: "kimi-k2.5-fw",
|
| 17 |
-
code: "gemma-4-31b",
|
| 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 غير موجود!");
|
|
@@ -45,46 +90,60 @@ async function generateImage(prompt) {
|
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
-
* معالجة طلبات
|
| 49 |
-
* body: { action, message, reasoning? }
|
| 50 |
*/
|
| 51 |
app.post('/api/genisi', async (req, res) => {
|
| 52 |
-
const { action, message
|
| 53 |
const POE_KEY = process.env.POE_API_KEY;
|
| 54 |
|
| 55 |
if (!POE_KEY && action !== 'image') {
|
| 56 |
-
return res.status(500).json({ error: "
|
| 57 |
}
|
| 58 |
|
| 59 |
try {
|
| 60 |
-
// معالجة الصور
|
| 61 |
if (action === 'image') {
|
| 62 |
const imageData = await generateImage(message);
|
| 63 |
if (!imageData) throw new Error("لم يتم استلام بيانات الصورة");
|
| 64 |
-
return res.json({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|
| 81 |
messages: [{ role: "user", content: message }],
|
| 82 |
stream: true,
|
| 83 |
-
// gemma يعمل بشكل افتراضي بدون reasoning
|
| 84 |
};
|
| 85 |
|
| 86 |
-
// تفعيل
|
| 87 |
-
if (
|
| 88 |
requestOptions.extra_body = {
|
| 89 |
reasoning_effort: "high" // أو "medium" حسب الرغبة
|
| 90 |
};
|
|
@@ -103,7 +162,7 @@ app.post('/api/genisi', async (req, res) => {
|
|
| 103 |
res.end();
|
| 104 |
|
| 105 |
} catch (err) {
|
| 106 |
-
console.error("
|
| 107 |
if (!res.headersSent) {
|
| 108 |
res.status(500).json({ success: false, error: err.message });
|
| 109 |
} else {
|
|
@@ -118,11 +177,11 @@ app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'index.html')));
|
|
| 118 |
const PORT = process.env.PORT || 7860;
|
| 119 |
app.listen(PORT, () => {
|
| 120 |
console.log(`
|
| 121 |
-
|
| 122 |
-
-------------------------
|
| 123 |
Port: ${PORT}
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
-------------------------
|
| 127 |
`);
|
| 128 |
});
|
|
|
|
| 13 |
app.use(express.static(__dirname));
|
| 14 |
|
| 15 |
const MODELS = {
|
| 16 |
+
chat: "kimi-k2.5-fw",
|
| 17 |
+
code: "gemma-4-31b",
|
| 18 |
image: "flux.1-dev"
|
| 19 |
};
|
| 20 |
|
| 21 |
+
/**
|
| 22 |
+
* محلل ذكي يقرر متى نستخدم التفكير العميق
|
| 23 |
+
*/
|
| 24 |
+
function shouldUseReasoning(message) {
|
| 25 |
+
const text = message.toLowerCase();
|
| 26 |
+
|
| 27 |
+
// كلمات مفتاحية تستدعي التفكير العميق
|
| 28 |
+
const reasoningKeywords = [
|
| 29 |
+
// رياضيات ومنطق
|
| 30 |
+
'solve', 'equation', 'calculate', 'math', 'logic', 'puzzle', 'proof',
|
| 31 |
+
'proof', 'احسب', 'معادلة', 'مسألة', 'حل', 'رياضيات', 'منطق', 'لغز',
|
| 32 |
+
|
| 33 |
+
// برمجة معقدة
|
| 34 |
+
'debug', 'error', 'fix', 'optimize', 'refactor', 'complex algorithm',
|
| 35 |
+
'خطأ', 'إصلاح', 'تصحيح', 'أخطاء', 'معقد', 'algorithm',
|
| 36 |
+
|
| 37 |
+
// تحليل و مقارنات
|
| 38 |
+
'analyze deeply', 'compare', 'contrast', 'difference between',
|
| 39 |
+
'pros and cons', 'تحليل', 'قارن', 'مقارنة', 'فرق بين', 'إيجابيات وسلبيات',
|
| 40 |
+
|
| 41 |
+
// استراتيجيات
|
| 42 |
+
'strategy', 'plan', 'approach', 'best way to', 'how to structure',
|
| 43 |
+
'استراتيجية', 'خطة', 'أفضل طريقة', 'كيف أنظم',
|
| 44 |
+
|
| 45 |
+
// أسئلة متعددة الخطوات
|
| 46 |
+
'step by step', 'explain why', 'reason', 'justify',
|
| 47 |
+
'خطوة بخطوة', 'اشرح لماذا', 'برر', 'أعطني سبب'
|
| 48 |
+
];
|
| 49 |
+
|
| 50 |
+
// إذا كان السؤال طويلاً (>200 حرف) غالباً يحتاج تفكير
|
| 51 |
+
const isComplexLength = message.length > 200;
|
| 52 |
+
|
| 53 |
+
// فحص وجود كلمات مفتاحية
|
| 54 |
+
const hasKeywords = reasoningKeywords.some(keyword => text.includes(keyword));
|
| 55 |
+
|
| 56 |
+
// فحص إذا كان السؤال يحتوي على أرقام (غالباً حسابات)
|
| 57 |
+
const hasNumbers = /\d+/.test(text);
|
| 58 |
+
|
| 59 |
+
// القرار: إذا تحقق شرطان أو أكثر
|
| 60 |
+
return (hasKeywords && hasNumbers) || (hasKeywords && isComplexLength) ||
|
| 61 |
+
(isComplexLength && text.includes('?') && hasNumbers);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
/**
|
| 65 |
+
* توليد الصور (NVIDIA)
|
| 66 |
+
*/
|
| 67 |
async function generateImage(prompt) {
|
| 68 |
const NVIDIA_KEY = process.env.NVIDIA_API_KEY;
|
| 69 |
if (!NVIDIA_KEY) throw new Error("مفتاح NVIDIA API غير موجود!");
|
|
|
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
+
* معالجة الطلبات الرئيسية
|
|
|
|
| 94 |
*/
|
| 95 |
app.post('/api/genisi', async (req, res) => {
|
| 96 |
+
const { action, message } = req.body;
|
| 97 |
const POE_KEY = process.env.POE_API_KEY;
|
| 98 |
|
| 99 |
if (!POE_KEY && action !== 'image') {
|
| 100 |
+
return res.status(500).json({ error: "POE_API_KEY غير موجود" });
|
| 101 |
}
|
| 102 |
|
| 103 |
try {
|
| 104 |
+
// معالجة الصور
|
| 105 |
if (action === 'image') {
|
| 106 |
const imageData = await generateImage(message);
|
| 107 |
if (!imageData) throw new Error("لم يتم استلام بيانات الصورة");
|
| 108 |
+
return res.json({
|
| 109 |
+
success: true,
|
| 110 |
+
type: 'image',
|
| 111 |
+
data: imageData
|
| 112 |
+
});
|
| 113 |
}
|
| 114 |
|
| 115 |
+
// إعداد Streaming
|
| 116 |
res.setHeader('Content-Type', 'text/event-stream');
|
| 117 |
res.setHeader('Cache-Control', 'no-cache');
|
| 118 |
res.setHeader('Connection', 'keep-alive');
|
|
|
|
| 119 |
|
| 120 |
const client = new OpenAI({
|
| 121 |
apiKey: POE_KEY,
|
| 122 |
baseURL: "https://api.poe.com/v1"
|
| 123 |
});
|
| 124 |
|
| 125 |
+
// تحديد النموذج والإعدادات
|
| 126 |
+
const model = action === 'code' ? MODELS.code : MODELS.chat;
|
| 127 |
+
|
| 128 |
+
// ✅ القرار الذكي: هل نفعل التفكير؟ (فقط لـ Kimi)
|
| 129 |
+
let useReasoning = false;
|
| 130 |
+
if (action === 'chat') {
|
| 131 |
+
useReasoning = shouldUseReasoning(message);
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// إرسال معلومة للـ Frontend أننا نستخدم التفكير (للعرض)
|
| 135 |
+
if (useReasoning) {
|
| 136 |
+
res.write(`data: ${JSON.stringify({ meta: { reasoning: true } })}\n\n`);
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
const requestOptions = {
|
| 140 |
+
model: model,
|
| 141 |
messages: [{ role: "user", content: message }],
|
| 142 |
stream: true,
|
|
|
|
| 143 |
};
|
| 144 |
|
| 145 |
+
// تفعيل التفكير إذا قررنا أنه ضروري
|
| 146 |
+
if (useReasoning) {
|
| 147 |
requestOptions.extra_body = {
|
| 148 |
reasoning_effort: "high" // أو "medium" حسب الرغبة
|
| 149 |
};
|
|
|
|
| 162 |
res.end();
|
| 163 |
|
| 164 |
} catch (err) {
|
| 165 |
+
console.error("Error:", err);
|
| 166 |
if (!res.headersSent) {
|
| 167 |
res.status(500).json({ success: false, error: err.message });
|
| 168 |
} else {
|
|
|
|
| 177 |
const PORT = process.env.PORT || 7860;
|
| 178 |
app.listen(PORT, () => {
|
| 179 |
console.log(`
|
| 180 |
+
🧠 Genisi AI - Auto-Reasoning Mode
|
| 181 |
+
----------------------------------
|
| 182 |
Port: ${PORT}
|
| 183 |
+
Kimi: يختار التفكير تلقائياً
|
| 184 |
+
Gemma: دائماً سريع (بدون تفكير)
|
| 185 |
+
----------------------------------
|
| 186 |
`);
|
| 187 |
});
|