aboutsummaryrefslogtreecommitdiff
path: root/src/app
diff options
context:
space:
mode:
Diffstat (limited to 'src/app')
-rw-r--r--src/app/init.rs6
-rw-r--r--src/app/llm.rs24
2 files changed, 25 insertions, 5 deletions
diff --git a/src/app/init.rs b/src/app/init.rs
index aa74ae5..f62b2d0 100644
--- a/src/app/init.rs
+++ b/src/app/init.rs
@@ -1,9 +1,11 @@
use crate::app::llm::{Message, MessageType, LLM};
-use crate::helper::init::print_in_file;
+use crate::helper::init::warn;
+use uuid::Uuid;
use tokio;
pub struct App {
pub messages: Vec<Message>, // History of recorded message
+ conv_id: Uuid,
chat_llm: LLM,
resume_llm: LLM,
}
@@ -16,6 +18,7 @@ impl App {
MessageType::SYSTEM,
chat_llm.system_prompt.clone(),
)],
+ conv_id: Uuid::new_v4(),
chat_llm,
resume_llm: LLM::new("config/resume-LLM.json".to_string()).unwrap(),
}
@@ -23,6 +26,7 @@ impl App {
fn append_message(&mut self, msg: String, role: MessageType) {
let message = Message::new(role, msg);
+ message.save_message(self.conv_id.to_string());
self.messages.push(message);
}
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 {
ArKa projects. All rights to me, and your next child right arm.