Skip to content

Commit

Permalink
chore(rust): fix current clippy lint (yetone#504)
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
  • Loading branch information
aarnphm authored Sep 4, 2024
1 parent e57a3f2 commit 41c7812
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,15 @@ jobs:
components: clippy, rustfmt
- name: Run rustfmt
run: make ruststylecheck
rustlint:
name: Lint Rust
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: clippy, rustfmt
- name: Run rustfmt
run: make rustlint
13 changes: 7 additions & 6 deletions crates/avante-templates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<'a> State<'a> {
}
}

#[derive(Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
struct TemplateContext {
use_xml_format: bool,
ask: bool,
Expand All @@ -29,16 +29,17 @@ struct TemplateContext {

// Given the file name registered after add, the context table in Lua, resulted in a formatted
// Lua string
fn render(state: &State, template: String, context: TemplateContext) -> LuaResult<String> {
#[allow(clippy::needless_pass_by_value)]
fn render(state: &State, template: &str, context: TemplateContext) -> LuaResult<String> {
let environment = state.environment.lock().unwrap();
match environment.as_ref() {
Some(environment) => {
let template = environment
.get_template(&template)
let jinja_template = environment
.get_template(template)
.map_err(LuaError::external)
.unwrap();

Ok(template
Ok(jinja_template
.render(context! {
use_xml_format => context.use_xml_format,
ask => context.ask,
Expand Down Expand Up @@ -84,7 +85,7 @@ fn avante_templates(lua: &Lua) -> LuaResult<LuaTable> {
"render",
lua.create_function_mut(move |lua, (template, context): (String, LuaValue)| {
let ctx = lua.from_value(context)?;
render(&state_clone, template, ctx)
render(&state_clone, template.as_str(), ctx)
})?,
)?;
Ok(exports)
Expand Down
28 changes: 16 additions & 12 deletions crates/avante-tokenizers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ struct Tiktoken {
}

impl Tiktoken {
fn new(model: String) -> Self {
let bpe = get_bpe_from_model(&model).unwrap();
fn new(model: &str) -> Self {
let bpe = get_bpe_from_model(model).unwrap();
Tiktoken { bpe }
}

fn encode(&self, text: String) -> (Vec<usize>, usize, usize) {
let tokens = self.bpe.encode_with_special_tokens(&text);
fn encode(&self, text: &str) -> (Vec<usize>, usize, usize) {
let tokens = self.bpe.encode_with_special_tokens(text);
let num_tokens = tokens.len();
let num_chars = text.chars().count();
(tokens, num_tokens, num_chars)
Expand All @@ -26,13 +26,17 @@ struct HuggingFaceTokenizer {
}

impl HuggingFaceTokenizer {
fn new(model: String) -> Self {
fn new(model: &str) -> Self {
let tokenizer = Tokenizer::from_pretrained(model, None).unwrap();
HuggingFaceTokenizer { tokenizer }
}

fn encode(&self, text: String) -> (Vec<usize>, usize, usize) {
let encoding = self.tokenizer.encode(text, false).unwrap();
fn encode(&self, text: &str) -> (Vec<usize>, usize, usize) {
let encoding = self
.tokenizer
.encode(text, false)
.map_err(LuaError::external)
.unwrap();
let tokens: Vec<usize> = encoding.get_ids().iter().map(|x| *x as usize).collect();
let num_tokens = tokens.len();
let num_chars = encoding.get_offsets().last().unwrap().1;
Expand All @@ -57,7 +61,7 @@ impl State {
}
}

fn encode(state: &State, text: String) -> LuaResult<(Vec<usize>, usize, usize)> {
fn encode(state: &State, text: &str) -> LuaResult<(Vec<usize>, usize, usize)> {
let tokenizer = state.tokenizer.lock().unwrap();
match tokenizer.as_ref() {
Some(TokenizerType::Tiktoken(tokenizer)) => Ok(tokenizer.encode(text)),
Expand All @@ -68,9 +72,9 @@ fn encode(state: &State, text: String) -> LuaResult<(Vec<usize>, usize, usize)>
}
}

fn from_pretrained(state: &State, model: String) {
fn from_pretrained(state: &State, model: &str) {
let mut tokenizer_mutex = state.tokenizer.lock().unwrap();
*tokenizer_mutex = Some(match model.as_str() {
*tokenizer_mutex = Some(match model {
"gpt-4o" => TokenizerType::Tiktoken(Tiktoken::new(model)),
_ => TokenizerType::HuggingFace(HuggingFaceTokenizer::new(model)),
});
Expand All @@ -86,13 +90,13 @@ fn avante_tokenizers(lua: &Lua) -> LuaResult<LuaTable> {
exports.set(
"from_pretrained",
lua.create_function(move |_, model: String| {
from_pretrained(&state, model);
from_pretrained(&state, model.as_str());
Ok(())
})?,
)?;
exports.set(
"encode",
lua.create_function(move |_, text: String| encode(&state_clone, text))?,
lua.create_function(move |_, text: String| encode(&state_clone, text.as_str()))?,
)?;
Ok(exports)
}

0 comments on commit 41c7812

Please sign in to comment.