Spaces:
Running
Running
Update router/api.js
Browse files- router/api.js +97 -1
router/api.js
CHANGED
@@ -1 +1,97 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import express from 'express';
|
2 |
+
import axios from 'axios';
|
3 |
+
import crypto from 'crypto';
|
4 |
+
import FormData from 'form-data'; // Import modul form-data untuk menghandle multipart
|
5 |
+
import Groq from 'groq-sdk';
|
6 |
+
|
7 |
+
const APIrouter = express.Router();
|
8 |
+
|
9 |
+
const ISauthenticate = async (req, res, next) => {
|
10 |
+
try {
|
11 |
+
// Contoh sederhana: menggunakan menit UTC sebagai secret
|
12 |
+
const secretResponse = new Date().getUTCMinutes().toString();
|
13 |
+
const generatedApiKey = generateApiKey(secretResponse);
|
14 |
+
console.log(generatedApiKey);
|
15 |
+
console.log(req.headers);
|
16 |
+
const authHeader = req.headers['authorization'];
|
17 |
+
if (!authHeader || authHeader !== `Bearer ${generatedApiKey}`) {
|
18 |
+
return res.status(403).json({ success: false, message: 'Unauthorized' });
|
19 |
+
}
|
20 |
+
next();
|
21 |
+
} catch (error) {
|
22 |
+
console.error('Authentication error:', error);
|
23 |
+
return res.status(500).json({ success: false, message: 'Internal Server Error' });
|
24 |
+
}
|
25 |
+
};
|
26 |
+
|
27 |
+
// Inisialisasi Groq SDK
|
28 |
+
const client = new Groq({
|
29 |
+
apiKey: process.env.GROQ_API_KEY || "",
|
30 |
+
dangerouslyAllowBrowser: true,
|
31 |
+
});
|
32 |
+
|
33 |
+
APIrouter.get('/', (req, res) => {
|
34 |
+
res.send('Hello World');
|
35 |
+
});
|
36 |
+
|
37 |
+
APIrouter.post('/gpt/completions', ISauthenticate, async (req, res) => {
|
38 |
+
console.log(req.body);
|
39 |
+
const { messages, model, temperature, max_completion_tokens, top_p, stream, stop } = req.body;
|
40 |
+
try {
|
41 |
+
|
42 |
+
// Atur header untuk streaming
|
43 |
+
res.setHeader('Content-Type', 'text/event-stream');
|
44 |
+
res.setHeader('Cache-Control', 'no-cache');
|
45 |
+
res.setHeader('Connection', 'keep-alive');
|
46 |
+
|
47 |
+
const chatStream = await client.chat.completions.create({
|
48 |
+
messages,
|
49 |
+
model,
|
50 |
+
temperature,
|
51 |
+
max_completion_tokens,
|
52 |
+
top_p,
|
53 |
+
stream,
|
54 |
+
stop,
|
55 |
+
});
|
56 |
+
|
57 |
+
// Atau jika perlu mengatur secara manual:
|
58 |
+
chatStream.on('data', chunk => {
|
59 |
+
res.write(`data: ${chunk}\n\n`);
|
60 |
+
});
|
61 |
+
chatStream.on('end', () => {
|
62 |
+
res.write('data: [DONE]\n\n');
|
63 |
+
res.end();
|
64 |
+
});
|
65 |
+
chatStream.on('error', err => {
|
66 |
+
console.error('Stream error:', err);
|
67 |
+
res.end();
|
68 |
+
});
|
69 |
+
} catch (error) {
|
70 |
+
console.error('GPT completion error:', error);
|
71 |
+
res.status(500).json({ message: 'Internal Server Error' });
|
72 |
+
}
|
73 |
+
});
|
74 |
+
|
75 |
+
APIrouter.get('/gpt/modellist', ISauthenticate, async (req, res) => {
|
76 |
+
try {
|
77 |
+
const models = await client.models.list();
|
78 |
+
res.json(models);
|
79 |
+
} catch (error) {
|
80 |
+
console.error('GPT completion error:', error);
|
81 |
+
res.status(500).json({ message: 'Internal Server Error' });
|
82 |
+
}
|
83 |
+
});
|
84 |
+
|
85 |
+
export default APIrouter;
|
86 |
+
|
87 |
+
function generateApiKey(secret) {
|
88 |
+
// Ambil menit saat ini sebagai nilai integer
|
89 |
+
const currentMinute = Math.floor(Date.now() / 60000).toString();
|
90 |
+
|
91 |
+
// Buat HMAC menggunakan algoritma SHA256 dan secret key
|
92 |
+
const hmac = crypto.createHmac('sha256', secret);
|
93 |
+
hmac.update(currentMinute);
|
94 |
+
|
95 |
+
// Kembalikan hash dalam format hexadecimal
|
96 |
+
return hmac.digest('hex');
|
97 |
+
}
|