Merge pull request #1967 from Yidadaa/bugfix-0614

fix: #1931 try to fix cors issues
This commit is contained in:
Yifei Zhang 2023-06-15 00:31:00 +08:00 committed by GitHub
commit 4e80096ee4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 5 deletions

View File

@ -12,6 +12,10 @@ async function handle(
) {
console.log("[OpenAI Route] params ", params);
if (req.method === "OPTIONS") {
return NextResponse.json({ body: "OK" }, { status: 200 });
}
const subpath = params.path.join("/");
if (!ALLOWD_PATH.has(subpath)) {

View File

@ -11,6 +11,7 @@ import { StoreKey } from "../constant";
import { api, RequestMessage } from "../client/api";
import { ChatControllerPool } from "../client/controller";
import { prettyObject } from "../utils/format";
import { estimateTokenLength } from "../utils/token";
export type ChatMessage = RequestMessage & {
date: string;
@ -102,7 +103,7 @@ interface ChatStore {
}
function countMessages(msgs: ChatMessage[]) {
return msgs.reduce((pre, cur) => pre + cur.content.length, 0);
return msgs.reduce((pre, cur) => pre + estimateTokenLength(cur.content), 0);
}
export const useChatStore = create<ChatStore>()(

View File

@ -92,6 +92,10 @@ export const ALL_MODELS = [
name: "gpt-3.5-turbo-16k",
available: true,
},
{
name: "gpt-3.5-turbo-16k-0613",
available: true,
},
{
name: "qwen-v1", // 通义千问
available: false,

22
app/utils/token.ts Normal file
View File

@ -0,0 +1,22 @@
export function estimateTokenLength(input: string): number {
let tokenLength = 0;
for (let i = 0; i < input.length; i++) {
const charCode = input.charCodeAt(i);
if (charCode < 128) {
// ASCII character
if (charCode <= 122 && charCode >= 65) {
// a-Z
tokenLength += 0.25;
} else {
tokenLength += 0.5;
}
} else {
// Unicode character
tokenLength += 1.5;
}
}
return tokenLength;
}

View File

@ -18,18 +18,21 @@ if (mode !== "export") {
nextConfig.headers = async () => {
return [
{
source: "/:path*",
source: "/api/:path*",
headers: [
{ key: "Access-Control-Allow-Credentials", value: "true" },
{ key: "Access-Control-Allow-Origin", value: "*" },
{
key: "Access-Control-Allow-Methods",
value: "GET,OPTIONS,PATCH,DELETE,POST,PUT",
value: "*",
},
{
key: "Access-Control-Allow-Headers",
value:
"X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version",
value: "*",
},
{
key: "Access-Control-Max-Age",
value: "86400",
},
],
},