From decf88cde9ff04ea18b4d8142247b855de071300 Mon Sep 17 00:00:00 2001 From: devleejb Date: Thu, 29 Feb 2024 19:41:06 +0900 Subject: [PATCH] Fix formatting --- src/commands/mod.rs | 2 +- src/commands/search/mod.rs | 68 +++++++++++++++++-------------------- src/commands/set/key/mod.rs | 13 +++---- src/commands/set/mod.rs | 13 +++---- src/lib.rs | 13 ++++--- src/main.rs | 16 ++++----- 6 files changed, 55 insertions(+), 70 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index d64499c..f942539 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -14,4 +14,4 @@ pub async fn handle_command(command: Commands) -> Result<(), Box search::handle_search(search).await, Commands::Set(set) => set::handle_set(set).await, } -} \ No newline at end of file +} diff --git a/src/commands/search/mod.rs b/src/commands/search/mod.rs index b41c362..340c467 100644 --- a/src/commands/search/mod.rs +++ b/src/commands/search/mod.rs @@ -1,37 +1,37 @@ use clap::Parser; -use std::env; -use spinners::{Spinner, Spinners}; +use cli_clipboard::{ClipboardContext, ClipboardProvider}; use llm_chain::{ - chains::conversation::Chain, executor, parameters, prompt, step::Step, + chains::conversation::Chain, + executor, options::{ModelRef, OptionsBuilder}, - prompt::{Conversation, ChatMessageCollection}, + parameters, prompt, + prompt::{ChatMessageCollection, Conversation}, + step::Step, }; use llm_chain_openai::chatgpt::Model; -use cli_clipboard::{ClipboardContext, ClipboardProvider}; +use spinners::{Spinner, Spinners}; +use std::env; #[derive(Debug, Parser)] -#[clap( - name = "search", - about="Search a command from the LLM model", -)] +#[clap(name = "search", about = "Search a command from the LLM model")] pub struct Search { // The command to search - #[clap(help="The command to search")] - qeury: String, + #[clap(help = "The command to search")] + qeury: String, } pub fn few_shot_template(list: Vec<(String, String)>) -> ChatMessageCollection { - let mut ret_prompt = Conversation::new(); for (user, assistant) in &list { - ret_prompt = ret_prompt.with_user(user.to_string()).with_assistant(assistant.to_string()); + ret_prompt = ret_prompt + .with_user(user.to_string()) + .with_assistant(assistant.to_string()); } ret_prompt } pub async fn handle_search(search: Search) -> Result<(), Box> { - if !env::var("OPENAI_API_KEY").is_ok() { println!("Please set your OpenAI API key using the `set key` command."); return Ok(()); @@ -45,18 +45,14 @@ pub async fn handle_search(search: Search) -> Result<(), Box = vec![ ("Show all pods in k8s".to_string(), "kubectl get pods".to_string()), ("Find all files recursively within the current directory that contain 'a' in their filenames.".to_string(), "find . -type f -name '*a*' -print".to_string()), ("Provide the command to build and push a Docker image from the current directory.".to_string(), "docker build -t myapp:latest --path".to_string()), ]; - - + let mut conversation = Conversation::new() .with_system_template( "I want you to act as generating a command for request tasks on {{os_name}}. Also please don't explain the commands, just generate the command.", @@ -67,30 +63,30 @@ pub async fn handle_search(search: Search) -> Result<(), Box>()[1].to_string().trim().to_string(); + let res = res.split("Assistant: ").collect::>()[1] + .to_string() + .trim() + .to_string(); let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap(); ctx.set_contents(res.clone().to_string()).unwrap(); - - spinner.stop_and_persist("✔", "Finished searching for the command and copied to your clipboard :)".into()); + + spinner.stop_and_persist( + "✔", + "Finished searching for the command and copied to your clipboard :)".into(), + ); println!("{}", res); Ok(()) -} \ No newline at end of file +} diff --git a/src/commands/set/key/mod.rs b/src/commands/set/key/mod.rs index 9debb76..ee3a201 100644 --- a/src/commands/set/key/mod.rs +++ b/src/commands/set/key/mod.rs @@ -5,18 +5,14 @@ use std::fs::File; use std::io::prelude::*; #[derive(Debug, Parser)] -#[clap( - name = "key", - about="Register for an API key to use the OpenAI API", -)] +#[clap(name = "key", about = "Register for an API key to use the OpenAI API")] pub struct Key { // The API key to set - #[clap(help="OpenAI API Key")] + #[clap(help = "OpenAI API Key")] api_key: String, } pub async fn handle_key(key: Key) -> Result<(), Box> { - let home_dir = dirs::home_dir().unwrap(); let save_dir = home_dir.join(".cllm"); let config_path = save_dir.join("credentials.json"); @@ -28,8 +24,7 @@ pub async fn handle_key(key: Key) -> Result<(), Box> { let mut config = if config_path.exists() { let config = std::fs::read_to_string(config_path.clone())?; serde_json::from_str(&config)? - } - else { + } else { serde_json::json!({}) }; @@ -40,4 +35,4 @@ pub async fn handle_key(key: Key) -> Result<(), Box> { println!("API key set successfully."); Ok(()) -} \ No newline at end of file +} diff --git a/src/commands/set/mod.rs b/src/commands/set/mod.rs index bd4509d..ad9e443 100644 --- a/src/commands/set/mod.rs +++ b/src/commands/set/mod.rs @@ -1,24 +1,21 @@ mod key; -use clap::{Subcommand, Parser}; +use clap::{Parser, Subcommand}; #[derive(Debug, Subcommand)] -#[clap( - name = "set", - about="Configure application resources", -)] +#[clap(name = "set", about = "Configure application resources")] pub enum Commands { // Set the API key - Key(key::Key) + Key(key::Key), } #[derive(Debug, Parser)] pub struct Set { #[clap(subcommand)] - subcmd: Commands + subcmd: Commands, } pub async fn handle_set(set: Set) -> Result<(), Box> { match set.subcmd { Commands::Key(key) => key::handle_key(key).await, } -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index 412ef34..f76b67b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,16 +1,16 @@ pub(crate) mod commands; -use std::env; use clap::Parser; -use commands::{Commands, handle_command}; +use commands::{handle_command, Commands}; use dirs; +use std::env; #[derive(Debug, Parser)] #[clap( version, - about="Empower your CLI experience with a command search tool driven by LLM magic!\n\ - Github: https://github.com/dev-backpack/cllm\n\ - If you have any questions or suggestions, feel free to open an issue on the github repo." + about = "Empower your CLI experience with a command search tool driven by LLM magic!\n\ + If you have any questions or suggestions, feel free to open an issue on the github repo.\n\ + GitHub: https://github.com/dev-backpack/cllm" )] struct Cli { #[clap(subcommand)] @@ -18,7 +18,6 @@ struct Cli { } pub async fn run() -> Result<(), Box> { - // Set the OPENAI_API_KEY environment variable let home_dir = dirs::home_dir().unwrap(); let save_dir = home_dir.join(".cllm"); @@ -41,4 +40,4 @@ pub async fn run() -> Result<(), Box> { } Ok(()) -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 60c4bea..99ce57f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,16 @@ pub mod commands; -use std::env; use clap::Parser; -use commands::{Commands, handle_command}; +use commands::{handle_command, Commands}; use dirs; +use std::env; #[derive(Debug, Parser)] #[clap( version, - about="Empower your CLI experience with a command search tool driven by LLM magic!\n\ - Github: https://github.com/dev-backpack/cllm\n\ - If you have any questions or suggestions, feel free to open an issue on the github repo." + about = "Empower your CLI experience with a command search tool driven by LLM magic!\n\ + If you have any questions or suggestions, feel free to open an issue on the github repo.\n\ + GitHub: https://github.com/dev-backpack/cllm" )] struct Cli { #[clap(subcommand)] @@ -19,7 +19,6 @@ struct Cli { #[tokio::main] async fn main() { - // Set the OPENAI_API_KEY environment variable let home_dir = dirs::home_dir().unwrap(); let save_dir = home_dir.join(".cllm"); @@ -38,6 +37,5 @@ async fn main() { // Parse the command line arguments let cli = Cli::parse(); - if let Err(_error) = handle_command(cli.commands).await { - } -} + if let Err(_error) = handle_command(cli.commands).await {} +}