feat: add OpenAI generation stats

This commit is contained in:
Jun Siang Cheah
2024-05-27 10:11:53 +01:00
parent b6b71c08f3
commit 99b1661638
6 changed files with 61 additions and 7 deletions

View File

@@ -8,6 +8,16 @@ type TextStreamUpdate = {
citations?: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
error?: any;
usage?: ResponseUsage;
};
type ResponseUsage = {
/** Including images and tools if any */
prompt_tokens: number;
/** The tokens generated */
completion_tokens: number;
/** Sum of the above two fields */
total_tokens: number;
};
// createOpenAITextStream takes a responseBody with a SSE response,
@@ -59,7 +69,11 @@ async function* openAIStreamToIterator(
continue;
}
yield { done: false, value: parsedData.choices?.[0]?.delta?.content ?? '' };
yield {
done: false,
value: parsedData.choices?.[0]?.delta?.content ?? '',
usage: parsedData.usage
};
} catch (e) {
console.error('Error extracting delta from SSE event:', e);
}