cost in stream mode

This commit is contained in:
2023-03-31 05:09:25 +08:00
parent 3b09abaf66
commit 5a328db87d

View File

@@ -83,11 +83,25 @@ export default function ChatBOX(props: {
// console.log("push to history", allChunkMessage);
const content = allChunkMessage.join("");
const token = calculate_token_length(content);
// estimate cost
if (chatStore.responseModelName) {
chatStore.cost +=
token * models[chatStore.responseModelName].price.completion;
let sum = 0;
for (const msg of chatStore.history
.filter(({ hide }) => !hide)
.slice(chatStore.postBeginIndex)) {
sum += msg.token;
}
chatStore.cost +=
sum * models[chatStore.responseModelName].price.prompt;
}
chatStore.history.push({
role: "assistant",
content,
hide: false,
token: calculate_token_length(content),
token,
});
// manually copy status from client to chatStore
chatStore.maxTokens = client.max_tokens;