diff --git a/README.md b/README.md index 699a4de..7a6f7b2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ChatGPT-Simple -Build a simple locally hosted version of ChatGPT in less than 100 lines of code. Note: This is an unofficial ChatGPT repo and is not associated with OpenAI in anyway! +Build a simple locally hosted version of ChatGPT in less than 100 lines of code. Note: This is an unofficial ChatGPT repo and is not associated with OpenAI in any way! ## Getting started @@ -8,7 +8,7 @@ To run the example code, you need to create an [OpenAI API key](https://platform 1. Install requirments using ```bash -$ pip install requirements.txt +$ pip install -r requirements.txt ``` 2. Create a .env file and paste your API key there ```.env @@ -16,7 +16,12 @@ OPENAI_API_KEY = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx ``` 3. Run the code and Enjoy ```bash -$ python server.py +$ python server.py --web +``` + +4. Run from the cli +```bash +$ python server.py --cli --message "Your question goes here" ``` ## The best part diff --git a/server.py b/server.py index b77ddd1..4f2a097 100644 --- a/server.py +++ b/server.py @@ -1,4 +1,6 @@ import os +import sys +import argparse import openai from dotenv import load_dotenv @@ -9,15 +11,17 @@ app = Flask(__name__) - @app.route("/") def index(): return render_template("index.html") - @app.route("/get_response") -def get_response(): +def get_flask_response(): message = request.args.get("message") + response = get_response(message) + return response + +def get_response(message): completion = openai.ChatCompletion.create( # You can switch this to `gpt-4` if you have access to that model. model="gpt-3.5-turbo", @@ -26,6 +30,25 @@ def get_response(): response = completion["choices"][0]["message"]["content"] return response +def run_cli(message): + response = get_response(message) + print(response) + +def main(): + parser = argparse.ArgumentParser(description="GPT-3.5 chat app") + parser.add_argument("--mode", type=str, choices=["cli", "web"], default="web", help="Choose between CLI or web mode") + parser.add_argument("--message", type=str, help="User message (only required for CLI mode)") + + args = parser.parse_args() + + if args.mode == "cli": + if args.message is None: + print("Error: message argument is required for CLI mode") + sys.exit(1) + run_cli(args.message) + else: + app.run(debug=True) if __name__ == "__main__": - app.run(debug=True) + main() +