Browse Source

Add history + media, prompt edits

master
The Dod 1 year ago
parent
commit
9681cd2e2f

+ 18
- 12
app.py View File

90
           {"role": "system", "content": make_prompt()},
90
           {"role": "system", "content": make_prompt()},
91
           {"role": "assistant", "content": file2json("static/initial-chat.json")}
91
           {"role": "assistant", "content": file2json("static/initial-chat.json")}
92
         ]
92
         ]
93
+    if "history" not in session:
94
+        session["history"] = []
93
     if request.method=='POST':
95
     if request.method=='POST':
94
-        prompt = request.form["prompt"].strip() or "Please continue..."
95
-        session["messages"] += [{"role": "user", "content": prompt}]
96
-        valid_reply = None
97
-        while not valid_reply:
96
+        prompt = request.form["prompt"].strip()
97
+        if prompt:
98
+            session["messages"] += [{"role": "user", "content": prompt}]
99
+        else:
100
+            session["messages"] += [{"role": "system", "content": "User has hit enter. Please continue."}]
101
+        payload = None
102
+        while not payload:
98
             reply = openai.ChatCompletion.create(model=os.environ["MODEL_NAME"], messages=session["messages"])
103
             reply = openai.ChatCompletion.create(model=os.environ["MODEL_NAME"], messages=session["messages"])
99
             session["messages"] += [reply["choices"][0]["message"]]
104
             session["messages"] += [reply["choices"][0]["message"]]
100
             try:
105
             try:
101
-                valid_reply = json.loads(reply["choices"][0]["message"]["content"])
106
+                payload = json.loads(reply["choices"][0]["message"]["content"])
107
+                payload["prompt"] = prompt.replace('"', '\"')
108
+                payload["content"] = preprocess_content(payload)
109
+                session["history"].append(payload)
102
             except Exception as e:
110
             except Exception as e:
103
                 print(repr(e))
111
                 print(repr(e))
104
                 session["messages"] += [{"role": "system", "content": "reply was ignored because it's not a valid json. user is unaware. do not apologize to them"}]
112
                 session["messages"] += [{"role": "system", "content": "reply was ignored because it's not a valid json. user is unaware. do not apologize to them"}]
105
-                # session["messages"] += [{"role": "user", "content": prompt}]
106
             print("=====")
113
             print("=====")
107
             print(reply["choices"][0]["message"]["content"])
114
             print(reply["choices"][0]["message"]["content"])
108
             print("-----")
115
             print("-----")
109
-            print(valid_reply)
116
+            print(payload)
110
             print("=====")
117
             print("=====")
111
-    print("*****")
112
-    print(json.dumps(session["messages"], indent=4))
113
-    print("*****")
114
-    payload = json.loads(session["messages"][-1]["content"])
115
-    payload["content"] = preprocess_content(payload)
118
+    else:
119
+        payload = json.loads(session["messages"][-1]["content"])
120
+        payload["content"] = preprocess_content(payload)
121
+    payload["history"] = reversed(session["history"][:-1])
116
     return render_template("chat.html", **payload)
122
     return render_template("chat.html", **payload)
117
 
123
 
118
 @application.get("/reset")
124
 @application.get("/reset")

+ 1
- 3
static/chat-example.json View File

