Update index.js
Browse files
index.js
CHANGED
@@ -1,64 +1,71 @@
|
|
1 |
-
/**
|
2 |
-
* Sunny - OpenAI-like API Server for test your code (only NODEJS)
|
3 |
-
*/
|
4 |
const express = require('express');
|
|
|
|
|
|
|
5 |
const app = express();
|
6 |
const port = 7860;
|
7 |
-
|
8 |
app.use(bodyParser.json());
|
9 |
-
app.use(bodyParser.urlencoded({ extended: true }));
|
10 |
-
app.get("/", (req, res) => {
|
11 |
-
res.set('Content-Type', 'text/plain');
|
12 |
-
res.send(`☀️ Sunny!
|
13 |
-
> You're using this server, eval your code with Sunny!
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
{
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
},
|
40 |
-
|
41 |
-
}
|
42 |
-
]
|
43 |
-
}))
|
44 |
-
})
|
45 |
-
app.get("/models", (req, res) => {
|
46 |
-
res.set('Content-Type','application/json');
|
47 |
-
res.send(JSON.stringify({
|
48 |
-
"object": "list",
|
49 |
-
"data": [
|
50 |
-
{
|
51 |
-
"id": "sunny",
|
52 |
-
"object": "model",
|
53 |
-
"owned_by": "openai",
|
54 |
-
"created": new Date().toISOString(),
|
55 |
-
}, {
|
56 |
-
"id": "sunny-beta",
|
57 |
-
"object": "model",
|
58 |
-
"owned_by": "openai",
|
59 |
-
"created": new Date().toISOString()
|
60 |
}
|
61 |
-
]
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
const express = require('express');
|
2 |
+
const bodyParser = require('body-parser');
|
3 |
+
const vm = require('vm');
|
4 |
+
|
5 |
const app = express();
|
6 |
const port = 7860;
|
7 |
+
|
8 |
app.use(bodyParser.json());
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
app.post("/chat/completions", async (req, res) => {
|
11 |
+
const input = req.body?.messages?.[0]?.content || "";
|
12 |
+
let output = '';
|
13 |
+
let logs = [];
|
14 |
+
let promptTokens = input.split(/\s+/).length; // Rough estimate of prompt tokens
|
15 |
|
16 |
+
try {
|
17 |
+
const sandbox = {
|
18 |
+
console: {
|
19 |
+
log: (...args) => logs.push(args.join(" "))
|
20 |
+
},
|
21 |
+
result: undefined
|
22 |
+
};
|
23 |
|
24 |
+
const context = vm.createContext(sandbox);
|
25 |
+
const wrappedCode = `
|
26 |
+
(async () => {
|
27 |
+
try {
|
28 |
+
result = await (async () => { ${input} })();
|
29 |
+
} catch (e) {
|
30 |
+
result = 'Runtime Error: ' + e.message;
|
31 |
+
}
|
32 |
+
})()
|
33 |
+
`;
|
34 |
+
|
35 |
+
const script = new vm.Script(wrappedCode);
|
36 |
+
|
37 |
+
await script.runInContext(context, { timeout: 1000 });
|
38 |
+
|
39 |
+
output = sandbox.result;
|
40 |
+
} catch (err) {
|
41 |
+
output = 'Execution Timeout or VM Error: ' + err.message;
|
42 |
+
}
|
43 |
+
|
44 |
+
let completionTokens = output.split(/\s+/).length; // Rough estimate of completion tokens
|
45 |
+
|
46 |
+
res.json({
|
47 |
+
id: "chatcmpl-" + Math.random().toString(36).substr(2, 9),
|
48 |
+
object: "chat.completion",
|
49 |
+
created: Math.floor(Date.now() / 1000),
|
50 |
+
model: req.body.model || "sunny",
|
51 |
+
choices: [
|
52 |
{
|
53 |
+
index: 0,
|
54 |
+
message: {
|
55 |
+
role: "assistant",
|
56 |
+
content: output
|
57 |
},
|
58 |
+
finish_reason: "stop"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
}
|
60 |
+
],
|
61 |
+
usage: {
|
62 |
+
prompt_tokens: promptTokens,
|
63 |
+
completion_tokens: completionTokens,
|
64 |
+
total_tokens: promptTokens + completionTokens
|
65 |
+
}
|
66 |
+
});
|
67 |
+
});
|
68 |
+
|
69 |
+
app.listen(port, () => {
|
70 |
+
console.log(`☀️ Sunny listening on port ${port}!`);
|
71 |
+
});
|