-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding completion params passing down (#27)
- Loading branch information
Showing
5 changed files
with
162 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
apps/desktop/src-tauri/src/inference_thread/stop_handler.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/// Based on https://github.com/LLukas22/llm-rs-python/blob/e2a6d459e39df9be14442676fa43397a3b8753a4/src/stopwords.rs#L33 | ||
/// Copyright (c) 2023 Lukas Kreussel | ||
/// MIT License | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashSet; | ||
|
||
#[derive(Clone, Serialize, Deserialize)] | ||
struct Buffer<T> { | ||
pub data: Vec<T>, | ||
capacity: usize, | ||
} | ||
|
||
impl<T> Buffer<T> { | ||
fn new(capacity: usize) -> Self { | ||
Buffer { | ||
data: Vec::with_capacity(capacity), | ||
capacity, | ||
} | ||
} | ||
|
||
fn push(&mut self, item: T) { | ||
if self.data.len() == self.capacity { | ||
self.data.remove(0); | ||
} | ||
self.data.push(item); | ||
} | ||
} | ||
|
||
#[derive(Clone, Serialize, Deserialize)] | ||
pub struct StopHandler { | ||
pub stops: HashSet<Vec<u8>>, | ||
buffer: Buffer<u8>, | ||
capacity: usize, | ||
} | ||
|
||
impl StopHandler { | ||
pub fn new(model: &dyn llm::Model, stops: &[String]) -> StopHandler { | ||
let stop_tokens: HashSet<Vec<u8>> = stops | ||
.iter() | ||
.map(|word| { | ||
model | ||
.vocabulary() | ||
.tokenize(word, false) | ||
.unwrap() | ||
.iter() | ||
.flat_map(|(encoding, _)| encoding.to_owned()) | ||
.collect::<Vec<u8>>() | ||
}) | ||
.collect(); | ||
|
||
let capacity = stop_tokens.iter().map(|v| v.len()).max().unwrap_or(0); | ||
|
||
StopHandler { | ||
stops: stop_tokens, | ||
buffer: Buffer::new(capacity), | ||
capacity, | ||
} | ||
} | ||
|
||
pub fn check(&mut self, new_tokens: &Vec<u8>) -> bool { | ||
if self.capacity == 0 { | ||
return false; | ||
} | ||
|
||
for token in new_tokens { | ||
self.buffer.push(token.to_owned()); | ||
for i in 0..self.buffer.data.len() { | ||
let slice = self.buffer.data[i..].to_vec(); | ||
if self.stops.contains(&slice) { | ||
return true; | ||
} | ||
} | ||
} | ||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7adf294
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
local-ai-web – ./
local-ai-web-git-main-louisgv.vercel.app
local-ai-web.vercel.app
localai.app
www.localai.app
local-ai-web-louisgv.vercel.app