File size: 2,219 Bytes
c517271 7626743 c517271 7626743 cf6dabc 7626743 1945d73 7626743 |
1 2 3 4 5 6 7 8 9 10 11 12 13 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
const fs = require("fs").promises;
const express = require("express");
const axios = require("axios");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
app.use(express.static(path.join(__dirname, ".")));
app.use(bodyParser.json());
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname + "/index.html"));
});
app.get("/endpoints", async (req, res) => {
try {
let secrets_path = path.join(__dirname, "secrets.json");
const data = await fs.readFile(secrets_path, "utf-8");
const secrets = JSON.parse(data);
const local_points = secrets.endpoints;
res.json(local_points);
} catch (error) {
console.error(error);
res.status(500).json({
error: "Failed to get local endpoints: Maybe secrets.json not existed?",
});
}
});
app.post("/chat/completions", async (req, res) => {
try {
const {
openai_endpoint,
openai_request_method,
openai_request_headers,
openai_request_body,
} = req.body;
const response = await axios({
method: openai_request_method,
url: openai_endpoint + "/chat/completions",
data: openai_request_body,
headers: openai_request_headers,
responseType: "stream",
});
response.data.pipe(res);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to request OpenAI Endpoint" });
}
});
app.post("/models", async (req, res) => {
try {
const {
openai_endpoint,
openai_request_method,
openai_request_headers,
} = req.body;
const response = await axios({
method: openai_request_method,
url: openai_endpoint + "/models",
headers: openai_request_headers,
});
res.json(response.data);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to request OpenAI Endpoint" });
}
});
const port = 23456;
app.listen(port, "0.0.0.0", () => {
console.log(`Server is running on http://0.0.0.0:${port}`);
});
|