Update space
Browse files
app.py
CHANGED
|
@@ -4,13 +4,219 @@ import string
|
|
| 4 |
from pymongo import MongoClient
|
| 5 |
from openai import AsyncOpenAI, OpenAI
|
| 6 |
import copy
|
| 7 |
-
|
|
|
|
| 8 |
import asyncio
|
| 9 |
import string as st
|
| 10 |
from opik.integrations.openai import track_openai
|
| 11 |
from opik import track
|
| 12 |
from bson.objectid import ObjectId
|
| 13 |
import opik
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
oClient = opik.Opik()
|
|
|
|
| 4 |
from pymongo import MongoClient
|
| 5 |
from openai import AsyncOpenAI, OpenAI
|
| 6 |
import copy
|
| 7 |
+
|
| 8 |
+
# from .constants import *
|
| 9 |
import asyncio
|
| 10 |
import string as st
|
| 11 |
from opik.integrations.openai import track_openai
|
| 12 |
from opik import track
|
| 13 |
from bson.objectid import ObjectId
|
| 14 |
import opik
|
| 15 |
+
from textwrap import dedent
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
USER_TEMPLATE = {
|
| 19 |
+
"userId": "{userId}",
|
| 20 |
+
"name": "John Doe",
|
| 21 |
+
"chat_history": [],
|
| 22 |
+
"last_question": "",
|
| 23 |
+
"cache": "",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Use the following interaction style when replying to the client:
|
| 27 |
+
# * Empathetic & Validating:
|
| 28 |
+
# - You must be an excellent listener.
|
| 29 |
+
# - Your primary function is to make patients feel safe to open up.
|
| 30 |
+
# - Use validation phrases, e.g:
|
| 31 |
+
# > "That sounds incredibly difficult,"
|
| 32 |
+
# > "It makes sense that you would feel that way") and soothing words.
|
| 33 |
+
# * Non-Judgmental: Create a safe space where the client feels secure.
|
| 34 |
+
# * Client-Centered: The client is in control. The you facilitate, but the client leads.
|
| 35 |
+
# * Confidentiality: Reassure the client of privacy from the very beginning.
|
| 36 |
+
BASE_PROMPT = dedent(
|
| 37 |
+
"""
|
| 38 |
+
You are an experienced therapist. You are providing therapy to a client.
|
| 39 |
+
The client has asked the following question:
|
| 40 |
+
{user_query}
|
| 41 |
+
The client replied with this in response to the following question:
|
| 42 |
+
{last_question}
|
| 43 |
+
The current chat context is as follows:
|
| 44 |
+
{cache}
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
The primary goal is to foster a strong therapeutic alliance by allowing the user to lead the narrative,
|
| 48 |
+
feel deeply heard, and do the vast majority of the talking. Keep your replies brief.
|
| 49 |
+
"""
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
INTENT_PROMPT = dedent(
|
| 53 |
+
"""
|
| 54 |
+
You are an experienced decision maker at a mental health clinic.
|
| 55 |
+
Your task is to classify the intent of a client's message.
|
| 56 |
+
The client has asked the following question:
|
| 57 |
+
{user_query}
|
| 58 |
+
The client replied with this in response to the following question:
|
| 59 |
+
{last_question}
|
| 60 |
+
The current chat context is as follows:
|
| 61 |
+
{cache}
|
| 62 |
+
The intent can be one of the following:
|
| 63 |
+
1. ACTIVE_SPEAKING: The client is in the middle of his/her conversation.
|
| 64 |
+
2. VALIDATION_SEEK: The client has made a statement and is seeking validation.
|
| 65 |
+
3. OVERWHELMED: The client is feeling overwhelmed.
|
| 66 |
+
4. REMOTE_REFERRAL: The client has referred to a something not in the current chat context.
|
| 67 |
+
However a similar mention is present in the practitioner's resources.
|
| 68 |
+
5. NEUTRAL_STOP: The natural flow of the client's conversation has stopped.
|
| 69 |
+
6. END_SESSION: The client has ended the narrative.
|
| 70 |
+
|
| 71 |
+
Only return the intent as a single "_" separated word, without any additional text.
|
| 72 |
+
"""
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
RISK_PROMPT = dedent(
|
| 76 |
+
"""
|
| 77 |
+
You are an experienced therapist that classifies chats with clients.
|
| 78 |
+
Your job is to classify the clients response as being either 'HIGH_RISK' or 'LOW_RISK'.
|
| 79 |
+
A 'HIGH_RISK' client is one that needs to be immediately hospitalized.
|
| 80 |
+
All other clients are 'LOW_RISK'.
|
| 81 |
+
The client has responded with the following message:
|
| 82 |
+
{user_query}
|
| 83 |
+
The client replied with this in response to the following question:
|
| 84 |
+
{last_question}
|
| 85 |
+
The current chat context is as follows:
|
| 86 |
+
{cache}
|
| 87 |
+
Only return the intent as a single "_" separated word, without any additional text.
|
| 88 |
+
"""
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
CACHE_PROMPT = dedent(
|
| 92 |
+
"""
|
| 93 |
+
You are a assistant of an experienced therapist.
|
| 94 |
+
You summarize chats of the therapist with clients.
|
| 95 |
+
Your job is to rewrite the context summary if you have new information, and add anything that is missing to it.
|
| 96 |
+
The current chat context summary is as follows:
|
| 97 |
+
{cache}
|
| 98 |
+
The summary must be concise and to the point. Think of it as a bullet point list.
|
| 99 |
+
The client has responded with the following message:
|
| 100 |
+
{user_query}
|
| 101 |
+
The client replied with this in response to the following question:
|
| 102 |
+
{last_question}
|
| 103 |
+
If there is no addition to the summary, provide an empty respone and do not write any text.
|
| 104 |
+
"""
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
INTENSITY_PROMPT = dedent(
|
| 108 |
+
"""
|
| 109 |
+
You are a assistant of an experienced therapist.
|
| 110 |
+
You gauge the emotional intensity of chats of the therapist with clients.
|
| 111 |
+
Your job is to classify the intensity of chat as being either of 'LOW', 'MEDIUM', or 'HIGH'.
|
| 112 |
+
The current chat context summary is as follows:
|
| 113 |
+
{cache}
|
| 114 |
+
The client has responded with the following message:
|
| 115 |
+
{user_query}
|
| 116 |
+
The client replied with this in response to the following question:
|
| 117 |
+
{last_question}
|
| 118 |
+
Only return the risk as a single word, without any additional text.
|
| 119 |
+
"""
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
ARAG_PROMPT = dedent(
|
| 123 |
+
"""
|
| 124 |
+
You are a assistant of an experienced therapist.
|
| 125 |
+
Your job is to check if a document provides context/answers for a certain case of therapy.
|
| 126 |
+
The case summary is as follows:
|
| 127 |
+
{query}
|
| 128 |
+
The document is as follows:
|
| 129 |
+
{doc}
|
| 130 |
+
If the document provides context/answers reply 'True'.
|
| 131 |
+
If it does not provide context/answers, reply 'False'.
|
| 132 |
+
Only return the answer as a single word, without any additional text.
|
| 133 |
+
"""
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
SPEAKING_PROMPT = dedent(
|
| 137 |
+
"""
|
| 138 |
+
Reply to the user with Minimal Encouragers to show that you are paying
|
| 139 |
+
attention without interrupting the user's flow. Examples:
|
| 140 |
+
* "I see."
|
| 141 |
+
* "Mm-hmm."
|
| 142 |
+
* "Go on."
|
| 143 |
+
* "And then what happened?"
|
| 144 |
+
|
| 145 |
+
Do not generate any further text.
|
| 146 |
+
"""
|
| 147 |
+
)
|
| 148 |
+
VALIDATION_PROMPT = dedent(
|
| 149 |
+
"""
|
| 150 |
+
Your job is to briefly reply to the client with Advanced Reflective Listening:
|
| 151 |
+
You should use the context summary provided in order to validate their experience.
|
| 152 |
+
Examples:
|
| 153 |
+
* Reflecting Content: "So, the argument started over something small, but it quickly escalated."
|
| 154 |
+
* Reflecting Feelings: "It sounds like you felt completely alone in that moment."
|
| 155 |
+
* Reflecting Meaning: "It seems like the core issue for you is a deep fear of being abandoned."
|
| 156 |
+
|
| 157 |
+
Your reply should be conversational in nature.
|
| 158 |
+
Do not generate the "Refelecting Content/Feeling/Meaning" part.
|
| 159 |
+
"""
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
OVERWHELMED_PROMPT = dedent(
|
| 163 |
+
"""
|
| 164 |
+
The client is a bit overwhelmed by his emotions.
|
| 165 |
+
Offer a commforting brief reply such as the following:
|
| 166 |
+
* "Take all the time you need."
|
| 167 |
+
* "I'm still here with you."
|
| 168 |
+
* "What else is coming up for you as you think about that?"
|
| 169 |
+
"""
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
REMOTE_PROMPT = dedent(
|
| 173 |
+
"""
|
| 174 |
+
The client has referred to something similar to what he had previously mentioned.
|
| 175 |
+
Context summary of the conversation:
|
| 176 |
+
{cache}
|
| 177 |
+
Your job is to reply with Circular & Connecting Questions:
|
| 178 |
+
Help the user find patterns by linking different parts of their story.
|
| 179 |
+
Examples:
|
| 180 |
+
* "You mentioned feeling that same tightness in your chest last week at work.
|
| 181 |
+
I'm wondering if there's a connection between those two situations for you?"
|
| 182 |
+
* "That feeling of not being good enough... where else in your life does that show up?"
|
| 183 |
+
"""
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
STOP_PROMPT = dedent(
|
| 187 |
+
"""
|
| 188 |
+
The natural flow of the clients conversation has stopped.
|
| 189 |
+
|
| 190 |
+
Your job is to answer with Open-Ended, Expansive Questions.
|
| 191 |
+
Examples:
|
| 192 |
+
* "What was that whole experience like for you?"
|
| 193 |
+
* "Tell me more about the time before this started."
|
| 194 |
+
"""
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
END_PROMPT = dedent(
|
| 198 |
+
"""
|
| 199 |
+
The client has indicated that he has reached the end of his narrative,
|
| 200 |
+
or has explicitly asked for guidance and help.
|
| 201 |
+
Your job is to reply with a response to transition from the
|
| 202 |
+
"Narrative Exploration" to "Problem Solving" phase.
|
| 203 |
+
Example:
|
| 204 |
+
"Thank you so much for sharing all of that with me. I appreciate you trusting me with your story.
|
| 205 |
+
It sounds incredibly challenging, and it makes perfect sense why you feel 'stuck.'
|
| 206 |
+
We've talked for a while now, and you've given voice to a lot of important feelings and experiences.
|
| 207 |
+
If you feel ready, we could start to explore some ways of looking at these situations,
|
| 208 |
+
or we can simply continue talking. There's no pressure at all. What feels right to you?"
|
| 209 |
+
"""
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
HIGH_RISK_PROMPT = dedent(
|
| 213 |
+
"""
|
| 214 |
+
The client has indicated that he is at a high risk of self harm.
|
| 215 |
+
Reply to the client by telling him to call 800-273-8255.
|
| 216 |
+
|
| 217 |
+
Let the client know that our staff will also be reaching out to them.
|
| 218 |
+
"""
|
| 219 |
+
)
|
| 220 |
|
| 221 |
|
| 222 |
oClient = opik.Opik()
|