-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
64 lines (51 loc) · 1.92 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import openai
import os
from flask import Flask, redirect, render_template, request, url_for, jsonify
from flask_cors import CORS
from dotenv import load_dotenv, find_dotenv, set_key
app = Flask(__name__)
CORS(app)
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
last_conv = []
last_conv.append(
{'role': 'system', 'content': "you are a super rude assisstant."})
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == "POST":
checkAPI = request.form.get('api_key')
if (checkAPI):
openai.api_key = request.form['api_key']
dotenv_file = find_dotenv()
set_key(dotenv_file, "OPENAI_API_KEY", request.form['api_key'])
print('key: ', request.form['api_key'])
return "API Key Updated", 200
try:
version = request.form['version']
prompt = request.form['prompt']
question = {}
question['role'] = 'user'
question['content'] = prompt
last_conv.append(question)
answer = generate_response(last_conv, version)
except Exception as e:
print("Error:", e)
answer = {'content': str(e)}
return jsonify(answer), 401
return jsonify(answer), 200
result = {'role': 'assisstant'}
result['content'] = request.args.get('result')
return render_template('index.html', result=result)
def generate_response(last_conv, version):
response = openai.ChatCompletion.create(
model=version,
messages=last_conv,
temperature=.6
)
answer = {}
answer['role'] = response['choices'][0]['message']['role']
answer['content'] = response['choices'][0]['message']['content'].replace('\n', '<br>')
last_conv.append(answer)
return answer
if __name__ == '__main__':
app.run(host='localhost', port=5000, debug=True)