{ "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 }