Skip to content

Commit

Permalink
feat: new token count function
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed Jun 14, 2023
1 parent 8590750 commit 76fdd04
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>()(
Expand Down
22 changes: 22 additions & 0 deletions app/utils/token.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 76fdd04

Please sign in to comment.