|
|
|
|
|
|
|
const express = require('express'); |
|
const app = express(); |
|
const port = 7860; |
|
const bodyParser = require('body-parser'); |
|
const vm = require('vm'); |
|
|
|
app.use(bodyParser.json()); |
|
app.use(bodyParser.urlencoded({ extended: true })); |
|
|
|
|
|
app.get("/", (req, res) => { |
|
res.set('Content-Type', 'text/plain'); |
|
res.send(`βοΈ Sunny! |
|
> You're using this server to eval your code with Sunny! |
|
|
|
This server model only supports NodeJS. It doesn't generate text, only executes code for testing. |
|
|
|
π For educational purposes only. Do not use this server for illegal activities. |
|
|
|
> Usage: |
|
- Use OpenWebUI: |
|
Administrators Settings > Connections > Add Connection (OpenAI) |
|
- Use h3 HF Spaces: |
|
https://huggingface.co/spaces/katsukiai/h3 |
|
`); |
|
}); |
|
|
|
|
|
app.get("/models", (req, res) => { |
|
res.set('Content-Type', 'application/json'); |
|
res.send({ |
|
object: "list", |
|
data: [ |
|
{ |
|
id: "sunny", |
|
object: "model", |
|
created: Math.floor(Date.now() / 1000), |
|
owned_by: "sunny-dev", |
|
permission: [], |
|
root: "sunny", |
|
parent: null |
|
}, |
|
{ |
|
id: "sunny-beta", |
|
object: "model", |
|
created: Math.floor(Date.now() / 1000), |
|
owned_by: "sunny-dev", |
|
permission: [], |
|
root: "sunny-beta", |
|
parent: null |
|
} |
|
] |
|
}); |
|
}); |
|
|
|
|
|
app.post("/chat/completions", async (req, res) => { |
|
const input = req.body?.messages?.[0]?.content || ""; |
|
let output = ''; |
|
let logs = []; |
|
let promptTokens = input.split(/\s+/).length; |
|
|
|
try { |
|
const sandbox = { |
|
console: { |
|
log: (...args) => logs.push(args.join(" ")) |
|
}, |
|
result: undefined |
|
}; |
|
|
|
const context = vm.createContext(sandbox); |
|
const wrappedCode = ` |
|
(async () => { |
|
try { |
|
result = await (async () => { ${input} })(); |
|
} catch (e) { |
|
result = 'Runtime Error: ' + e.message; |
|
} |
|
})() |
|
`; |
|
|
|
const script = new vm.Script(wrappedCode); |
|
|
|
await script.runInContext(context, { timeout: 1000 }); |
|
|
|
output = sandbox.result; |
|
} catch (err) { |
|
output = 'Execution Timeout or VM Error: ' + err.message; |
|
} |
|
|
|
let completionTokens = output.split(/\s+/).length; |
|
|
|
res.json({ |
|
id: "chatcmpl-" + Math.random().toString(36).substr(2, 9), |
|
object: "chat.completion", |
|
created: Math.floor(Date.now() / 1000), |
|
model: req.body.model || "sunny", |
|
choices: [ |
|
{ |
|
index: 0, |
|
message: { |
|
role: "assistant", |
|
content: output |
|
}, |
|
finish_reason: "stop" |
|
} |
|
], |
|
usage: { |
|
prompt_tokens: promptTokens, |
|
completion_tokens: completionTokens, |
|
total_tokens: promptTokens + completionTokens |
|
} |
|
}); |
|
}); |
|
|
|
app.listen(port, () => { |
|
console.log(`βοΈ Sunny listening on port ${port}!`); |
|
}); |
|
|