record each message's token and hide status, calc postBeginIndex based on token

This commit is contained in:
2023-03-31 04:16:23 +08:00
parent bdfe03699f
commit 26f9632f41
4 changed files with 154 additions and 48 deletions

View File

@@ -1,7 +1,11 @@
import { createRef } from "preact";
import { StateUpdater, useEffect, useState } from "preact/hooks";
import type { ChatStore } from "./app";
import ChatGPT, { ChunkMessage, FetchResponse } from "./chatgpt";
import ChatGPT, {
calculate_token_length,
ChunkMessage,
FetchResponse,
} from "./chatgpt";
import Message from "./message";
import Settings from "./settings";
@@ -77,17 +81,16 @@ export default function ChatBOX(props: {
setShowGenerating(false);
// console.log("push to history", allChunkMessage);
const content = allChunkMessage.join("");
chatStore.history.push({
role: "assistant",
content: allChunkMessage.join(""),
content,
hide: false,
token: calculate_token_length(content),
});
// manually copy status from client to chatStore
chatStore.maxTokens = client.max_tokens;
chatStore.tokenMargin = client.tokens_margin;
chatStore.totalTokens =
client.total_tokens +
39 +
client.calculate_token_length(allChunkMessage.join(""));
setChatStore({ ...chatStore });
setGeneratingMessage("");
setShowGenerating(false);
@@ -100,7 +103,12 @@ export default function ChatBOX(props: {
const data = (await response.json()) as FetchResponse;
chatStore.responseModelName = data.model ?? "";
const content = client.processFetchResponse(data);
chatStore.history.push({ role: "assistant", content });
chatStore.history.push({
role: "assistant",
content,
hide: false,
token: data.usage.completion_tokens ?? calculate_token_length(content),
});
setShowGenerating(false);
};
@@ -109,11 +117,35 @@ export default function ChatBOX(props: {
// manually copy status from chatStore to client
client.apiEndpoint = chatStore.apiEndpoint;
client.sysMessageContent = chatStore.systemMessageContent;
client.messages = chatStore.history.slice(chatStore.postBeginIndex);
client.tokens_margin = chatStore.tokenMargin;
client.messages = chatStore.history
.slice(chatStore.postBeginIndex)
// only copy non hidden message
.filter(({ hide }) => !hide)
// only copy content and role attribute to client for posting
.map(({ content, role }) => {
return {
content,
role,
};
});
client.model = chatStore.model;
client.max_tokens = chatStore.maxTokens;
// try forget message before sending request
client.forgetSomeMessages();
// todo move code
const max = chatStore.maxTokens - chatStore.tokenMargin;
let sum = 0;
chatStore.postBeginIndex = chatStore.history.filter(
({ hide }) => !hide
).length;
for (const msg of chatStore.history.slice().reverse()) {
sum += msg.token;
if (sum > max) break;
chatStore.postBeginIndex -= 1;
}
chatStore.postBeginIndex =
chatStore.postBeginIndex < 0 ? 0 : chatStore.postBeginIndex;
try {
setShowGenerating(true);
const response = await client._fetch(chatStore.streamMode);
@@ -129,11 +161,21 @@ export default function ChatBOX(props: {
chatStore.maxTokens = client.max_tokens;
chatStore.tokenMargin = client.tokens_margin;
chatStore.totalTokens = client.total_tokens;
// when total token > max token - margin token:
// ChatGPT will "forgot" some historical message
// so client.message.length will be less than chatStore.history.length
// todo move code
const max = chatStore.maxTokens - chatStore.tokenMargin;
let sum = 0;
chatStore.postBeginIndex = chatStore.history.filter(
({ hide }) => !hide
).length;
for (const msg of chatStore.history.slice().reverse()) {
sum += msg.token;
if (sum > max) break;
chatStore.postBeginIndex -= 1;
}
chatStore.postBeginIndex =
chatStore.history.length - client.messages.length;
chatStore.postBeginIndex < 0 ? 0 : chatStore.postBeginIndex;
console.log("postBeginIndex", chatStore.postBeginIndex);
setChatStore({ ...chatStore });
} catch (error) {
@@ -153,7 +195,12 @@ export default function ChatBOX(props: {
return;
}
chatStore.responseModelName = "";
chatStore.history.push({ role: "user", content: inputMsg.trim() });
chatStore.history.push({
role: "user",
content: inputMsg.trim(),
hide: false,
token: calculate_token_length(inputMsg.trim()),
});
// manually calculate token length
chatStore.totalTokens += client.calculate_token_length(inputMsg.trim());
client.total_tokens += client.calculate_token_length(inputMsg.trim());
@@ -191,7 +238,9 @@ export default function ChatBOX(props: {
Tokens: {chatStore.totalTokens} / {chatStore.maxTokens}
</span>{" "}
<span>{chatStore.model}</span>{" "}
<span>Messages: {chatStore.history.length}</span>{" "}
<span>
Messages: {chatStore.history.filter(({ hide }) => !hide).length}
</span>{" "}
<span>Cut: {chatStore.postBeginIndex}</span>
</div>
</p>
@@ -252,6 +301,23 @@ export default function ChatBOX(props: {
{chatStore.responseModelName && (
<p className="p-2 my-2 text-center opacity-50 dark:text-white">
Generated by {chatStore.responseModelName}
{chatStore.postBeginIndex !== 0 && (
<>
<br />
{chatStore.postBeginIndex}
</>
)}
{chatStore.chatgpt_api_web_version < "v1.3.0" && (
<>
<br />
{chatStore.chatgpt_api_web_version}
<br />
v1.3.0
使
<br />
</>
)}
</p>
)}
{showRetry && (