forked from wangyunjeff/EasyChatGPT-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
37 lines (33 loc) · 1.12 KB
/
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
from flask import Flask, render_template, request
from markupsafe import Markup
import openai
import markdown
import markdown.extensions.fenced_code
import markdown.extensions.codehilite
openai.api_key = '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'
app = Flask(__name__)
messages = []
@app.route('/')
def home():
return render_template('index.html')
@app.route('/get_response', methods=['POST'])
def get_bot_response():
user_input = request.form['user_input']
# print(user_input)
messages.append({'role': 'user', 'content': user_input})
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
ai_response = completion.choices[0].message['content']
# print(ai_response)
messages.append({'role': 'assistant', 'content': ai_response})
print(messages)
return Markup(markdown.markdown(ai_response, extensions=['fenced_code', 'codehilite']))
@app.route('/reset')
def reset():
global messages
messages = []
return "Conversation history has been reset."
if __name__ == '__main__':
app.run(debug=True)