File size: 9,761 Bytes
3e595a5
 
 
 
e4cc30b
3e595a5
 
 
 
 
 
 
 
 
 
 
afe1590
3e595a5
 
7828fde
 
3e595a5
 
b487b99
3e595a5
 
 
 
 
 
 
95243ee
 
 
3e595a5
 
 
 
e4cc30b
3e595a5
 
 
 
 
 
 
 
 
 
 
afe1590
3e595a5
 
b487b99
3e595a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b487b99
3e595a5
e4cc30b
3e595a5
 
 
 
e4cc30b
3e595a5
 
 
 
 
 
 
 
 
 
 
e4cc30b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e595a5
b487b99
 
 
3e595a5
 
95243ee
 
 
 
 
3e595a5
18793f5
 
3e595a5
 
 
 
 
 
b9a49e3
3e595a5
 
 
b9a49e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e595a5
c2d68af
3e595a5
afe1590
 
 
 
 
 
 
3e595a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afe1590
3e595a5
 
 
 
 
 
 
 
 
 
 
 
 
afe1590
3e595a5
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-01-29T20:09:11.440091Z",
     "iopub.status.busy": "2025-01-29T20:09:11.439766Z",
     "iopub.status.idle": "2025-01-29T20:09:11.751153Z",
     "shell.execute_reply": "2025-01-29T20:09:11.750263Z",
     "shell.execute_reply.started": "2025-01-29T20:09:11.440060Z"
    },
    "id": "xaiioUQni_ga",
    "trusted": true
   },
   "outputs": [],
   "source": [
    "from modules.data_class import DataState\n",
    "from modules.tools import data_node\n",
    "from modules.nodes import chatbot_with_tools, human_node, maybe_exit_human_node, maybe_route_to_tools\n",
    "\n",
    "from langgraph.graph import StateGraph, START, END\n",
    "from langgraph.checkpoint.memory import MemorySaver\n",
    "\n",
    "from IPython.display import Image, display\n",
    "from pprint import pprint\n",
    "from typing import Literal\n",
    "\n",
    "from langgraph.prebuilt import ToolNode\n",
    "\n",
    "from collections.abc import Iterable\n",
    "from IPython.display import display, clear_output\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-01-29T20:09:11.906458Z",
     "iopub.status.busy": "2025-01-29T20:09:11.905241Z",
     "iopub.status.idle": "2025-01-29T20:09:11.994921Z",
     "shell.execute_reply": "2025-01-29T20:09:11.993761Z",
     "shell.execute_reply.started": "2025-01-29T20:09:11.906419Z"
    },
    "id": "9rqkQzlZxrzp",
    "trusted": true
   },
   "outputs": [],
   "source": [
    "graph_builder = StateGraph(DataState)\n",
    "memory = MemorySaver()\n",
    "\n",
    "# Nodes\n",
    "graph_builder.add_node(\"chatbot_healthassistant\", chatbot_with_tools)\n",
    "graph_builder.add_node(\"patient\", human_node)\n",
    "graph_builder.add_node(\"documenting\", data_node)\n",
    "\n",
    "# Chatbot -> {ordering, tools, human, END}\n",
    "graph_builder.add_conditional_edges(\"chatbot_healthassistant\", maybe_route_to_tools)\n",
    "# Human -> {chatbot, END}\n",
    "graph_builder.add_conditional_edges(\"patient\", maybe_exit_human_node)\n",
    "# TestCase_Paintrek\n",
    "# Tools (both kinds) always route back to chat afterwards.\n",
    "graph_builder.add_edge(\"documenting\", \"chatbot_healthassistant\")\n",
    "\n",
    "graph_builder.add_edge(START, \"chatbot_healthassistant\")\n",
    "graph_with_order_tools = graph_builder.compile(checkpointer=memory)\n",
    "\n",
    "# Image(graph_with_order_tools.get_graph().draw_mermaid_png())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-01-29T20:09:38.185616Z",
     "iopub.status.busy": "2025-01-29T20:09:38.185131Z",
     "iopub.status.idle": "2025-01-29T20:10:08.474591Z",
     "shell.execute_reply": "2025-01-29T20:10:08.472926Z",
     "shell.execute_reply.started": "2025-01-29T20:09:38.185577Z"
    },
    "id": "NCRSgaBUfIHF",
    "trusted": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Executing the chatbot graph...\n",
      "Model: Welcome to the Paintrek world. I am a health assistant, an interactive clinical recording system. I will ask you questions about your pain and related symptoms and record your responses.  I will then store this information securely. At any time, you can type `q` to quit.\n",
      "User: Let's start\n",
      "Model: Great! Let’s begin by gathering some basic information. Could you please provide your full name, date of birth, and contact number? This will help us proceed with the medical record setup.\n",
      "User: My name is Frank my DOB is 19860214, my contact number is 123456\n",
      "Model: Thank you for providing your information, Frank. Could you please confirm your full date of birth and provide any additional contact details such as an email address or another phone number? Also, could you tell me about your emergency contacts if applicable?\n",
      "If you're ready to proceed with the rest of the intake process now, let me know!\n",
      "User: my date of birth is Feb 14 1986, emergency contact is Zoe and her phone number is 568790\n",
      "Your input data:\n",
      "{'ID': {'name': '', 'DOB': datetime.date(1900, 1, 1), 'gender': '', 'contact': '', 'emergency_contact': ''}, 'symptom': {'main_symptom': '', 'symptom_length': ''}, 'pain': {'pain_location': '', 'pain_side': '', 'pain_intensity': 0, 'pain_description': '', 'start_time': datetime.date(1900, 1, 1), 'radiation': False, 'triggers': '', 'symptom': ''}, 'medical_hist': {'medical_condition': '', 'first_time': datetime.date(1900, 1, 1), 'surgery_history': '', 'medication': '', 'allergy': ''}, 'family_hist': {'family_history': ''}, 'social_hist': {'occupation': '', 'smoke': False, 'alcohol': False, 'drug': False, 'support_system': '', 'living_condition': ''}, 'review_system': {'weight_change': '', 'fever': False, 'chill': False, 'night_sweats': False, 'sleep': '', 'gastrointestinal': '', 'urinary': ''}, 'pain_manage': {'pain_medication': '', 'specialist': False, 'other_therapy': '', 'effectiveness': False}, 'functional': {'life_quality': '', 'limit_activity': '', 'mood': ''}, 'plan': {'goal': '', 'expectation': '', 'alternative_treatment': ''}}\n",
      "Model: Let's start by gathering your personal information and medical history. Could you please provide me with the following details:\n",
      "\n",
      "1. Your full name, date of birth, and contact information.\n",
      "2. The reason for your visit today (symptoms or concerns).\n",
      "3. Any current pain issues you are experiencing.\n",
      "\n",
      "Once I have this initial information, we can proceed to gather more detailed medical history and other relevant data. Please start with the first two points if possible. For the third point, could you describe any areas where you're feeling pain, how long it has been going on, and what makes it better or worse?\n",
      "User: q\n"
     ]
    }
   ],
   "source": [
    "# This is for the checkpointer to have a thread id to remember for each user, and I don't know if letter mix with number will work here \n",
    "# The default recursion limit for traversing nodes is 25 - setting it higher means you can try a more complex order with multiple steps and round-trips.\n",
    "config = {\"configurable\": {\"thread_id\": \"1\"}, \"recursion_limit\": 1000}\n",
    "\n",
    "# Uncomment this line to execute the graph:\n",
    "# Clear output before running new states\n",
    "clear_output(wait=True)\n",
    "\n",
    "# Ensure messages print immediately\n",
    "print(\"Executing the chatbot graph...\", flush=True)\n",
    "state = graph_with_order_tools.invoke({\"messages\": []}, config)\n",
    "# display(state)  # Ensures state is shown in Jupyter\n",
    "# sys.stdout.flush()\n",
    "\n",
    "# pprint(state)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "trusted": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ID': {'name': '',\n",
       "  'DOB': datetime.date(1900, 1, 1),\n",
       "  'gender': '',\n",
       "  'contact': '',\n",
       "  'emergency_contact': ''},\n",
       " 'symptom': {'main_symptom': '', 'symptom_length': ''},\n",
       " 'pain': {'pain_location': '',\n",
       "  'pain_side': '',\n",
       "  'pain_intensity': 0,\n",
       "  'pain_description': '',\n",
       "  'start_time': datetime.date(1900, 1, 1),\n",
       "  'radiation': False,\n",
       "  'triggers': '',\n",
       "  'symptom': ''},\n",
       " 'medical_hist': {'medical_condition': '',\n",
       "  'first_time': datetime.date(1900, 1, 1),\n",
       "  'surgery_history': '',\n",
       "  'medication': '',\n",
       "  'allergy': ''},\n",
       " 'family_hist': {'family_history': ''},\n",
       " 'social_hist': {'occupation': '',\n",
       "  'smoke': False,\n",
       "  'alcohol': False,\n",
       "  'drug': False,\n",
       "  'support_system': '',\n",
       "  'living_condition': ''},\n",
       " 'review_system': {'weight_change': '',\n",
       "  'fever': False,\n",
       "  'chill': False,\n",
       "  'night_sweats': False,\n",
       "  'sleep': '',\n",
       "  'gastrointestinal': '',\n",
       "  'urinary': ''},\n",
       " 'pain_manage': {'pain_medication': '',\n",
       "  'specialist': False,\n",
       "  'other_therapy': '',\n",
       "  'effectiveness': False},\n",
       " 'functional': {'life_quality': '', 'limit_activity': '', 'mood': ''},\n",
       " 'plan': {'goal': '', 'expectation': '', 'alternative_treatment': ''}}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state[\"data\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "name": "day-3-building-an-agent-with-langgraph.ipynb",
   "toc_visible": true
  },
  "kaggle": {
   "accelerator": "none",
   "dataSources": [],
   "dockerImageVersionId": 30786,
   "isGpuEnabled": false,
   "isInternetEnabled": true,
   "language": "python",
   "sourceType": "notebook"
  },
  "kernelspec": {
   "display_name": "paintrek",
   "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.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}