-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathserver.py
More file actions
31 lines (22 loc) · 736 Bytes
/
server.py
File metadata and controls
31 lines (22 loc) · 736 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
import openai
from dotenv import load_dotenv
from flask import Flask, render_template, request
load_dotenv() # load env vars from .env file
openai.api_key = os.getenv("OPENAI_API_KEY")
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/get_response")
def get_response():
message = request.args.get("message")
completion = openai.ChatCompletion.create(
# You can switch this to `gpt-4` if you have access to that model.
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": message}],
)
response = completion["choices"][0]["message"]["content"]
return response
if __name__ == "__main__":
app.run(debug=True)