calculate response token in stream mode
This commit is contained in:
@@ -46,6 +46,7 @@ export default function ChatBOX(props: {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const _completeWithStreamMode = async (response: Response) => {
|
const _completeWithStreamMode = async (response: Response) => {
|
||||||
|
let responseTokenCount = 0;
|
||||||
chatStore.streamMode = true;
|
chatStore.streamMode = true;
|
||||||
// call api, return reponse text
|
// call api, return reponse text
|
||||||
console.log("response", response);
|
console.log("response", response);
|
||||||
@@ -70,10 +71,12 @@ export default function ChatBOX(props: {
|
|||||||
if (!i) return false;
|
if (!i) return false;
|
||||||
if (i === "data: [DONE]" || i === "data:[DONE]") {
|
if (i === "data: [DONE]" || i === "data:[DONE]") {
|
||||||
responseDone = true;
|
responseDone = true;
|
||||||
|
responseTokenCount += 1;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
responseTokenCount += lines.length;
|
||||||
console.log("lines", lines);
|
console.log("lines", lines);
|
||||||
const jsons: ChunkMessage[] = lines
|
const jsons: ChunkMessage[] = lines
|
||||||
.map((line) => {
|
.map((line) => {
|
||||||
@@ -105,13 +108,12 @@ export default function ChatBOX(props: {
|
|||||||
|
|
||||||
// console.log("push to history", allChunkMessage);
|
// console.log("push to history", allChunkMessage);
|
||||||
const content = allChunkMessage.join("");
|
const content = allChunkMessage.join("");
|
||||||
const token = calculate_token_length(content);
|
|
||||||
|
|
||||||
// estimate cost
|
// estimate cost
|
||||||
let cost = 0;
|
let cost = 0;
|
||||||
if (chatStore.responseModelName) {
|
if (chatStore.responseModelName) {
|
||||||
cost +=
|
cost +=
|
||||||
token *
|
responseTokenCount *
|
||||||
(models[chatStore.responseModelName]?.price?.completion ?? 0);
|
(models[chatStore.responseModelName]?.price?.completion ?? 0);
|
||||||
let sum = 0;
|
let sum = 0;
|
||||||
for (const msg of chatStore.history
|
for (const msg of chatStore.history
|
||||||
@@ -129,7 +131,7 @@ export default function ChatBOX(props: {
|
|||||||
role: "assistant",
|
role: "assistant",
|
||||||
content,
|
content,
|
||||||
hide: false,
|
hide: false,
|
||||||
token,
|
token: responseTokenCount,
|
||||||
});
|
});
|
||||||
// manually copy status from client to chatStore
|
// manually copy status from client to chatStore
|
||||||
chatStore.maxTokens = client.max_tokens;
|
chatStore.maxTokens = client.max_tokens;
|
||||||
|
|||||||
Reference in New Issue
Block a user