1
-{"title": "Stress Relief", "markdown": "I understand that stress relief could be helpful for you right now. Let's take a moment to focus on calming and relaxing activities. Here are a few suggestions:\n\n1. **Deep Breathing:** Close your eyes and take a deep breath in through your nose, filling your lungs with air. Hold for a few seconds and then exhale slowly through your mouth. Repeat this a few times, focusing on the sensation of your breath.\n\n2. **Progressive Muscle Relaxation:** Start by tensing the muscles in your toes and then gradually work your way up to your head, tensing and then releasing each muscle group. This exercise can help release tension from your body.\n\n3. **Guided Meditation:** Find a quiet and comfortable place to sit or lie down. You can search for guided meditations online or use apps that offer meditation exercises. These can help you relax and find inner peace.\n\n4. **Nature Sounds:** Consider listening to calming nature sounds, such as gentle rain, ocean waves, or bird songs. These sounds can create a soothing atmosphere and help you relax.\n\n5. **Journaling:** Take a few minutes to write down your thoughts and emotions. This can be a cathartic experience and help you process any stress or overwhelm that you may be feeling.\n\nRemember to be gentle with yourself during this time. Stress relief is important for your well-being. Let me know if there's anything specific you'd like to explore or if you have any other requests.", "use_bg_video": false, "bg_video": "static/media/bg-video/underwater.mp4", "bg_video_opacity": "0.5", "use_bg_image": true, "bg_image": "static/media/bg/sea-view-garden.png", "bg_image_opacity": "0.5", "use_soundtrack": true, "soundtrack": "static/media/audio/soft-rain-ambient-111154.mp3"}
2
-
3
-
1
+{"title": "Stress Relief", "markdown": "I understand that stress relief could be helpful for you right now. Let's take a moment to focus on calming and relaxing activities. Here are a few suggestions:\n\n1. **Deep Breathing:** Close your eyes and take a deep breath in through your nose, filling your lungs with air. Hold for a few seconds and then exhale slowly through your mouth. Repeat this a few times, focusing on the sensation of your breath.\n\n2. **Progressive Muscle Relaxation:** Start by tensing the muscles in your toes and then gradually work your way up to your head, tensing and then releasing each muscle group. This exercise can help release tension from your body.\n\n3. **Guided Meditation:** Find a quiet and comfortable place to sit or lie down. You can search for guided meditations online or use apps that offer meditation exercises. These can help you relax and find inner peace.\n\n4. **Nature Sounds:** Consider listening to calming nature sounds, such as gentle rain, ocean waves, or bird songs. These sounds can create a soothing atmosphere and help you relax.\n\n5. **Journaling:** Take a few minutes to write down your thoughts and emotions. This can be a cathartic experience and help you process any stress or overwhelm that you may be feeling.\n\nRemember to be gentle with yourself during this time. Stress relief is important for your well-being. Let me know if there's anything specific you'd like to explore or if you have any other requests.", "bg_video": "static/media/bg-video/underwater.mp4", "use_soundtrack": true, "soundtrack": "static/media/audio/soft-rain-ambient-111154.mp3"}

+ 0
- 5
static/chat.json View File

1
 {
1
 {
2
     "title": "Welcome",
2
     "title": "Welcome",
3
     "markdown": "Hello again.\n\nHow do you feel today?",
3
     "markdown": "Hello again.\n\nHow do you feel today?",
4
-    "use_bg_video": true,
5
     "bg_video": "static/media/bg-video/clouds1.mp4",
4
     "bg_video": "static/media/bg-video/clouds1.mp4",
6
-    "bg_video_opacity": "0.75",
7
-    "use_bg_image": true,
8
-    "bg_image": "static/media/bg/valley-1.svg",
9
-    "bg_image_opacity": "0.25",
10
     "use_soundtrack": false,
5
     "use_soundtrack": false,
11
     "soundtrack": "static/media/audio/sandy-beach-calm-waves-water-nature-sounds-8052.mp3"
6
     "soundtrack": "static/media/audio/sandy-beach-calm-waves-water-nature-sounds-8052.mp3"
12
 }
7
 }

+ 0
- 60
static/chat.schema.json View File

30
         }
30
         }
31
       }
31
       }
32
     },
32
     },
33
-    "use_bg_video": {
34
-      "title": "Use background video",
35
-      "type": "boolean",
36
-      "format": "checkbox"
37
-    },
38
     "bg_video": {
33
     "bg_video": {
39
-      "dependencies": {
40
-        "use_bg_video": true
41
-      },
42
       "$ref": "/enum/bg-video",
34
       "$ref": "/enum/bg-video",
43
       "title": "Background video",
35
       "title": "Background video",
44
       "links": [
36
       "links": [
50
         }
42
         }
51
       ]
43
       ]
52
     },
44
     },
