From 8063a0ca8b500a549026bf4aef49e822de312f8e Mon Sep 17 00:00:00 2001 From: tryedandcatched Date: Tue, 27 Feb 2024 21:58:15 +0100 Subject: [PATCH] starting to implement llama --- src/llama.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/llama.rs diff --git a/src/llama.rs b/src/llama.rs new file mode 100644 index 0000000..a80ff30 --- /dev/null +++ b/src/llama.rs @@ -0,0 +1,52 @@ +use std::io::{BufRead, BufReader}; +use std::process::{self, Command, Stdio}; +pub fn execute(llama_path: &String, model_path: &String, prompt: &String) -> String { + let mut command = process::Command::new("prime"); + let command = command + .arg(llama_path) + .arg("--model") + .arg(model_path) + .arg("--prompt") + .arg(prompt); + + let mut child = command + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + + // Create readers for the output streams + let stdout = child.stdout.take().unwrap(); + let stderr = child.stderr.take().unwrap(); + let stdout_reader = BufReader::new(stdout); + let stderr_reader = BufReader::new(stderr); + + // Iterate over output lines and process them + let mut start_printing = false; + let mut output = String::new(); + for line_result in stdout_reader.lines() { + let line = line_result.unwrap(); + let mut processed_line = line.replace(prompt, "").replace("\n", "").replace("`", ""); + processed_line = processed_line.trim().to_string(); + + if line.contains(prompt) { + start_printing = true; + } + + if start_printing { + output = output + &processed_line + "\n"; + } + + } + + // Handle potential errors from the child process + let exit_status = child.wait().unwrap(); + if !exit_status.success() { + for line_result in stderr_reader.lines() { + let error_line = line_result.unwrap(); + eprintln!("Error: {}", error_line); + } + } + + output +}