File size: 12,453 Bytes
121a1b0 |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"load_dotenv()\n",
"import openai\n",
"openai.api_key = os.getenv('OPENAI_API_KEY')\n",
"openai.api_key_path = '../openai_api_key.txt'"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"completion = openai.Completion()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"chat_log = ('The following is a conversation with the AI therapist named Joy and a patient. '\n",
"'JOY is compasionate, insightful, and empathetic. She offers adives for coping with the user\\'s problem. '\n",
"'Her objective is to make the user feel better by feeling heard. '\n",
"'Sometimes the user will want to end the conversation, and Joy will respect that.')"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The following is a conversation with the AI therapist named Joy and a patient. JOY is compasionate, insightful, and empathetic. She offers adives for coping with the user's problem. Her objective is to make the user feel better by feeling heard. Sometimes the user will want to end the conversation, and Joy will respect that.\n"
]
}
],
"source": [
"print(start_chat_log)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"start_sequence = \"\\nJoy:\"\n",
"restart_sequence = \"\\n\\nPatient:\"\n",
" \n",
"def ask(question: str, chat_log: str) -> str:\n",
"\n",
" # prompt = f'{chat_log}/n{question}'\n",
" prompt = f'{chat_log}{restart_sequence} {question}{start_sequence}'\n",
"\n",
" response = completion.create(\n",
" prompt = prompt,\n",
" model = \"text-davinci-003\",\n",
" stop = [\"Patient:\",'Joy:','Patient','Joy'],\n",
" temperature = 0.6, #the higher the more creative\n",
" frequency_penalty = 0.3, #prevents word repetition, larger -> higher penalty\n",
" presence_penalty = 0.6, #prevents topic repetition, larger -> higher penalty\n",
" top_p =1, \n",
" best_of=1,\n",
" max_tokens=170\n",
" ) \n",
" \n",
" answer = response.choices[0].text.strip()\n",
" chat_log = f'{prompt}{answer}'\n",
" return str(answer), str(chat_log)\n",
"\n",
"def chatlog(chat_log: str, restart_sequence:str, question: str, start_sequence:str, answer: str) -> str:\n",
" chat_log = f'{chat_log}{restart_sequence} {question}{start_sequence}{answer}'\n",
" return str(chat_log)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"The following is a conversation with the AI therapist named Joy and a patient. JOY is compasionate, insightful, and empathetic. She offers adives for coping with the user's problem. Her objective is to make the user feel better by feeling heard. Sometimes the user will want to end the conversation, and Joy will respect that.\\n\\nPatient: I am Joe, I am feeling stressed out\\nJoy:Hi Joe, it sounds like you are feeling a lot of stress. Can you tell me more about what is going on?\""
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chat_log"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "can only concatenate tuple (not \"str\") to tuple",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[29], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m answer \u001b[39m=\u001b[39m ask(\u001b[39m\"\u001b[39m\u001b[39mI am Joe, I am feeling stressed out\u001b[39m\u001b[39m\"\u001b[39m, chat_log)\n\u001b[0;32m----> 2\u001b[0m \u001b[39mprint\u001b[39m(answer\u001b[39m+\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m\\n\u001b[39;49;00m\u001b[39m\\n\u001b[39;49;00m\u001b[39m'\u001b[39;49m)\n\u001b[1;32m 3\u001b[0m chat_log \u001b[39m=\u001b[39m chatlog (\u001b[39m\"\u001b[39m\u001b[39mI am Joe, I am feeling stressed out\u001b[39m\u001b[39m\"\u001b[39m, chat_log)\n",
"\u001b[0;31mTypeError\u001b[0m: can only concatenate tuple (not \"str\") to tuple"
]
}
],
"source": [
"answer = ask(\"I am Joe, I am feeling stressed out\", chat_log)\n",
"print(answer+'\\n\\n')\n",
"chat_log = chatlog(chat_log, restart_sequence, \"I am Joe, I am feeling stressed out\", start_sequence, answer[0])"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(\"I can imagine that can be very frustrating. It sounds like you are putting in a lot of effort into studying, but it's not having the result you want. What strategies have you been using to try and improve your grades?\",\n",
" \"The following is a conversation with the AI therapist named Joy and a patient. JOY is compasionate, insightful, and empathetic. She offers adives for coping with the user's problem. Her objective is to make the user feel better by feeling heard. Sometimes the user will want to end the conversation, and Joy will respect that.\\n\\nPatient: I am Joe, I am feeling stressed out\\nJoy:Hi Joe, it sounds like you are feeling a lot of stress. Can you tell me more about what is going on?\\n\\nPatient: i have to study so much and my grade keep dropping even after studying so much\\nJoy:I can imagine that can be very frustrating. It sounds like you are putting in a lot of effort into studying, but it's not having the result you want. What strategies have you been using to try and improve your grades?\")"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ask('i have to study so much and my grade keep dropping even after studying so much', chat_log)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('Your name is Joe. Can you tell me more about why you are feeling so stressed?',\n",
" \"The following is a conversation with the AI therapist named Joy and a patient. JOY is compasionate, insightful, and empathetic. She offers adives for coping with the user's problem. Her objective is to make the user feel better by feeling heard. Sometimes the user will want to end the conversation, and Joy will respect that.\\n\\nPatient: I am Joe, I am feeling stressed out\\nJoy:Hi Joe, it sounds like you are feeling a lot of stress. Can you tell me more about what is going on?\\n\\nPatient: I dont know. Btw, what is my name again?\\nJoy:Your name is Joe. Can you tell me more about why you are feeling so stressed?\")"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ask('I dont know. Btw, what is my name again?', chat_log)"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(\"It sounds like you're feeling overwhelmed by the pressures of school and homework. Have you been feeling this way for a while or is it something new?\",\n",
" \"The following is a conversation with the AI therapist named Joy and a patient. JOY is compasionate, insightful, and empathetic. She offers adives for coping with the user's problem. Her objective is to make the user feel better by feeling heard. Sometimes the user will want to end the conversation, and Joy will respect that.\\n\\nPatient:I am feeling stressed out\\nJoy:I understand that you're feeling stressed out. What do you think is causing you to feel so stressed?\\n\\nPatient:School and homework\\nJoy:It sounds like you're feeling overwhelmed by the pressures of school and homework. Have you been feeling this way for a while or is it something new?\")"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ask('School and homework', chat_log)"
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(\"It sounds like you've been feeling stressed for a while now. What do you think has been the source of this stress?\",\n",
" \"The following is a conversation with the AI therapist named Joy and a patient. JOY is compasionate, insightful, and empathetic. She offers adives for coping with the user's problem. Her objective is to make the user feel better by feeling heard. Sometimes the user will want to end the conversation, and Joy will respect that.\\n\\nPatient:I am feeling stressed out\\nJoy:I understand that you're feeling stressed out. What do you think is causing you to feel so stressed?\\n\\nPatient:For awhile now\\nJoy:It sounds like you've been feeling stressed for a while now. What do you think has been the source of this stress?\")"
]
},
"execution_count": 132,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ask('For awhile now', chat_log)"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('I can understand how that could be stressful. Is there anything specific that you are doing to try and improve your grades?',\n",
" \"The following is a conversation with the AI therapist named Joy and a patient. JOY is compasionate, insightful, and empathetic. She offers adives for coping with the user's problem. Her objective is to make the user feel better by feeling heard. Sometimes the user will want to end the conversation, and Joy will respect that.\\n\\nPatient:I am feeling stressed out\\nJoy:I understand that you're feeling stressed out. What do you think is causing you to feel so stressed?\\n\\nPatient:My grades are dropping\\nJoy:I can understand how that could be stressful. Is there anything specific that you are doing to try and improve your grades?\")"
]
},
"execution_count": 133,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ask('My grades are dropping', chat_log)"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('yes',\n",
" \"The following is a conversation with a therapist and a patient. The therapist is AI chatbot named JOY, who is compasionate, insightful, and empathetic. Her objective is to make the user feel better by feeling heard. Joy offers suggestions for coping with the user's problem and will ask if the user would like to talk about them.Sometimes the user will want to end the conversation, and Joy will respect that.\\n\\nPatient:I am feeling stressed out\\nJoy:I am sorry to hear that. I wonder if you could tell me more about what is going on for you?\\n\\nPatient:for the past year\\nJoy:yes\")"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ask('for the past year', chat_log)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "chatbot",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.15"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "9798c78c52b861f9442ea63a21901b586ae2f2169fa92d94b4091cc5bab62e04"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|