Просмотр исходного кода

Merge pull request #1967 from Yidadaa/bugfix-0614

fix: #1931 try to fix cors issues
Yifei Zhang 1 год назад
Родитель
Сommit
4e80096ee4
5 измененных файлов с 39 добавлено и 5 удалено
  1. 4 0
      app/api/openai/[...path]/route.ts
  2. 2 1
      app/store/chat.ts
  3. 4 0
      app/store/config.ts
  4. 22 0
      app/utils/token.ts
  5. 7 4
      next.config.mjs

+ 4 - 0
app/api/openai/[...path]/route.ts

@@ -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)) {

+ 2 - 1
app/store/chat.ts

@@ -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>()(

+ 4 - 0
app/store/config.ts

@@ -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 - 0
app/utils/token.ts

@@ -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;
+}

+ 7 - 4
next.config.mjs

@@ -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",
           },
         ],
       },