|
@@ -106,12 +106,13 @@ def home():
|
106
|
106
|
payload = None
|
107
|
107
|
while not payload:
|
108
|
108
|
reply = openai.ChatCompletion.create(model=os.environ["MODEL_NAME"], messages=session["messages"])
|
109
|
|
- session["messages"] += [reply["choices"][0]["message"]]
|
|
109
|
+ message = dict(reply["choices"][0]["message"])
|
|
110
|
+ session["messages"] += [message]
|
110
|
111
|
try:
|
111
|
|
- payload = json.loads(reply["choices"][0]["message"]["content"])
|
|
112
|
+ payload = json.loads(message["content"])
|
112
|
113
|
payload["prompt"] = prompt.replace('"', '\"')
|
113
|
114
|
payload["content"] = preprocess_content(payload)
|
114
|
|
- session["history"].append(payload)
|
|
115
|
+ session["history"] += [payload]
|
115
|
116
|
except Exception as e:
|
116
|
117
|
print(repr(e))
|
117
|
118
|
session["messages"] += [{"role": "system", "content": "reply was ignored because it's not a valid json or doesn't comply with the schema. user is unaware. do not apologize to them"}]
|
|
@@ -123,7 +124,7 @@ def home():
|
123
|
124
|
else:
|
124
|
125
|
payload = json.loads(session["messages"][-1]["content"])
|
125
|
126
|
payload["content"] = preprocess_content(payload)
|
126
|
|
- payload["history"] = reversed(session["history"][:-1])
|
|
127
|
+ payload["history"] = list(reversed(session["history"][:-1]))
|
127
|
128
|
return render_template("chat.html", **payload)
|
128
|
129
|
|
129
|
130
|
@application.get("/reset")
|
|
@@ -199,6 +200,25 @@ def choices(topic):
|
199
|
200
|
}
|
200
|
201
|
abort(404)
|
201
|
202
|
|
|
203
|
+@application.route("/save", methods=['GET', 'POST'])
|
|
204
|
+def save():
|
|
205
|
+ if request.method=="POST":
|
|
206
|
+ filename=request.form["filename"].rsplit("/",1)[-1]
|
|
207
|
+ if filename:
|
|
208
|
+ moment = {
|
|
209
|
+ key: session.get(key, [])
|
|
210
|
+ for key in ["messages", "history"]
|
|
211
|
+ }
|
|
212
|
+ print(moment)
|
|
213
|
+ json.dump(moment, open("archive/{}.json".format(filename),"w"), indent=4)
|
|
214
|
+ return redirect(url_for("home"))
|
|
215
|
+ else:
|
|
216
|
+ return render_template("save.html",
|
|
217
|
+ suggestion=time.strftime(
|
|
218
|
+ "moment-%Y-%m-%d-%H.%M.%S"),
|
|
219
|
+ files = [os.path.basename(path).rsplit(".",1)[0] for path in glob("archive/*.json")])
|
|
220
|
+
|
|
221
|
+
|
202
|
222
|
@application.get("/chat-editor")
|
203
|
223
|
def chat_editor():
|
204
|
224
|
return render_template("chat-editor.html")
|