Skip to content

Commit

Permalink
feat: return usage in ChatCompletionStreamResponse (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
GirinMan committed Jun 11, 2024
1 parent 84fb56d commit 4187cab
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ struct ChatCompletionStreamResponse {
created: i64,
model: String,
choices: Vec<ChatCompletionStreamResponseChoice>,
#[serde(skip_serializing_if = "Option::is_none")]
usage: Option<UsageInfo>,
}

#[derive(Serialize, ToSchema, PartialEq)]
Expand Down Expand Up @@ -824,6 +826,20 @@ impl From<GenerateResponse> for ChatCompletionResponse {

impl From<StreamResponse> for ChatCompletionStreamResponse {
fn from(resp: StreamResponse) -> Self {
let prompt_tokens = resp.details.as_ref().map(|x| x.prompt_tokens).unwrap_or(0);
let completion_tokens = resp.details.as_ref().map(|x| x.generated_tokens);
let total_tokens = prompt_tokens + completion_tokens.unwrap_or(0);

let usage: Option<UsageInfo> = if completion_tokens.is_some() {
Some(UsageInfo {
prompt_tokens: prompt_tokens,
total_tokens: total_tokens,
completion_tokens: completion_tokens,
})
} else {
None
};

let finish_reason = resp
.details
.map(|x| CompletionFinishReason::from(x.finish_reason));
Expand All @@ -849,6 +865,7 @@ impl From<StreamResponse> for ChatCompletionStreamResponse {
},
finish_reason: finish_reason,
}],
usage: usage,
}
}
}
Expand Down

0 comments on commit 4187cab

Please sign in to comment.