-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
39 lines (27 loc) · 917 Bytes
/
app.py
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
32
33
34
35
36
37
38
39
from flask import Flask, render_template, request
import openai
app = Flask(__name__)
# Set up OpenAI API credentials
openai.api_key = 'YOUR_API_KEY'
# Define the default route to return the index.html file
@app.route("/")
def index():
return render_template("index.html")
# Define the /api route to handle POST requests
@app.route("/api", methods=["POST"])
def api():
# Get the message from the POST request
message = request.json.get("message")
# Send the message to OpenAI's API and receive the response
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": message}
]
)
if completion.choices[0].message!=None:
return completion.choices[0].message
else :
return 'Failed to Generate response!'
if __name__=='__main__':
app.run()