fix: edit msg token length
This commit is contained in:
@@ -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,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -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 });
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user