Update genisi.js
Browse files
genisi.js
CHANGED
|
@@ -3,22 +3,49 @@ import { OpenAI } from "openai";
|
|
| 3 |
import cors from 'cors';
|
| 4 |
import path from 'path';
|
| 5 |
import { fileURLToPath } from 'url';
|
|
|
|
| 6 |
|
| 7 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 8 |
const app = express();
|
|
|
|
| 9 |
|
| 10 |
app.use(cors());
|
| 11 |
app.use(express.json());
|
| 12 |
app.use(express.static(__dirname));
|
| 13 |
|
| 14 |
-
// ✅ وضع ال
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
app.post('/api/think', async (req, res) => {
|
| 16 |
const { message } = req.body;
|
| 17 |
const POE_KEY = process.env.POE_API_KEY;
|
| 18 |
|
| 19 |
-
if (!POE_KEY) {
|
| 20 |
-
return res.status(401).json({ error: "POE_API_KEY غير موجود" });
|
| 21 |
-
}
|
| 22 |
|
| 23 |
res.setHeader('Content-Type', 'text/event-stream');
|
| 24 |
res.setHeader('Cache-Control', 'no-cache');
|
|
@@ -29,30 +56,23 @@ app.post('/api/think', async (req, res) => {
|
|
| 29 |
});
|
| 30 |
|
| 31 |
try {
|
| 32 |
-
// إرسال إشارة بدء التفكير أولاً
|
| 33 |
res.write(`data: ${JSON.stringify({ status: "thinking" })}\n\n`);
|
| 34 |
-
|
| 35 |
const stream = await client.chat.completions.create({
|
| 36 |
-
model: "kimi-k2.5-fw",
|
| 37 |
messages: [{ role: "user", content: message }],
|
| 38 |
-
stream: true
|
| 39 |
-
temperature: 0.7
|
| 40 |
-
// لا نحاول تعطيل التفكير هنا - نتركه يفكر
|
| 41 |
});
|
| 42 |
|
| 43 |
-
// إرسال إشارة بدء الرد الفعلي
|
| 44 |
res.write(`data: ${JSON.stringify({ status: "responding" })}\n\n`);
|
| 45 |
|
| 46 |
for await (const chunk of stream) {
|
| 47 |
const content = chunk.choices[0]?.delta?.content || "";
|
| 48 |
-
if (content) {
|
| 49 |
-
res.write(`data: ${JSON.stringify({ content })}\n\n`);
|
| 50 |
-
}
|
| 51 |
}
|
| 52 |
|
| 53 |
res.write('data: [DONE]\n\n');
|
| 54 |
res.end();
|
| 55 |
-
|
| 56 |
} catch (err) {
|
| 57 |
res.write(`data: ${JSON.stringify({ error: err.message })}\n\n`);
|
| 58 |
res.end();
|
|
@@ -63,5 +83,11 @@ app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'index.html')));
|
|
| 63 |
|
| 64 |
const PORT = process.env.PORT || 7860;
|
| 65 |
app.listen(PORT, () => {
|
| 66 |
-
console.log(`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
});
|
|
|
|
| 3 |
import cors from 'cors';
|
| 4 |
import path from 'path';
|
| 5 |
import { fileURLToPath } from 'url';
|
| 6 |
+
import { G4F } from 'g4f'; // npm install g4f
|
| 7 |
|
| 8 |
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
| 9 |
const app = express();
|
| 10 |
+
const g4f = new G4F(); // ✅ إنشاء عميل G4F مرة واحدة
|
| 11 |
|
| 12 |
app.use(cors());
|
| 13 |
app.use(express.json());
|
| 14 |
app.use(express.static(__dirname));
|
| 15 |
|
| 16 |
+
// ✅ الوضع السريع: G4F (مجاني، لا يحتاج مفتاح)
|
| 17 |
+
app.post('/api/fast', async (req, res) => {
|
| 18 |
+
const { message } = req.body;
|
| 19 |
+
|
| 20 |
+
try {
|
| 21 |
+
// G4F تدعم نماذج متعددة: gpt-4, claude, llama, etc.
|
| 22 |
+
const response = await g4f.chat.completions.create({
|
| 23 |
+
model: "gpt-4", // أو 'claude', 'llama-3-70b'
|
| 24 |
+
messages: [{ role: "user", content: message }],
|
| 25 |
+
// يمكن إضافة streaming: true إذا أردت
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
+
res.json({
|
| 29 |
+
success: true,
|
| 30 |
+
content: response.choices[0].message.content,
|
| 31 |
+
model: "G4F-Azure" // للتعريف
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
} catch (err) {
|
| 35 |
+
console.error('G4F Error:', err);
|
| 36 |
+
res.status(500).json({
|
| 37 |
+
success: false,
|
| 38 |
+
error: "فشل الاتصال بـ G4F: " + err.message
|
| 39 |
+
});
|
| 40 |
+
}
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
// ✅ الوضع المفكر: Kimi via Poe API
|
| 44 |
app.post('/api/think', async (req, res) => {
|
| 45 |
const { message } = req.body;
|
| 46 |
const POE_KEY = process.env.POE_API_KEY;
|
| 47 |
|
| 48 |
+
if (!POE_KEY) return res.status(401).json({ error: "POE_API_KEY missing" });
|
|
|
|
|
|
|
| 49 |
|
| 50 |
res.setHeader('Content-Type', 'text/event-stream');
|
| 51 |
res.setHeader('Cache-Control', 'no-cache');
|
|
|
|
| 56 |
});
|
| 57 |
|
| 58 |
try {
|
|
|
|
| 59 |
res.write(`data: ${JSON.stringify({ status: "thinking" })}\n\n`);
|
| 60 |
+
|
| 61 |
const stream = await client.chat.completions.create({
|
| 62 |
+
model: "kimi-k2.5-fw",
|
| 63 |
messages: [{ role: "user", content: message }],
|
| 64 |
+
stream: true
|
|
|
|
|
|
|
| 65 |
});
|
| 66 |
|
|
|
|
| 67 |
res.write(`data: ${JSON.stringify({ status: "responding" })}\n\n`);
|
| 68 |
|
| 69 |
for await (const chunk of stream) {
|
| 70 |
const content = chunk.choices[0]?.delta?.content || "";
|
| 71 |
+
if (content) res.write(`data: ${JSON.stringify({ content })}\n\n`);
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
|
| 74 |
res.write('data: [DONE]\n\n');
|
| 75 |
res.end();
|
|
|
|
| 76 |
} catch (err) {
|
| 77 |
res.write(`data: ${JSON.stringify({ error: err.message })}\n\n`);
|
| 78 |
res.end();
|
|
|
|
| 83 |
|
| 84 |
const PORT = process.env.PORT || 7860;
|
| 85 |
app.listen(PORT, () => {
|
| 86 |
+
console.log(`
|
| 87 |
+
🚀 Dual Mode Server Ready
|
| 88 |
+
---------------------------
|
| 89 |
+
⚡ Fast Mode: http://localhost:${PORT}/api/fast (G4F - Free)
|
| 90 |
+
🧠 Think Mode: http://localhost:${PORT}/api/think (Kimi - Poe)
|
| 91 |
+
---------------------------
|
| 92 |
+
`);
|
| 93 |
});
|