File size: 30,880 Bytes
43139c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The autoreload extension is already loaded. To reload it, use:\n",
      "  %reload_ext autoreload\n"
     ]
    }
   ],
   "source": [
    "from dotenv import load_dotenv\n",
    "from typing import Annotated, List, Tuple\n",
    "from typing_extensions import TypedDict\n",
    "from langchain.tools import tool, BaseTool\n",
    "from langchain.schema import Document\n",
    "from langgraph.graph import StateGraph, START, END, MessagesState\n",
    "from langgraph.graph.message import add_messages\n",
    "from langgraph.prebuilt import ToolNode, tools_condition\n",
    "from langgraph.checkpoint.memory import MemorySaver\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate\n",
    "from langchain.schema import SystemMessage, HumanMessage, AIMessage\n",
    "from langchain.retrievers.multi_query import MultiQueryRetriever\n",
    "import gradio as gr\n",
    "\n",
    "from IPython.display import Image, display\n",
    "import sys\n",
    "import os\n",
    "import uuid\n",
    "\n",
    "\n",
    "load_dotenv('/Users/nadaa/Documents/code/py_innovations/srf_chatbot_v2/.env')\n",
    "\n",
    "sys.path.append(os.path.abspath('..'))\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import src.utils.qdrant_manager as qm\n",
    "import prompts.system_prompts as sp\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ToolManager:\n",
    "    def __init__(self, collection_name=\"openai_large_chunks_1500char\"):\n",
    "        self.tools = []\n",
    "        self.qdrant = qm.QdrantManager(collection_name=collection_name)\n",
    "        self.vectorstore = self.qdrant.get_vectorstore()\n",
    "        self.add_vector_search_tool()\n",
    "\n",
    "    def get_tools(self):\n",
    "        return self.tools\n",
    "\n",
    "    def add_vector_search_tool(self):\n",
    "        @tool\n",
    "        def vector_search(query: str, k: int = 5) -> list[Document]:\n",
    "            \"\"\"Useful for simple queries. This tool will search a vector database for passages from the teachings of Paramhansa Yogananda and other publications from the Self Realization Fellowship (SRF).\n",
    "            The user has the option to specify the number of passages they want the search to return, otherwise the number of passages will be set to the default value.\"\"\"\n",
    "            retriever = self.vectorstore.as_retriever(search_kwargs={\"k\": k})\n",
    "            documents = retriever.invoke(query)\n",
    "            return documents\n",
    "        \n",
    "        \n",
    "        @tool\n",
    "        def multiple_query_vector_search(query: str, k: int = 5) -> list[Document]:\n",
    "            \"\"\"Useful when the user's query is vague, complex, or involves multiple concepts. \n",
    "            This tool will write multiple versions of the user's query and search the vector database for relevant passages. \n",
    "            Returns a list of relevant passages.\"\"\"\n",
    "            \n",
    "            llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0.5)\n",
    "            retriever_from_llm = MultiQueryRetriever.from_llm(retriever=self.vectorstore.as_retriever(), llm=llm)\n",
    "            documents = retriever_from_llm.invoke(query)\n",
    "            return documents\n",
    "        \n",
    "        \n",
    "        self.tools.append(vector_search)\n",
    "        self.tools.append(multiple_query_vector_search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# class ChatbotState(TypedDict):\n",
    "#     # Messages have the type \"list\". The `add_messages` function defines how this state key should be updated\n",
    "#     # (in this case, it appends messages to the list, rather than overwriting them)\n",
    "#     messages: Annotated[list, add_messages]\n",
    "\n",
    "\n",
    "class SRFChatbot:\n",
    "    def __init__(\n",
    "        self,\n",
    "        chatbot_instructions: str,\n",
    "        model: str = 'gpt-4o-mini',\n",
    "        temperature: float = 0,\n",
    "    ):\n",
    "        # Initialize the LLM and the system message\n",
    "        self.llm = ChatOpenAI(model=model, temperature=temperature)\n",
    "        self.system_message = SystemMessage(content=chatbot_instructions)\n",
    "        self.tools = ToolManager().get_tools()\n",
    "        self.llm_with_tools = self.llm.bind_tools(self.tools)\n",
    "\n",
    "        # Build the graph\n",
    "        self.graph = self.build_graph()\n",
    "        # Get the configurable\n",
    "        self.config = self.get_configurable()\n",
    "    \n",
    "\n",
    "    def get_configurable(self):\n",
    "        self.thread_id = str(uuid.uuid4())\n",
    "        return {\"configurable\": {\"thread_id\": self.thread_id}}\n",
    "        \n",
    "    # Add the system message onto the llm\n",
    "    def chatbot(self, state: MessagesState):\n",
    "        messages = [self.system_message] + state[\"messages\"]\n",
    "        return {\"messages\": [self.llm_with_tools.invoke(messages)]}\n",
    "    \n",
    "    def build_graph(self):\n",
    "\n",
    "        # Add chatbot state\n",
    "        graph_builder = StateGraph(MessagesState)\n",
    "\n",
    "        # Add nodes\n",
    "        tool_node = ToolNode(self.tools)\n",
    "        graph_builder.add_node(\"tools\", tool_node)\n",
    "        graph_builder.add_node(\"chatbot\", self.chatbot)\n",
    "\n",
    "        # Add a conditional edge wherein the chatbot can decide whether or not to go to the tools\n",
    "        graph_builder.add_conditional_edges(\n",
    "            \"chatbot\",\n",
    "            tools_condition,\n",
    "        )\n",
    "\n",
    "        # Add fixed edges\n",
    "        graph_builder.add_edge(START, \"chatbot\")\n",
    "        graph_builder.add_edge(\"tools\", \"chatbot\")\n",
    "\n",
    "        # Instantiate the memory saver\n",
    "        memory = MemorySaver()\n",
    "\n",
    "        # Compile the graph\n",
    "        return graph_builder.compile(checkpointer=memory)\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "chatbot_instructions = sp.system_prompt_templates['Open-Ended Bot']\n",
    "chatbot = SRFChatbot(chatbot_instructions=chatbot_instructions)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"I am having a lot of trouble sleeping. What can I do?\"\n",
    "results = chatbot.graph.invoke({\"messages\": [HumanMessage(content=query)]}, chatbot.config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='I am having a lot of trouble sleeping. What can I do?', additional_kwargs={}, response_metadata={}, id='49aec330-967f-48ec-a14c-3e2f351281b1'),\n",
       " AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_9mRC9uFfiJ3E7F1wmYuR7fSS', 'function': {'arguments': '{\"query\":\"trouble sleeping\"}', 'name': 'vector_search'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 16, 'prompt_tokens': 249, 'total_tokens': 265, 'completion_tokens_details': {'reasoning_tokens': 0}}, 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_e9627b5346', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-868473e1-e6e9-49be-9c87-973a9c9289dd-0', tool_calls=[{'name': 'vector_search', 'args': {'query': 'trouble sleeping'}, 'id': 'call_9mRC9uFfiJ3E7F1wmYuR7fSS', 'type': 'tool_call'}], usage_metadata={'input_tokens': 249, 'output_tokens': 16, 'total_tokens': 265}),\n",
       " ToolMessage(content=\"[Document(metadata={'chapter_name': 'The Nervous System Connects You to the World and to God', 'split_id_sequential': 606, 'split_id_uuid': '21278b52-b4c0-4c0a-8355-a31448c7ce72', 'publication_name': 'Journey to Self-Realization', '_id': '0a69ff6f-1d13-451c-815a-6db330a19d6e', '_collection_name': 'openai_large_chunks_1500char'}, page_content='Sleep is an unconscious way of turning off life energy from the nerves. You therefore get some rest during slumber, but you do not have the conscious experience of bliss that the superconscious state produces. When you awaken from sleep, you are just the same as you were before sleep. But if you can cross the subconscious realm into the superconscious land of light, you will have the most wonderful experiences, and these produce lasting spiritual changes in the consciousness. The more you can remain in that interiorized state of bliss in meditation, the more you will feel that joy with you all the time, even in the midst of activities.'), Document(metadata={'chapter_number': 12, 'split_id_sequential': 215, 'split_id_uuid': 'fcfcaed4-65da-41c8-a5e7-6e68a5773829', 'chapter_name': 'Chapter 12: Years In My Master’S Hermitage', 'publication_name': 'Autobiography of a Yogi', '_id': '0a4aeeaa-eb45-4f4b-b0af-716b68ac70ed', '_collection_name': 'openai_large_chunks_1500char'}, page_content='As midnight approached, my guru might fall into a doze with the naturalness of a child. There was no fuss about bedding. He often lay down, without even a pillow, on a narrow davenport which was the background for his customary tiger-skin seat. A night-long philosophical discussion was not rare; any disciple could summon it by intensity of interest. I felt no tiredness then, no desire for sleep; Master’s living words were sufficient. “Oh, it is dawn! Let us walk by the Ganges.” So ended many of my periods of nocturnal edification. My early months with Sri Yukteswar culminated in a useful lesson: “How to Outwit a Mosquito.” At home my family always used protective curtains at night. I was dismayed to discover that in the Serampore hermitage this prudent custom was honoured in the breach. Yet the insects were in full residence; I was bitten from head to foot. My guru took pity on me. “Buy yourself a curtain, and also one for me.” He laughed and added, “If you buy only one, for yourself, all mosquitoes will concentrate on me!” I was more than thankful to comply. On every night that I spent in Serampore, my guru would ask me to arrange the bedtime curtains. One night, when a cloud of mosquitoes surrounded us, Master failed to issue his usual instructions. I listened nervously to the anticipatory hum of the insects. Getting into bed, I threw a propitiatory prayer in their general direction. A half hour later, I coughed pretentiously to attract my guru’s attention.'), Document(metadata={'publication_name': 'The Second Coming of Christ', 'split_id_uuid': 'dfffff39-bce8-4cc5-9a8a-383dc14db69a', 'chapter_number': 14, 'chapter_name': 'Discourse 14 - The Ascension Of Man—Lifting Up The Serpent In The Wilderness', 'split_id_sequential': 497, '_id': '73103f5f-8745-4eb5-8b27-7560a7395531', '_collection_name': 'openai_large_chunks_1500char'}, page_content='Why does man feel joy in sleep? Because when he is in the stage of deep, dreamless sleep, unconscious of the body, physical limitations are forgotten and the mind momentarily taps a higher consciousness. The yogi knows the scientific art of withdrawing consciously from his sensory nerves, so that no outer disturbance of sight, sound, touch, taste, or smell can gain entry into the inner sanctum of his peace-saturated meditation. Soldiers posted for days on the front lines are able to fall asleep despite the constant roar of battle, because of the body’s mechanism of unconsciously withdrawing the energy from the ears and other sensory organs. The yogi reasons that this can be done consciously. By knowledge and practice of the definite laws and scientific techniques of concentration, yogis switch off the senses at will—going beyond subconscious slumber into blissful superconscious interiorization. Though the soul is given periods of freedom from body consciousness at regular intervals in its existence—for a few hours each night, and for a longer respite between physical incarnations during the sleep of death—the unenlightened man inevitably finds that his unfulfilled earthly yearnings stir him once again to the consciousness of the body.'), Document(metadata={'split_id_sequential': 471, 'publication_name': 'The Second Coming of Christ', 'split_id_uuid': '4052d4dc-3f3d-4134-9724-1a3ce5b29178', 'chapter_number': 13, 'chapter_name': 'Discourse 13 - The Second Birth Of Man — In Spirit', '_id': 'cf225e5e-6892-41e5-984d-612275968193', '_collection_name': 'openai_large_chunks_1500char'}, page_content='Man remains firmly convinced that he is essentially a body, even though he daily receives proof to the contrary. Every night in sleep, “the little death,” he discards his identification with the physical form and is reborn as invisible consciousness. Why is it that man is compelled to sleep? Because sleep is a reminder of what is beyond the state of sleep—the state of the soul.13 Mortal existence could not be borne without at least subconscious contact with the soul, which is provided by sleep. At nighttime man dumps the body into the subconscious and becomes an angel; in the daytime he becomes once more a devil, divorced from Spirit by the desires and sensations of the body. By Kriya Yoga meditation he can be a god in the daytime, like Christ and the Great Ones. He goes beyond the subconscious to the superconscious, and dissolves the consciousness of the body in the ecstasy of God. One who can do this is born again. He knows his soul as a waft of the invisible wind of Spirit—soaring free in the unbounded heavens, entrapped no longer in a whirling dust devil traipsing heedlessly over the toilsome tracks of matter. This earth is a habitat of trouble and suffering, but the kingdom of God that is behind this material plane is an abode of freedom and bliss. The soul of the awakening man has followed a hard-earned way—many incarnations of upward evolution—in order to arrive at the human state and the possibility to reclaim his lost divinity.'), Document(metadata={'chapter_name': 'Examine Yourself to See What Makes You Nervous', 'publication_name': 'Journey to Self-Realization', 'split_id_sequential': 160, 'split_id_uuid': '0900b6ab-2a48-48db-a66b-2ca0e80f9c70', '_id': 'de7f20d2-d3ac-45b6-8588-1a0b568e2ec9', '_collection_name': 'openai_large_chunks_1500char'}, page_content='Other cars were speeding past us on the steep, winding grade. I thought they were hurrying to get to the mountaintop in time to see the sunrise. To my great amazement, when we arrived we were the only ones outside to enjoy the view. All the others were in the restaurant drinking coffee and eating doughnuts. Imagine! They rushed to the top and then rushed back, just for the thrill of being able to say when they got home that they had been there, and had coffee and doughnuts on Pikes Peak. That is what nervousness does. We should take time to enjoy things—the beauties of God’s creation, the many blessings of life—but avoid undue excitement, restlessness, and sudden emotions, which burn up the nervous system. Talking too much, including the habit of engaging in long conversations on the telephone, creates nervousness. Habitual twitching—such as drumming the fingers or moving the toes—burns energy in the nerves. Another cause of nervousness, though you may not be aware of it, is the noise of the radio or television going on for hours at a time. All sounds cause the nerves to react.1 A study conducted in the police department in Chicago showed that if human beings were not subjected to the bombardment of the sounds of modern living, which are especially harsh in cities, they could live years longer. Learn to enjoy silence; don’t listen to the radio or television for hours on end, or have them blaring mindlessly in the background all the time.')]\", name='vector_search', id='cec3278a-776b-4f4c-a7df-680bc132d50f', tool_call_id='call_9mRC9uFfiJ3E7F1wmYuR7fSS'),\n",
       " AIMessage(content=\"If you're having trouble sleeping, here are some insights from the teachings of Paramhansa Yogananda that may help:\\n\\n1. **Understanding Sleep**: Sleep is described as an unconscious way of turning off life energy from the nerves. While you get some rest during sleep, it does not provide the conscious experience of bliss that meditation can offer. By practicing meditation and striving to reach a superconscious state, you can experience lasting spiritual changes and a deeper sense of joy that can accompany you even during your waking hours.\\n\\n2. **Conscious Withdrawal**: Yogananda explains that the yogi learns to consciously withdraw from sensory nerves, allowing for a peaceful state of meditation. This practice can help you manage disturbances that might affect your sleep. By training yourself to switch off the senses at will, you can go beyond mere subconscious slumber into a blissful state of interiorization.\\n\\n3. **The Nature of Sleep**: Sleep serves as a reminder of the soul's existence beyond the physical body. It allows for a temporary escape from physical limitations. Engaging in practices like Kriya Yoga meditation can help you connect with your soul and experience a sense of freedom and bliss, even during the day.\\n\\n4. **Managing Nervousness**: Nervousness can significantly impact your ability to sleep. Yogananda advises avoiding undue excitement, restlessness, and sudden emotions, as these can burn up the nervous system. Creating a peaceful environment, minimizing noise, and enjoying silence can help calm your nerves and improve your sleep quality.\\n\\n5. **Meditation Practice**: Incorporating meditation into your daily routine can help you achieve a state of calmness and bliss, which may alleviate sleep troubles. The more you can interiorize and experience that bliss during meditation, the more it can positively affect your overall well-being, including your sleep.\\n\\nConsider integrating these practices into your life to help improve your sleep quality.\", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 384, 'prompt_tokens': 2208, 'total_tokens': 2592, 'completion_tokens_details': {'reasoning_tokens': 0}}, 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_1bb46167f9', 'finish_reason': 'stop', 'logprobs': None}, id='run-6e2e6206-fe5a-4ade-8688-d89532b26c60-0', usage_metadata={'input_tokens': 2208, 'output_tokens': 384, 'total_tokens': 2592})]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chatbot.graph.get_state(chatbot.config).values['messages']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "If you're having trouble sleeping, here are some insights from the teachings of Paramhansa Yogananda that may help:\n",
      "\n",
      "1. **Understanding Sleep**: Sleep is described as an unconscious way of turning off life energy from the nerves. While you get some rest during sleep, it does not provide the conscious experience of bliss that meditation can offer. By practicing meditation and striving to reach a superconscious state, you can experience lasting spiritual changes and a deeper sense of joy that can accompany you even during your waking hours.\n",
      "\n",
      "2. **Conscious Withdrawal**: Yogananda explains that the yogi learns to consciously withdraw from sensory nerves, allowing for a peaceful state of meditation. This practice can help you manage disturbances that might affect your sleep. By training yourself to switch off the senses at will, you can go beyond mere subconscious slumber into a blissful state of interiorization.\n",
      "\n",
      "3. **The Nature of Sleep**: Sleep serves as a reminder of the soul's existence beyond the physical body. It allows for a temporary escape from physical limitations. Engaging in practices like Kriya Yoga meditation can help you connect with your soul and experience a sense of freedom and bliss, even during the day.\n",
      "\n",
      "4. **Managing Nervousness**: Nervousness can significantly impact your ability to sleep. Yogananda advises avoiding undue excitement, restlessness, and sudden emotions, as these can burn up the nervous system. Creating a peaceful environment, minimizing noise, and enjoying silence can help calm your nerves and improve your sleep quality.\n",
      "\n",
      "5. **Meditation Practice**: Incorporating meditation into your daily routine can help you achieve a state of calmness and bliss, which may alleviate sleep troubles. The more you can interiorize and experience that bliss during meditation, the more it can positively affect your overall well-being, including your sleep.\n",
      "\n",
      "Consider integrating these practices into your life to help improve your sleep quality.\n"
     ]
    }
   ],
   "source": [
    "print(results['messages'][-1].content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7866\n",
      "Running on public URL: https://94b754ba708e7402d2.gradio.live\n",
      "\n",
      "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"https://94b754ba708e7402d2.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def respond(message, history):\n",
    "    # Format the history and new message into the expected structure\n",
    "    formatted_messages = []\n",
    "    for human, ai in history:\n",
    "        formatted_messages.append(HumanMessage(content=human))\n",
    "        if ai:  # AI might not have responded yet\n",
    "            formatted_messages.append(AIMessage(content=ai))\n",
    "    \n",
    "    # Add the new message\n",
    "    formatted_messages.append(HumanMessage(content=message))\n",
    "    \n",
    "    # Invoke the graph with properly formatted input\n",
    "    result = chatbot.graph.invoke({\"messages\": formatted_messages}, chatbot.config)\n",
    "    \n",
    "    # Extract the assistant's response\n",
    "    response = result[\"messages\"][-1].content\n",
    "    \n",
    "    return response\n",
    "\n",
    "# Create and launch the Gradio interface\n",
    "gradio = gr.ChatInterface(respond)\n",
    "gradio.launch(share=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7869\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7869/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"/Users/nadaa/.local/share/virtualenvs/srf_chatbot_v2-9vTIDuJW/lib/python3.11/site-packages/gradio/queueing.py\", line 536, in process_events\n",
      "    response = await route_utils.call_process_api(\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/Users/nadaa/.local/share/virtualenvs/srf_chatbot_v2-9vTIDuJW/lib/python3.11/site-packages/gradio/route_utils.py\", line 322, in call_process_api\n",
      "    output = await app.get_blocks().process_api(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/Users/nadaa/.local/share/virtualenvs/srf_chatbot_v2-9vTIDuJW/lib/python3.11/site-packages/gradio/blocks.py\", line 1935, in process_api\n",
      "    result = await self.call_function(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/Users/nadaa/.local/share/virtualenvs/srf_chatbot_v2-9vTIDuJW/lib/python3.11/site-packages/gradio/blocks.py\", line 1520, in call_function\n",
      "    prediction = await anyio.to_thread.run_sync(  # type: ignore\n",
      "                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/Users/nadaa/.local/share/virtualenvs/srf_chatbot_v2-9vTIDuJW/lib/python3.11/site-packages/anyio/to_thread.py\", line 56, in run_sync\n",
      "    return await get_async_backend().run_sync_in_worker_thread(\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/Users/nadaa/.local/share/virtualenvs/srf_chatbot_v2-9vTIDuJW/lib/python3.11/site-packages/anyio/_backends/_asyncio.py\", line 2405, in run_sync_in_worker_thread\n",
      "    return await future\n",
      "           ^^^^^^^^^^^^\n",
      "  File \"/Users/nadaa/.local/share/virtualenvs/srf_chatbot_v2-9vTIDuJW/lib/python3.11/site-packages/anyio/_backends/_asyncio.py\", line 914, in run\n",
      "    result = context.run(func, *args)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/Users/nadaa/.local/share/virtualenvs/srf_chatbot_v2-9vTIDuJW/lib/python3.11/site-packages/gradio/utils.py\", line 826, in wrapper\n",
      "    response = f(*args, **kwargs)\n",
      "               ^^^^^^^^^^^^^^^^^^\n",
      "  File \"/var/folders/wm/nll4dzqx2_n5zxq0y2h84cs40000gn/T/ipykernel_49088/3264473872.py\", line 24, in chatbot_conversation\n",
      "    result = respond(user_query, history)\n",
      "                                 ^^^^^^^\n",
      "NameError: name 'history' is not defined\n"
     ]
    }
   ],
   "source": [
    "import gradio as gr\n",
    "\n",
    "# Get the prompt templates\n",
    "prompt_templates = sp.system_prompt_templates\n",
    "\n",
    "# Global variable to hold the current SRFChatbot instance\n",
    "chatbot = SRFChatbot(chatbot_instructions=prompt_templates['Open-Ended Bot'])\n",
    "\n",
    "def initialize_chatbot(prompt_name):\n",
    "    selected_prompt = prompt_templates[prompt_name]\n",
    "    chatbot = SRFChatbot(chatbot_instructions=selected_prompt)\n",
    "    return selected_prompt, []  # Return the prompt text and reset the chat history\n",
    "\n",
    "def chatbot_conversation(chat_history, user_query):\n",
    "    global chatbot\n",
    "    if chatbot is None:\n",
    "        # Initialize with default prompt if not already initialized\n",
    "        default_prompt = list(prompt_templates.keys())[0]\n",
    "        chatbot = SRFChatbot(chatbot_instructions=prompt_templates[default_prompt])\n",
    "    \n",
    "    \n",
    "    # Process the conversation\n",
    "    result = respond(user_query, history)\n",
    "    \n",
    "    # Extract the bot's response\n",
    "    bot_response = result[\"messages\"][-1].content\n",
    "    \n",
    "    # Update the chat history\n",
    "    chat_history.append((user_query, bot_response))\n",
    "    \n",
    "    return chat_history, \"\"\n",
    "\n",
    "# Gradio interface\n",
    "with gr.Blocks() as demo:\n",
    "    gr.Markdown(\"# SRF Chatbot\")\n",
    "    \n",
    "    with gr.Row():\n",
    "        with gr.Column(scale=4):\n",
    "            chatbot_output = gr.Chatbot()\n",
    "            user_input = gr.Textbox(placeholder=\"Type your message here...\")\n",
    "            submit_button = gr.Button(\"Submit\")\n",
    "        \n",
    "        with gr.Column(scale=1):\n",
    "            system_prompt_dropdown = gr.Dropdown(\n",
    "                choices=list(prompt_templates.keys()),\n",
    "                label=\"Select System Prompt\",\n",
    "                value=list(prompt_templates.keys())[0]\n",
    "            )\n",
    "            system_prompt_display = gr.Textbox(\n",
    "                value=prompt_templates[list(prompt_templates.keys())[0]],\n",
    "                label=\"Current System Prompt\",\n",
    "                lines=5,\n",
    "                interactive=False\n",
    "            )\n",
    "    \n",
    "    system_prompt_dropdown.change(\n",
    "        fn=initialize_chatbot,\n",
    "        inputs=[system_prompt_dropdown],\n",
    "        outputs=[system_prompt_display]\n",
    "    )\n",
    "    \n",
    "    submit_button.click(\n",
    "        fn=chatbot_conversation,\n",
    "        inputs=[chatbot_output, user_input],\n",
    "        outputs=[chatbot_output]\n",
    "    )\n",
    "\n",
    "# Initialize the chatbot with the default prompt\n",
    "initialize_chatbot(list(prompt_templates.keys())[0])\n",
    "\n",
    "demo.launch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "srf_chatbot_v2",
   "language": "python",
   "name": "srf_chatbot_v2"
  },
  "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
}