From 7be127cb087f5b57ffa712f7ab9fbd405474475a Mon Sep 17 00:00:00 2001 From: devleejb Date: Thu, 29 Feb 2024 19:35:29 +0900 Subject: [PATCH 1/4] Add install script for linux --- install.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 install.sh diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..c709ab8 --- /dev/null +++ b/install.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# cllm Installation Script (Integrated) + +# cllm download URL and file name +CLLM_URL="" +CLLM_FILE="cllm.tar.gz" + +# Installation directory +INSTALL_DIR="/usr/local/bin" + +# User-defined temporary directory +TMP_DIR="/tmp/cllm" + +# Check the operating system +OS=$(uname -s) + +# Download and install cllm files based on the operating system +case "$OS" in + Linux*) + # cllm download URL and file name for Linux + CLLM_URL="https://github.com/dev-backpack/cllm/releases/download/v0.1.1/cllm-x86_64-unknown-linux-gnu.tar.gz" + ;; + Darwin*) + # cllm download URL and file name for macOS + CLLM_URL="https://github.com/dev-backpack/cllm/releases/download/v0.1.1/cllm-x86_64-apple-darwin.tar.gz" + ;; + *) + echo "Unsupported operating system." + exit 1 + ;; +esac + +# Download and install cllm +mkdir -p "$TMP_DIR" +chmod 755 $TMP_DIR +echo "Downloading cllm..." +curl -sfL "$CLLM_URL" -o "$TMP_DIR/$CLLM_FILE" +echo "Extracting cllm files..." +tar xzf "$TMP_DIR/$CLLM_FILE" -C "$TMP_DIR" +sudo chown root "$TMP_DIR/$CLLM_FILE" +sudo mv "$TMP_DIR"/cllm "$INSTALL_DIR" + +# Display installation completion message +echo "cllm installation completed!" + +# Delete temporary files +rm -rf "$TMP_DIR" From dbed00c1ff991b20e6f58789e1aa3bd2d244e264 Mon Sep 17 00:00:00 2001 From: devleejb Date: Thu, 29 Feb 2024 19:39:07 +0900 Subject: [PATCH 2/4] Update installation guide --- README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4846f2b..d8fb4a6 100644 --- a/README.md +++ b/README.md @@ -13,16 +13,21 @@ $ cllm search "Provide the command to build and push a Docker image from the cur docker build -t myapp:latest . --push ``` - ## Installation -`cllm` can be installed using `brew`! +### Homebrew (macOS and Linux) ```bash brew tap dev-backpack/cllm brew install cllm ``` +### Install Script (macOS and Linux) + +```bash +curl https://raw.githubusercontent.com/dev-backpack/cllm/main/install.sh | sh +``` + ## Commands #### Register OpenAI API Key @@ -35,4 +40,4 @@ cllm set key [API_KEY] ```bash cllm search [QUERY] -``` \ No newline at end of file +``` From decf88cde9ff04ea18b4d8142247b855de071300 Mon Sep 17 00:00:00 2001 From: devleejb Date: Thu, 29 Feb 2024 19:41:06 +0900 Subject: [PATCH 3/4] 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 {} +} From d0497d89eb878e103b7e216dfedca7ccbb9f0b43 Mon Sep 17 00:00:00 2001 From: devleejb Date: Thu, 29 Feb 2024 20:26:55 +0900 Subject: [PATCH 4/4] Add dependency for cli-clipboard --- install.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/install.sh b/install.sh index c709ab8..07fa1ef 100644 --- a/install.sh +++ b/install.sh @@ -20,6 +20,9 @@ case "$OS" in Linux*) # cllm download URL and file name for Linux CLLM_URL="https://github.com/dev-backpack/cllm/releases/download/v0.1.1/cllm-x86_64-unknown-linux-gnu.tar.gz" + + # Install dependencies for cli-clipboard + sudo apt install xorg-dev libxcb-composite0-dev ;; Darwin*) # cllm download URL and file name for macOS