commit 19d9d4d9a90111fa329cf85a1c19e73af64d3679 Author: Fierelier Date: Mon Oct 30 23:57:12 2023 +0100 First commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..67fe97b --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2023 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..63cfea0 --- /dev/null +++ b/README.txt @@ -0,0 +1,10 @@ +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) + +Syntax: ./simpleGPT diff --git a/simpleGPT.py b/simpleGPT.py new file mode 100755 index 0000000..51fc8ef --- /dev/null +++ b/simpleGPT.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +import sys +import os +import subprocess +import json +import time +if len(sys.argv) < 3: print("Arguments: "); sys.exit(1) +prettyDistro = "simpleGPT" +distro = "me.fier." +prettyDistro +if "APPDATA" in os.environ: userDir = os.environ["APPDATA"] +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 +if "OPENAI_API_KEY" in os.environ: + openai.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") + +rolePrefix = "* " +roleSuffix = " says:" +roles = ["system","user","assistant"] + +def convertListToHumanReadable(lst): + txt = "" + for message in lst: + txt += rolePrefix +message["role"]+ roleSuffix + "\n" +message["content"].strip("\n")+ "\n\n" + return txt + +def convertHumanReadableToList(txt): + lst = [] + txt = txt.split("\n") + message = False + for line in txt: + isrole = False + for role in roles: + if line == rolePrefix + role + roleSuffix: + if message != False: + message["content"] = message["content"].strip("\n") + lst.append(message) + + message = {"role": role, "content": ""} + isrole = True + break + + if isrole: continue + message["content"] += line + "\n" + lst.append(message) + return lst + +userFile = os.path.join(userDir,"conversations",sys.argv[1] + ".txt") +userFileTmp = userFile + ".tmp" +if not os.path.isfile(userFile): + lst = [] + lst.append({"role": "system", "content": "You are a smart and helpful chat assistant."}) + lst.append({"role": "user", "content": ""}) + open(userFile,"w",encoding="utf-8").write(convertListToHumanReadable(lst).strip(" \t\r\n") + "\n") + +while True: + subprocess.Popen([sys.argv[2],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) + print("Awaiting response ...") + response = openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=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)