From 54901515efad3ac5c2cfe638c5c2da9801c0f5cf Mon Sep 17 00:00:00 2001 From: Meng Zhang Date: Tue, 6 Jun 2023 15:47:54 -0700 Subject: [PATCH] fmt --- crates/ctranslate2-bindings/src/lib.rs | 38 ++++++++++++------- crates/tabby/src/serve/completions.rs | 5 +-- .../tabby/src/serve/completions/languages.rs | 5 +-- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/crates/ctranslate2-bindings/src/lib.rs b/crates/ctranslate2-bindings/src/lib.rs index a29ae71..12fbe68 100644 --- a/crates/ctranslate2-bindings/src/lib.rs +++ b/crates/ctranslate2-bindings/src/lib.rs @@ -69,18 +69,22 @@ pub struct TextInferenceOptions { #[builder(default = "1.0")] sampling_temperature: f32, - stop_words: &'static Vec<&'static str> + stop_words: &'static Vec<&'static str>, } pub struct InferenceContext { stop_re: Option, cancel: CancellationToken, - reversed_output_text: String + reversed_output_text: String, } impl InferenceContext { fn new(stop_re: Option, cancel: CancellationToken) -> Self { - InferenceContext { stop_re, cancel, reversed_output_text: "".to_owned() } + InferenceContext { + stop_re, + cancel, + reversed_output_text: "".to_owned(), + } } } @@ -116,8 +120,11 @@ impl TextInferenceEngine { None } else { // FIXME(meng): consider cache the regexp. - let encodings = self.tokenizer.encode_batch(options.stop_words.clone(), false).unwrap(); - let stop_tokens : Vec = encodings + let encodings = self + .tokenizer + .encode_batch(options.stop_words.clone(), false) + .unwrap(); + let stop_tokens: Vec = encodings .iter() .map(|x| x.get_tokens().join("")) // Reverse for efficient suffix matching. @@ -146,18 +153,21 @@ impl TextInferenceEngine { } } -fn inference_callback(context: &mut InferenceContext, _step: usize, _token_id: u32, token: String) -> bool { +fn inference_callback( + context: &mut InferenceContext, + _step: usize, + _token_id: u32, + token: String, +) -> bool { if context.cancel.is_cancelled() { true + } else if let Some(re) = &context.stop_re { + let mut new_token = reverse(token); + new_token.push_str(&context.reversed_output_text); + context.reversed_output_text = new_token; + re.find(&context.reversed_output_text).is_some() } else { - if let Some(re) = &context.stop_re { - let mut new_token = reverse(token); - new_token.push_str(&context.reversed_output_text); - context.reversed_output_text = new_token; - re.find(&context.reversed_output_text).is_some() - } else { - false - } + false } } diff --git a/crates/tabby/src/serve/completions.rs b/crates/tabby/src/serve/completions.rs index 8edaf87..0e4c136 100644 --- a/crates/tabby/src/serve/completions.rs +++ b/crates/tabby/src/serve/completions.rs @@ -100,10 +100,7 @@ pub async fn completion( Json(CompletionResponse { id: completion_id, - choices: vec![Choice { - index: 0, - text, - }], + choices: vec![Choice { index: 0, text }], }) } diff --git a/crates/tabby/src/serve/completions/languages.rs b/crates/tabby/src/serve/completions/languages.rs index a0f4ef3..8ed5a3e 100644 --- a/crates/tabby/src/serve/completions/languages.rs +++ b/crates/tabby/src/serve/completions/languages.rs @@ -6,10 +6,7 @@ lazy_static! { static ref DEFAULT: Vec<&'static str> = vec!("\n\n"); static ref LANGUAGES: HashMap<&'static str, Vec<&'static str>> = { let mut map = HashMap::new(); - map.insert( - "python", - vec!("\n\n", "\ndef", "\n#", "\nfrom", "\nclass") - ); + map.insert("python", vec!["\n\n", "\ndef", "\n#", "\nfrom", "\nclass"]); map }; }