Skip to content

Commit

Permalink
perf: token count (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu authored May 16, 2024
1 parent 6067f5a commit 2d1ec9b
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/service/common/string/tiktoken/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const countGptMessagesTokens = (
clearTimeout(timer);
};

// 可以进一步优化(传递100w token数据,实际需要300ms,较慢)
worker.postMessage({
id,
messages,
Expand Down
1 change: 1 addition & 0 deletions packages/service/worker/tiktoken/countGptMessagesTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ parentPort?.on(
data: total
});
} catch (error) {
console.log(error);
parentPort?.postMessage({
id,
type: 'success',
Expand Down
18 changes: 0 additions & 18 deletions projects/app/src/middleware.ts

This file was deleted.

2 changes: 1 addition & 1 deletion projects/app/src/pages/api/core/ai/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ async function handler(
req: ApiRequestProps<tokenBody, tokenQuery>,
res: ApiResponseType<any>
): Promise<tokenResponse> {
await authCert({ req, authRoot: true });
const start = Date.now();
await authCert({ req, authRoot: true });

const tokens = await countGptMessagesTokens(req.body.messages);

Expand Down

0 comments on commit 2d1ec9b

Please sign in to comment.