print new messages with history trait
This commit is contained in:
parent
5f4e811e6f
commit
85bc4265cf
14
src/api.rs
14
src/api.rs
|
@ -5,7 +5,7 @@ use std::process::exit;
|
||||||
use reqwest::{header::{HeaderMap, HeaderValue}, Client};
|
use reqwest::{header::{HeaderMap, HeaderValue}, Client};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{config::Config, session::Session};
|
use crate::{config::Config, history::HistoryObject, session::Session};
|
||||||
use crate::{WARN, RED, RESET};
|
use crate::{WARN, RED, RESET};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
@ -14,6 +14,15 @@ pub struct ChatMessagePayload {
|
||||||
pub content: String,
|
pub content: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ChatMessagePayload {
|
||||||
|
pub fn empty(role: String) -> Self {
|
||||||
|
Self {
|
||||||
|
role,
|
||||||
|
content: String::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct ChatPayload {
|
pub struct ChatPayload {
|
||||||
pub model: String,
|
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();
|
let payload = serde_json::to_string(&payload).unwrap();
|
||||||
|
|
||||||
|
println!("{}", init_msg.display_as_history());
|
||||||
|
|
||||||
let req = cli.post("https://duckduckgo.com/duckchat/v1/chat")
|
let req = cli.post("https://duckduckgo.com/duckchat/v1/chat")
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0")
|
.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;
|
let mut error = None;
|
||||||
|
println!("{}", ChatMessagePayload::empty("assistant".into()).display_as_history());
|
||||||
while let Some(chunk) = res.chunk().await.unwrap() {
|
while let Some(chunk) = res.chunk().await.unwrap() {
|
||||||
|
|
||||||
if let Ok(obj) = serde_json::from_slice::<ErrChatChunk>(&chunk) {
|
if let Ok(obj) = serde_json::from_slice::<ErrChatChunk>(&chunk) {
|
||||||
|
|
|
@ -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}")
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,7 @@ use crate::api::{get_res, get_vqd, simulate_browser_reqs};
|
||||||
mod config;
|
mod config;
|
||||||
mod api;
|
mod api;
|
||||||
mod session;
|
mod session;
|
||||||
|
mod history;
|
||||||
|
|
||||||
pub const GREEN: &str = "\x1b[1;32m";
|
pub const GREEN: &str = "\x1b[1;32m";
|
||||||
pub const RED: &str = "\x1b[1;31m";
|
pub const RED: &str = "\x1b[1;31m";
|
||||||
|
@ -72,8 +73,6 @@ async fn main() {
|
||||||
None => get_vqd(&cli).await.unwrap()
|
None => get_vqd(&cli).await.unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("{vqd:?}");
|
|
||||||
|
|
||||||
get_res(&cli, query, vqd, &config).await;
|
get_res(&cli, query, vqd, &config).await;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,8 @@ use chrono::{DateTime, Local};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::api::{ChatChunk, ChatMessagePayload};
|
use crate::api::{ChatChunk, ChatMessagePayload};
|
||||||
use crate::{BLUE, GRAY, GREEN, RESET};
|
use crate::{GRAY, RESET};
|
||||||
|
use crate::history::HistoryObject;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
|
@ -126,25 +127,9 @@ impl Session {
|
||||||
println!();
|
println!();
|
||||||
|
|
||||||
for message in self.messages.iter() {
|
for message in self.messages.iter() {
|
||||||
let user = &message.role;
|
println!("{}", message.display_as_history());
|
||||||
let msg = &message.content;
|
if message.role == "assistant" {
|
||||||
|
println!("\n");
|
||||||
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!();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue