Skip to content

Commit

Permalink
feat: prefer stop over eos_token to align with openai finish_reason (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
drbh authored Aug 6, 2024
1 parent e11f5f1 commit f8a5b38
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 10 additions & 1 deletion router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ impl ChatCompletion {
message,
logprobs: return_logprobs
.then(|| ChatCompletionLogprobs::from((details.tokens, details.top_tokens))),
finish_reason: details.finish_reason.to_string(),
finish_reason: details.finish_reason.format(true),
}],
usage: Usage {
prompt_tokens: details.prefill.len() as u32,
Expand Down Expand Up @@ -1117,6 +1117,15 @@ impl std::fmt::Display for FinishReason {
}
}

impl FinishReason {
pub fn format(&self, use_stop: bool) -> String {
match self {
FinishReason::EndOfSequenceToken if use_stop => "stop".to_string(),
_ => self.to_string(),
}
}
}

#[derive(Serialize, ToSchema)]
pub(crate) struct BestOfSequence {
#[schema(example = "test")]
Expand Down
4 changes: 2 additions & 2 deletions router/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ async fn completions(
total_tokens += details.prefill.len() as u32 + details.generated_tokens;

Ok(CompletionComplete {
finish_reason: details.finish_reason.to_string(),
finish_reason: details.finish_reason.format(true),
index: index as u32,
logprobs: None,
text: generation.generated_text,
Expand Down Expand Up @@ -1212,7 +1212,7 @@ async fn chat_completions(
tool_calls,
current_time,
logprobs,
stream_token.details.map(|d| d.finish_reason.to_string()),
stream_token.details.map(|d| d.finish_reason.format(true)),
),
))
.unwrap_or_else(|e| {
Expand Down

0 comments on commit f8a5b38

Please sign in to comment.