From 9bbb2c34c23ff1a46feddbf080ca0211b1f3b2bb Mon Sep 17 00:00:00 2001 From: zhulg Date: Sat, 15 Apr 2023 15:59:18 +0800 Subject: [PATCH 1/4] Save chat to file --- src/main.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/main.rs b/src/main.rs index 290824b..bae9ec2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,6 +50,13 @@ async fn main() -> Result<(), Box> { .default_value("") .help("Sets the prompt for this session."), ) + .arg( + Arg::new("save") + .short('s') + .long("save") + .default_value("conversation.txt") + .help("Save the conversation to a file (default: conversation.txt)."), + ) .arg( Arg::new("model") .action(ArgAction::Set) @@ -86,6 +93,8 @@ async fn main() -> Result<(), Box> { let model = matches.get_one::("model").unwrap(); let temperature = matches.get_one::("temperature").unwrap(); let api_key_cli = matches.get_one::("APIKey").unwrap(); + let save_to_file = matches.get_one::("save").unwrap(); + let url = format!("https://{}/v1/chat/completions", domain_name); let mut api_key = String::new(); if api_key_cli.is_empty() { @@ -110,6 +119,10 @@ async fn main() -> Result<(), Box> { content: prompt.to_string(), }); + if !save_to_file.is_empty() { + save_message_to_file(save_to_file, &messages.last().unwrap())?; + } + let assistant_message_result = request_gpt(GptRequestParams { url: &url, api_key: &api_key, @@ -123,6 +136,9 @@ async fn main() -> Result<(), Box> { match assistant_message_result { Ok(assistant_message) => { messages.push(assistant_message.clone()); + if !save_to_file.is_empty() { + save_message_to_file(&save_to_file, &messages.last().unwrap())?; + } } Err(RequestError::Cancelled) => { println!("Request canceled by user."); @@ -150,6 +166,10 @@ async fn main() -> Result<(), Box> { role: "user".to_string(), content: line.to_string(), }); + + if !save_to_file.is_empty() { + save_message_to_file(&save_to_file, &messages.last().unwrap())?; + } let assistant_message_result = request_gpt(GptRequestParams { url: &url, api_key: &api_key, @@ -163,6 +183,9 @@ async fn main() -> Result<(), Box> { match assistant_message_result { Ok(assistant_message) => { messages.push(assistant_message.clone()); + if !save_to_file.is_empty() { + save_message_to_file(&save_to_file, &messages.last().unwrap())?; + } } Err(RequestError::Cancelled) => { cancel_request.store(false, Ordering::SeqCst); @@ -228,3 +251,21 @@ async fn request_gpt(params: GptRequestParams<'_>) -> Result std::io::Result<()> { + use std::fs::OpenOptions; + use std::io::prelude::*; + + let mut file = OpenOptions::new() + .write(true) + .create(true) + .append(true) + .open(filename)?; + + if message.role == "user" { + writeln!(file, "user: {}", message.content)?; + } else if message.role == "assistant" { + writeln!(file, "chatgpt: {}", message.content)?; + } + Ok(()) +} From c2a21a7a1ceaf059cc01cb45f6d74b140fa4f64b Mon Sep 17 00:00:00 2001 From: zhulg Date: Sat, 15 Apr 2023 16:00:28 +0800 Subject: [PATCH 2/4] update readme --- Cargo.toml | 2 +- README.md | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d056705..edc6547 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "chatgpt_rust" -version = "0.3.2" +version = "0.4.0" edition = "2021" authors = ["zhulg "] license = "MIT" diff --git a/README.md b/README.md index 9d80668..37aa7e8 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ - A tool for chatting using the ChatGPT API, written in Rust CLI. - You can use this tool to chat, just by setting your API Key. - You can modify the API domain and other API parameters when you start the chat. -- If you like this tool, please join me to complete the TODO list, and let's improve this tool together ## QuickStart @@ -101,7 +100,3 @@ echo $OPENAI_API_KEY ``` The value of your API key will be the resulting output. -## TODO: -- [ ] Support save message to file -- [ ] import chat message from file -- [ ] Support read prompt from file From d33b44faf6d84f109b5757ff18b27a9b586b5cf2 Mon Sep 17 00:00:00 2001 From: zhulg Date: Sat, 15 Apr 2023 16:17:22 +0800 Subject: [PATCH 3/4] update readme --- Cargo.toml | 2 +- README.md | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index edc6547..531e164 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "chatgpt_rust" -version = "0.4.0" +version = "0.4.1" edition = "2021" authors = ["zhulg "] license = "MIT" diff --git a/README.md b/README.md index 37aa7e8..06575b1 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,8 @@ # ChatGPT CLI - -- A tool for chatting using the ChatGPT API, written in Rust CLI. -- You can use this tool to chat, just by setting your API Key. -- You can modify the API domain and other API parameters when you start the chat. +- This Rust CLI tool uses the ChatGPT API for chatting. To begin chatting, simply provide your API key. +- In addition to chatting, this tool allows you to customize the API domain and parameters. +- Furthermore, the tool supports saving conversations to a file during chat sessions. ## QuickStart From 11db61b5f5374d168495c4fb8ef744e64a84dfb0 Mon Sep 17 00:00:00 2001 From: zhulg Date: Sat, 15 Apr 2023 16:25:38 +0800 Subject: [PATCH 4/4] update readme --- Cargo.toml | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 531e164..f579b32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "chatgpt_rust" -version = "0.4.1" +version = "0.4.2" edition = "2021" authors = ["zhulg "] license = "MIT" diff --git a/README.md b/README.md index 06575b1..739b2be 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # ChatGPT CLI - This Rust CLI tool uses the ChatGPT API for chatting. To begin chatting, simply provide your API key. -- In addition to chatting, this tool allows you to customize the API domain and parameters. +- In addition to chatting, this tool allows you to customize the API domain and parameters.You can even set a proxy domain and bypass VPN access. - Furthermore, the tool supports saving conversations to a file during chat sessions.