From 4e2ec51445b2985a992b4052b9cf2a7398baf3b9 Mon Sep 17 00:00:00 2001 From: wunderwuzzi23 Date: Sun, 26 Feb 2023 23:25:06 -0800 Subject: [PATCH] Add key path support --- requirements.txt | 3 ++- yolo.py | 28 ++++++++++++---------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/requirements.txt b/requirements.txt index a1db346..cd5d142 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ openai==0.26.5 -termcolor==2.2.0 \ No newline at end of file +termcolor==2.2.0 +pathlib \ No newline at end of file diff --git a/yolo.py b/yolo.py index 72e5d53..eb9b4bd 100755 --- a/yolo.py +++ b/yolo.py @@ -5,25 +5,17 @@ import openai import sys import subprocess from termcolor import colored +from pathlib import Path -# Check if we have an Open API key set +# Two options for the user to specify they openai api key openai.api_key = os.getenv("OPENAI_API_KEY") +openai.api_key_path = Path.home()/".openai.apikey" -if openai.api_key == "": - openai.api_key = open("~/.openai.apikey", "r").read() - print("Environment variable OPENAI_API_KEY is not set.") - print("~/.openai.apikey is not set either. Exiting.") - sys.exit(-1) - -# to allow easy/natural use we don't require -# the input to be a single string -# so the user can just type yolo what is my name? -# without having to put the question between '' - -ask = False -yolo = "" -command_start_idx = 1 +ask = False # safety switch -a +yolo = "" # user's answer to safety switch +command_start_idx = 1 # command starts at which argv index? +# parsing weirdness if len(sys.argv) <2: sys.exit(-1) @@ -31,9 +23,13 @@ if sys.argv[1] == "-a": ask = True command_start_idx = 2 +# to allow easy/natural use we don't require the input to be a +# single string. So, the user can just type yolo what is my name? +# without having to put the question between '' arguments = sys.argv[command_start_idx:] user_prompt = " ".join(arguments) +# do we have a prompt from the user? if user_prompt == "": print ("No user prompt specified.") sys.exit(-1) @@ -67,4 +63,4 @@ if ask == True: print() if yolo == "Y" or yolo == "": - subprocess.run(resulting_command, shell=True) + subprocess.run(resulting_command, shell=True)