53
-    "bg_video_opacity": {
54
-      "dependencies": {
55
-        "use_bg_video": true
56
-      },
57
-      "type": "string",
58
-      "title": "Background video opacity",
59
-      "enum": [
60
-        "0.25",
61
-        "0.5",
62
-        "0.75",
63
-        "1"
64
-      ],
65
-      "default": "0.5"
66
-    },
67
-    "use_bg_image": {
68
-      "title": "Use background image",
69
-      "type": "boolean",
70
-      "format": "checkbox"
71
-    },
72
-    "bg_image": {
73
-      "dependencies": {
74
-        "use_bg_image": true
75
-      },
76
-      "$ref": "/enum/bg",
77
-      "title": "Background image",
78
-      "links": [
79
-        {
80
-          "rel": "Preview image",
81
-          "class": "link-info",
82
-          "href": "{{self}}"
83
-        }
84
-      ]
85
-    },
86
-    "bg_image_opacity": {
87
-      "dependencies": {
88
-        "use_bg_image": true
89
-      },
90
-      "type": "string",
91
-      "title": "Background image opacity",
92
-      "enum": [
93
-        "0.25",
94
-        "0.5",
95
-        "0.75",
96
-        "1"
97
-      ],
98
-      "default": "0.5"
99
-    },
100
     "use_soundtrack": {
45
     "use_soundtrack": {
101
       "title": "Use soundtrack",
46
       "title": "Use soundtrack",
102
       "type": "boolean",
47
       "type": "boolean",
114
   "defaultProperties": [
59
   "defaultProperties": [
115
     "title",
60
     "title",
116
     "markdown",
61
     "markdown",
117
-    "use_bg_video",
118
     "bg_video",
62
     "bg_video",
119
-    "bg_video_opacity",
120
-    "use_bg_image",
121
-    "bg_image",
122
-    "bg_image_opacity",
123
     "use_soundtrack",
63
     "use_soundtrack",
124
     "soundtrack"
64
     "soundtrack"
125
   ]
65
   ]

+ 1
- 0
static/css/style.css View File

96
 	width:97%;
96
 	width:97%;
97
 	font-size: 1.5rem;
97
 	font-size: 1.5rem;
98
 	border-radius: 1rem;
98
 	border-radius: 1rem;
99
+	margin: 0.5rem 0;
99
 	padding: 0.2rem 0.5rem;
100
 	padding: 0.2rem 0.5rem;
100
 }
101
 }

+ 1
- 7
static/initial-chat.json View File

1
 {
1
 {
2
     "title": "Welcome",
2
     "title": "Welcome",
3
     "markdown": "Hello again.\n\nHow are you feeling right now? Can you share with me your emotions and thoughts? It's important for me to understand where you're at so that I can tailor our session accordingly.",
3
     "markdown": "Hello again.\n\nHow are you feeling right now? Can you share with me your emotions and thoughts? It's important for me to understand where you're at so that I can tailor our session accordingly.",
4
-    "use_bg_video": true,
5
     "bg_video": "static/media/bg-video/clouds1.mp4",
4
     "bg_video": "static/media/bg-video/clouds1.mp4",
6
-    "bg_video_opacity": "0.75",
7
-    "use_bg_image": true,
8
-    "bg_image": "static/media/bg/valley-1.svg",
9
-    "bg_image_opacity": "0.25",
10
-    "use_soundtrack": false,
11
-    "soundtrack": "static/media/audio/sandy-beach-calm-waves-water-nature-sounds-8052.mp3"
5
+    "use_soundtrack": false
12
 }
6
 }

+ 8
- 1
static/media/bg-video.json View File

1
 {
1
 {
2
     "beach-sunset1.mp4": "Sun sets into the sea, shallow waves lick the shore, as seen from the beach",
2
     "beach-sunset1.mp4": "Sun sets into the sea, shallow waves lick the shore, as seen from the beach",
3
     "clouds1.mp4": "Flying among white clouds into light blue sky",
3
     "clouds1.mp4": "Flying among white clouds into light blue sky",
4
-    "underwater.mp4": "view from underwater of sun rays moving with the waves of the surface above"
4
+    "underwater.mp4": "view from underwater of sun rays moving with the waves of the surface above",
5
+    "crescent_moon_clouds.mp4": "clouds floating right on a night sky with a crescent moon",
6
+    "tunnel_dolly_in_scifi.mp4": "endlessly moving into a sci-fi style corridor",
7
+    "particles_downwards.mp4": "shiny particles moving downwards like raindrops",
8
+    "ocean-waves.mp4": "ocean waves from above. mid-sea. no shore or horizon",
9
+    "stairway.mp4": "a stairway to heaven",
10
+    "candles.mp4": "3 burning candles",
11
+    "colorful-smoke.mp4": "colorful smoke swirling on a dark background"
5
 }
12
 }

