-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (40 loc) · 1.72 KB
/
main.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
from flask import Flask, render_template, jsonify, request
from flask_pymongo import PyMongo
import openai
openai.api_key = "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"
app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb+srv://harry123:7*Q9KMh%[email protected]/chatgpt"
mongo = PyMongo(app)
@app.route("/")
def home():
chats = mongo.db.chats.find({})
myChats = [chat for chat in chats]
print(myChats)
return render_template("index.html", myChats = myChats)
@app.route("/api", methods=["GET", "POST"])
def qa():
if request.method == "POST":
print(request.json)
question = request.json.get("question")
chat = mongo.db.chats.find_one({"question": question})
print(chat)
if chat:
data = {"question": question, "answer": f"{chat['answer']}"}
return jsonify(data)
else:
response = openai.Completion.create(
model="text-davinci-003",
prompt=question,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
print(response)
data = {"question": question, "answer": response["choices"][0]["text"]}
mongo.db.chats.insert_one({"question": question, "answer": response["choices"][0]["text"]})
return jsonify(data)
data = {"result": "Thank you! I'm just a machine learning model designed to respond to questions and generate text based on my training data. Is there anything specific you'd like to ask or discuss? "}
return jsonify(data)
app.run(debug=True, port=5001)