katsukiai commited on
Commit
956ccbc
·
verified ·
1 Parent(s): 3c0ab8b

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +61 -54
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
- const bodyParser = require('body-parser');
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
- This server's model only support NodeJS. and no text generated, It' only for test your code so you can use it for test your code.
 
 
 
 
16
 
17
- 🛂 For educational purposes only, do not use this server for illegal purposes.
 
 
 
 
 
 
18
 
19
- > Usages:
20
- - Use OpenWebUI:
21
- Adminstrators Settings > Connections > Add Connection (OpenAI)
22
- - Use h3 HF Spaces:
23
- https://huggingface.co/spaces/katsukiai/h3 (Announced by @katsukiai on "Discussions" tab)
24
- `);
25
- });
26
- app.post("/chat/completions", (req, res) => {
27
- res.set('Content-Type', 'application/json');
28
- res.send(JSON.stringify({
29
- "id": "chatcmpl-" + Math.random().toString(36).substr(2, 9),
30
- "object": "chat.completion",
31
- "created": new Date().toISOString(),
32
- "model": req.body.model,
33
- "choices": [
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  {
35
- "index": 0,
36
- "message": {
37
- "role": "assistant",
38
- "content": eval(req.body.messages[0].content)
39
  },
40
- "finish_reason": "stop"
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
- app.listen(port, () => console.log(`Sunny listening on port ${port}!`))
 
 
 
 
 
 
 
 
 
 
 
 
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
+ });