Ver código fonte

Add /save

master
Nimrod Kerrett 1 ano atrás
pai
commit
bdbb6be3ab
4 arquivos alterados com 81 adições e 10 exclusões
  1. 24
    4
      app.py
  2. 1
    1
      static/chat.schema.json
  3. 17
    5
      templates/prompt.txt
  4. 39
    0
      templates/save.html

+ 24
- 4
app.py Ver arquivo

@@ -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")

+ 1
- 1
static/chat.schema.json Ver arquivo

@@ -56,7 +56,7 @@
56 56
       "$ref": "/enum/audio"
57 57
     }
58 58
   },
59
-  "defaultProperties": [
59
+  "required": [
60 60
     "title",
61 61
     "markdown",
62 62
     "bg_video",

+ 17
- 5
templates/prompt.txt Ver arquivo

@@ -4,11 +4,7 @@ user is odelia, she was informed that her cancer was incurable. she is 48 years
4 4
 
5 5
 The goal of your session is to create ceremonies or enchanted moments via the multimedia resources available to you (see below), but first you should inquire how the user feels now and keep monitoring the changes in their emotions. this can help you decide the media that could suit the moment. 
6 6
 
7
-IMPORTANT!
8
-
9
-1. Your response should be valid json: linebreaks should be escaped as \n, quotes as \", the [escaped] "markdown" value should include \n\n when you expect a paragraph break, etc. It should also be valid according to the json-schema definition above. When there's an enum,the field is MANDATORY and MUST be a valid choice (you can rely on "enum_title" as textual explanations of related choices).
10
-
11
-2. Although the markdown area is scrollable, best is to keep the text under 350 characters (less if there are paragraph breaks or bullet list). If you have more to say, just indicate it. The user can always ask you to continue by hitting enter.
7
+Your responses should be JSON that is valid according to this json-schema
12 8
 
13 9
 ```
14 10
 {{schema}}
@@ -18,3 +14,19 @@ For example:
18 14
 ```
19 15
 {{example}}
20 16
 ```
17
+
18
+Try to pick bg_video and [optionally] soundtrack that fit the conversation topic and is beneficial to the user emotionally.
19
+
20
+IMPORTANT!
21
+
22
+1. Your resond should be valid JSON: linebreaks should be escaped as \n, quotes as \", the [escaped] "markdown" value should include \n\n when you expect a paragraph break, etc.
23
+
24
+2. It should also be valid according to the json-schema definition above:
25
+
26
+  * "required" fields are required.
27
+
28
+  *  when there's an enum, the value MUST be a valid choice (you can rely on "options/enum_title" as textual explanations of corresponding choices).
29
+
30
+  * etc.
31
+
32
+3. Although the markdown area is scrollable, best is to keep the text under 350 characters (less if there are paragraph breaks or bullet list). If you have more to say, just indicate it. The user can always ask you to continue by hitting enter.

+ 39
- 0
templates/save.html Ver arquivo

@@ -0,0 +1,39 @@
1
+<!doctype html>
2
+<html lang="en">
3
+  <head>
4
+    <meta charset="utf-8">
5
+    <meta name="viewport" content="width=device-width, initial-scale=1">
6
+    <title>Image generator</title>
7
+    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
8
+    <link rel="stylesheet" href=
9
+        "https://use.fontawesome.com/releases/v5.6.3/css/all.css"
10
+        integrity=
11
+            "sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/"
12
+            crossorigin="anonymous">
13
+  </head>
14
+  <body>
15
+    <div class="container">
16
+      <h3 class="text-center">Save current moment</h3>
17
+      <div class="row mt-4">
18
+        <div class="col-md-6 offset-md-3">
19
+          <form "card-text" method="POST" action="">
20
+            <input class="form-control" id="filename" name="filename" placeholder="File name" value="{{suggestion}}" />
21
+	    <input type="submit" class="btn btn-success form-control" value="Save">
22
+          </form>
23
+        </div>
24
+      </div>
25
+      <div class="row mt-1">
26
+        <div class="col-md-6 offset-md-3">
27
+		<select class="form-select" onchange="document.getElementById('filename').value=this.value">
28
+		  <option value="">[type filename or select existing file from list below]</option>
29
+		  {% for file in files %}
30
+		    <option value="{{file}}">{{file}}</option>
31
+		  {% endfor %}
32
+		</select>
33
+        </div>
34
+      </div>
35
+    </div>
36
+    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
37
+
38
+  </body>
39
+</html>

Carregando…
Cancelar
Salvar