-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use aichat as the chat backend
- Loading branch information
Showing
4 changed files
with
138 additions
and
81 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::process::Command; | ||
|
||
pub struct AiChat { | ||
binary_location: String, | ||
} | ||
|
||
impl Default for AiChat { | ||
fn default() -> Self { | ||
AiChat::new("aichat".to_string()) | ||
} | ||
} | ||
|
||
impl AiChat { | ||
pub fn new(binary_location: String) -> Self { | ||
AiChat { binary_location } | ||
} | ||
|
||
pub fn list_models(&self) -> Vec<String> { | ||
// Run the binary with the `list` argument | ||
let output = Command::new(&self.binary_location) | ||
.arg("--list-models") | ||
.output() | ||
.expect("Failed to execute command"); | ||
|
||
// split each line of the output into it's own string and return | ||
output | ||
.stdout | ||
.split(|c| *c == b'\n') | ||
.map(|s| String::from_utf8(s.to_vec()).unwrap()) | ||
.filter(|s| !s.is_empty()) | ||
.collect() | ||
} | ||
|
||
pub fn execute(&self, model: Option<String>, prompt: String) -> Result<String, ()> { | ||
let mut command = Command::new(&self.binary_location); | ||
if let Some(model) = model { | ||
command.arg("--model").arg(model); | ||
} | ||
command.arg("--").arg(prompt); | ||
eprintln!("Running command: {:?}", command); | ||
|
||
let output = command.output().expect("Failed to execute command"); | ||
|
||
// return the output as a string | ||
String::from_utf8(output.stdout).map_err(|_| ()) | ||
} | ||
} |
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