handle preflight
This commit is contained in:
@@ -45,12 +45,16 @@ var provider = new YouProvider(config);
|
|||||||
await provider.init(config);
|
await provider.init(config);
|
||||||
|
|
||||||
// handle preflight request
|
// handle preflight request
|
||||||
app.options("/v1/messages", (req, res) => {
|
app.use((req, res, next) => {
|
||||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
if (req.method === "OPTIONS") {
|
||||||
res.setHeader("Access-Control-Allow-Methods", "*");
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||||
res.setHeader("Access-Control-Allow-Headers", "*");
|
res.setHeader("Access-Control-Allow-Methods", "*");
|
||||||
res.setHeader("Access-Control-Max-Age", "86400");
|
res.setHeader("Access-Control-Allow-Headers", "*");
|
||||||
res.status(200).end();
|
res.setHeader("Access-Control-Max-Age", "86400");
|
||||||
|
res.status(200).end();
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
// openai format model request
|
// openai format model request
|
||||||
app.get("/v1/models", OpenAIApiKeyAuth, (req, res) => {
|
app.get("/v1/models", OpenAIApiKeyAuth, (req, res) => {
|
||||||
@@ -122,30 +126,28 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
|||||||
completion.on("completion", (id, text) => {
|
completion.on("completion", (id, text) => {
|
||||||
if (jsonBody.stream) {
|
if (jsonBody.stream) {
|
||||||
// send message delta
|
// send message delta
|
||||||
if (jsonBody.stream) {
|
res.write(
|
||||||
res.write(
|
createEvent("data", {
|
||||||
createEvent("data", {
|
choices: [
|
||||||
choices: [
|
{
|
||||||
{
|
content_filter_results: {
|
||||||
content_filter_results: {
|
hate: { filtered: false, severity: "safe" },
|
||||||
hate: { filtered: false, severity: "safe" },
|
self_harm: { filtered: false, severity: "safe" },
|
||||||
self_harm: { filtered: false, severity: "safe" },
|
sexual: { filtered: false, severity: "safe" },
|
||||||
sexual: { filtered: false, severity: "safe" },
|
violence: { filtered: false, severity: "safe" },
|
||||||
violence: { filtered: false, severity: "safe" },
|
|
||||||
},
|
|
||||||
delta: { content: text },
|
|
||||||
finish_reason: null,
|
|
||||||
index: 0,
|
|
||||||
},
|
},
|
||||||
],
|
delta: { content: text },
|
||||||
created: Math.floor(new Date().getTime() / 1000),
|
finish_reason: null,
|
||||||
id: id,
|
index: 0,
|
||||||
model: jsonBody.model,
|
},
|
||||||
object: "chat.completion.chunk",
|
],
|
||||||
system_fingerprint: "114514",
|
created: Math.floor(new Date().getTime() / 1000),
|
||||||
})
|
id: id,
|
||||||
);
|
model: jsonBody.model,
|
||||||
}
|
object: "chat.completion.chunk",
|
||||||
|
system_fingerprint: "114514",
|
||||||
|
})
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
// 只会发一次,发送final response
|
// 只会发一次,发送final response
|
||||||
res.write(
|
res.write(
|
||||||
@@ -312,15 +314,13 @@ app.post("/v1/messages", AnthropicApiKeyAuth, (req, res) => {
|
|||||||
completion.on("completion", (id, text) => {
|
completion.on("completion", (id, text) => {
|
||||||
if (jsonBody.stream) {
|
if (jsonBody.stream) {
|
||||||
// send message delta
|
// send message delta
|
||||||
if (jsonBody.stream) {
|
res.write(
|
||||||
res.write(
|
createEvent("content_block_delta", {
|
||||||
createEvent("content_block_delta", {
|
type: "content_block_delta",
|
||||||
type: "content_block_delta",
|
index: 0,
|
||||||
index: 0,
|
delta: { type: "text_delta", text: text },
|
||||||
delta: { type: "text_delta", text: text },
|
})
|
||||||
})
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 只会发一次,发送final response
|
// 只会发一次,发送final response
|
||||||
res.write(
|
res.write(
|
||||||
|
|||||||
Reference in New Issue
Block a user