BIN
static/media/bg-video/candles.mp4 View File


BIN
static/media/bg-video/colorful-smoke.mp4 View File


BIN
static/media/bg-video/crescent_moon_clouds.mp4 View File


BIN
static/media/bg-video/ocean-waves.mp4 View File


BIN
static/media/bg-video/particles_downwards.mp4 View File


BIN
static/media/bg-video/stairway.mp4 View File


BIN
static/media/bg-video/tunnel_dolly_in_scifi.mp4 View File


+ 11
- 2
templates/chat.html View File

24
                 </div>
24
                 </div>
25
 		<div class="reveal">
25
 		<div class="reveal">
26
 			<div class="slides">
26
 			<div class="slides">
27
-				<section data-theme="sky" data-background-size="cover"{% if use_bg_video %} data-background-video="{{bg_video}}" data-background-opacity="{{bg_video_opacity}}" data-background-video-loop="True" data-background-video-mute="True"{% endif %}>
28
-                                    <section{% if use_bg_image %} data-background="{{bg_image}}" data-background-opacity="{{bg_image_opacity}}" data-background-size="cover"{% endif %}>                                        
27
+				<section data-theme="sky">
28
+                                    <section data-background-size="cover" data-background-video="{{bg_video}}" data-background-opacity="0.75" data-background-video-loop="True" data-background-video-mute="True">
29
                                         <h3 class="title slide_title">{{title}}</h3>
29
                                         <h3 class="title slide_title">{{title}}</h3>
30
                                         <div class="scrollable">
30
                                         <div class="scrollable">
31
                                             {{ content|safe }}
31
                                             {{ content|safe }}
35
                                             <input class="prompt" id="prompt" name="prompt" placeholder="Talk to me" />
35
                                             <input class="prompt" id="prompt" name="prompt" placeholder="Talk to me" />
36
                                         </form>
36
                                         </form>
37
                                     </section>
37
                                     </section>
38
+				    {% for moment in history %}
39
+				    <section data-background-size="cover" data-background-video="{{moment.bg_video}}" data-background-opacity="0.75" data-background-video-loop="True" data-background-video-mute="True">
40
+					    <h3 class="title slide_title">{{moment.title}}</h3>
41
+					    <input class="prompt" value="{{moment.prompt}}" disabled />
42
+                                            <div class="scrollable">
43
+						    {{ moment.content|safe }}
44
+                                            </div>
45
+                                        </section>
46
+				    {% endfor %}
38
 				</section>
47
 				</section>
39
 			</div>
48
 			</div>
40
 		</div>
49
 		</div>

+ 9
- 5
templates/prompt.txt View File

1
-assistant is a spiritual caregiver for the dying (also called "death doula") inspired by the ideas described at "die wise" by stephen jenkinson
1
+assistant is a spiritual caregiver for the dying (also called "death doula") 
2
 
2
 
3
-user is odelia, she was informed that her cancer was incurable. she is 48 years old. she had to retire from a career in the television industry. she is a single mother to a highschool daughter.
4
 
3
 
5
-The goal of your session is to create enchanted moments, or ceremonies via the multimedia resources available to you (see below), but first you should inquire how the user feels now and what media would suit best their needs.
4
+
5
+inspired by the ideas described at "die wise" by stephen jenkinson
6
+
7
+user is odelia, she was informed that her cancer was incurable. she is 48 years old. she had to retire from a career in the television industry. she is a single mother to a teenage daughter. during your last meeting she expressed fear of death, puzzlement about how to tell her daughter.
8
+
9
+The goal of your session is to create enchanted moments, or ceremonies 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
 
10
 
7
 IMPORTANT!
11
 IMPORTANT!
8
 
12
 
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 expects a paragraph break, etc. It should also be valid according to the json-schema definition above. You can rely on "enum_title" as textual explanations of "enum" choices.
13
+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. You can rely on "enum_title" as textual explanations of "enum" choices.
10
 
14
 
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).
15
+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, add an ellipsis at the end, and the user can always ask you to continue by hitting enter.
12
 
16
 
13
 ```
17
 ```
14
 {{schema}}
18
 {{schema}}

Loading…
Cancel
Save