From e4eaecc8ce7fb3e84977c41597eff80edd4d73c7 Mon Sep 17 00:00:00 2001 From: Oxbian Date: Sun, 2 Mar 2025 21:32:20 -0500 Subject: feat: conversation saved in file --- src/app/llm.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src/app/llm.rs') 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 { -- cgit v1.2.3