From 8590750e4c883a79d9462f23fd21b32b13ab4c04 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Wed, 14 Jun 2023 23:22:59 +0800 Subject: [PATCH 1/3] feat: close #1960 add gpt-3.5-turbo-16k-0613 --- app/store/config.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/store/config.ts b/app/store/config.ts index e837011a..3378b9c3 100644 --- a/app/store/config.ts +++ b/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, From 76fdd047e7a9427dee18785d1cf60cc0e0999554 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Thu, 15 Jun 2023 00:14:38 +0800 Subject: [PATCH 2/3] feat: new token count function --- app/store/chat.ts | 3 ++- app/utils/token.ts | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 app/utils/token.ts diff --git a/app/store/chat.ts b/app/store/chat.ts index 3c9953e5..f56d9b9d 100644 --- a/app/store/chat.ts +++ b/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()( diff --git a/app/utils/token.ts b/app/utils/token.ts new file mode 100644 index 00000000..ec8139b2 --- /dev/null +++ b/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; +} From 0fb775d71a0fac5ce4aa802bc3eb0b066c12ed7b Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Thu, 15 Jun 2023 00:28:47 +0800 Subject: [PATCH 3/3] fix: #1931 try to fix cors issues --- app/api/openai/[...path]/route.ts | 4 ++++ next.config.mjs | 11 +++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/api/openai/[...path]/route.ts b/app/api/openai/[...path]/route.ts index 04f3b6da..36f92d0f 100644 --- a/app/api/openai/[...path]/route.ts +++ b/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)) { diff --git a/next.config.mjs b/next.config.mjs index b2a47deb..540fc027 100644 --- a/next.config.mjs +++ b/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", }, ], },