Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
# 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

To run the example code, you need to create an [OpenAI API key](https://platform.openai.com/account/api-keys)

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
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
Expand Down
31 changes: 27 additions & 4 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import sys
import argparse

import openai
from dotenv import load_dotenv
Expand All @@ -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",
Expand All @@ -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()