File size: 4,675 Bytes
c517271 7626743 d7a32b8 c517271 d6474bb d7a32b8 d6474bb c517271 d6474bb c517271 cb0dedb d7a32b8 cb0dedb 70064ec edf7164 8a35d38 edf7164 7626743 8a35d38 7626743 73813dd 7626743 8a35d38 edf7164 7626743 cf6dabc edf7164 cf6dabc edf7164 cf6dabc 8a35d38 cf6dabc 73813dd edf7164 cf6dabc edf7164 8a35d38 3670643 8a35d38 edf7164 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
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"));
});
async function is_file_exists(file) {
return fs
.access(file, fs.constants.F_OK)
.then(() => true)
.catch(() => false);
}
app.get("/endpoints", async (req, res) => {
try {
let endpoints_configs_path = path.join(
__dirname,
"configs",
"endpoints.json"
);
if (!(await is_file_exists(endpoints_configs_path))) {
endpoints_configs_path = path.join(
__dirname,
"configs",
"endpoints_template.json"
);
console.log("agents.json not found. Use template.");
}
const data = await fs.readFile(endpoints_configs_path, "utf-8");
const local_points = JSON.parse(data);
res.json(local_points);
} catch (error) {
console.error(error);
res.status(500).json({
error: "Failed to get local endpoints: Maybe configs/endpoints.json not existed?",
});
}
});
app.get("/agents", async (req, res) => {
try {
let agents_configs_path = path.join(
__dirname,
"configs",
"agents.json"
);
if (!(await is_file_exists(agents_configs_path))) {
agents_configs_path = path.join(
__dirname,
"configs",
"agents_template.json"
);
console.log("endpoints.json not found. Use template.");
}
const data = await fs.readFile(agents_configs_path, "utf-8");
const local_agents = JSON.parse(data);
res.json(local_agents);
} catch (error) {
console.error(error);
res.status(500).json({
error: "Failed to get local agents: Maybe configs/agents.json not existed?",
});
}
});
let httpProxyDict = false;
const loadHttpProxy = async () => {
try {
let secrets_configs_path = path.join(
__dirname,
"configs",
"secrets.json"
);
const data = await fs.readFile(secrets_configs_path, "utf-8");
const secrets = JSON.parse(data);
if (secrets.http_proxy) {
const url = new URL(secrets.http_proxy);
httpProxyDict = {
protocol: url.protocol.slice(0, -1),
host: url.hostname,
port: parseInt(url.port),
};
} else {
console.warn("http_proxy not found in secrets");
}
} catch (error) {
console.warn(
"Failed to load http_proxy: Maybe configs/secrets.json not existed?"
);
}
};
loadHttpProxy();
app.post("/models", async (req, res) => {
try {
const {
openai_endpoint,
openai_request_method,
openai_request_headers,
} = req.body;
let axios_config = {
method: openai_request_method,
url: openai_endpoint + "/v1/models",
headers: openai_request_headers,
};
if (httpProxyDict) {
axios_config.proxy = httpProxyDict;
}
const response = await axios(axios_config);
res.json(response.data);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to request OpenAI Endpoint" });
}
});
app.post("/chat/completions", async (req, res) => {
try {
const {
openai_endpoint,
openai_request_method,
openai_request_headers,
openai_request_body,
} = req.body;
let axios_config = {
method: openai_request_method,
url: openai_endpoint + "/v1/chat/completions",
data: openai_request_body,
headers: openai_request_headers,
responseType: "stream",
};
if (httpProxyDict) {
axios_config.proxy = httpProxyDict;
}
console.log(`request to ${axios_config.url}`);
const response = await axios(axios_config);
response.data.pipe(res);
} 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}`);
});
|