print new messages with history trait

This commit is contained in:
b1ek 2024-10-22 14:26:40 +10:00
parent 5f4e811e6f
commit 85bc4265cf
Signed by: blek
GPG Key ID: A622C22C9BC616B2
4 changed files with 46 additions and 23 deletions

View File

@ -5,7 +5,7 @@ use std::process::exit;
use reqwest::{header::{HeaderMap, HeaderValue}, Client};
use serde::{Deserialize, Serialize};
use crate::{config::Config, session::Session};
use crate::{config::Config, history::HistoryObject, session::Session};
use crate::{WARN, RED, RESET};
#[derive(Debug, Clone, Serialize, Deserialize)]
@ -14,6 +14,15 @@ pub struct ChatMessagePayload {
pub content: String,
}
impl ChatMessagePayload {
pub fn empty(role: String) -> Self {
Self {
role,
content: String::new()
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatPayload {
pub model: String,
@ -114,6 +123,8 @@ pub async fn get_res<'a>(cli: &Client, query: String, vqd: String, config: &Conf
};
let payload = serde_json::to_string(&payload).unwrap();
println!("{}", init_msg.display_as_history());
let req = cli.post("https://duckduckgo.com/duckchat/v1/chat")
.header("Content-Type", "application/json")
.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0")
@ -135,6 +146,7 @@ pub async fn get_res<'a>(cli: &Client, query: String, vqd: String, config: &Conf
}
let mut error = None;
println!("{}", ChatMessagePayload::empty("assistant".into()).display_as_history());
while let Some(chunk) = res.chunk().await.unwrap() {
if let Ok(obj) = serde_json::from_slice::<ErrChatChunk>(&chunk) {

27
src/history.rs Normal file
View File

@ -0,0 +1,27 @@
use crate::api::ChatMessagePayload;
use crate::{RESET, GREEN, GRAY, BLUE};
pub trait HistoryObject {
fn display_as_history(&self) -> String;
}
impl HistoryObject for ChatMessagePayload {
fn display_as_history(&self) -> String {
let user = &self.role;
let msg = &self.content;
let role_color = {
if user == "user" { GREEN }
else if user == "assistant" { BLUE }
else { GRAY }
};
let short_user = {
if user == "user" { "you" }
else if user == "assistant" { "ai" }
else { user }
}.to_string();
format!("{role_color}{short_user}{RESET}\t: {msg}{RESET}")
}
}

View File

@ -12,6 +12,7 @@ use crate::api::{get_res, get_vqd, simulate_browser_reqs};
mod config;
mod api;
mod session;
mod history;
pub const GREEN: &str = "\x1b[1;32m";
pub const RED: &str = "\x1b[1;31m";
@ -72,8 +73,6 @@ async fn main() {
None => get_vqd(&cli).await.unwrap()
};
println!("{vqd:?}");
get_res(&cli, query, vqd, &config).await;
}

View File

@ -7,7 +7,8 @@ use chrono::{DateTime, Local};
use serde::{Deserialize, Serialize};
use crate::api::{ChatChunk, ChatMessagePayload};
use crate::{BLUE, GRAY, GREEN, RESET};
use crate::{GRAY, RESET};
use crate::history::HistoryObject;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Session {
@ -126,25 +127,9 @@ impl Session {
println!();
for message in self.messages.iter() {
let user = &message.role;
let msg = &message.content;
let role_color = {
if user == "user" { GREEN }
else if user == "assistant" { BLUE }
else { GRAY }
};
let short_user = {
if user == "user" { "you" }
else if user == "assistant" { "ai" }
else { user }
}.to_string();
println!("{role_color}{short_user}{RESET}\t: {msg}{RESET}");
if user == "assistant" {
println!();
println!("{}", message.display_as_history());
if message.role == "assistant" {
println!("\n");
}
}