1 Star 0 Fork 1

yfl849/chatgpt-node-proxy

forked from N0ts/chatgpt-node-proxy 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.js 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
N0ts 提交于 2023-12-06 09:40 +08:00 . 完善
const express = require("express");
const cors = require("cors");
const axios = require("axios");
const app = express();
app.use(express.json());
app.use(cors());
axios.interceptors.response.use(
function (response) {
return response.data;
},
function (error) {
return Promise.reject(error);
}
);
app.post("/v1/chat/completions", async function (request, response) {
let { stream } = request.body;
const { authorization } = request.headers;
if (!stream) {
let result = null;
try {
result = await axios({
method: "POST",
url: "https://api.openai.com/v1/chat/completions",
data: request.body,
headers: {
authorization
}
});
} catch (err) {
return response.send(err.response.data);
}
return response.send(result);
}
response.writeHead(200, {
"Content-Type": "text/event-stream"
});
axios({
method: "POST",
url: "https://api.openai.com/v1/chat/completions",
data: request.body,
headers: {
authorization
},
responseType: "stream"
})
.then((res) => {
res.on("data", (chunk) => {
response.write(Buffer.from(chunk.toString()));
});
res.on("end", () => {
response.end();
});
})
.catch((err) => {
err.response.data.on("data", (chunk) => {
response.write(Buffer.from(chunk.toString()));
});
err.response.data.on("end", () => {
response.end();
});
});
});
app.get("/*", (request, response) => {
response.send("部署完成");
});
app.post("/*", (request, response) => {
response.send("部署完成");
});
app.listen(9000, function () {
console.log("http://localhost:9000");
});
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yfl849/chatgpt-node-proxy.git
git@gitee.com:yfl849/chatgpt-node-proxy.git
yfl849
chatgpt-node-proxy
chatgpt-node-proxy
master

搜索帮助