Skip to content

Commit

Permalink
Gracefully stop when token generated does not make sense. (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
cryscan authored Feb 3, 2025
1 parent b9b6a3b commit 9990b6b
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions crates/ai00-core/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use crate::{
Environment, FinishReason, GenerateKind, GenerateRequest, ReloadRequest, Token, TokenCounter,
};

const END_OF_LINE_TOKEN: u16 = 261;
const MIN_PROMPT_CACHE_TOKENS: usize = 32;
const MAX_CACHE_ITEMS: usize = 256;

Expand Down Expand Up @@ -900,16 +899,20 @@ impl Runtime {
);
}

// map token 0 output to "\n\n"
let token = match token {
0 => END_OF_LINE_TOKEN,
_ => *token,
};
let token = *token;
let mut stop_token = token == 0;

assert_eq!(context.suffix.len(), 0);
context.suffix.0.push(token);

let mut word = self.tokenizer.decode(&[token])?;
let mut word = match self.tokenizer.decode(&[token]) {
Ok(word) => word,
Err(err) => {
log::warn!("{err}");
stop_token = true;
Default::default()
}
};
context.model_text.append(&mut word.clone());
context.buffer.append(&mut word);
context.model_tokens.push(token);
Expand Down Expand Up @@ -1031,7 +1034,7 @@ impl Runtime {
}
let _ = context.sender.send(Token::Choose(perplexities));
done = true;
} else if halt || stop_matched {
} else if halt || stop_matched || stop_token {
let output = String::from_utf8_lossy(head);
let _ = context.sender.send(Token::Content(output.into()));
stop(FinishReason::Stop);
Expand Down

0 comments on commit 9990b6b

Please sign in to comment.