File size: 1,132 Bytes
7626743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7116524
7626743
 
 
 
 
7116524
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
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.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 requests OpenAI Endpoint" });
    }
});

const port = 12345;
app.listen(port, () => {
    console.log(`Server is running on http://127.0.0.1:${port}`);
});