Skip to content

Commit

Permalink
Add tokens count for chat completion (#97)
Browse files Browse the repository at this point in the history
Signed-off-by: NguyenNguyen205 <[email protected]>
  • Loading branch information
NguyenNguyen205 authored Nov 25, 2024
1 parent 78503df commit 8d4b208
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions actions/inference.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ function generateResponseContent(
system_fingerprint,
stream,
content,
stopped
stopped,
tokens_predicted,
tokens_evaluated
) {
const resp = {
id,
Expand All @@ -61,9 +63,9 @@ function generateResponseContent(
};
if (!stream) {
resp.usage = {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0,
prompt_tokens: tokens_evaluated,
completion_tokens: tokens_predicted,
total_tokens: tokens_evaluated + tokens_predicted,
};
}
return resp;
Expand Down Expand Up @@ -187,18 +189,18 @@ export async function chatCompletion(req, res) {
res.setHeader("Connection", "Keep-Alive");
}
doInference(request_body, (data) => {
const { content, stop } = data;
const { content, stop, tokens_predicted, tokens_evaluated } = data;
if(isStream) {
res.write(JSON.stringify(
generateResponseContent(
api_key, 'chat.completion.chunk', model, system_fingerprint, isStream, content, stop
api_key, 'chat.completion.chunk', model, system_fingerprint, isStream, content, stop, tokens_predicted, tokens_evaluated
)
)+'\n\n');
if(stop) res.end();
} else {
res.send(generateResponseContent(
api_key, 'chat.completion', model, system_fingerprint,
isStream, content, true
isStream, content, true, tokens_predicted, tokens_evaluated
))
}
}, isStream)
Expand Down

0 comments on commit 8d4b208

Please sign in to comment.