Add key path support
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
openai==0.26.5
|
openai==0.26.5
|
||||||
termcolor==2.2.0
|
termcolor==2.2.0
|
||||||
|
pathlib
|
||||||
@@ -5,25 +5,17 @@ import openai
|
|||||||
import sys
|
import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
from termcolor import colored
|
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 = os.getenv("OPENAI_API_KEY")
|
||||||
|
openai.api_key_path = Path.home()/".openai.apikey"
|
||||||
|
|
||||||
if openai.api_key == "":
|
ask = False # safety switch -a
|
||||||
openai.api_key = open("~/.openai.apikey", "r").read()
|
yolo = "" # user's answer to safety switch
|
||||||
print("Environment variable OPENAI_API_KEY is not set.")
|
command_start_idx = 1 # command starts at which argv index?
|
||||||
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
|
|
||||||
|
|
||||||
|
# parsing weirdness
|
||||||
if len(sys.argv) <2:
|
if len(sys.argv) <2:
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
@@ -31,9 +23,13 @@ if sys.argv[1] == "-a":
|
|||||||
ask = True
|
ask = True
|
||||||
command_start_idx = 2
|
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:]
|
arguments = sys.argv[command_start_idx:]
|
||||||
user_prompt = " ".join(arguments)
|
user_prompt = " ".join(arguments)
|
||||||
|
|
||||||
|
# do we have a prompt from the user?
|
||||||
if user_prompt == "":
|
if user_prompt == "":
|
||||||
print ("No user prompt specified.")
|
print ("No user prompt specified.")
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
@@ -67,4 +63,4 @@ if ask == True:
|
|||||||
print()
|
print()
|
||||||
|
|
||||||
if yolo == "Y" or yolo == "":
|
if yolo == "Y" or yolo == "":
|
||||||
subprocess.run(resulting_command, shell=True)
|
subprocess.run(resulting_command, shell=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user