diff options
author | Oxbian <oxbian@mailbox.org> | 2025-03-02 21:32:20 -0500 |
---|---|---|
committer | Oxbian <oxbian@mailbox.org> | 2025-03-02 21:32:20 -0500 |
commit | e4eaecc8ce7fb3e84977c41597eff80edd4d73c7 (patch) | |
tree | fd33e7d93a680b2e0e93cb0eacb66137bd77ecab /src/app/llm.rs | |
parent | 25cf2d92f3198ba7541dad979eca1f9c1238ff04 (diff) | |
download | NAI-e4eaecc8ce7fb3e84977c41597eff80edd4d73c7.tar.gz NAI-e4eaecc8ce7fb3e84977c41597eff80edd4d73c7.zip |
feat: conversation saved in file
Diffstat (limited to 'src/app/llm.rs')
-rw-r--r-- | src/app/llm.rs | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/app/llm.rs b/src/app/llm.rs index 8603395..59e045b 100644 --- a/src/app/llm.rs +++ b/src/app/llm.rs @@ -1,9 +1,10 @@ -use crate::helper::init::print_in_file; +use crate::helper::init::warn; use reqwest::{header::CONTENT_TYPE, Client}; use serde::{Deserialize, Serialize}; use serde_json::Value; use std::fmt; -use std::fs; +use std::fs::{OpenOptions, create_dir_all}; +use std::io::Write; #[derive(Deserialize, Debug)] pub struct LLM { @@ -40,7 +41,7 @@ impl LLM { while let Some(chunk) = res.chunk().await? { let answer: Value = serde_json::from_slice(chunk.as_ref())?; - print_in_file(answer.to_string()); + warn(answer.to_string()); if answer["done"].as_bool().unwrap_or(false) { break; } @@ -53,7 +54,7 @@ impl LLM { Err(e) => return Err(Box::new(e)), } - print_in_file(full_message.clone()); + warn(full_message.clone()); Ok(full_message) } } @@ -85,6 +86,21 @@ impl Message { pub fn new(role: MessageType, content: String) -> Message { Message { role, content } } + + pub fn save_message(&self, conv_id: String) { + // Create conv directory if doesn't exist + create_dir_all("conv").unwrap(); + + // Save message + let mut file = OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open("conv/".to_string() + &conv_id) + .unwrap(); + + writeln!(file, "{}", serde_json::to_string(self).unwrap()).unwrap(); + } } impl fmt::Display for Message { |