{ "cells": [ { "cell_type": "code", "execution_count": 30, "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": 31, "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": 32, "metadata": {}, "outputs": [], "source": [ "\n", "class SRFChatbot:\n", " def __init__(\n", " self,\n", " chatbot_instructions: str = sp.system_prompt_templates['Open-Ended Bot'],\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", " def reset_system_prompt(self, chatbot_instructions_dropdown: str):\n", " # Get chatbot instructions\n", " chatbot_instructions = sp.system_prompt_templates[chatbot_instructions_dropdown]\n", " # Reset the system prompt\n", " self.system_message = SystemMessage(content=chatbot_instructions)\n", " # Reset the configurable\n", " self.config = self.get_configurable()\n", "\n", " return chatbot_instructions\n", "\n", " def get_configurable(self):\n", " # This thread id is used to keep track of the chatbot's conversation\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": 33, "metadata": {}, "outputs": [], "source": [ "chatbot = SRFChatbot()\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "query = \"Tell me more about reducing nervousness to improve sleep\"\n", "results = chatbot.graph.invoke({\"messages\": [HumanMessage(content=query)]}, chatbot.config)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "To reduce nervousness and improve sleep, it is essential to recognize that much of our nervousness stems from our own reactions and emotional states. Paramhansa Yogananda emphasizes that restlessness and emotional excitement can overload the nervous system, leading to fatigue and anxiety. Engaging in deep meditation can help calm the nerves, as it allows the body to rest and rejuvenate. Additionally, dietary choices play a significant role; consuming calming foods such as whole grains, fruits, and avoiding stimulants like caffeine and alcohol can support a healthier nervous system. Practicing silence and reducing exposure to noise can also alleviate nervousness, creating a more peaceful environment conducive to sleep.\n", "\n", "**Quotes:**\n", "1. \"You always blame other things for making you nervous, but never accuse yourself. Yet it is you who make yourself nervous; ninety-nine percent is your own fault.\" - *Journey to Self-Realization, Probing the Core of Nervousness*\n", "2. \"When you stop overloading the nervous system, as when you are in deep sleep or a state of calmness in meditation, you are not nervous at all.\" - *Journey to Self-Realization, Probing the Core of Nervousness*\n", "3. \"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.\" - *Journey to Self-Realization, Examine Yourself to See What Makes You Nervous*\n", "4. \"The greatest healing of nervousness takes place when we attune our lives to God.\" - *Journey to Self-Realization, Attunement With God: Greatest Cure for Nervousness*\n", "\n", "**Recommended Reading:**\n", "- *Journey to Self-Realization* by Paramhansa Yogananda\n", "\n", "**Follow-up Questions:**\n", "1. What specific techniques can I use in meditation to calm my nerves?\n", "2. Are there particular foods or drinks that are especially beneficial for reducing nervousness?\n", "3. How can I create a more peaceful environment at home to improve my sleep?\n" ] } ], "source": [ "print(results['messages'][-1].content)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[HumanMessage(content='Tell me more about reducing nervousness to improve sleep', additional_kwargs={}, response_metadata={}, id='c1d3f9ba-0e06-4a6a-b38c-3dcd3be78bbf'),\n", " AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_fjyQpF0pjUQZ8gobl2SAozXV', 'function': {'arguments': '{\"query\":\"reducing nervousness improve sleep\"}', 'name': 'vector_search'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 19, 'prompt_tokens': 433, 'total_tokens': 452, 'completion_tokens_details': {'reasoning_tokens': 0}}, 'model_name': 'gpt-4o-mini-2024-07-18', 'system_fingerprint': 'fp_1bb46167f9', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-4a39fe1a-6413-4ba3-8aa3-290dc31a27bb-0', tool_calls=[{'name': 'vector_search', 'args': {'query': 'reducing nervousness improve sleep'}, 'id': 'call_fjyQpF0pjUQZ8gobl2SAozXV', 'type': 'tool_call'}], usage_metadata={'input_tokens': 433, 'output_tokens': 19, 'total_tokens': 452}),\n", " ToolMessage(content=\"[Document(metadata={'chapter_name': 'Probing the Core of Nervousness', 'split_id_sequential': 418, 'split_id_uuid': '5ca2777d-c2a0-4e32-96b7-e43ed71c3aa2', 'publication_name': 'Journey to Self-Realization', '_id': '2151431e-7e07-44b6-8f33-7c8e277993c8', '_collection_name': 'openai_large_chunks_1500char'}, page_content='Probing the Core of Nervousness Self-Realization Fellowship Temple, San Diego, California, June 15, 1947 Everyone has at times been nervous, more or less, without knowing why. I may shake this piece of cloth and say it is nervous, but what is making the cloth move? When I cease moving my hand, the cloth lies limp. You always blame other things for making you nervous, but never accuse yourself. Yet it is you who make yourself nervous; ninety-nine percent is your own fault. Restlessness, emotional excitement, concentrates too much energy in the nerves so that they begin to wear out. After years and years, the adverse effects of that nervousness begin to show. The nerves are very tough—God made them so, because they have to last a lifetime—but it is necessary to give them proper care. When you stop overloading the nervous system, as when you are in deep sleep or a state of calmness in meditation, you are not nervous at all. In meditative ecstasy the nerves become highly rested and rejuvenated.'), Document(metadata={'split_id_sequential': 526, 'publication_name': 'Journey to Self-Realization', 'split_id_uuid': '2cecd1d8-68bf-4831-a6d2-de6028ff5fc5', 'chapter_name': 'The Best Diet for the Nerves', '_id': 'ff7b43a1-b42d-4983-8472-6eaeed695f4b', '_collection_name': 'openai_large_chunks_1500char'}, page_content='A yogic drink that is very good for the nervous system is made by adding crushed rock candy and fresh lime juice to a glass of water. It should be thoroughly mixed and evenly blended so that the taste is equally sweet and sour. I have recommended this to many people with excellent results. Another beneficial practice when you are very nervous is to take a cold bath. I once told this to a newspaperman. He said, “Well, if I did that every time I was nervous, I would have to carry a bathtub with me all the time!” I said, “Not necessary. Take a large piece of ice and rub it all over the body, especially over all the openings of the body. With this yoga technique, you will find that your nerves will become much calmer.”'), Document(metadata={'split_id_sequential': 525, 'publication_name': 'Journey to Self-Realization', 'chapter_name': 'The Best Diet for the Nerves', 'split_id_uuid': '5b366387-d311-486a-bc74-0559cb45d041', '_id': 'e2a33e74-64d8-47f4-9edd-41eebe277585', '_collection_name': 'openai_large_chunks_1500char'}, page_content='The Best Diet for the Nerves Even foods, which also are material condensations of astral rays of life, have effects that are correlated to their colour. Various kinds of natural white foods are good for the nervous system; they benefit the white matter of the brain. Berries are good for the gray matter of the brain—that is, blueberries or blackberries (which are really purple). Most fruits are gold in colour (or variants of gold, such as red and orange). As gold is the colour of the creative vibratory energy in matter, such fruits help the muscles, the blood, and the tissues. Goat’s milk, unbleached almonds, and raisins are very good for the nervous system. But all forms of meat of higher animals, especially beef and pork, are harmful to the nervous system; they are hyperstimulating and cause aggressiveness. Avoid too much starch, especially foods made from refined flour. Eat whole grains, cottage cheese, and plenty of fruits, fruit juices, and fresh vegetables—these are important. Needless to say, alcoholic beverages and drugs destroy the nervous system; stay away from them. A yogic drink that is very good for the nervous system is made by adding crushed rock candy and fresh lime juice to a glass of water. It should be thoroughly mixed and evenly blended so that the taste is equally sweet and sour. I have recommended this to many people with excellent results. Another beneficial practice when you are very nervous is to take a cold bath. I once told this to a newspaperman.'), 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.'), Document(metadata={'chapter_name': 'Attunement With God: Greatest Cure for Nervousness', 'split_id_sequential': 33, 'split_id_uuid': '28716ee9-dae2-46e5-80e6-f53bffa190c1', 'publication_name': 'Journey to Self-Realization', '_id': 'b079c905-181b-42ef-8c5f-849c0ba81f9b', '_collection_name': 'openai_large_chunks_1500char'}, page_content='Attunement With God: Greatest Cure for Nervousness Remember that the greatest healing of nervousness takes place when we attune our lives to God. The highest commandments given to man are to love God with all your heart, and with all your soul, and with all your mind, and with all your strength; and secondly, to love your neighbour as yourself.13 If you follow these, everything will come in its own way, and in the right way. It is not enough just to be a strict moralist—stones and goats do not break moral laws; still, they do not know God. But when you love God deeply enough, even if you are the greatest of sinners, you will be transformed and redeemed. The great saint Mirabai14 said, “To find the Divine One, the only indispensable is love.” That truth touched me deeply. All the prophets observe these two foremost commandments. Loving God with all your heart means to love Him with the love that you feel for the person who is dearest to you—with the love of the mother or father for the child, or the lover for the beloved. Give that kind of unconditional love to God. Loving God with all your soul means you can truly love Him when through deep meditation you know yourself as a soul, a child of God, made in His image. Loving God with all your mind means that when you are praying, your whole attention is on Him, not distracted by restless thoughts. In meditation, think only of God; don’t let the mind wander to everything else but God.')]\", name='vector_search', id='34f93038-5940-4a3b-bfd2-7f595ee69775', tool_call_id='call_fjyQpF0pjUQZ8gobl2SAozXV'),\n", " AIMessage(content='To reduce nervousness and improve sleep, it is essential to recognize that much of our nervousness stems from our own reactions and emotional states. Paramhansa Yogananda emphasizes that restlessness and emotional excitement can overload the nervous system, leading to fatigue and anxiety. Engaging in deep meditation can help calm the nerves, as it allows the body to rest and rejuvenate. Additionally, dietary choices play a significant role; consuming calming foods such as whole grains, fruits, and avoiding stimulants like caffeine and alcohol can support a healthier nervous system. Practicing silence and reducing exposure to noise can also alleviate nervousness, creating a more peaceful environment conducive to sleep.\\n\\n**Quotes:**\\n1. \"You always blame other things for making you nervous, but never accuse yourself. Yet it is you who make yourself nervous; ninety-nine percent is your own fault.\" - *Journey to Self-Realization, Probing the Core of Nervousness*\\n2. \"When you stop overloading the nervous system, as when you are in deep sleep or a state of calmness in meditation, you are not nervous at all.\" - *Journey to Self-Realization, Probing the Core of Nervousness*\\n3. \"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.\" - *Journey to Self-Realization, Examine Yourself to See What Makes You Nervous*\\n4. \"The greatest healing of nervousness takes place when we attune our lives to God.\" - *Journey to Self-Realization, Attunement With God: Greatest Cure for Nervousness*\\n\\n**Recommended Reading:**\\n- *Journey to Self-Realization* by Paramhansa Yogananda\\n\\n**Follow-up Questions:**\\n1. What specific techniques can I use in meditation to calm my nerves?\\n2. Are there particular foods or drinks that are especially beneficial for reducing nervousness?\\n3. How can I create a more peaceful environment at home to improve my sleep?', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 416, 'prompt_tokens': 2347, 'total_tokens': 2763, '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-0814edcd-020f-4ae0-bab2-b696f9948fec-0', usage_metadata={'input_tokens': 2347, 'output_tokens': 416, 'total_tokens': 2763})]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "chatbot.graph.get_state(chatbot.config)[0]['messages']" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7875\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "