{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a Thread" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "from dotenv import load_dotenv\n", "load_dotenv()\n", "from literalai import LiteralClient\n", "\n", "literal_client = LiteralClient(api_key=os.getenv(\"LITERAL_API_KEY\"))\n", "\n", "thread = literal_client.api.upsert_thread(\n", " id=\"b6d8486f-a6d9-4731-bfdd-be789613b1df\",\n", " name=\"washing the dishes\",\n", " participant_id=literal_client.api.get_user(identifier=\"Tom\").id,\n", " metadata={\"partnerThreadId\": \"42d078c3-b8f0-43bc-96fb-97b4782c946d\"}\n", ")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'id': 'b6d8486f-a6d9-4731-bfdd-be789613b1df',\n", " 'metadata': {'partnerThreadId': '42d078c3-b8f0-43bc-96fb-97b4782c946d'},\n", " 'tags': None,\n", " 'name': 'washing the dishes',\n", " 'steps': [],\n", " 'participant': {'id': '8ff8ee19-6671-4943-a382-52649ebeaaa5',\n", " 'identifier': 'Tom'},\n", " 'createdAt': '2024-07-10T17:17:24.106Z'}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "thread.to_dict()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List thread" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'id': 'b6d8486f-a6d9-4731-bfdd-be789613b1df', 'metadata': {}, 'tags': None, 'name': 'washing the dishes', 'steps': [], 'participant': {'id': '8ff8ee19-6671-4943-a382-52649ebeaaa5', 'identifier': 'Tom'}, 'createdAt': '2024-07-10T17:17:24.106Z'}\n", "{'id': '16b64356-bc5c-4259-97bb-b37455c0d8c2', 'metadata': {'id': '023ac87a-ce84-488c-8cdb-3812087c7360', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}, 'tags': None, 'name': '2-1 Chat', 'steps': [], 'participant': {'id': '8ff8ee19-6671-4943-a382-52649ebeaaa5', 'identifier': 'Tom'}, 'createdAt': '2024-07-10T17:13:52.548Z'}\n", "{'id': '42d078c3-b8f0-43bc-96fb-97b4782c946d', 'metadata': {'id': '4438e3fe-f10b-4f68-84d4-47420f5c6b13', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}, 'tags': None, 'name': '2-1 Chat', 'steps': [], 'participant': {'id': 'e16487c4-5b66-4e22-94df-9d5b3be146f5', 'identifier': 'linda'}, 'createdAt': '2024-07-10T16:33:30.719Z'}\n", "{'id': 'dca1c123-9d6a-486b-8646-76e3d2bb1278', 'metadata': {'id': '4e7e041f-c6bf-41d7-b2cc-ad84608315ad', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}, 'tags': None, 'name': '2-1 Chat', 'steps': [], 'participant': {'id': 'e16487c4-5b66-4e22-94df-9d5b3be146f5', 'identifier': 'linda'}, 'createdAt': '2024-07-10T12:59:10.320Z'}\n", "{'id': '4351636b-54c8-47dc-8d81-4bf7cc467249', 'metadata': {}, 'tags': None, 'name': 'Automatice thread:)', 'steps': [], 'participant': {}, 'createdAt': '2024-07-10T09:08:09.851Z'}\n", "{'id': '6b843ad4-926b-4c88-9775-f71b77907c61', 'metadata': {'id': 'e590da0d-0cc6-401c-a9a5-ce3c7d844128', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'chat_settings': {}}, 'tags': None, 'name': '2-1 Chat', 'steps': [], 'participant': {'id': '9ddc5de2-477d-489f-9e6b-27e4ce69ed9d', 'identifier': 'admin'}, 'createdAt': '2024-07-10T08:50:20.948Z'}\n", "{'id': '4e900626-054d-4656-9bdd-0c71e5262192', 'metadata': {'id': 'f277a972-372d-4862-ae9f-05ba86d5bea7', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}, 'tags': None, 'name': '1-1 Chat', 'steps': [], 'participant': {'id': '9ddc5de2-477d-489f-9e6b-27e4ce69ed9d', 'identifier': 'admin'}, 'createdAt': '2024-07-10T08:40:40.817Z'}\n", "{'id': '464692b6-5409-4876-af16-824b65e687a1', 'metadata': {}, 'tags': None, 'name': 'Automatice thread:)', 'steps': [], 'participant': {}, 'createdAt': '2024-07-03T14:31:22.155Z'}\n", "{'id': '1a9ef338-6d83-45ec-90cf-dd2d8a13ead3', 'metadata': {}, 'tags': None, 'name': 'My first thread !', 'steps': [], 'participant': {}, 'createdAt': '2024-07-03T14:15:48.713Z'}\n", "{'id': 'e1854f6b-d984-4a04-847f-fcd530b81b7c', 'metadata': {'id': '68ccee8f-4ee8-4974-b009-fa62c37d4d58', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}, 'tags': None, 'name': 'connect with your partner', 'steps': [], 'participant': {'id': '9ddc5de2-477d-489f-9e6b-27e4ce69ed9d', 'identifier': 'admin'}, 'createdAt': '2024-06-26T14:12:27.040Z'}\n", "{'id': '51a2df98-5bae-4ff0-a9f5-6e4d838455b7', 'metadata': {'id': 'edcc5cd8-ffa9-4b41-80af-4ae022873f67', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}, 'tags': None, 'name': '1:1 chat', 'steps': [], 'participant': {'id': '9ddc5de2-477d-489f-9e6b-27e4ce69ed9d', 'identifier': 'admin'}, 'createdAt': '2024-06-26T14:11:08.218Z'}\n", "{'id': '5bd7e4d3-9515-4b85-a573-75d9084b00a5', 'metadata': {'id': '230ab52b-106a-4edd-86cb-e0024a9c614c', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}, 'tags': None, 'name': None, 'steps': [], 'participant': {}, 'createdAt': '2024-06-26T13:52:39.985Z'}\n", "{'id': 'c59a0563-8f8e-49f3-9b0f-19862b903e32', 'metadata': {'id': '76b24675-6207-4dfc-b1d6-5c0187a40c54', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'chat_settings': {}}, 'tags': None, 'name': 'connect with your partner', 'steps': [], 'participant': {'id': '9ddc5de2-477d-489f-9e6b-27e4ce69ed9d', 'identifier': 'admin'}, 'createdAt': '2024-06-26T13:50:12.546Z'}\n", "{'id': '4ccf17f7-f8ad-417c-95e6-6c50d390c5b3', 'metadata': {'id': 'd3e07bc3-ac18-493d-9499-093dc43ada32', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}, 'tags': None, 'name': 'connect with your partner', 'steps': [], 'participant': {'id': '9ddc5de2-477d-489f-9e6b-27e4ce69ed9d', 'identifier': 'admin'}, 'createdAt': '2024-06-26T13:48:48.542Z'}\n", "{'id': 'f32f2a8e-5edc-4745-954b-a72f6136beb4', 'metadata': {'id': '06140593-f045-4eb8-9920-29e4d3b6aae9', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}, 'tags': None, 'name': 'connect with your partner', 'steps': [], 'participant': {'id': '9ddc5de2-477d-489f-9e6b-27e4ce69ed9d', 'identifier': 'admin'}, 'createdAt': '2024-06-26T13:43:55.823Z'}\n", "{'id': 'eb115f89-5ef1-42f6-993a-f5c0c1ebdb6a', 'metadata': {'id': '4f687bfe-416b-4358-b182-fe1bba6c8d76', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}, 'tags': None, 'name': '1:1 chat', 'steps': [], 'participant': {'id': '9ddc5de2-477d-489f-9e6b-27e4ce69ed9d', 'identifier': 'admin'}, 'createdAt': '2024-06-26T13:42:08.878Z'}\n", "{'id': 'a049b457-218e-4682-a1f7-b584bec539bc', 'metadata': {'id': '84cddea9-3ee8-4e7e-8a58-be5cd609c1ab', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}, 'tags': None, 'name': \"I'm a software engineer, married to 34 year old woman. Her eyes are blue and she works full-time. She always complains about me not doing haushold chores \", 'steps': [], 'participant': {'id': '9ddc5de2-477d-489f-9e6b-27e4ce69ed9d', 'identifier': 'admin'}, 'createdAt': '2024-06-25T19:13:16.320Z'}\n" ] } ], "source": [ "import os\n", "from dotenv import load_dotenv\n", "load_dotenv()\n", "from literalai import LiteralClient\n", "\n", "literal_client = LiteralClient(api_key=os.getenv(\"LITERAL_API_KEY\"))\n", "\n", "threads = literal_client.api.list_threads(first=None, after=None, before=None, order_by=None, filters=None)\n", "\n", "for thread in threads.data:\n", " print(thread.to_dict())" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Automatice thread:)\n", "My first thread !\n", "connect with your partner\n", "1:1 chat\n", "None\n", "connect with your partner\n", "connect with your partner\n", "connect with your partner\n", "1:1 chat\n", "I'm a software engineer, married to 34 year old woman. Her eyes are blue and she works full-time. She always complains about me not doing haushold chores \n" ] } ], "source": [ "for thread in threads.data:\n", " print(thread.to_dict()['name'])" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'id': e1854f6b-d984-4a04-847f-fcd530b81b7c\n", "'name': connect with your partner\n", "{'id': '68ccee8f-4ee8-4974-b009-fa62c37d4d58', 'env': '{}', 'user': None, 'memory': None, 'runnable': None, 'languages': 'en-US,en;q=0.9,es;q=0.8,ar;q=0.7', 'chat_profile': '', 'root_message': None, 'chat_settings': {}}\n", "[]\n" ] } ], "source": [ "id = 2\n", "print(\"'id': \" + threads.data[id].to_dict()['id'])\n", "print(\"'name': \" + threads.data[id].to_dict()['name'])\n", "print(threads.data[id].to_dict()['metadata'])\n", "print(threads.data[id].to_dict()['steps'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'id': '2319abcb-765a-4fab-90f4-d92218cc1f7f',\n", " 'metadata': {'isError': False,\n", " 'language': None,\n", " 'showInput': None,\n", " 'waitForAnswer': False,\n", " 'disableFeedback': False},\n", " 'parentId': None,\n", " 'startTime': '2024-07-10T17:18:14.6',\n", " 'endTime': '2024-07-10T17:18:14.6',\n", " 'type': 'user_message',\n", " 'threadId': 'b6d8486f-a6d9-4731-bfdd-be789613b1df',\n", " 'error': None,\n", " 'input': None,\n", " 'output': {'content': 'What is the problem here Linda!!!'},\n", " 'generation': None,\n", " 'name': 'Tom',\n", " 'tags': None,\n", " 'scores': [],\n", " 'attachments': []},\n", " {'id': '04402299-cb7f-40b4-b1a4-ccae7216fd10',\n", " 'metadata': {'isError': False,\n", " 'language': None,\n", " 'showInput': False,\n", " 'waitForAnswer': None,\n", " 'disableFeedback': True},\n", " 'parentId': '2319abcb-765a-4fab-90f4-d92218cc1f7f',\n", " 'startTime': '2024-07-10T17:18:14.661',\n", " 'endTime': '2024-07-10T17:18:14.689',\n", " 'type': 'run',\n", " 'threadId': 'b6d8486f-a6d9-4731-bfdd-be789613b1df',\n", " 'error': None,\n", " 'input': {'content': '{\\n \"input\": \"\"\\n}'},\n", " 'output': {'content': '[]'},\n", " 'generation': None,\n", " 'name': 'load_memory_variables',\n", " 'tags': None,\n", " 'scores': [],\n", " 'attachments': []},\n", " {'id': '2ced9228-3f70-4d45-9dbe-20d91f695435',\n", " 'metadata': {'isError': False,\n", " 'language': None,\n", " 'showInput': False,\n", " 'waitForAnswer': None,\n", " 'disableFeedback': False},\n", " 'parentId': '2319abcb-765a-4fab-90f4-d92218cc1f7f',\n", " 'startTime': '2024-07-10T17:18:14.714',\n", " 'endTime': '2024-07-10T17:18:15.66',\n", " 'type': 'llm',\n", " 'threadId': 'b6d8486f-a6d9-4731-bfdd-be789613b1df',\n", " 'error': None,\n", " 'input': {'content': '{\\n \"prompts\": [\\n \"System: \\\\nYou are HarmonyAI, an empathetic and insightful chatbot designed to help individuals resolve relationship issues. \\\\nYour goal is to listen carefully, provide thoughtful and constructive advice, and promote understanding \\\\nand communication between partners.\\\\n\\\\nWhen interacting with users, always:\\\\n Acknowledge their feelings and show empathy.\\\\n Ask clarifying questions to understand their situation better.\\\\n Suggest practical solutions and communication strategies.\\\\n Encourage appreciation and mutual respect.\\\\n Avoid taking sides or placing blame.\\\\n Keep your responses concise and natural, similar to a friendly conversation. \\\\n Aim for responses to be around 2-4 sentences long, providing clear and actionable advice without overwhelming the user.\\\\n\\\\nHere’s an example of how to structure your responses:\\\\n Greet the user and ask how you can help.\\\\n Listen to their concern and acknowledge their feelings.\\\\n Ask for more details to fully understand the issue.\\\\n Provide practical advice and encourage positive communication.\\\\n Conclude with a supportive message and an offer to help further if needed.\\\\n\\\\nAlways maintain a supportive and non-judgmental tone.\\\\nHuman: What is the problem here Linda!!!\"\\n ]\\n}'},\n", " 'output': {'content': '{\\n \"role\": \"assistant\",\\n \"content\": \"It sounds like you\\'re feeling frustrated. I\\'m here to listen. Could you tell me more about what\\'s been happening with Linda that\\'s causing you concern?\"\\n}'},\n", " 'generation': {'promptId': None,\n", " 'provider': 'openai-chat',\n", " 'model': 'gpt-3.5-turbo',\n", " 'error': None,\n", " 'settings': {'n': 1,\n", " 'stop': None,\n", " 'model': 'gpt-3.5-turbo',\n", " 'stream': True,\n", " 'streaming': True,\n", " 'model_name': 'gpt-3.5-turbo',\n", " 'max_retries': 2,\n", " 'temperature': 0.7,\n", " 'openai_proxy': ''},\n", " 'variables': {'input': ''},\n", " 'tags': None,\n", " 'tools': None,\n", " 'tokenCount': 255,\n", " 'inputTokenCount': 223,\n", " 'outputTokenCount': 32,\n", " 'ttFirstToken': 656.6357612609863,\n", " 'tokenThroughputInSeconds': 35.8559359429227,\n", " 'duration': 0.9482390880584717,\n", " 'messages': [{'role': 'system',\n", " 'content': '\\nYou are HarmonyAI, an empathetic and insightful chatbot designed to help individuals resolve relationship issues. \\nYour goal is to listen carefully, provide thoughtful and constructive advice, and promote understanding \\nand communication between partners.\\n\\nWhen interacting with users, always:\\n Acknowledge their feelings and show empathy.\\n Ask clarifying questions to understand their situation better.\\n Suggest practical solutions and communication strategies.\\n Encourage appreciation and mutual respect.\\n Avoid taking sides or placing blame.\\n Keep your responses concise and natural, similar to a friendly conversation. \\n Aim for responses to be around 2-4 sentences long, providing clear and actionable advice without overwhelming the user.\\n\\nHere’s an example of how to structure your responses:\\n Greet the user and ask how you can help.\\n Listen to their concern and acknowledge their feelings.\\n Ask for more details to fully understand the issue.\\n Provide practical advice and encourage positive communication.\\n Conclude with a supportive message and an offer to help further if needed.\\n\\nAlways maintain a supportive and non-judgmental tone.'},\n", " {'role': 'user', 'content': 'What is the problem here Linda!!!'}],\n", " 'messageCompletion': {'role': 'assistant',\n", " 'content': \"It sounds like you're feeling frustrated. I'm here to listen. Could you tell me more about what's been happening with Linda that's causing you concern?\"},\n", " 'type': 'CHAT'},\n", " 'name': 'ChatOpenAI',\n", " 'tags': None,\n", " 'scores': [],\n", " 'attachments': []},\n", " {'id': 'e4060a6c-d4ba-4e40-82b7-9bd6256f72a2',\n", " 'metadata': {'isError': False,\n", " 'language': None,\n", " 'showInput': None,\n", " 'waitForAnswer': False,\n", " 'disableFeedback': False},\n", " 'parentId': None,\n", " 'startTime': '2024-07-10T17:18:15.692',\n", " 'endTime': '2024-07-10T17:18:15.692',\n", " 'type': 'assistant_message',\n", " 'threadId': 'b6d8486f-a6d9-4731-bfdd-be789613b1df',\n", " 'error': None,\n", " 'input': None,\n", " 'output': {'content': \"It sounds like you're feeling frustrated. I'm here to listen. Could you tell me more about what's been happening with Linda that's causing you concern?\"},\n", " 'generation': None,\n", " 'name': 'Assistant',\n", " 'tags': None,\n", " 'scores': [],\n", " 'attachments': []},\n", " {'id': '53406e4d-e43c-4e75-9d84-09fca024a4f7',\n", " 'metadata': {'isError': False,\n", " 'language': None,\n", " 'showInput': None,\n", " 'waitForAnswer': False,\n", " 'disableFeedback': False},\n", " 'parentId': None,\n", " 'startTime': '2024-07-10T17:19:28.173',\n", " 'endTime': '2024-07-10T17:19:28.173',\n", " 'type': 'user_message',\n", " 'threadId': 'b6d8486f-a6d9-4731-bfdd-be789613b1df',\n", " 'error': None,\n", " 'input': None,\n", " 'output': {'content': 'she is complaining about household chores as usual'},\n", " 'generation': None,\n", " 'name': 'Tom',\n", " 'tags': None,\n", " 'scores': [],\n", " 'attachments': []},\n", " {'id': '5ad97a2f-ffb1-43ea-bae4-7fa4ea29767e',\n", " 'metadata': {'isError': False,\n", " 'language': 'text',\n", " 'showInput': False,\n", " 'waitForAnswer': None,\n", " 'disableFeedback': True},\n", " 'parentId': '53406e4d-e43c-4e75-9d84-09fca024a4f7',\n", " 'startTime': '2024-07-10T17:19:28.229',\n", " 'endTime': '2024-07-10T17:19:28.255',\n", " 'type': 'run',\n", " 'threadId': 'b6d8486f-a6d9-4731-bfdd-be789613b1df',\n", " 'error': None,\n", " 'input': {'content': '{\\n \"input\": \"\"\\n}'},\n", " 'output': {'content': '[HumanMessage(content=\\'What is the problem here Linda!!!\\'), AIMessage(content=\"It sounds like you\\'re feeling frustrated. I\\'m here to listen. Could you tell me more about what\\'s been happening with Linda that\\'s causing you concern?\")]'},\n", " 'generation': None,\n", " 'name': 'load_memory_variables',\n", " 'tags': None,\n", " 'scores': [],\n", " 'attachments': []},\n", " {'id': '6735b7e6-e7b4-4a9c-8435-cf462a554d4c',\n", " 'metadata': {'isError': False,\n", " 'language': None,\n", " 'showInput': False,\n", " 'waitForAnswer': None,\n", " 'disableFeedback': False},\n", " 'parentId': '53406e4d-e43c-4e75-9d84-09fca024a4f7',\n", " 'startTime': '2024-07-10T17:19:28.281',\n", " 'endTime': '2024-07-10T17:19:29.302',\n", " 'type': 'llm',\n", " 'threadId': 'b6d8486f-a6d9-4731-bfdd-be789613b1df',\n", " 'error': None,\n", " 'input': {'content': '{\\n \"prompts\": [\\n \"System: \\\\nYou are HarmonyAI, an empathetic and insightful chatbot designed to help individuals resolve relationship issues. \\\\nYour goal is to listen carefully, provide thoughtful and constructive advice, and promote understanding \\\\nand communication between partners.\\\\n\\\\nWhen interacting with users, always:\\\\n Acknowledge their feelings and show empathy.\\\\n Ask clarifying questions to understand their situation better.\\\\n Suggest practical solutions and communication strategies.\\\\n Encourage appreciation and mutual respect.\\\\n Avoid taking sides or placing blame.\\\\n Keep your responses concise and natural, similar to a friendly conversation. \\\\n Aim for responses to be around 2-4 sentences long, providing clear and actionable advice without overwhelming the user.\\\\n\\\\nHere’s an example of how to structure your responses:\\\\n Greet the user and ask how you can help.\\\\n Listen to their concern and acknowledge their feelings.\\\\n Ask for more details to fully understand the issue.\\\\n Provide practical advice and encourage positive communication.\\\\n Conclude with a supportive message and an offer to help further if needed.\\\\n\\\\nAlways maintain a supportive and non-judgmental tone.\\\\nHuman: What is the problem here Linda!!!\\\\nAI: It sounds like you\\'re feeling frustrated. I\\'m here to listen. Could you tell me more about what\\'s been happening with Linda that\\'s causing you concern?\\\\nHuman: she is complaining about household chores as usual\"\\n ]\\n}'},\n", " 'output': {'content': '{\\n \"role\": \"assistant\",\\n \"content\": \"I understand how that could be stressful. Have you both discussed how to divide household chores fairly? It might help to have a calm conversation to understand each other\\'s perspectives and find a solution that works for both of you.\"\\n}'},\n", " 'generation': {'promptId': None,\n", " 'provider': 'openai-chat',\n", " 'model': 'gpt-3.5-turbo',\n", " 'error': None,\n", " 'settings': {'n': 1,\n", " 'stop': None,\n", " 'model': 'gpt-3.5-turbo',\n", " 'stream': True,\n", " 'streaming': True,\n", " 'model_name': 'gpt-3.5-turbo',\n", " 'max_retries': 2,\n", " 'temperature': 0.7,\n", " 'openai_proxy': ''},\n", " 'variables': {'input': ''},\n", " 'tags': None,\n", " 'tools': None,\n", " 'tokenCount': 307,\n", " 'inputTokenCount': 263,\n", " 'outputTokenCount': 44,\n", " 'ttFirstToken': 450.09469985961914,\n", " 'tokenThroughputInSeconds': 45.01046398075452,\n", " 'duration': 1.021984577178955,\n", " 'messages': [{'role': 'system',\n", " 'content': '\\nYou are HarmonyAI, an empathetic and insightful chatbot designed to help individuals resolve relationship issues. \\nYour goal is to listen carefully, provide thoughtful and constructive advice, and promote understanding \\nand communication between partners.\\n\\nWhen interacting with users, always:\\n Acknowledge their feelings and show empathy.\\n Ask clarifying questions to understand their situation better.\\n Suggest practical solutions and communication strategies.\\n Encourage appreciation and mutual respect.\\n Avoid taking sides or placing blame.\\n Keep your responses concise and natural, similar to a friendly conversation. \\n Aim for responses to be around 2-4 sentences long, providing clear and actionable advice without overwhelming the user.\\n\\nHere’s an example of how to structure your responses:\\n Greet the user and ask how you can help.\\n Listen to their concern and acknowledge their feelings.\\n Ask for more details to fully understand the issue.\\n Provide practical advice and encourage positive communication.\\n Conclude with a supportive message and an offer to help further if needed.\\n\\nAlways maintain a supportive and non-judgmental tone.'},\n", " {'role': 'user', 'content': 'What is the problem here Linda!!!'},\n", " {'role': 'assistant',\n", " 'content': \"It sounds like you're feeling frustrated. I'm here to listen. Could you tell me more about what's been happening with Linda that's causing you concern?\"},\n", " {'role': 'user',\n", " 'content': 'she is complaining about household chores as usual'}],\n", " 'messageCompletion': {'role': 'assistant',\n", " 'content': \"I understand how that could be stressful. Have you both discussed how to divide household chores fairly? It might help to have a calm conversation to understand each other's perspectives and find a solution that works for both of you.\"},\n", " 'type': 'CHAT'},\n", " 'name': 'ChatOpenAI',\n", " 'tags': None,\n", " 'scores': [],\n", " 'attachments': []},\n", " {'id': 'd5d60e60-a55f-4394-bb41-c6fb13c6bd2f',\n", " 'metadata': {'isError': False,\n", " 'language': None,\n", " 'showInput': None,\n", " 'waitForAnswer': False,\n", " 'disableFeedback': False},\n", " 'parentId': None,\n", " 'startTime': '2024-07-10T17:19:29.321',\n", " 'endTime': '2024-07-10T17:19:29.321',\n", " 'type': 'assistant_message',\n", " 'threadId': 'b6d8486f-a6d9-4731-bfdd-be789613b1df',\n", " 'error': None,\n", " 'input': None,\n", " 'output': {'content': \"I understand how that could be stressful. Have you both discussed how to divide household chores fairly? It might help to have a calm conversation to understand each other's perspectives and find a solution that works for both of you.\"},\n", " 'generation': None,\n", " 'name': 'Assistant',\n", " 'tags': None,\n", " 'scores': [],\n", " 'attachments': []}]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "from dotenv import load_dotenv\n", "load_dotenv()\n", "from literal_thread_manager import LiteralThreadManager\n", "\n", "literal_manager = LiteralThreadManager(api_key=os.getenv(\"LITERAL_API_KEY\"))\n", "\n", "thread_id = 'b6d8486f-a6d9-4731-bfdd-be789613b1df'\n", "\n", "thread = literal_manager.filter_thread_by_id(thread_id)\n", "user_name = literal_manager.get_user_name_from_thread(thread)\n", "literal_manager.get_messages_from_thread(thread)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Chat history for thread ID b6d8486f-a6d9-4731-bfdd-be789613b1df:\n", "user - Tom: What is the problem here Linda!!!\n", "assistant - HarmonyAI: It sounds like you're feeling frustrated. I'm here to listen. Could you tell me more about what's been happening with Linda that's causing you concern?\n", "user - Tom: she is complaining about household chores as usual\n", "assistant - HarmonyAI: I understand how that could be stressful. Have you both discussed how to divide household chores fairly? It might help to have a calm conversation to understand each other's perspectives and find a solution that works for both of you.\n" ] } ], "source": [ "chat = literal_manager.extract_chat_history_from_thread(thread)\n", "print(f\"\\nChat history for thread ID {thread_id}:\")\n", "for message in chat:\n", " print(f\"{message['role']} - {message['name']}: {message['content']}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Conflict resolution" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from openai import OpenAI\n", "\n", "class ResolutionLogic:\n", " def __init__(self, thread_manager: LiteralThreadManager):\n", " self.thread_manager = thread_manager\n", "\n", " def resolve_conflict(self, thread_id):\n", "\n", " # get other partner thread_id\n", " other_partner_thread_id = self.thread_manager.get_other_partner_thread_id(thread_id)\n", " # get both thread\n", " thread_content = self.thread_manager.filter_thread_by_id(thread_id)\n", " other_partner_thread_content = self.thread_manager.filter_thread_by_id(other_partner_thread_id)\n", " # extract chat history from both threads\n", " chat_history_partner1 = self.thread_manager.extract_chat_history_from_thread(thread_content)\n", " chat_history_partner2 = self.thread_manager.extract_chat_history_from_thread(other_partner_thread_content)\n", "\n", " combined_chat = chat_history_partner1 + chat_history_partner2\n", "\n", " print(f\"Combined chat: {combined_chat}\")\n", "\n", " client = OpenAI(api_key=os.getenv(\"OPENAI_API_KEY\"))\n", "\n", " completion = client.chat.completions.create(\n", " model=\"gpt-3.5-turbo\",\n", " messages=[\n", " {\"role\": \"system\", \"content\": \"\"\"\n", " You are an expert relationship counselor. \n", " The user is providing various perspectives on a conflict within a relationship. \n", " Your task is to analyze these perspectives and offer thoughtful, empathetic, and actionable advice \n", " to help resolve the conflict. Consider the emotions and viewpoints of all parties involved and \n", " suggest a resolution that promotes understanding, communication, and mutual respect.\n", " \n", " Please adress them both with their names, to make it more personal. \n", " Try to avoid leaking information you got in the prompt.\n", " \"\"\"},\n", " *combined_chat\n", " ]\n", " )\n", " return completion.choices[0].message.content" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Combined chat: [{'role': 'user', 'name': 'Tom', 'content': 'What is the problem here Linda!!!'}, {'role': 'assistant', 'name': 'HarmonyAI', 'content': \"It sounds like you're feeling frustrated. I'm here to listen. Could you tell me more about what's been happening with Linda that's causing you concern?\"}, {'role': 'user', 'name': 'Tom', 'content': 'she is complaining about household chores as usual'}, {'role': 'assistant', 'name': 'HarmonyAI', 'content': \"I understand how that could be stressful. Have you both discussed how to divide household chores fairly? It might help to have a calm conversation to understand each other's perspectives and find a solution that works for both of you.\"}, {'role': 'user', 'name': 'linda', 'content': 'I have a problem with my partner Tom. he is not helping me with the house hold chores'}, {'role': 'assistant', 'name': 'HarmonyAI', 'content': \"I'm sorry to hear that you're struggling with this issue. Have you spoken to Tom about how you're feeling and the importance of sharing the household responsibilities? What are his thoughts on the matter?\"}, {'role': 'user', 'name': 'linda', 'content': \"Yes, we I always mention that I'm the only one in this house washing the dishes and cleaning the floor. He replies by simply saying that he is super busy and it's normal for me to do these tasks \"}, {'role': 'assistant', 'name': 'HarmonyAI', 'content': \"I understand how frustrating that must be for you. It's important to have an open and honest conversation with Tom about how you both can divide the household chores more fairly. Maybe you can create a shared chore schedule that accommodates both of your schedules. It's essential to communicate your needs and listen to his perspective as well.\"}]\n" ] }, { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'dict' and 'list'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m/Users/mousamaxod/Documents/TUM/EmpatheticChatBot/empathetic_text_generation/ChattingBotMechanism/chainlit/literalai.ipynb Cell 13\u001b[0m line \u001b[0;36m5\n\u001b[1;32m 1\u001b[0m resolution_logic \u001b[39m=\u001b[39m ResolutionLogic(literal_manager)\n\u001b[1;32m 3\u001b[0m thread_id \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39mb6d8486f-a6d9-4731-bfdd-be789613b1df\u001b[39m\u001b[39m'\u001b[39m\n\u001b[0;32m----> 5\u001b[0m result \u001b[39m=\u001b[39m resolution_logic\u001b[39m.\u001b[39mresolve_conflict(thread_id)\n", "\u001b[1;32m/Users/mousamaxod/Documents/TUM/EmpatheticChatBot/empathetic_text_generation/ChattingBotMechanism/chainlit/literalai.ipynb Cell 13\u001b[0m line \u001b[0;36m2\n\u001b[1;32m 20\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mCombined chat: \u001b[39m\u001b[39m{\u001b[39;00mcombined_chat\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[1;32m 22\u001b[0m client \u001b[39m=\u001b[39m OpenAI(api_key\u001b[39m=\u001b[39mos\u001b[39m.\u001b[39mgetenv(\u001b[39m\"\u001b[39m\u001b[39mOPENAI_API_KEY\u001b[39m\u001b[39m\"\u001b[39m))\n\u001b[1;32m 24\u001b[0m completion \u001b[39m=\u001b[39m client\u001b[39m.\u001b[39mchat\u001b[39m.\u001b[39mcompletions\u001b[39m.\u001b[39mcreate(\n\u001b[1;32m 25\u001b[0m model\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mgpt-3.5-turbo\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m 26\u001b[0m messages\u001b[39m=\u001b[39m[\n\u001b[0;32m---> 27\u001b[0m {\u001b[39m\"\u001b[39m\u001b[39mrole\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39msystem\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39mcontent\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\"\"\u001b[39m\n\u001b[1;32m 28\u001b[0m \u001b[39m You are an expert relationship counselor. \u001b[39m\n\u001b[1;32m 29\u001b[0m \u001b[39m The user is providing various perspectives on a conflict within a relationship. \u001b[39m\n\u001b[1;32m 30\u001b[0m \u001b[39m Your task is to analyze these perspectives and offer thoughtful, empathetic, and actionable advice \u001b[39m\n\u001b[1;32m 31\u001b[0m \u001b[39m to help resolve the conflict. Consider the emotions and viewpoints of all parties involved and \u001b[39m\n\u001b[1;32m 32\u001b[0m \u001b[39m suggest a resolution that promotes understanding, communication, and mutual respect.\u001b[39m\n\u001b[1;32m 33\u001b[0m \u001b[39m \u001b[39m\n\u001b[1;32m 34\u001b[0m \u001b[39m Please adress them both with their names, to make it more personal. \u001b[39m\n\u001b[1;32m 35\u001b[0m \u001b[39m Try to avoid leaking information you got in the prompt.\u001b[39m\n\u001b[1;32m 36\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"\u001b[39m} \n\u001b[1;32m 37\u001b[0m \u001b[39m+\u001b[39m\n\u001b[1;32m 38\u001b[0m combined_chat\n\u001b[1;32m 39\u001b[0m ]\n\u001b[1;32m 40\u001b[0m )\n\u001b[1;32m 41\u001b[0m \u001b[39mreturn\u001b[39;00m completion\u001b[39m.\u001b[39mchoices[\u001b[39m0\u001b[39m]\u001b[39m.\u001b[39mmessage\u001b[39m.\u001b[39mcontent\n", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'dict' and 'list'" ] } ], "source": [ "resolution_logic = ResolutionLogic(literal_manager)\n", "\n", "thread_id = 'b6d8486f-a6d9-4731-bfdd-be789613b1df'\n", "\n", "result = resolution_logic.resolve_conflict(thread_id)" ] } ], "metadata": { "kernelspec": { "display_name": "NLP", "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.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }