Skip to content

Commit 8f3d69d

Browse files
committed
fix todo: estimate user's token
1 parent 11d9b09 commit 8f3d69d

File tree

3 files changed

+36
-46
lines changed

3 files changed

+36
-46
lines changed

src/app.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,31 @@ export function App() {
108108
const [chatStore, _setChatStore] = useState(
109109
getChatStoreByIndex(selectedChatIndex)
110110
);
111-
const setChatStore = (cs: ChatStore) => {
111+
const setChatStore = (chatStore: ChatStore) => {
112112
console.log("saved chat", selectedChatIndex, chatStore);
113113
localStorage.setItem(
114114
`${STORAGE_NAME}-${selectedChatIndex}`,
115-
JSON.stringify(cs)
115+
JSON.stringify(chatStore)
116116
);
117-
_setChatStore(cs);
117+
118+
console.log("recalculate postBeginIndex");
119+
const max = chatStore.maxTokens - chatStore.tokenMargin;
120+
let sum = 0;
121+
chatStore.postBeginIndex = chatStore.history.filter(
122+
({ hide }) => !hide
123+
).length;
124+
for (const msg of chatStore.history
125+
.filter(({ hide }) => !hide)
126+
.slice()
127+
.reverse()) {
128+
if (sum + msg.token > max) break;
129+
sum += msg.token;
130+
chatStore.postBeginIndex -= 1;
131+
}
132+
chatStore.postBeginIndex =
133+
chatStore.postBeginIndex < 0 ? 0 : chatStore.postBeginIndex;
134+
135+
_setChatStore(chatStore);
118136
};
119137
useEffect(() => {
120138
_setChatStore(getChatStoreByIndex(selectedChatIndex));

src/chatbox.tsx

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ export default function ChatBOX(props: {
127127
(models[data.model]?.price?.completion ?? 0);
128128
}
129129
const content = client.processFetchResponse(data);
130+
131+
// estimate user's input message token
132+
let aboveToken = 0;
133+
for (const msg of chatStore.history
134+
.filter(({ hide }) => !hide)
135+
.slice(chatStore.postBeginIndex, -1)) {
136+
aboveToken += msg.token;
137+
}
138+
if (data.usage.prompt_tokens) {
139+
const userMessageToken = data.usage.prompt_tokens - aboveToken;
140+
console.log("set user message token");
141+
chatStore.history.slice(-1)[0].token = userMessageToken;
142+
}
143+
130144
chatStore.history.push({
131145
role: "assistant",
132146
content,
@@ -143,9 +157,9 @@ export default function ChatBOX(props: {
143157
client.sysMessageContent = chatStore.systemMessageContent;
144158
client.tokens_margin = chatStore.tokenMargin;
145159
client.messages = chatStore.history
146-
.slice(chatStore.postBeginIndex)
147160
// only copy non hidden message
148161
.filter(({ hide }) => !hide)
162+
.slice(chatStore.postBeginIndex)
149163
// only copy content and role attribute to client for posting
150164
.map(({ content, role }) => {
151165
return {
@@ -156,20 +170,6 @@ export default function ChatBOX(props: {
156170
client.model = chatStore.model;
157171
client.max_tokens = chatStore.maxTokens;
158172

159-
// todo move code
160-
const max = chatStore.maxTokens - chatStore.tokenMargin;
161-
let sum = 0;
162-
chatStore.postBeginIndex = chatStore.history.filter(
163-
({ hide }) => !hide
164-
).length;
165-
for (const msg of chatStore.history.slice().reverse()) {
166-
sum += msg.token;
167-
if (sum > max) break;
168-
chatStore.postBeginIndex -= 1;
169-
}
170-
chatStore.postBeginIndex =
171-
chatStore.postBeginIndex < 0 ? 0 : chatStore.postBeginIndex;
172-
173173
try {
174174
setShowGenerating(true);
175175
const response = await client._fetch(chatStore.streamMode);
@@ -186,20 +186,6 @@ export default function ChatBOX(props: {
186186
chatStore.tokenMargin = client.tokens_margin;
187187
chatStore.totalTokens = client.total_tokens;
188188

189-
// todo move code
190-
const max = chatStore.maxTokens - chatStore.tokenMargin;
191-
let sum = 0;
192-
chatStore.postBeginIndex = chatStore.history.filter(
193-
({ hide }) => !hide
194-
).length;
195-
for (const msg of chatStore.history.slice().reverse()) {
196-
sum += msg.token;
197-
if (sum > max) break;
198-
chatStore.postBeginIndex -= 1;
199-
}
200-
chatStore.postBeginIndex =
201-
chatStore.postBeginIndex < 0 ? 0 : chatStore.postBeginIndex;
202-
203189
console.log("postBeginIndex", chatStore.postBeginIndex);
204190
setChatStore({ ...chatStore });
205191
} catch (error) {

src/message.tsx

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ export default function Message(props: Props) {
1818
chatStore.history[messageIndex].hide =
1919
!chatStore.history[messageIndex].hide;
2020

21-
// todo move code
22-
const max = chatStore.maxTokens - chatStore.tokenMargin;
23-
let sum = 0;
24-
chatStore.postBeginIndex = chatStore.history.filter(
25-
({ hide }) => !hide
26-
).length;
27-
for (const msg of chatStore.history.slice().reverse()) {
28-
sum += msg.token;
29-
if (sum > max) break;
30-
chatStore.postBeginIndex -= 1;
31-
}
32-
chatStore.postBeginIndex =
33-
chatStore.postBeginIndex < 0 ? 0 : chatStore.postBeginIndex;
34-
3521
//chatStore.totalTokens =
3622
chatStore.totalTokens = 0;
3723
for (const i of chatStore.history

0 commit comments

Comments
 (0)