YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
feat: add mistral but unused RN
Louis Beaumont6337a709db69
diff --git a/screenpipe-core/examples/mistral.rs b/screenpipe-core/examples/mistral.rsnew file mode 100644index 000000000..1202f80b9--- /dev/null+++ b/screenpipe-core/examples/mistral.rs@@ -0,0 +1,13 @@+use anyhow::Result;+#[cfg(feature = "llm")]+use screenpipe_core::mistral::MistralConfig;+use screenpipe_core::stream_text;++fn main() -> Result<()> {+ #[cfg(feature = "llm")]+ stream_text(MistralConfig::default(), |text| {+ println!("{}", text);+ Ok(())+ })?;+ Ok(())+}diff --git a/screenpipe-core/examples/phi.rs b/screenpipe-core/examples/phi.rsindex dbf293a6a..33beae7a9 100644--- a/screenpipe-core/examples/phi.rs+++ b/screenpipe-core/examples/phi.rs@@ -1,7 +1,7 @@use anyhow::Result;use candle::Device;use screenpipe_core::{generate_text_streaming, load_llama_model};-use std::io::BufRead;+use std::env;fn main() -> Result<()> {let device = Device::new_metal(0).unwrap_or(Device::new_cuda(0).unwrap_or(Device::Cpu));@@ -9,11 +9,10 @@ fn main() -> Result<()> {let (mut model, tokenizer) = load_llama_model(&device)?;// use input from stdin if available- let prompt = std::io::stdin().lock().lines().next().map_or(- "explain quantum computing in simple terms:".to_string(),- |line| line.unwrap().to_string(),- );- let max_tokens = 10;+ let prompt = env::args()+ .nth(1)+ .unwrap_or_else(|| "explain quantum computing in simple terms:".to_string());+ let max_tokens = 50;let temperature = 0.7;let top_p = 0.9;let seed = 42;diff --git a/screenpipe-core/src/lib.rs b/screenpipe-core/src/lib.rsindex bc0226c1e..5d9af82b9 100644--- a/screenpipe-core/src/lib.rs+++ b/screenpipe-core/src/lib.rs@@ -4,6 +4,10 @@ pub use ffmpeg::find_ffmpeg_path;pub mod llm;#[cfg(feature = "llm")]pub use llm::*;+#[cfg(feature = "llm")]+pub mod mistral;+#[cfg(feature = "llm")]+pub use mistral::*;#[cfg(feature = "pipes")]pub mod pipes;#[cfg(feature = "pipes")]diff --git a/screenpipe-core/src/llm.rs b/screenpipe-core/src/llm.rsindex cf18af87d..9d7d1bab1 100755--- a/screenpipe-core/src/llm.rs+++ b/screenpipe-core/src/llm.rs@@ -85,28 +85,22 @@ mod llm_module {let mut logits_processor = LogitsProcessor::new(seed, Some(temperature), Some(top_p));let tokens = tokenizer.encode(prompt, true).unwrap();if tokens.is_empty() {- anyhow::bail!("Empty prompts are not supported in the phi model.")- }- if true {- // DEBUG- for (token, id) in tokens.get_tokens().iter().zip(tokens.get_ids().iter()) {- let token = token.replace('▁', " ").replace("<0x0A>", "\n");- println!("{id:7} -> '{token}'");- }+ anyhow::bail!("empty prompt")}let mut tokens = tokens.get_ids().to_vec();let eos_token = match tokenizer.token_to_id("<|endoftext|>") {Some(token) => token,None => anyhow::bail!("cannot find the endoftext token"),};- print!("{prompt}");+let mut pos = 0;- for index in 0..max_tokens {- let context_size = if index > 0 { 1 } else { tokens.len() };+ for _ in 0..max_tokens {+ let context_size = if pos > 0 { 1 } else { tokens.len() };let ctxt = &tokens[tokens.len().saturating_sub(context_size)..];let input = Tensor::new(ctxt, device)?.unsqueeze(0)?;let logits = model.forward(&input, pos)?;let logits = logits.squeeze(0)?.to_dtype(DType::F32)?;+let logits = if repeat_penalty == 1. {logits} else {@@ -128,17 +122,12 @@ mod llm_module {let next_token = logits_processor.sample(&logits)?;tokens.push(next_token);if next_token == eos_token {- if let Ok(t) = tokenizer.decode(&[next_token], true) {- print!("{t}");- callback(t)?;- }break;}- if let Ok(t) = tokenizer.decode(&[next_token], true) {- print!("{t}");+ if let Ok(t) = tokenizer.decode(&[next_token], false) {callback(t)?;}- pos += context_size;+ pos += 1;}Ok(())diff --git a/screenpipe-core/src/mistral.rs b/screenpipe-core/src/mistral.rsnew file mode 100644index 000000000..b270a01ea--- /dev/null+++ b/screenpipe-core/src/mistral.rs@@ -0,0 +1,461 @@+#[cfg(feature = "llm")]+mod llm_module {++ use anyhow::{Error as E, Result};++ use candle_transformers::models::mistral::{Config, Model as Mistral};+ use candle_transformers::models::quantized_mistral::Model as QMistral;++ use candle::{DType, Device, Tensor};+ use candle_nn::VarBuilder;+ use candle_transformers::generation::{LogitsProcessor, Sampling};+ use hf_hub::api::sync::ApiBuilder;+ use hf_hub::{Repo, RepoType};+ use tokenizers::Tokenizer;++ use crate::hub_load_safetensors;++ /// This is a wrapper around a tokenizer to ensure that tokens can be returned to the user in a+ /// streaming way rather than having to wait for the full decoding.+ pub struct TokenOutputStream {+ tokenizer: tokenizers::Tokenizer,+ tokens: Vec<u32>,+ prev_index: usize,+ current_index: usize,+ }++ impl TokenOutputStream {+ pub fn new(tokenizer: tokenizers::Tokenizer) -> Self {+ Self {+ tokenizer,+ tokens: Vec::new(),+ prev_index: 0,+ current_index: 0,+ }+ }++ pub fn into_inner(self) -> tokenizers::Tokenizer {+ self.tokenizer+ }++ fn decode(&self, tokens: &[u32]) -> Result<String> {+ match self.tokenizer.decode(tokens, true) {+ Ok(str) => Ok(str),+ Err(err) => anyhow::bail!("cannot decode: {err}"),+ }+ }++ // https://github.com/huggingface/text-generation-inference/blob/5ba53d44a18983a4de32d122f4cb46f4a17d9ef6/server/text_generation_server/models/model.py#L68+ pub fn next_token(&mut self, token: u32) -> Result<Option<String>> {+ let prev_text = if self.tokens.is_empty() {+ String::new()+ } else {+ let tokens = &self.tokens[self.prev_index..self.current_index];+ self.decode(tokens)?+ };+ self.tokens.push(token);+ let text = self.decode(&self.tokens[self.prev_index..])?;+ if text.len() > prev_text.len() && text.chars().last().unwrap().is_alphanumeric() {+ let text = text.split_at(prev_text.len());+ self.prev_index = self.current_index;+ self.current_index = self.tokens.len();+ Ok(Some(text.1.to_string()))+ } else {+ Ok(None)+ }+ }++ pub fn decode_rest(&self) -> Result<Option<String>> {+ let prev_text = if self.tokens.is_empty() {+ String::new()+ } else {+ let tokens = &self.tokens[self.prev_index..self.current_index];+ self.decode(tokens)?+ };+ let text = self.decode(&self.tokens[self.prev_index..])?;+ if text.len() > prev_text.len() {+ let text = text.split_at(prev_text.len());+ Ok(Some(text.1.to_string()))+ } else {+ Ok(None)+ }+ }++ pub fn decode_all(&self) -> Result<String> {+ self.decode(&self.tokens)+ }++ pub fn get_token(&self, token_s: &str) -> Option<u32> {+ self.tokenizer.get_vocab(true).get(token_s).copied()+ }++ pub fn tokenizer(&self) -> &tokenizers::Tokenizer {+ &self.tokenizer+ }++ pub fn clear(&mut self) {+ self.tokens.clear();+ self.prev_index = 0;+ self.current_index = 0;+ }+ }++ enum Model {+ Mistral(Mistral),+ Quantized(QMistral),+ }++ struct TextGeneration {+ model: Model,+ device: Device,+ tokenizer: TokenOutputStream,+ logits_processor: LogitsProcessor,+ repeat_penalty: f32,+ repeat_last_n: usize,+ }++ impl TextGeneration {+ #[allow(clippy::too_many_arguments)]+ fn new(+ model: Model,+ tokenizer: Tokenizer,+ seed: u64,+ temp: Option<f64>,+ top_p: Option<f64>,+ top_k: Option<usize>,+ repeat_penalty: f32,+ repeat_last_n: usize,+ device: &Device,+ ) -> Self {+ let logits_processor = {+ let temperature = temp.unwrap_or(0.);+ let sampling = if temperature <= 0. {+ Sampling::ArgMax+ } else {+ match (top_k, top_p) {+ (None, None) => Sampling::All { temperature },+ (Some(k), None) => Sampling::TopK { k, temperature },+ (None, Some(p)) => Sampling::TopP { p, temperature },+ (Some(k), Some(p)) => Sampling::TopKThenTopP { k, p, temperature },+ }+ };+ LogitsProcessor::from_sampling(seed, sampling)+ };++ Self {+ model,+ tokenizer: TokenOutputStream::new(tokenizer),+ logits_processor,+ repeat_penalty,+ repeat_last_n,+ device: device.clone(),+ }+ }++ fn run(&mut self, prompt: &str, sample_len: usize) -> Result<()> {+ use std::io::Write;+ self.tokenizer.clear();+ let mut tokens = self+ .tokenizer+ .tokenizer()+ .encode(prompt, true)+ .map_err(E::msg)?+ .get_ids()+ .to_vec();+ for &t in tokens.iter() {+ if let Some(t) = self.tokenizer.next_token(t)? {+ print!("{t}")+ }+ }+ std::io::stdout().flush()?;++ let mut generated_tokens = 0usize;+ let eos_token = match self.tokenizer.get_token("</s>") {+ Some(token) => token,+ None => anyhow::bail!("cannot find the </s> token"),+ };+ let start_gen = std::time::Instant::now();+ for index in 0..sample_len {+ let context_size = if index > 0 { 1 } else { tokens.len() };+ let start_pos = tokens.len().saturating_sub(context_size);+ let ctxt = &tokens[start_pos..];+ let input = Tensor::new(ctxt, &self.device)?.unsqueeze(0)?;+ let logits = match &mut self.model {+ Model::Mistral(m) => m.forward(&input, start_pos)?,+ Model::Quantized(m) => m.forward(&input, start_pos)?,+ };+ let logits = logits.squeeze(0)?.squeeze(0)?.to_dtype(DType::F32)?;+ let logits = if self.repeat_penalty == 1. {+ logits+ } else {+ let start_at = tokens.len().saturating_sub(self.repeat_last_n);+ candle_transformers::utils::apply_repeat_penalty(+ &logits,+ self.repeat_penalty,+ &tokens[start_at..],+ )?+ };++ let next_token = self.logits_processor.sample(&logits)?;+ tokens.push(next_token);+ generated_tokens += 1;+ if next_token == eos_token {+ break;+ }+ if let Some(t) = self.tokenizer.next_token(next_token)? {+ print!("{t}");+ std::io::stdout().flush()?;+ }+ }+ let dt = start_gen.elapsed();+ if let Some(rest) = self.tokenizer.decode_rest().map_err(E::msg)? {+ print!("{rest}");+ }+ std::io::stdout().flush()?;+ println!(+ "\n{generated_tokens} tokens generated ({:.2} token/s)",+ generated_tokens as f64 / dt.as_secs_f64(),+ );+ Ok(())+ }+ }++ #[derive(Clone, Debug, Copy, PartialEq, Eq)]+ enum Which {+ Mistral7bV01,+ Mistral7bV02,+ Mistral7bInstructV01,+ Mistral7bInstructV02,+ Mistral7bInstructV03,+ Mathstral7bV01,+ MistralNemo2407,+ MistralNemoInstruct2407,+ }++ impl Which {+ fn as_str(&self) -> &'static str {+ match self {+ Which::Mistral7bV01 => "7b-v0.1",+ Which::Mistral7bV02 => "7b-v0.2",+ Which::Mistral7bInstructV01 => "7b-instruct-v0.1",+ Which::Mistral7bInstructV02 => "7b-instruct-v0.2",+ Which::Mistral7bInstructV03 => "7b-instruct-v0.3",+ Which::Mathstral7bV01 => "7b-maths-v0.1",+ Which::MistralNemo2407 => "nemo-2407",+ Which::MistralNemoInstruct2407 => "nemo-instruct-2407",+ }+ }+ }++ impl ToString for Which {+ fn to_string(&self) -> String {+ self.as_str().to_string()+ }+ }++ #[derive(Debug)]+ pub struct MistralConfig {+ /// Run on CPU rather than on GPU.+ cpu: bool,++ /// Enable tracing (generates a trace-timestamp.json file).+ tracing: bool,++ use_flash_attn: bool,++ prompt: String,++ /// The temperature used to generate samples.+ temperature: Option<f64>,++ /// Nucleus sampling probability cutoff.+ top_p: Option<f64>,++ /// Only sample among the top K samples.+ top_k: Option<usize>,++ /// The seed to use when generating random samples.+ seed: u64,++ /// The length of the sample to generate (in tokens).+ sample_len: usize,++ /// The model size to use.+ which: Which,++ model_id: Option<String>,++ revision: String,++ tokenizer_file: Option<String>,++ config_file: Option<String>,++ weight_files: Option<String>,++ quantized: bool,++ /// Penalty to be applied for repeating tokens, 1. means no penalty.+ repeat_penalty: f32,++ /// The context size to consider for the repeat penalty.+ repeat_last_n: usize,++ /// Use the slower dmmv cuda kernel.+ force_dmmv: bool,+ }++ impl Default for MistralConfig {+ fn default() -> Self {+ Self {+ cpu: false,+ tracing: false,+ use_flash_attn: false,+ prompt: String::new(),+ temperature: Some(0.8),+ top_p: Some(0.95),+ top_k: None,+ seed: 299792458,+ sample_len: 100,+ which: Which::Mistral7bInstructV03,+ model_id: None,+ revision: "main".to_string(),+ tokenizer_file: None,+ config_file: None,+ weight_files: None,+ quantized: false,+ repeat_penalty: 1.1,+ repeat_last_n: 64,+ force_dmmv: false,+ }+ }+ }++ pub fn stream_text<F>(args: MistralConfig, mut callback: F) -> Result<()>+ where+ F: FnMut(String) -> Result<()>,+ {+ #[cfg(feature = "cuda")]+ candle::quantized::cuda::set_force_dmmv(args.force_dmmv);++ println!(+ "avx: {}, neon: {}, simd128: {}, f16c: {}",+ candle::utils::with_avx(),+ candle::utils::with_neon(),+ candle::utils::with_simd128(),+ candle::utils::with_f16c()+ );+ println!(+ "temp: {:.2} repeat-penalty: {:.2} repeat-last-n: {}",+ args.temperature.unwrap_or(0.),+ args.repeat_penalty,+ args.repeat_last_n+ );++ let start = std::time::Instant::now();+ let api = ApiBuilder::new()+ // ! hardcoded louis token dont CARE+ .with_token(Some("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".to_string()))+ .build()?;+ let model_id = match args.model_id {+ Some(model_id) => model_id,+ None => {+ if args.quantized {+ if args.which != Which::Mistral7bV01 {+ anyhow::bail!("only 7b-v0.1 is available as a quantized model for now")+ }+ "lmz/candle-mistral".to_string()+ } else {+ let name = match args.which {+ // https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.3+ Which::Mistral7bV01 => "mistralai/Mistral-7B-v0.1",+ Which::Mistral7bV02 => "mistralai/Mistral-7B-v0.2",+ Which::Mistral7bInstructV01 => "mistralai/Mistral-7B-Instruct-v0.1",+ Which::Mistral7bInstructV02 => "mistralai/Mistral-7B-Instruct-v0.2",+ Which::Mistral7bInstructV03 => "mistralai/Mistral-7B-Instruct-v0.3",+ Which::Mathstral7bV01 => "mistralai/mathstral-7B-v0.1",+ Which::MistralNemo2407 => "mistralai/Mistral-Nemo-Base-2407",+ Which::MistralNemoInstruct2407 => "mistralai/Mistral-Nemo-Instruct-2407",+ };+ name.to_string()+ }+ }+ };+ let repo = api.repo(Repo::with_revision(+ model_id,+ RepoType::Model,+ args.revision,+ ));+ let tokenizer_filename = match args.tokenizer_file {+ Some(file) => std::path::PathBuf::from(file),+ None => repo.get("tokenizer.json")?,+ };+ let filenames = match args.weight_files {+ Some(files) => files+ .split(',')+ .map(std::path::PathBuf::from)+ .collect::<Vec<_>>(),+ None => {+ if args.quantized {+ vec![repo.get("model-q4k.gguf")?]+ } else {+ hub_load_safetensors(&repo, "model.safetensors.index.json")?+ }+ }+ };+ println!("retrieved the files in {:?}", start.elapsed());+ let tokenizer = Tokenizer::from_file(tokenizer_filename).map_err(E::msg)?;++ let start = std::time::Instant::now();+ let config = match args.config_file {+ Some(config_file) => serde_json::from_slice(&std::fs::read(config_file)?)?,+ None => {+ if args.quantized {+ Config::config_7b_v0_1(args.use_flash_attn)+ } else {+ let config_file = repo.get("config.json")?;+ serde_json::from_slice(&std::fs::read(config_file)?)?+ }+ }+ };+ let device = Device::new_metal(0).unwrap_or(Device::new_cuda(0).unwrap_or(Device::Cpu));++ let (model, device) = if args.quantized {+ let filename = &filenames[0];+ let vb = candle_transformers::quantized_var_builder::VarBuilder::from_gguf(+ filename, &device,+ )?;+ let model = QMistral::new(&config, vb)?;+ (Model::Quantized(model), device)+ } else {+ let dtype = if device.is_cuda() {+ DType::BF16+ } else {+ DType::F32+ };+ let vb = unsafe { VarBuilder::from_mmaped_safetensors(&filenames, dtype, &device)? };+ let model = Mistral::new(&config, vb)?;+ (Model::Mistral(model), device)+ };++ println!("loaded the model in {:?}", start.elapsed());++ let mut pipeline = TextGeneration::new(+ model,+ tokenizer,+ args.seed,+ args.temperature,+ args.top_p,+ args.top_k,+ args.repeat_penalty,+ args.repeat_last_n,+ &device,+ );+ pipeline.run(&args.prompt, args.sample_len)?;+ Ok(())+ }+}++// Optionally, you can re-export the module contents if needed+#[cfg(feature = "llm")]+pub use llm_module::*;