fixed token count reading
This commit is contained in:
@@ -76,10 +76,13 @@ async function* completeStream(prompt, options = {}) {
|
|||||||
const json = JSON.parse(line.slice(6));
|
const json = JSON.parse(line.slice(6));
|
||||||
const delta = json.choices?.[0]?.delta?.content;
|
const delta = json.choices?.[0]?.delta?.content;
|
||||||
|
|
||||||
// Capture final metadata from the stop chunk
|
if (json.choices?.[0]?.finish_reason === 'stop') {
|
||||||
if (json.choices?.[0]?.finish_reason === "stop") {
|
|
||||||
finalModel = json.model ?? finalModel;
|
finalModel = json.model ?? finalModel;
|
||||||
finalTokenCount = json.usage?.completion_tokens ?? finalTokenCount;
|
}
|
||||||
|
|
||||||
|
// usage arrives in a separate final chunk with empty choices array
|
||||||
|
if (json.usage) {
|
||||||
|
finalTokenCount = (json.usage.completion_tokens ?? 0) + (json.usage.prompt_tokens ?? 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (delta) yield { response: delta, done: false };
|
if (delta) yield { response: delta, done: false };
|
||||||
|
|||||||
Reference in New Issue
Block a user