calculate token and forget some message
This commit is contained in:
@@ -102,6 +102,8 @@ export default function ChatBOX(props: {
|
|||||||
client.apiEndpoint = chatStore.apiEndpoint;
|
client.apiEndpoint = chatStore.apiEndpoint;
|
||||||
client.sysMessageContent = chatStore.systemMessageContent;
|
client.sysMessageContent = chatStore.systemMessageContent;
|
||||||
client.messages = chatStore.history.slice(chatStore.postBeginIndex);
|
client.messages = chatStore.history.slice(chatStore.postBeginIndex);
|
||||||
|
// try forget message before sending request
|
||||||
|
client.forgetSomeMessages();
|
||||||
try {
|
try {
|
||||||
setShowGenerating(true);
|
setShowGenerating(true);
|
||||||
const response = await client._fetch(chatStore.streamMode);
|
const response = await client._fetch(chatStore.streamMode);
|
||||||
@@ -140,6 +142,9 @@ export default function ChatBOX(props: {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
chatStore.history.push({ role: "user", content: inputMsg.trim() });
|
chatStore.history.push({ role: "user", content: inputMsg.trim() });
|
||||||
|
// manually calculate token length
|
||||||
|
chatStore.totalTokens += client.calculate_token_length(inputMsg.trim());
|
||||||
|
client.total_tokens += client.calculate_token_length(inputMsg.trim());
|
||||||
setChatStore({ ...chatStore });
|
setChatStore({ ...chatStore });
|
||||||
setInputMsg("");
|
setInputMsg("");
|
||||||
await complete();
|
await complete();
|
||||||
|
|||||||
@@ -25,6 +25,14 @@ export interface FetchResponse {
|
|||||||
index: number | undefined;
|
index: number | undefined;
|
||||||
}[];
|
}[];
|
||||||
}
|
}
|
||||||
|
// https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them
|
||||||
|
export function calculate_token_length(content: string): number {
|
||||||
|
const totalCount = content.length;
|
||||||
|
const chineseCount = content.match(/[\u00ff-\uffff]|\S+/g)?.length ?? 0;
|
||||||
|
const englishCount = totalCount - chineseCount;
|
||||||
|
const tokenLength = englishCount / 4 + (chineseCount * 4) / 3;
|
||||||
|
return ~~tokenLength;
|
||||||
|
}
|
||||||
|
|
||||||
class Chat {
|
class Chat {
|
||||||
OPENAI_API_KEY: string;
|
OPENAI_API_KEY: string;
|
||||||
@@ -114,13 +122,8 @@ class Chat {
|
|||||||
return this._fetch(true);
|
return this._fetch(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them
|
|
||||||
calculate_token_length(content: string): number {
|
calculate_token_length(content: string): number {
|
||||||
const totalCount = content.length;
|
return calculate_token_length(content);
|
||||||
const chineseCount = content.match(/[\u00ff-\uffff]|\S+/g)?.length ?? 0;
|
|
||||||
const englishCount = totalCount - chineseCount;
|
|
||||||
const tokenLength = englishCount / 4 + (chineseCount * 4) / 3;
|
|
||||||
return ~~tokenLength;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
user(...messages: string[]) {
|
user(...messages: string[]) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { ChatStore } from "./app";
|
import { ChatStore } from "./app";
|
||||||
|
import { calculate_token_length } from "./chatgpt";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
messageIndex: number;
|
messageIndex: number;
|
||||||
@@ -24,6 +25,10 @@ export default function Message(props: Props) {
|
|||||||
) {
|
) {
|
||||||
chatStore.history.splice(messageIndex, 1);
|
chatStore.history.splice(messageIndex, 1);
|
||||||
chatStore.postBeginIndex = Math.max(chatStore.postBeginIndex - 1, 0);
|
chatStore.postBeginIndex = Math.max(chatStore.postBeginIndex - 1, 0);
|
||||||
|
chatStore.totalTokens = Math.max(
|
||||||
|
0,
|
||||||
|
chatStore.totalTokens - calculate_token_length(chat.content)
|
||||||
|
);
|
||||||
setChatStore({ ...chatStore });
|
setChatStore({ ...chatStore });
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
|||||||
Reference in New Issue
Block a user