Do not use python3-openai to reduce bloat
This commit is contained in:
parent
d8e0446f40
commit
b85b0d33a8
|
@ -2,7 +2,6 @@ Talk to ChatGPT without a browser.
|
|||
|
||||
Requirements:
|
||||
* python3
|
||||
* python3-openai (See: https://pypi.org/project/openai/)
|
||||
|
||||
Usage:
|
||||
* Put your OpenAI API key into the environment variable OPENAI_API_KEY or the file $USER/.config/me.fier.simpleGPT/api_key.txt (Linux) / %appdata%\me.fier.simpleGPT/api_key.txt (Windows)
|
||||
|
|
33
simpleGPT.py
33
simpleGPT.py
|
@ -12,11 +12,15 @@ if "HOME" in os.environ: userDir = os.path.join(os.environ["HOME"],".config")
|
|||
userDir = os.path.join(userDir,distro)
|
||||
os.makedirs(os.path.join(userDir,"conversations"),exist_ok = True)
|
||||
|
||||
import openai
|
||||
api_key = False
|
||||
if "OPENAI_API_KEY" in os.environ:
|
||||
openai.api_key = os.environ["OPENAI_API_KEY"]
|
||||
api_key = os.environ["OPENAI_API_KEY"]
|
||||
elif os.path.isfile(os.path.join(userDir,"api_key.txt")):
|
||||
openai.api_key = open(os.path.join(userDir,"api_key.txt"),"r").read().strip(" \t\r\n")
|
||||
api_key = open(os.path.join(userDir,"api_key.txt"),"r").read().strip(" \t\r\n")
|
||||
|
||||
if api_key == False:
|
||||
print("Couldn't find API key, please set OPENAI_API_KEY or create " +os.path.join(userDir,"api_key.txt"))
|
||||
os.exit(1)
|
||||
|
||||
rolePrefix = "* "
|
||||
roleSuffix = " says:"
|
||||
|
@ -49,6 +53,22 @@ def convertHumanReadableToList(txt):
|
|||
lst.append(message)
|
||||
return lst
|
||||
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
def chatCompletion(lst):
|
||||
lstOut = {}
|
||||
lstOut["model"] = "gpt-3.5-turbo"
|
||||
lstOut["messages"] = lst
|
||||
data = json.dumps(lstOut).encode("utf-8")
|
||||
dataLength = len(data)
|
||||
request = urllib.request.Request("https://api.openai.com/v1/chat/completions",data=data)
|
||||
request.add_header("Content-Type","application/json")
|
||||
request.add_header("Authorization","Bearer " + api_key)
|
||||
response = urllib.request.urlopen(request)
|
||||
rtn = json.loads(response.read().decode("utf-8"))
|
||||
response.close()
|
||||
return rtn
|
||||
|
||||
userFile = os.path.join(userDir,"conversations",sys.argv[2] + ".txt")
|
||||
userFileTmp = userFile + ".tmp"
|
||||
if not os.path.isfile(userFile):
|
||||
|
@ -60,10 +80,11 @@ if not os.path.isfile(userFile):
|
|||
while True:
|
||||
subprocess.Popen([sys.argv[1],userFile]).wait()
|
||||
lst = convertHumanReadableToList(open(userFile,"r",encoding="utf8").read())
|
||||
if lst[-1]["role"] != "user" or lst[-1]["content"].strip(" \t\r\n") == "": sys.exit(0)
|
||||
lst[-1]["content"] = lst[-1]["content"].strip(" \t\r\n")
|
||||
if not (lst[-1]["role"] in ["user","system"]) or lst[-1]["content"] == "": sys.exit(0)
|
||||
print("Awaiting response ...")
|
||||
response = openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=lst)
|
||||
lst.append({"role": "assistant", "content": response["choices"][0]["message"].content})
|
||||
response = chatCompletion(lst)
|
||||
lst.append({"role": "assistant", "content": response["choices"][0]["message"]["content"]})
|
||||
lst.append({"role": "user", "content": ""})
|
||||
open(userFileTmp,"w",encoding="utf-8").write(convertListToHumanReadable(lst).strip(" \t\r\n") + "\n")
|
||||
os.replace(userFileTmp,userFile)
|
||||
|
|
Loading…
Reference in New Issue