From 6aca74a7b486bb4d2fa44692df71947830d2410b Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Tue, 15 Oct 2024 18:19:51 +0800 Subject: [PATCH] remove: update_total_token() --- src/message.tsx | 2 -- src/pages/AddToolMsg.tsx | 5 +---- src/pages/App.tsx | 10 ++++++++++ src/pages/Chatbox.tsx | 18 ------------------ 4 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/message.tsx b/src/message.tsx index 3b624d3..5535631 100644 --- a/src/message.tsx +++ b/src/message.tsx @@ -26,7 +26,6 @@ interface Props { messageIndex: number; chatStore: ChatStore; setChatStore: (cs: ChatStore) => void; - update_total_tokens: () => void; } export default function Message(props: Props) { @@ -205,7 +204,6 @@ export default function Message(props: Props) { className="input input-bordered input-xs w-16" onChange={(event: any) => { chat.token = parseInt(event.target.value); - props.update_total_tokens(); setChatStore({ ...chatStore }); }} /> diff --git a/src/pages/AddToolMsg.tsx b/src/pages/AddToolMsg.tsx index d52a185..a04a3e4 100644 --- a/src/pages/AddToolMsg.tsx +++ b/src/pages/AddToolMsg.tsx @@ -9,10 +9,8 @@ const AddToolMsg = (props: { setShowAddToolMsg: Dispatch>; chatStore: ChatStore; setChatStore: (cs: ChatStore) => void; - update_total_tokens: () => void; }) => { - const { setShowAddToolMsg, chatStore, update_total_tokens, setChatStore } = - props; + const { setShowAddToolMsg, chatStore, setChatStore } = props; const [newToolCallID, setNewToolCallID] = useState(""); const [newToolContent, setNewToolContent] = useState(""); @@ -77,7 +75,6 @@ const AddToolMsg = (props: { audio: null, logprobs: null, }); - update_total_tokens(); setChatStore({ ...chatStore }); setNewToolCallID(""); setNewToolContent(""); diff --git a/src/pages/App.tsx b/src/pages/App.tsx index 8303a20..4a7322b 100644 --- a/src/pages/App.tsx +++ b/src/pages/App.tsx @@ -81,6 +81,16 @@ export function App() { console.log("saved chat", selectedChatIndex, chatStore); (await db).put(STORAGE_NAME, chatStore, selectedChatIndex); + // update total tokens + chatStore.totalTokens = calculate_token_length( + chatStore.systemMessageContent, + ); + for (const msg of chatStore.history + .filter(({ hide }) => !hide) + .slice(chatStore.postBeginIndex)) { + chatStore.totalTokens += msg.token; + } + _setChatStore(chatStore); }; useEffect(() => { diff --git a/src/pages/Chatbox.tsx b/src/pages/Chatbox.tsx index ccc89d1..a323093 100644 --- a/src/pages/Chatbox.tsx +++ b/src/pages/Chatbox.tsx @@ -79,19 +79,6 @@ export default function ChatBOX(props: { const client = new ChatGPT(chatStore.apiKey); - const update_total_tokens = () => { - // manually estimate token - client.total_tokens = calculate_token_length( - chatStore.systemMessageContent, - ); - for (const msg of chatStore.history - .filter(({ hide }) => !hide) - .slice(chatStore.postBeginIndex)) { - client.total_tokens += msg.token; - } - chatStore.totalTokens = client.total_tokens; - }; - const _completeWithStreamMode = async (response: Response) => { let responseTokenCount = 0; const allChunkMessage: string[] = []; @@ -194,7 +181,6 @@ export default function ChatBOX(props: { // manually copy status from client to chatStore chatStore.maxTokens = client.max_tokens; chatStore.tokenMargin = client.tokens_margin; - update_total_tokens(); setChatStore({ ...chatStore }); setGeneratingMessage(""); setShowGenerating(false); @@ -592,7 +578,6 @@ export default function ChatBOX(props: { chatStore={chatStore} setChatStore={setChatStore} messageIndex={messageIndex} - update_total_tokens={update_total_tokens} /> ))} {showGenerating && ( @@ -613,7 +598,6 @@ export default function ChatBOX(props: { } //chatStore.totalTokens = - update_total_tokens(); setChatStore({ ...chatStore }); await complete(); @@ -771,7 +755,6 @@ export default function ChatBOX(props: { audio: null, logprobs: null, }); - update_total_tokens(); setInputMsg(""); setChatStore({ ...chatStore }); }} @@ -806,7 +789,6 @@ export default function ChatBOX(props: { chatStore={chatStore} setChatStore={setChatStore} setShowAddToolMsg={setShowAddToolMsg} - update_total_tokens={update_total_tokens} /> )}