fix: edit msg token length

This commit is contained in:
2023-11-09 13:16:13 +08:00
parent 415b7a02d8
commit 1033298187
3 changed files with 13 additions and 7 deletions

View File

@@ -243,8 +243,10 @@ export default function ChatBOX(props: {
}); });
// manually calculate token length // manually calculate token length
chatStore.totalTokens += client.calculate_token_length(inputMsg.trim()); chatStore.totalTokens +=
client.total_tokens += client.calculate_token_length(inputMsg.trim()); calculate_token_length(inputMsg.trim()) + calculate_token_length(images);
client.total_tokens = chatStore.totalTokens;
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });
setInputMsg(""); setInputMsg("");
setImages([]); setImages([]);
@@ -916,7 +918,9 @@ export default function ChatBOX(props: {
chatStore.history.push({ chatStore.history.push({
role: "assistant", role: "assistant",
content: inputMsg, content: inputMsg,
token: calculate_token_length(inputMsg), token:
calculate_token_length(inputMsg) +
calculate_token_length(images),
hide: false, hide: false,
example: false, example: false,
}); });

View File

@@ -71,7 +71,7 @@ export function calculate_token_length(
tokens += m.image_url?.detail === "high" ? 65 * 4 : 65; tokens += m.image_url?.detail === "high" ? 65 * 4 : 65;
} }
} }
return 0; return tokens;
} }
class Chat { class Chat {
@@ -267,11 +267,11 @@ class Chat {
return this._fetch(true); return this._fetch(true);
} }
calculate_token_length(content: string): number { calculate_token_length(content: string | MessageDetail[]): number {
return calculate_token_length(content); return calculate_token_length(content);
} }
user(...messages: string[]) { user(...messages: (string | MessageDetail[])[]) {
for (const msg of messages) { for (const msg of messages) {
this.messages.push({ role: "user", content: msg }); this.messages.push({ role: "user", content: msg });
this.total_tokens += this.calculate_token_length(msg); this.total_tokens += this.calculate_token_length(msg);
@@ -279,7 +279,7 @@ class Chat {
} }
} }
assistant(...messages: string[]) { assistant(...messages: (string | MessageDetail[])[]) {
for (const msg of messages) { for (const msg of messages) {
this.messages.push({ role: "assistant", content: msg }); this.messages.push({ role: "assistant", content: msg });
this.total_tokens += this.calculate_token_length(msg); this.total_tokens += this.calculate_token_length(msg);

View File

@@ -57,6 +57,7 @@ function EditMessage(props: EditMessageProps) {
if (typeof chat.content === "string") return; if (typeof chat.content === "string") return;
chat.content[index].text = event.target.value; chat.content[index].text = event.target.value;
chat.token = calculate_token_length(chat.content); chat.token = calculate_token_length(chat.content);
console.log("calculated token length", chat.token);
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });
}} }}
onKeyPress={(event: any) => { onKeyPress={(event: any) => {
@@ -129,6 +130,7 @@ function EditMessage(props: EditMessageProps) {
const obj = chat.content[index].image_url; const obj = chat.content[index].image_url;
if (obj === undefined) return; if (obj === undefined) return;
obj.detail = obj.detail === "high" ? "low" : "high"; obj.detail = obj.detail === "high" ? "low" : "high";
chat.token = calculate_token_length(chat.content);
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });
}} }}
> >