remove: update_total_token()
All checks were successful
Build static content / build (push) Successful in 8m10s

This commit is contained in:
2024-10-15 18:19:51 +08:00
parent a763355420
commit 6aca74a7b4
4 changed files with 11 additions and 24 deletions

View File

@@ -26,7 +26,6 @@ interface Props {
messageIndex: number; messageIndex: number;
chatStore: ChatStore; chatStore: ChatStore;
setChatStore: (cs: ChatStore) => void; setChatStore: (cs: ChatStore) => void;
update_total_tokens: () => void;
} }
export default function Message(props: Props) { export default function Message(props: Props) {
@@ -205,7 +204,6 @@ export default function Message(props: Props) {
className="input input-bordered input-xs w-16" className="input input-bordered input-xs w-16"
onChange={(event: any) => { onChange={(event: any) => {
chat.token = parseInt(event.target.value); chat.token = parseInt(event.target.value);
props.update_total_tokens();
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });
}} }}
/> />

View File

@@ -9,10 +9,8 @@ const AddToolMsg = (props: {
setShowAddToolMsg: Dispatch<StateUpdater<boolean>>; setShowAddToolMsg: Dispatch<StateUpdater<boolean>>;
chatStore: ChatStore; chatStore: ChatStore;
setChatStore: (cs: ChatStore) => void; setChatStore: (cs: ChatStore) => void;
update_total_tokens: () => void;
}) => { }) => {
const { setShowAddToolMsg, chatStore, update_total_tokens, setChatStore } = const { setShowAddToolMsg, chatStore, setChatStore } = props;
props;
const [newToolCallID, setNewToolCallID] = useState(""); const [newToolCallID, setNewToolCallID] = useState("");
const [newToolContent, setNewToolContent] = useState(""); const [newToolContent, setNewToolContent] = useState("");
@@ -77,7 +75,6 @@ const AddToolMsg = (props: {
audio: null, audio: null,
logprobs: null, logprobs: null,
}); });
update_total_tokens();
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });
setNewToolCallID(""); setNewToolCallID("");
setNewToolContent(""); setNewToolContent("");

View File

@@ -81,6 +81,16 @@ export function App() {
console.log("saved chat", selectedChatIndex, chatStore); console.log("saved chat", selectedChatIndex, chatStore);
(await db).put(STORAGE_NAME, chatStore, selectedChatIndex); (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); _setChatStore(chatStore);
}; };
useEffect(() => { useEffect(() => {

View File

@@ -79,19 +79,6 @@ export default function ChatBOX(props: {
const client = new ChatGPT(chatStore.apiKey); 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) => { const _completeWithStreamMode = async (response: Response) => {
let responseTokenCount = 0; let responseTokenCount = 0;
const allChunkMessage: string[] = []; const allChunkMessage: string[] = [];
@@ -194,7 +181,6 @@ export default function ChatBOX(props: {
// manually copy status from client to chatStore // manually copy status from client to chatStore
chatStore.maxTokens = client.max_tokens; chatStore.maxTokens = client.max_tokens;
chatStore.tokenMargin = client.tokens_margin; chatStore.tokenMargin = client.tokens_margin;
update_total_tokens();
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });
setGeneratingMessage(""); setGeneratingMessage("");
setShowGenerating(false); setShowGenerating(false);
@@ -592,7 +578,6 @@ export default function ChatBOX(props: {
chatStore={chatStore} chatStore={chatStore}
setChatStore={setChatStore} setChatStore={setChatStore}
messageIndex={messageIndex} messageIndex={messageIndex}
update_total_tokens={update_total_tokens}
/> />
))} ))}
{showGenerating && ( {showGenerating && (
@@ -613,7 +598,6 @@ export default function ChatBOX(props: {
} }
//chatStore.totalTokens = //chatStore.totalTokens =
update_total_tokens();
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });
await complete(); await complete();
@@ -771,7 +755,6 @@ export default function ChatBOX(props: {
audio: null, audio: null,
logprobs: null, logprobs: null,
}); });
update_total_tokens();
setInputMsg(""); setInputMsg("");
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });
}} }}
@@ -806,7 +789,6 @@ export default function ChatBOX(props: {
chatStore={chatStore} chatStore={chatStore}
setChatStore={setChatStore} setChatStore={setChatStore}
setShowAddToolMsg={setShowAddToolMsg} setShowAddToolMsg={setShowAddToolMsg}
update_total_tokens={update_total_tokens}
/> />
)} )}
</div> </div>