From 0e87ef1742c1c1596e08e48d36125168bf2fde78 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Thu, 14 Sep 2023 01:08:56 -0700 Subject: [PATCH 01/17] basic functionalities --- go_cli.py | 171 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 95 insertions(+), 76 deletions(-) diff --git a/go_cli.py b/go_cli.py index 14f0ef8..ac14ad9 100644 --- a/go_cli.py +++ b/go_cli.py @@ -17,6 +17,8 @@ import sys import uuid import requests +import json +import io import subprocess import urllib.parse import sys @@ -25,8 +27,7 @@ __version__ = "0.0.10" # current version SERVER_URL = "https://cli.gorilla-llm.com" -UPDATE_CHECK_FILE = os.path.expanduser("~/.gorilla-cli-last-update-check") -USERID_FILE = os.path.expanduser("~/.gorilla-cli-userid") +CONFIG_FILE = os.path.expanduser("~/.gorilla-cli-config.json") ISSUE_URL = f"https://github.com/gorilla-llm/gorilla-cli/issues/new" GORILLA_EMOJI = "🦍 " if go_questionary.try_encode_gorilla() else "" WELCOME_TEXT = f"""===***=== @@ -45,13 +46,8 @@ ===***===""" -def check_for_updates(): +def check_for_updates(last_check_date): # Check if a new version of gorilla-cli is available once a day - try: - with open(UPDATE_CHECK_FILE, "r") as f: - last_check_date = datetime.datetime.strptime(f.read(), "%Y-%m-%d") - except FileNotFoundError: - last_check_date = datetime.datetime.now() - datetime.timedelta(days=1) if datetime.datetime.now() - last_check_date >= datetime.timedelta(days=1): try: response = requests.get("https://pypi.org/pypi/gorilla-cli/json") @@ -61,11 +57,6 @@ def check_for_updates(): print(f"A new version is available: {latest_version}. Update with `pip install --upgrade gorilla-cli`") except Exception as e: print("Unable to check for updates:", e) - try: - with open(UPDATE_CHECK_FILE, "w") as f: - f.write(datetime.datetime.now().strftime("%Y-%m-%d")) - except Exception as e: - print("Unable to write update check file:", e) def get_user_id(): @@ -74,63 +65,50 @@ def get_user_id(): # research prototype. Please don't spam the system or use it # for commercial serving. If you would like to request rate # limit increases for your GitHub handle, please raise an issue. + + authenticated = False try: - with open(USERID_FILE, "r") as f: - user_id = str(f.read()) - # If file found and user_id is blank. User hasn't setup github - if user_id == "": - user_id = str(uuid.uuid4()) - return user_id - except FileNotFoundError: - # First time - try: - user_id = ( - subprocess.check_output(["git", "config", "--global", "user.email"]) - .decode("utf-8") - .strip() - ) - print(WELCOME_TEXT) - response = ( - input(f"Use your Github handle ({user_id}) as user id? [Y/n]: ") - .strip() - .lower() - ) - if response in ["n", "no"]: - user_id = str(uuid.uuid4()) - except Exception as e: - # If git not installed then generate and use a random user id - issue_title = urllib.parse.quote( - f"Problem with generating userid from GitHub: {str(e)}" - ) - issue_body = urllib.parse.quote(f"Unable to generate userid: {str(e)}") - print( - f"Git not installed, so cannot import userid from Git. \n Please run 'gorilla ' again after initializing git. \n Will use a random user-id. If the problem persists, please raise an issue: \ - {ISSUE_URL}?title={issue_title}&body={issue_body}" - ) + user_id = ( + subprocess.check_output(["git", "config", "--global", "user.email"]) + .decode("utf-8") + .strip() + ) + response = ( + input(f"Use your Github handle ({user_id}) as user id? [Y/n]: ") + .strip() + .lower() + ) + if response in ["n", "no"]: user_id = str(uuid.uuid4()) - print(WELCOME_TEXT) - - try: - # Write user_id to file - with open(USERID_FILE, "w") as f: - f.write(user_id) - return user_id - except Exception as e: - issue_title = urllib.parse.quote("Problem with userid file") - issue_body = urllib.parse.quote(f"Unable to write userid file: {str(e)}") - print("Unable to write userid to file:", e) - print( - f"Try deleting USERID_FILE and run 'gorilla ' again. If the problem persists, please raise an issue:\ - {ISSUE_URL}?title={issue_title}&body={issue_body}" - ) - print( - f"Using a temporary UID {user_id} for now.." - ) - return user_id + else: + authenticated = True + except Exception as e: + # If git not installed then generate and use a random user id + issue_title = urllib.parse.quote( + f"Problem with generating userid from GitHub: {str(e)}" + ) + issue_body = urllib.parse.quote(f"Unable to generate userid: {str(e)}") + print( + f"Git not installed, so cannot import userid from Git. \n Please run 'gorilla ' again after initializing git. \n Will use a random user-id. If the problem persists, please raise an issue: \ + {ISSUE_URL}?title={issue_title}&body={issue_body}" + ) + user_id = str(uuid.uuid4()) + + return (user_id, authenticated) + +def specify_models(file): + # By default, Gorilla-CLI combines the capabilities of multiple Language Learning Models. + # The specify_models command will make Gorilla exclusively utilize the inputted models. + try: + with open(CONFIG_FILE, "r+") as config_file: + config_json = json.load(config_file) + config_json["models"] = models + json.dump(config_json, config_file) + except io.UnsupportedOperation: + print("Config.json has not been initialized") -def main(): - def execute_command(cmd): +def execute_command(cmd): process = subprocess.run(cmd, shell=True, stderr=subprocess.PIPE) error_msg = process.stderr.decode("utf-8", "ignore") if error_msg: @@ -138,9 +116,45 @@ def execute_command(cmd): return error_msg return str(process.returncode) +def load_config(): + # Load the user's configuration file and perform any necessary checks + config_json = {} + if os.path.isfile(CONFIG_FILE): + with open(CONFIG_FILE, "r") as config_file: + config_json = json.load(config_file) + + if "last_check_date" in config_json: + last_check_date = datetime.datetime.strptime(config_json["last_check_date"], "%Y-%m-%d") + else: + last_check_date = datetime.datetime.now() - datetime.timedelta(days=1) + check_for_updates(last_check_date) + config_json["last_check_date"] = datetime.datetime.now().strftime("%Y-%m-%d") + + # Check for user_id. Only add new user_id to the config file if the user has been authenticated. + user_id = config_json["user_id"] + if "user_id" not in config_json: + user_id, authenticated = get_user_id() + if authenticated: + config_json["user_id"] = user_id + + with open(CONFIG_FILE, "w") as config_file: + config_file.write(json.dumps(config_json)) + + config_json["user_id"] = user_id + + return config_json + +def main(): + config = load_config() + args = sys.argv[1:] + if args[0] == "--model": + if len(args) != 2: + print('--model command must follow the following format: gorilla --model ') + return + specify_models[args[1]] + user_input = " ".join(args) - user_id = get_user_id() # Generate a unique interaction ID interaction_id = str(uuid.uuid4()) @@ -148,7 +162,7 @@ def execute_command(cmd): with Halo(text=f"{GORILLA_EMOJI}Loading", spinner="dots"): try: data_json = { - "user_id": user_id, + "user_id": config["user_id"], "user_input": user_input, "interaction_id": interaction_id, } @@ -161,24 +175,29 @@ def execute_command(cmd): print("Try updating Gorilla-CLI with 'pip install --upgrade gorilla-cli'") return - check_for_updates() - + print(WELCOME_TEXT) + if commands: selected_command = go_questionary.select( "", choices=commands, instruction="" ).ask() exit_condition = execute_command(selected_command) + json = { + "user_id": config["user_id"], + "command": selected_command, + "exit_condition": exit_condition, + "interaction_id": interaction_id, + } + if "model" in config: + json["model"] = config["model"] + print("Only the following LLM models are used by Gorilla: ", config["model"]) + # Commands failed / succeeded? try: response = requests.post( f"{SERVER_URL}/command-execution-result", - json={ - "user_id": user_id, - "command": selected_command, - "exit_condition": exit_condition, - "interaction_id": interaction_id, - }, + json=json, timeout=30, ) if response.status_code != 200: From 576970474ad5a705755464602c313ec679d91592 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Thu, 14 Sep 2023 15:42:02 -0700 Subject: [PATCH 02/17] allow single model specification --- go_cli.py | 122 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 76 insertions(+), 46 deletions(-) diff --git a/go_cli.py b/go_cli.py index ac14ad9..0df57c5 100644 --- a/go_cli.py +++ b/go_cli.py @@ -27,7 +27,7 @@ __version__ = "0.0.10" # current version SERVER_URL = "https://cli.gorilla-llm.com" -CONFIG_FILE = os.path.expanduser("~/.gorilla-cli-config.json") +CONFIG_FILE = "./hello.json"#os.path.expanduser("~/.gorilla-cli-config.json") ISSUE_URL = f"https://github.com/gorilla-llm/gorilla-cli/issues/new" GORILLA_EMOJI = "🦍 " if go_questionary.try_encode_gorilla() else "" WELCOME_TEXT = f"""===***=== @@ -46,8 +46,19 @@ ===***===""" -def check_for_updates(last_check_date): +def check_for_updates(): # Check if a new version of gorilla-cli is available once a day + try: + with open(CONFIG_FILE, "r") as config_file: + config_json = json.load(config_file) + if "last_check_date" in config_json: + last_check_date = datetime.datetime.strptime(config_json["last_check_date"], "%Y-%m-%d") + else: + last_check_date = datetime.datetime.now() - datetime.timedelta(days=1) + except Exception as e: + config_json = {} + last_check_date = datetime.datetime.now() - datetime.timedelta(days=1) + if datetime.datetime.now() - last_check_date >= datetime.timedelta(days=1): try: response = requests.get("https://pypi.org/pypi/gorilla-cli/json") @@ -58,6 +69,10 @@ def check_for_updates(last_check_date): except Exception as e: print("Unable to check for updates:", e) + config_json["last_check_date"] = datetime.datetime.now().strftime("%Y-%m-%d") + with open(CONFIG_FILE, "w") as config_file: + json.dump(config_json, config_file) + def get_user_id(): # Unique user identifier for authentication and load balancing @@ -65,8 +80,14 @@ def get_user_id(): # research prototype. Please don't spam the system or use it # for commercial serving. If you would like to request rate # limit increases for your GitHub handle, please raise an issue. - - authenticated = False + try: + with open(CONFIG_FILE, "r") as config_file: + config_json = json.load(config_file) + if "user_id" in config_json: + return config_json["user_id"] + except Exception as e: + config_json = {} + try: user_id = ( subprocess.check_output(["git", "config", "--global", "user.email"]) @@ -81,7 +102,11 @@ def get_user_id(): if response in ["n", "no"]: user_id = str(uuid.uuid4()) else: - authenticated = True + print(WELCOME_TEXT) + config_json["user_id"] = user_id + with open(CONFIG_FILE, "w") as config_file: + config_json = json.dump(config_json, config_file) + except Exception as e: # If git not installed then generate and use a random user id issue_title = urllib.parse.quote( @@ -94,16 +119,28 @@ def get_user_id(): ) user_id = str(uuid.uuid4()) - return (user_id, authenticated) + return user_id -def specify_models(file): + +def specify_models(file_path): # By default, Gorilla-CLI combines the capabilities of multiple Language Learning Models. # The specify_models command will make Gorilla exclusively utilize the inputted models. try: - with open(CONFIG_FILE, "r+") as config_file: - config_json = json.load(config_file) - config_json["models"] = models + with open(file_path, "r") as models_file: + models_json = json.load(models_file) + except Exception as e: + print("Failed to read from " + file_path) + return + + try: + config_json = {} + if os.path.isfile(CONFIG_FILE): + with open(CONFIG_FILE, "r") as config_file: + config_json = json.load(config_file) + config_json["models"] = models_json["models"] + with open(CONFIG_FILE, "w") as config_file: json.dump(config_json, config_file) + print("models set to: " + str(config_json["models"])) except io.UnsupportedOperation: print("Config.json has not been initialized") @@ -116,56 +153,55 @@ def execute_command(cmd): return error_msg return str(process.returncode) + def load_config(): # Load the user's configuration file and perform any necessary checks - config_json = {} if os.path.isfile(CONFIG_FILE): with open(CONFIG_FILE, "r") as config_file: config_json = json.load(config_file) - - if "last_check_date" in config_json: - last_check_date = datetime.datetime.strptime(config_json["last_check_date"], "%Y-%m-%d") - else: - last_check_date = datetime.datetime.now() - datetime.timedelta(days=1) - check_for_updates(last_check_date) - config_json["last_check_date"] = datetime.datetime.now().strftime("%Y-%m-%d") - - # Check for user_id. Only add new user_id to the config file if the user has been authenticated. - user_id = config_json["user_id"] - if "user_id" not in config_json: - user_id, authenticated = get_user_id() - if authenticated: - config_json["user_id"] = user_id - - with open(CONFIG_FILE, "w") as config_file: - config_file.write(json.dumps(config_json)) - - config_json["user_id"] = user_id - return config_json + def main(): + user_id = get_user_id() + check_for_updates() + config = load_config() + models = None + if "models" in config: + models = config["models"] + args = sys.argv[1:] - if args[0] == "--model": + if args[0] == "--set_models": if len(args) != 2: - print('--model command must follow the following format: gorilla --model ') + print('--set_models command must follow the following format: gorilla --model ') return - specify_models[args[1]] + specify_models(args[1]) + return + + if args[0] in ["--model", "-m"]: + models = args[1] + try: + args = args[2:] + except Exception as e: + print("Unable to parse the arguments. To make Gorilla only use a specific model, run gorilla -m ") user_input = " ".join(args) # Generate a unique interaction ID interaction_id = str(uuid.uuid4()) - - with Halo(text=f"{GORILLA_EMOJI}Loading", spinner="dots"): - try: - data_json = { + data_json = { "user_id": config["user_id"], "user_input": user_input, "interaction_id": interaction_id, - } + } + if models: + data_json["models"] = models + print("Results are only chosen from the following LLM model(s): ", models) + + with Halo(text=f"{GORILLA_EMOJI}Loading", spinner="dots"): + try: response = requests.post( f"{SERVER_URL}/commands", json=data_json, timeout=30 ) @@ -175,23 +211,17 @@ def main(): print("Try updating Gorilla-CLI with 'pip install --upgrade gorilla-cli'") return - print(WELCOME_TEXT) - if commands: selected_command = go_questionary.select( "", choices=commands, instruction="" ).ask() exit_condition = execute_command(selected_command) - json = { - "user_id": config["user_id"], + "user_id": user_id, "command": selected_command, "exit_condition": exit_condition, "interaction_id": interaction_id, } - if "model" in config: - json["model"] = config["model"] - print("Only the following LLM models are used by Gorilla: ", config["model"]) # Commands failed / succeeded? try: From 095d87ba0a68e1bac2f49a9b3ed95b3a66aa0826 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Thu, 14 Sep 2023 15:48:52 -0700 Subject: [PATCH 03/17] refactor --- go_cli.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/go_cli.py b/go_cli.py index 0df57c5..bfc6b0a 100644 --- a/go_cli.py +++ b/go_cli.py @@ -51,12 +51,12 @@ def check_for_updates(): try: with open(CONFIG_FILE, "r") as config_file: config_json = json.load(config_file) - if "last_check_date" in config_json: - last_check_date = datetime.datetime.strptime(config_json["last_check_date"], "%Y-%m-%d") - else: - last_check_date = datetime.datetime.now() - datetime.timedelta(days=1) except Exception as e: - config_json = {} + config_json = {} + + if "last_check_date" in config_json: + last_check_date = datetime.datetime.strptime(config_json["last_check_date"], "%Y-%m-%d") + else: last_check_date = datetime.datetime.now() - datetime.timedelta(days=1) if datetime.datetime.now() - last_check_date >= datetime.timedelta(days=1): @@ -191,16 +191,16 @@ def main(): # Generate a unique interaction ID interaction_id = str(uuid.uuid4()) - data_json = { + + with Halo(text=f"{GORILLA_EMOJI}Loading", spinner="dots"): + data_json = { "user_id": config["user_id"], "user_input": user_input, "interaction_id": interaction_id, - } - if models: - data_json["models"] = models - print("Results are only chosen from the following LLM model(s): ", models) - - with Halo(text=f"{GORILLA_EMOJI}Loading", spinner="dots"): + } + if models: + data_json["models"] = models + print("Results are only chosen from the following LLM model(s): ", models) try: response = requests.post( f"{SERVER_URL}/commands", json=data_json, timeout=30 From afaaf87e83210cd981078d879cdf350b2400566c Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Thu, 14 Sep 2023 15:51:31 -0700 Subject: [PATCH 04/17] fix loading bug --- go_cli.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/go_cli.py b/go_cli.py index bfc6b0a..5eaeaee 100644 --- a/go_cli.py +++ b/go_cli.py @@ -192,15 +192,16 @@ def main(): # Generate a unique interaction ID interaction_id = str(uuid.uuid4()) - with Halo(text=f"{GORILLA_EMOJI}Loading", spinner="dots"): - data_json = { + data_json = { "user_id": config["user_id"], "user_input": user_input, "interaction_id": interaction_id, } - if models: - data_json["models"] = models - print("Results are only chosen from the following LLM model(s): ", models) + if models: + data_json["models"] = models + print("Results are only chosen from the following LLM model(s): ", models) + + with Halo(text=f"{GORILLA_EMOJI}Loading", spinner="dots"): try: response = requests.post( f"{SERVER_URL}/commands", json=data_json, timeout=30 From a2dd9a5e9614b9a73bb867e2c11277d444f5e9a2 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Thu, 14 Sep 2023 16:02:40 -0700 Subject: [PATCH 05/17] nit --- go_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go_cli.py b/go_cli.py index 5eaeaee..91a7a40 100644 --- a/go_cli.py +++ b/go_cli.py @@ -27,7 +27,7 @@ __version__ = "0.0.10" # current version SERVER_URL = "https://cli.gorilla-llm.com" -CONFIG_FILE = "./hello.json"#os.path.expanduser("~/.gorilla-cli-config.json") +CONFIG_FILE = os.path.expanduser("~/.gorilla-cli-config.json") ISSUE_URL = f"https://github.com/gorilla-llm/gorilla-cli/issues/new" GORILLA_EMOJI = "🦍 " if go_questionary.try_encode_gorilla() else "" WELCOME_TEXT = f"""===***=== From c517ba6e72946e3af413845a81111a2f54d61a24 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Thu, 14 Sep 2023 16:15:45 -0700 Subject: [PATCH 06/17] refactor/style consistency --- go_cli.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/go_cli.py b/go_cli.py index fd24089..cd82402 100644 --- a/go_cli.py +++ b/go_cli.py @@ -83,10 +83,11 @@ def get_user_id(): try: with open(CONFIG_FILE, "r") as config_file: config_json = json.load(config_file) - if "user_id" in config_json: - return config_json["user_id"] except Exception as e: config_json = {} + + if "user_id" in config_json: + return config_json["user_id"] try: user_id = ( @@ -133,16 +134,15 @@ def specify_models(file_path): return try: + with open(CONFIG_FILE, "r") as config_file: + config_json = json.load(config_file) + except Exception as e: config_json = {} - if os.path.isfile(CONFIG_FILE): - with open(CONFIG_FILE, "r") as config_file: - config_json = json.load(config_file) - config_json["models"] = models_json["models"] - with open(CONFIG_FILE, "w") as config_file: - json.dump(config_json, config_file) + + config_json["models"] = models_json["models"] + with open(CONFIG_FILE, "w") as config_file: + json.dump(config_json, config_file) print("models set to: " + str(config_json["models"])) - except io.UnsupportedOperation: - print("Config.json has not been initialized") def execute_command(cmd): @@ -196,7 +196,7 @@ def main(): "user_id": config["user_id"], "user_input": user_input, "interaction_id": interaction_id, - } + } if models: data_json["models"] = models print("Results are only chosen from the following LLM model(s): ", models) @@ -222,11 +222,11 @@ def main(): return exit_condition = execute_command(selected_command) json = { - "user_id": user_id, - "command": selected_command, - "exit_condition": exit_condition, - "interaction_id": interaction_id, - } + "user_id": user_id, + "command": selected_command, + "exit_condition": exit_condition, + "interaction_id": interaction_id, + } # Commands failed / succeeded? try: From cc9407aae414f0a39f1ffec9b62392bccd9906c1 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Fri, 15 Sep 2023 15:44:24 -0700 Subject: [PATCH 07/17] enabled parse options --- go_cli.py | 95 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 31 deletions(-) diff --git a/go_cli.py b/go_cli.py index cd82402..ceccab3 100644 --- a/go_cli.py +++ b/go_cli.py @@ -17,6 +17,7 @@ import sys import uuid import requests +import click import json import io import subprocess @@ -123,27 +124,43 @@ def get_user_id(): return user_id -def specify_models(file_path): +def specify_models(ctx, param, file_path): # By default, Gorilla-CLI combines the capabilities of multiple Language Learning Models. # The specify_models command will make Gorilla exclusively utilize the inputted models. + if not file_path or ctx.resilient_parsing: + return try: with open(file_path, "r") as models_file: models_json = json.load(models_file) except Exception as e: print("Failed to read from " + file_path) - return - + ctx.exit() try: with open(CONFIG_FILE, "r") as config_file: config_json = json.load(config_file) except Exception as e: config_json = {} - config_json["models"] = models_json["models"] with open(CONFIG_FILE, "w") as config_file: json.dump(config_json, config_file) print("models set to: " + str(config_json["models"])) + ctx.exit() + + +def reset_models(ctx, param, value): + if not value or ctx.resilient_parsing: + return + try: + with open(CONFIG_FILE, "r+") as config_file: + config_json = json.load(config_file) + if "models" in config_json: + del config_json["models"] + json.dump(config_json, config_file) + except Exception as e: + pass + ctx.exit() + def execute_command(cmd): process = subprocess.run(cmd, shell=True, stderr=subprocess.PIPE) @@ -162,32 +179,48 @@ def load_config(): return config_json -def main(): - user_id = get_user_id() - check_for_updates() +def print_version(ctx, param, value): + if not value or ctx.resilient_parsing: + return + click.echo(__version__) + ctx.exit() + +@click.command() +@click.option('--user_id', '--u', default=get_user_id(), help="User id [default: 'git config --global user.email' OR random uuid]") +@click.option('--server', default = SERVER_URL, help = "LLM host [default: 'cli.gorilla-llm.com']") +@click.option('--set_models', type=click.Path(), callback = specify_models, expose_value=True, + help = "Make Gorilla exclusively utilize the models in the json file specified") +@click.option('--reset_models', is_flag=True, callback =reset_models, expose_value=False, is_eager=True, + help = "Reset models configuration") +@click.option('--model', '-m', help = "Prompt Gorilla CLI to only use the specified model") +@click.option('--version', help = "Return the version of GORILLA_CLI", is_flag=True, callback= print_version, expose_value=False, is_eager=True) +@click.argument('prompt', nargs = -1) +def main( + user_id, + server, + model, + set_models, + prompt, +): + check_for_updates() config = load_config() - models = None - if "models" in config: - models = config["models"] - - args = sys.argv[1:] - if args[0] == "--set_models": - if len(args) != 2: - print('--set_models command must follow the following format: gorilla --model ') - return - specify_models(args[1]) + if len(prompt) == 0: + print("error: prompt not found, see gorilla-cli usage below " + "➡️") + with click.Context(main) as ctx: + click.echo(main.get_help(ctx)) return - - if args[0] in ["--model", "-m"]: - models = args[1] - try: - args = args[2:] - except Exception as e: - print("Unable to parse the arguments. To make Gorilla only use a specific model, run gorilla -m ") - user_input = " ".join(args) + #Check if the user has specific model preference. + if model: + chosen_models = model + elif "models" in config: + chosen_models = config["models"] + else: + chosen_models = None + + user_input = " ".join(prompt) # Generate a unique interaction ID interaction_id = str(uuid.uuid4()) @@ -197,18 +230,18 @@ def main(): "user_input": user_input, "interaction_id": interaction_id, } - if models: - data_json["models"] = models - print("Results are only chosen from the following LLM model(s): ", models) + if chosen_models: + data_json["models"] = chosen_models + print("Results are only chosen from the following LLM model(s): ", chosen_models) with Halo(text=f"{GORILLA_EMOJI}Loading", spinner="dots"): try: response = requests.post( - f"{SERVER_URL}/commands", json=data_json, timeout=30 + f"{server}/commands", json=data_json, timeout=30 ) commands = response.json() except requests.exceptions.RequestException as e: - print("Server is unreachable.") + print("\nServer " + server + " is unreachable.") print("Try updating Gorilla-CLI with 'pip install --upgrade gorilla-cli'") return @@ -231,7 +264,7 @@ def main(): # Commands failed / succeeded? try: response = requests.post( - f"{SERVER_URL}/command-execution-result", + f"{server}/command-execution-result", json=json, timeout=30, ) From 4393c9259d7c68ad7498ed0f94b255f19c69d94c Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Fri, 15 Sep 2023 17:00:41 -0700 Subject: [PATCH 08/17] bug fix + add click to setup --- go_cli.py | 10 ++++++---- setup.py | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/go_cli.py b/go_cli.py index ceccab3..e9d8f2d 100644 --- a/go_cli.py +++ b/go_cli.py @@ -85,6 +85,7 @@ def get_user_id(): with open(CONFIG_FILE, "r") as config_file: config_json = json.load(config_file) except Exception as e: + print(e) config_json = {} if "user_id" in config_json: @@ -152,10 +153,11 @@ def reset_models(ctx, param, value): if not value or ctx.resilient_parsing: return try: - with open(CONFIG_FILE, "r+") as config_file: + with open(CONFIG_FILE, "r") as config_file: config_json = json.load(config_file) - if "models" in config_json: - del config_json["models"] + if "models" in config_json: + del config_json["models"] + with open(CONFIG_FILE, "w") as config_file: json.dump(config_json, config_file) except Exception as e: pass @@ -226,7 +228,7 @@ def main( interaction_id = str(uuid.uuid4()) data_json = { - "user_id": config["user_id"], + "user_id": user_id, "user_input": user_input, "interaction_id": interaction_id, } diff --git a/setup.py b/setup.py index 896ea4b..140181a 100644 --- a/setup.py +++ b/setup.py @@ -29,6 +29,7 @@ "requests", "halo", "prompt-toolkit", + "click" ], entry_points={ "console_scripts": [ From 276736696f93f11f93a75dc1fcb0bc1f0e32c074 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Wed, 18 Oct 2023 22:24:55 -0700 Subject: [PATCH 09/17] nit --- go_cli.py | 1 - 1 file changed, 1 deletion(-) diff --git a/go_cli.py b/go_cli.py index e9d8f2d..1c305af 100644 --- a/go_cli.py +++ b/go_cli.py @@ -85,7 +85,6 @@ def get_user_id(): with open(CONFIG_FILE, "r") as config_file: config_json = json.load(config_file) except Exception as e: - print(e) config_json = {} if "user_id" in config_json: From 6edc46f8c881c794406848fdf25e54e8cde83dd5 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Wed, 18 Oct 2023 23:12:23 -0700 Subject: [PATCH 10/17] restructure --- go_cli.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/go_cli.py b/go_cli.py index 735b42a..6593dd6 100644 --- a/go_cli.py +++ b/go_cli.py @@ -21,9 +21,7 @@ import requests import click import json -import io import subprocess -import argparse import termios import urllib.parse import sys @@ -291,6 +289,15 @@ def get_history_commands(ctx, param, value): else: click.echo("No command history.") ctx.exit() + +def format_commands(commands): + for i, command in enumerate(commands): + if command[-1] == '\n': + commands[i] = command[:-1] + print(commands[i]) + break + return commands + @click.command() @@ -357,6 +364,7 @@ def main( return if commands: + commands = format_commands(commands) selected_command = go_questionary.select( "", choices=commands, instruction="Welcome to Gorilla. Use arrow keys to select. Ctrl-C to Exit" ).ask() @@ -364,8 +372,13 @@ def main( if not selected_command: # happens when Ctrl-C is pressed return - exit_condition = execute_command(selected_command) + + # Append command to bash history + if system_info == "Linux": + append_to_bash_history(selected_command) + prefill_shell_cmd(selected_command) + exit_condition = execute_command(selected_command) json = { "user_id": user_id, "command": selected_command, @@ -373,12 +386,6 @@ def main( "interaction_id": interaction_id, } - - # Append command to bash history - if system_info == "Linux": - append_to_bash_history(selected_command) - prefill_shell_cmd(selected_command) - # Commands failed / succeeded? try: response = requests.post( From 0f1be845f85d3a4de3bc3ccb8a9d3be742946154 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Wed, 18 Oct 2023 23:36:00 -0700 Subject: [PATCH 11/17] reformat messages --- go_cli.py | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/go_cli.py b/go_cli.py index 6593dd6..a24b2f9 100644 --- a/go_cli.py +++ b/go_cli.py @@ -30,7 +30,7 @@ __version__ = "0.0.11" # current version SERVER_URL = "https://cli.gorilla-llm.com" -CONFIG_FILE = os.path.expanduser("~/.gorilla-cli-config.json") +CONFIG_FILE = "./config.json"#os.path.expanduser("~/.gorilla-cli-config.json") HISTORY_FILE = os.path.expanduser("~/.gorilla_cli_history") ISSUE_URL = f"https://github.com/gorilla-llm/gorilla-cli/issues/new" GORILLA_EMOJI = "🦍 " if go_questionary.try_encode_gorilla() else "" @@ -60,10 +60,6 @@ def get_git_email(): def get_system_info(): return platform.system() -def write_uid_to_file(uid): - with open(USERID_FILE, "w") as f: - f.write(uid) - def append_to_bash_history(selected_command): try: with open(os.path.expanduser("~/.bash_history"), "a") as history_file: @@ -144,18 +140,14 @@ def get_user_id(): return config_json["user_id"] try: - user_id = ( - subprocess.check_output(["git", "config", "--global", "user.email"]) - .decode("utf-8") - .strip() - ) + user_id = get_git_email() response = ( input(f"Use your Github handle ({user_id}) as user id? [Y/n]: ") .strip() .lower() ) if response in ["n", "no"]: - user_id = str(uuid.uuid4()) + user_id = generate_random_uid() else: print(WELCOME_TEXT) config_json["user_id"] = user_id @@ -163,16 +155,11 @@ def get_user_id(): config_json = json.dump(config_json, config_file) except Exception as e: + print(e) # If git not installed then generate and use a random user id - issue_title = urllib.parse.quote( - f"Problem with generating userid from GitHub: {str(e)}" - ) - issue_body = urllib.parse.quote(f"Unable to generate userid: {str(e)}") - print( - f"Git not installed, so cannot import userid from Git. \n Please run 'gorilla ' again after initializing git. \n Will use a random user-id. If the problem persists, please raise an issue: \ - {ISSUE_URL}?title={issue_title}&body={issue_body}" - ) - user_id = str(uuid.uuid4()) + print(f"Unable to import userid from Git. Git not installed or git user.email not configured.") + print(f"Will use a random user-id. \n") + user_id = generate_random_uid() return user_id @@ -290,11 +277,10 @@ def get_history_commands(ctx, param, value): click.echo("No command history.") ctx.exit() -def format_commands(commands): +def format_output_commands(commands): for i, command in enumerate(commands): if command[-1] == '\n': commands[i] = command[:-1] - print(commands[i]) break return commands @@ -340,7 +326,6 @@ def main( args = sys.argv[1:] user_input = " ".join(args) - user_id = get_user_id() system_info = get_system_info() data_json = { "user_id": user_id, @@ -364,7 +349,7 @@ def main( return if commands: - commands = format_commands(commands) + commands = format_output_commands(commands) selected_command = go_questionary.select( "", choices=commands, instruction="Welcome to Gorilla. Use arrow keys to select. Ctrl-C to Exit" ).ask() From 732026ec44bc0b7658264dea49a0c29d03906f99 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Wed, 18 Oct 2023 23:39:00 -0700 Subject: [PATCH 12/17] nit --- go_cli.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/go_cli.py b/go_cli.py index a24b2f9..c91bda1 100644 --- a/go_cli.py +++ b/go_cli.py @@ -155,7 +155,6 @@ def get_user_id(): config_json = json.dump(config_json, config_file) except Exception as e: - print(e) # If git not installed then generate and use a random user id print(f"Unable to import userid from Git. Git not installed or git user.email not configured.") print(f"Will use a random user-id. \n") @@ -364,18 +363,17 @@ def main( prefill_shell_cmd(selected_command) exit_condition = execute_command(selected_command) - json = { - "user_id": user_id, - "command": selected_command, - "exit_condition": exit_condition, - "interaction_id": interaction_id, - } # Commands failed / succeeded? try: response = requests.post( f"{server}/command-execution-result", - json=json, + json={ + "user_id": user_id, + "command": selected_command, + "exit_condition": exit_condition, + "interaction_id": interaction_id, + }, timeout=30, ) if response.status_code != 200: From ddf4dfefcad0a2b690d73299b53a4ac32ddb8fc4 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Wed, 18 Oct 2023 23:40:37 -0700 Subject: [PATCH 13/17] nit --- go_cli.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go_cli.py b/go_cli.py index c91bda1..0e2e951 100644 --- a/go_cli.py +++ b/go_cli.py @@ -369,10 +369,10 @@ def main( response = requests.post( f"{server}/command-execution-result", json={ - "user_id": user_id, - "command": selected_command, - "exit_condition": exit_condition, - "interaction_id": interaction_id, + "user_id": user_id, + "command": selected_command, + "exit_condition": exit_condition, + "interaction_id": interaction_id, }, timeout=30, ) From 96b2cb787254cb3ffe181f85afcd78cc953e135e Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Wed, 18 Oct 2023 23:45:05 -0700 Subject: [PATCH 14/17] nit fix --- go_cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go_cli.py b/go_cli.py index 0e2e951..7c40b8b 100644 --- a/go_cli.py +++ b/go_cli.py @@ -30,7 +30,7 @@ __version__ = "0.0.11" # current version SERVER_URL = "https://cli.gorilla-llm.com" -CONFIG_FILE = "./config.json"#os.path.expanduser("~/.gorilla-cli-config.json") +CONFIG_FILE = os.path.expanduser("~/.gorilla-cli-config.json") HISTORY_FILE = os.path.expanduser("~/.gorilla_cli_history") ISSUE_URL = f"https://github.com/gorilla-llm/gorilla-cli/issues/new" GORILLA_EMOJI = "🦍 " if go_questionary.try_encode_gorilla() else "" From ea1fb2cad3b9ff2de80361f224240db21773c325 Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Thu, 19 Oct 2023 00:48:54 -0700 Subject: [PATCH 15/17] remove server --- go_cli.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/go_cli.py b/go_cli.py index 7c40b8b..c785986 100644 --- a/go_cli.py +++ b/go_cli.py @@ -287,7 +287,6 @@ def format_output_commands(commands): @click.command() @click.option('--user_id', '--u', default=get_user_id(), help="User id [default: 'git config --global user.email' OR random uuid]") -@click.option('--server', default = SERVER_URL, help = "LLM host [default: 'cli.gorilla-llm.com']") @click.option('--set_models', type=click.Path(), callback = specify_models, expose_value=True, help = "Make Gorilla exclusively utilize the models in the json file specified") @click.option('--reset_models', is_flag=True, callback =reset_models, expose_value=False, is_eager=True, @@ -298,7 +297,6 @@ def format_output_commands(commands): @click.argument('prompt', nargs = -1) def main( user_id, - server, model, set_models, prompt, @@ -339,11 +337,11 @@ def main( with Halo(text=f"{GORILLA_EMOJI}Loading", spinner="dots"): try: response = requests.post( - f"{server}/commands", json=data_json, timeout=30 + f"{SERVER_URL}/commands", json=data_json, timeout=30 ) commands = response.json() except requests.exceptions.RequestException as e: - print("\nServer " + server + " is unreachable.") + print("\nServer " + SERVER_URL + " is unreachable.") print("Try updating Gorilla-CLI with 'pip install --upgrade gorilla-cli'") return @@ -367,7 +365,7 @@ def main( # Commands failed / succeeded? try: response = requests.post( - f"{server}/command-execution-result", + f"{SERVER_URL}/command-execution-result", json={ "user_id": user_id, "command": selected_command, From 1d77fc5e52f66a573a6d3bcf79b0f45f33ca886f Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Thu, 19 Oct 2023 12:39:15 -0700 Subject: [PATCH 16/17] support legacy user id file --- go_cli.py | 58 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/go_cli.py b/go_cli.py index c785986..222ad83 100644 --- a/go_cli.py +++ b/go_cli.py @@ -138,30 +138,52 @@ def get_user_id(): if "user_id" in config_json: return config_json["user_id"] - - try: - user_id = get_git_email() - response = ( - input(f"Use your Github handle ({user_id}) as user id? [Y/n]: ") - .strip() - .lower() - ) - if response in ["n", "no"]: - user_id = generate_random_uid() - else: + + user_id = get_legacy_user_id() + if user_id: + config_json["user_id"] = user_id + else: + try: + user_id = get_git_email() + response = ( + input(f"Use your Github handle ({user_id}) as user id? [Y/n]: ") + .strip() + .lower() + ) + if response in ["n", "no"]: + user_id = generate_random_uid() + print(WELCOME_TEXT) config_json["user_id"] = user_id - with open(CONFIG_FILE, "w") as config_file: - config_json = json.dump(config_json, config_file) + except Exception as e: + # If git not installed then generate and use a random user id + print(f"Unable to import userid from Git. Git not installed or git user.email not configured.") + print(f"Will use a random user-id. \n") + user_id = generate_random_uid() + + try: + with open(CONFIG_FILE, "w") as config_file: + config_json = json.dump(config_json, config_file) except Exception as e: - # If git not installed then generate and use a random user id - print(f"Unable to import userid from Git. Git not installed or git user.email not configured.") - print(f"Will use a random user-id. \n") - user_id = generate_random_uid() - + print(f"Unable to write userid to file: {e}") + raise_issue("Problem with userid file", f"Unable to write userid file: {e}") + print(f"Using a temporary UID {user_id} for now.") + return user_id +def get_legacy_user_id(): + # Previously, user id were not stored in config.json and were stored in a separate file. + # This function checks whether or not the user already have their user_id set + # Before the update to use config.json. + USERID_FILE = os.path.expanduser("~/.gorilla-cli-userid") + try: + with open(USERID_FILE, "r") as f: + user_id = f.read().strip() + return user_id + except: + return None + def format_command(input_str): """ Standardize commands to be stored with a newline From f67355e65485c307724b134920e963b1c16c110b Mon Sep 17 00:00:00 2001 From: Noppapon Chalermchockcharoenkit Date: Thu, 19 Oct 2023 12:41:51 -0700 Subject: [PATCH 17/17] update get_user_id exception msg --- go_cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go_cli.py b/go_cli.py index 222ad83..d33355f 100644 --- a/go_cli.py +++ b/go_cli.py @@ -166,8 +166,8 @@ def get_user_id(): with open(CONFIG_FILE, "w") as config_file: config_json = json.dump(config_json, config_file) except Exception as e: - print(f"Unable to write userid to file: {e}") - raise_issue("Problem with userid file", f"Unable to write userid file: {e}") + print(f"Unable to write userid to config file: {e}") + raise_issue("Problem with cofnig file", f"Unable to write userid to cofnig file: {e}") print(f"Using a temporary UID {user_id} for now.") return user_id