Skip to content

Commit

Permalink
fix stream usage
Browse files Browse the repository at this point in the history
Signed-off-by: oilbeater <[email protected]>
  • Loading branch information
oilbeater committed Oct 15, 2024
1 parent afb05fa commit cf44c66
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/providers/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ function getTokenCount(c: Context): { input_tokens: number, output_tokens: numbe
const output = buf.trim().split('\n\n').at(-2);
if (output && output.startsWith('data: ')) {
const usage_message = JSON.parse(output.slice('data: '.length));
return {
input_tokens: usage_message.usage.prompt_tokens || 0,
if ('usage' in usage_message) {
return {
input_tokens: usage_message.usage.prompt_tokens || 0,
output_tokens: usage_message.usage.completion_tokens || 0
};
};
}
return { input_tokens: 0, output_tokens: 0 };
}
}
}
Expand Down

0 comments on commit cf44c66

Please sign in to comment.