Sameer Gupta commited on
Commit
b75fb8f
·
1 Parent(s): 83d33f9

V2 Chatbot with RAG

Browse files
Files changed (4) hide show
  1. configure.py +1 -19
  2. main.py +114 -32
  3. test.ipynb +306 -2
  4. utils.py +135 -117
configure.py CHANGED
@@ -1,21 +1,3 @@
1
- # # config.py
2
- # import os
3
-
4
-
5
- # # Paths
6
- # USER_DATA_PATH = "/home/sracha/Sattvastha/pipeline/new_pipeline/user_data.json"
7
- # RAG_BASE_DIRECTORY = "/home/sracha/Sattvastha/pipeline/all_content"
8
- # RAG_CATEGORIES = ["Ayurveda", "Lifestyle", "psychology", "Yoga", "Mental_health"]
9
- # # Prompt templates
10
- # QUESTION_CLASSIFICATION_PROMPT = """
11
- # Classify the following user input into one of these categories:
12
- # 1. "question" - If the user is asking a factual question that could be answered with knowledge
13
- # 2. "general" - If the user is just chatting or expressing feelings
14
-
15
- # User Input: {user_input}
16
-
17
- # Respond with only one word: either "question" or "general"
18
- # """
19
 
20
  # config.py
21
  import os
@@ -62,7 +44,7 @@ def llm_prompt():
62
 
63
  # Paths
64
  USER_DATA_PATH = "/home/surajracha/sameer/thon/user_data.json"
65
- RAG_BASE_DIRECTORY = "/home/surajracha/sameer/thon/all_content"
66
  RAG_CATEGORIES = ["Ayurveda", "Lifestyle", "psychology", "Yoga", "Mental_health"]
67
 
68
  # Prompt templates
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
  # config.py
3
  import os
 
44
 
45
  # Paths
46
  USER_DATA_PATH = "/home/surajracha/sameer/thon/user_data.json"
47
+ RAG_BASE_DIRECTORY = "/workspaces/codespaces-blank/Vidur_chat_bot/all_content"
48
  RAG_CATEGORIES = ["Ayurveda", "Lifestyle", "psychology", "Yoga", "Mental_health"]
49
 
50
  # Prompt templates
main.py CHANGED
@@ -413,8 +413,8 @@ from groq import Groq
413
  from input_to_llm import extract_chats, extract_goalfocus
414
  from utils import (
415
  # load_user_data,
416
- # initialize_rag,
417
- # get_rag_response,
418
  llm,
419
  get_mongo_collection
420
  )
@@ -446,7 +446,7 @@ client = Perplexity(api_key=PERPLEXITY_API_KEY)
446
  collection = get_mongo_collection()
447
 
448
  print("Initializing knowledge base...")
449
- # qa_chains = initialize_rag() # Uncomment if you enable RAG later
450
  print("Knowledge base ready!")
451
 
452
  @app.get("/")
@@ -485,7 +485,9 @@ async def chat_endpoint(request: ChatRequest):
485
  goal="blocked",
486
  resources=[]
487
  )
488
-
 
 
489
  # refusal_keywords = [
490
  # "unsafe",
491
  # "cannot",
@@ -555,35 +557,115 @@ async def chat_endpoint(request: ChatRequest):
555
  # goalandfocus = parsed_json
556
 
557
  context_response = f"""
558
- User Context:
559
- - Name: {username}
560
 
561
- Current Query: {user_input}
562
-
563
- Provide a helpful, supportive response.
564
- There's a lot of data avaiable and we have run rag on the data to get the best possible answer:
565
- utilise this to give the answer
566
- Also, at each step we are determining the topic and goal of the conversation to give a proper direction to the chats. This is to help you (who is acting as a guru to the user)
567
- to guide him better
568
- Ohk, so you are an expert in navigating paths through human conversations. So, let's say if someone is telling you about how they are feeling, what they did
569
- what other people did to them, what are their problems, what are their goals and aspirations in life and all that stuff.
570
- Now based on all the above information, you need to figure that as a teacher/Guru (which you are for the person)
571
- what should be the topic (or the broad thing that is going on currently as a part of discussion - it could discussion about office, or marriage or house problems or anything) you've to figure this out from based on previous converstaion history.
572
- At the same time, you have to ask/suggest/recommend further to the person as well right. So for that you have to define a goal (that is basically what should be the exact next step in this conversation - should you be asking a quuestion or should be recommending something or maybe just chatting normally). again this also you've to decide. But goal has to be something which defines the next step
573
- whereas topic is something which is broad and overall defines what is going on in the converstaion.
574
- goal is something which defines the next step which you should be taking in the converstaion, don't very rigidly follow it but try to take the direction from it
575
- Based on whatever topic and goal you've decided, you should continue your discussion along with the conversation history
576
- Here is the conversation history of past {ncon} conversations: {chat_history_str}, use this very wisely to extract the best out of them and then follow the goal and focus to guide the user further
577
- If you recieve nothing as goal and focus that means convessation has just started shaping
578
- Based on user current input, the current topic and goal, and the RAG output, provide a response. It could be anything, maybe a question a follow up you've to carefully decide on that. Also, while giving responses remember to give it such there is a link between the previous conversations and current one. It shouldn't sound like new conversation has started.
579
- Stitch the conversations well. Until and unless the conversation history is completely, don't treat the user as a new user
580
- Also, remember don't tell/reveal the user about the topic and goal thing, that is only for you to follow a certain path. The response to user should look very natural
581
- Here I am providing you the mode selected by the user: {mode}. So mode is basically how the user wants chatbot to behave like. It could be storyteller, Therapist or simply assistant. You have to follow this pattern. Behave like the person as selected by the user in mode.
582
- Here's an example on how different responses look like.
583
- Therapist = Think of your manager's chaos like a heavy bag he is trying to force you to hold. You might have to carry it while you are at work, but do not bring that heavy bag home with you in your mind. His lack of planning is his storm, not yours. Be like a strong tree—let his wind blow past you, but do not let it pull up your roots. Do your job to survive the day, but protect your peace inside.
584
- Storyteller = Imagine a lion walking through the forest. A tiny, angry dog runs out and starts barking frantically at the lion's feet. Does the lion roar back? Does the lion run away? No. The lion doesn't even look down. It keeps walking because it knows its power. Your boss is just the barking dog. Be the lion. Let him make noise while you keep walking toward your goal without breaking your stride.
585
- and etc. Also, you don't have to tell a story or talk like therapist in each conversation. It's only somethign deep has to be explained you should consider giving responses like that otherwise sound like a normal/natural conversation
586
- Keep the responses short, to the point following the topic and goal of discussion, don't give out long text until and unless there is something to explain to user in deep.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
587
 
588
  """
589
 
 
413
  from input_to_llm import extract_chats, extract_goalfocus
414
  from utils import (
415
  # load_user_data,
416
+ initialize_rag,
417
+ get_rag_response,
418
  llm,
419
  get_mongo_collection
420
  )
 
446
  collection = get_mongo_collection()
447
 
448
  print("Initializing knowledge base...")
449
+ qa_chains = initialize_rag() # Uncomment if you enable RAG later
450
  print("Knowledge base ready!")
451
 
452
  @app.get("/")
 
485
  goal="blocked",
486
  resources=[]
487
  )
488
+
489
+ rag_response = get_rag_response(user_input,qa_chains)
490
+ print(rag_response)
491
  # refusal_keywords = [
492
  # "unsafe",
493
  # "cannot",
 
557
  # goalandfocus = parsed_json
558
 
559
  context_response = f"""
 
 
560
 
561
+ **ROLE & PERSONA**
562
+ **You are a caring “guru” for the user.**
563
+ Your job is to keep the conversation flowing naturally, build on what the user has already shared, and guide them toward insight and actionable steps.
564
+
565
+ **Inputs you will receive (do **not** echo them to the user):**
566
+
567
+ | Variable | Meaning |
568
+ |----------|---------|
569
+ | **{username}** | User’s name (use only once, at the very start of the whole session, if ever). |
570
+ | **{user_input}** | The user’s latest message. |
571
+ | **{rag_response}** | The best answer retrieved from the knowledge base (use to enrich your reply). |
572
+ | **{mode}** | Desired style: **Therapist**, **Storyteller**, or **Assistant**. Follow the tone of the selected mode, but you may also respond in a normal conversational tone when a “deep” style isn’t required. |
573
+ | **{chat_history_str}** | Full prior conversation (do not treat the user as new). |
574
+ | **{ncon}** | Number of past conversation turns (for context only). |
575
+
576
+ ---
577
+
578
+ ### 1. Determine **Topic** and **Goal** (internal only)
579
+
580
+ * **Topic** the broad subject currently being discussed (e.g., work stress, relationship, self‑esteem).
581
+ * **Goal** the immediate next step you want to achieve in the dialogue (e.g., **offer a concrete coping tip**, **ask a clarifying question**, **summarise insight**, **encourage reflection**).
582
+
583
+ You do **not** reveal the topic or goal to the user. Use them only to steer your reply.
584
+
585
+ ---
586
+
587
+ ### 2. Craft the Reply
588
+
589
+ Your response must contain the following components, in this order:
590
+
591
+ 1. **Constructive solution / insight** – a practical suggestion, coping strategy, reframing, or brief wisdom that directly addresses the user’s current concern.
592
+ 2. OPTIONALLY → **Meaningful follow‑up** – a single, purposeful question or invitation that encourages the user to elaborate, reflect, or try the suggested step.
593
+
594
+ * Avoid Repetitive Phrasing
595
+ - Do NOT repeatedly start responses with phrases like “It seems like…”
596
+ - Use natural, varied sentence openings.
597
+ - Avoid sounding formulaic or templated..*
598
+ *Name Usage
599
+ - Do NOT repeatedly use the user's name.
600
+ - Use it only if emotionally appropriate or at meaningful moments.*
601
+ *
602
+ Meaningful Follow-Ups
603
+ If asking a question:
604
+ - It must help uncover root cause, belief, fear, or pattern.
605
+ - It must feel intentional, like a skilled counselor.
606
+ - Avoid generic questions like “How does that make you feel?” unless contextually necessary.
607
+ *
608
+
609
+
610
+ #### Length & Style
611
+ - Keep the overall reply **dynamic**: 1‑2 short sentences when only a quick tip is needed; up to 6‑7 sentences when a brief explanation adds value.
612
+ Do NOT keep conversations stuck in endless questioning.
613
+ Always move the conversation forward.
614
+ - Follow the **{mode}** tone:
615
+
616
+ * **Therapist** – gentle metaphors, grounding language, supportive framing.
617
+ * **Storyteller** – short vivid analogy or micro‑story that mirrors the issue.
618
+ * **Assistant** – clear, practical advice without poetic flourishes.
619
+
620
+ - If the situation does not call for a story or therapist‑style metaphor, respond in a natural, conversational manner.
621
+
622
+ #### Examples (do **not** copy verbatim, just illustrate the pattern)
623
+
624
+ - **Therapist**: “Imagine your mind as a garden; when weeds of worry appear, pause, breathe, and tend the soil with a calming breath. How did that feel when you tried it?”
625
+ - **Storyteller**: “A river meets a boulder and finds a new path around it. What small detour could you take around today’s obstacle?”
626
+ - **Assistant**: “Try a 5‑minute walk after work to reset your mind. Will you give it a try tomorrow?”
627
+
628
+ ---
629
+
630
+ ### 3. Continuity
631
+
632
+ - Reference relevant points from **{chat_history_str}** to show you remember past details.
633
+ - Never act as if this is a brand‑new conversation; always link back to earlier statements or progress.
634
+ - Incroporate the RAG response but at the same time utlise your own knowledge to expand on the retrieved knowledge
635
+ - Remember to consider only Indian Knowledge System based dataset for the responses
636
+ ---
637
+
638
+ ### 4. Technical Restrictions
639
+
640
+ - **No** opening phrases like “It seems like…”.
641
+ - **No** repeated use of the user’s name after the initial greeting.
642
+ - **Do not** ask only follow‑up questions; each turn must contain a **solution + follow‑up** pair.
643
+ - **Do not** mention “topic”, “goal”, or any internal process to the user.
644
+ - The chatbot UI will not have a back button; assume forward‑only flow.
645
+
646
+ ---
647
+
648
+ ### 5. Decision Flow (for you, the model)
649
+
650
+ 1. **Read** the latest **{user_input}**, **{rag_response}**, and **{chat_history_str}**.
651
+ 2. **Identify** the current **Topic** from the context.
652
+ 3. **Set** a clear **Goal** for this turn (solution + follow‑up).
653
+ 4. **Generate** the reply following the structure and style rules above.
654
+
655
+ ---
656
+
657
+ ========================
658
+ CRITICAL BEHAVIORAL RULES
659
+ ========================
660
+
661
+ - Do not over-validate without offering direction.
662
+ - Do not only ask questions.
663
+ - Do not provide rigid lectures.
664
+ - Do not sound robotic.
665
+ - Do not repeatedly restate the user’s words.
666
+ - Avoid repetitive emotional framing.
667
+ - Keep tone human and natural.
668
+ **Remember:** The purpose is to help the user feel heard, offer a tangible step forward, and gently probe for deeper insight—all while sounding natural and staying on‑track with the ongoing conversation.
669
 
670
  """
671
 
test.ipynb CHANGED
@@ -10,6 +10,22 @@
10
  "import os"
11
  ]
12
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  {
14
  "cell_type": "code",
15
  "execution_count": 2,
@@ -556,16 +572,304 @@
556
  },
557
  {
558
  "cell_type": "code",
559
- "execution_count": null,
560
  "id": "cfb1dc5a",
561
  "metadata": {},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
562
  "outputs": [],
563
  "source": []
564
  }
565
  ],
566
  "metadata": {
567
  "kernelspec": {
568
- "display_name": "calibre-env",
569
  "language": "python",
570
  "name": "python3"
571
  },
 
10
  "import os"
11
  ]
12
  },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": null,
16
+ "id": "fc39fb83",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": []
20
+ },
21
+ {
22
+ "cell_type": "code",
23
+ "execution_count": null,
24
+ "id": "4994b66a",
25
+ "metadata": {},
26
+ "outputs": [],
27
+ "source": []
28
+ },
29
  {
30
  "cell_type": "code",
31
  "execution_count": 2,
 
572
  },
573
  {
574
  "cell_type": "code",
575
+ "execution_count": 2,
576
  "id": "cfb1dc5a",
577
  "metadata": {},
578
+ "outputs": [
579
+ {
580
+ "name": "stdout",
581
+ "output_type": "stream",
582
+ "text": [
583
+ "1.1.3\n"
584
+ ]
585
+ }
586
+ ],
587
+ "source": [
588
+ "import langchain\n",
589
+ "print(langchain.__version__)"
590
+ ]
591
+ },
592
+ {
593
+ "cell_type": "code",
594
+ "execution_count": null,
595
+ "id": "b5678a21",
596
+ "metadata": {},
597
+ "outputs": [],
598
+ "source": [
599
+ "\n"
600
+ ]
601
+ },
602
+ {
603
+ "cell_type": "code",
604
+ "execution_count": 9,
605
+ "id": "cca6876e",
606
+ "metadata": {},
607
+ "outputs": [
608
+ {
609
+ "name": "stdout",
610
+ "output_type": "stream",
611
+ "text": [
612
+ "Found existing vector store for Ayurveda. Attempting to load...\n",
613
+ "Initialized Ayurveda QA chain.\n",
614
+ "Found existing vector store for Lifestyle. Attempting to load...\n",
615
+ "Initialized Lifestyle QA chain.\n",
616
+ "Found existing vector store for psychology. Attempting to load...\n",
617
+ "Initialized psychology QA chain.\n",
618
+ "Found existing vector store for Yoga. Attempting to load...\n",
619
+ "Initialized Yoga QA chain.\n",
620
+ "Found existing vector store for Mental_health. Attempting to load...\n",
621
+ "Initialized Mental_health QA chain.\n",
622
+ "I can totally understand how frustrating it must be to wake up with aches every morning. It's like your body is saying, \"Hey, slow down, I need a little extra care!\" Don't worry, I'm here to help.\n",
623
+ "\n",
624
+ "To start, you might want to try some gentle morning stretches to loosen up your muscles. One simple yet effective pose is the Dhanurasana, or Bow Pose, which can help relieve back pain and stiffness. You could also try some light yoga flows, like some gentle twists and forward bends, to get your blood flowing and warm up your muscles. Another option is to try some deep breathing exercises, like alternate nostril breathing, to calm your mind and relax your body.\n",
625
+ "\n",
626
+ "What do you think might be the underlying cause of your morning aches, and are you open to exploring some yoga practices to help alleviate them?\n"
627
+ ]
628
+ }
629
+ ],
630
+ "source": [
631
+ "import os\n",
632
+ "import shutil\n",
633
+ "from langchain_chroma import Chroma\n",
634
+ "from langchain_core.documents import Document\n",
635
+ "from langchain_core.prompts import PromptTemplate\n",
636
+ "from langchain_classic.chains import create_retrieval_chain\n",
637
+ "from langchain_classic.chains.combine_documents import create_stuff_documents_chain\n",
638
+ "\n",
639
+ "# Note: Ideally, these are defined in your config/main file, \n",
640
+ "# but included here for context based on your snippet.\n",
641
+ "from configure import USER_DATA_PATH, RAG_BASE_DIRECTORY, RAG_CATEGORIES\n",
642
+ "\n",
643
+ "from langchain_groq import ChatGroq\n",
644
+ "from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
645
+ "from langchain_huggingface import HuggingFaceEmbeddings\n",
646
+ "\n",
647
+ "from dotenv import load_dotenv\n",
648
+ "load_dotenv()\n",
649
+ "\n",
650
+ "llm = ChatGroq(\n",
651
+ " api_key=\"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\",\n",
652
+ " model=\"llama-3.3-70b-versatile\",\n",
653
+ " temperature=0,\n",
654
+ " max_tokens=4000\n",
655
+ ")\n",
656
+ "\n",
657
+ "# Text splitter\n",
658
+ "text_splitter = RecursiveCharacterTextSplitter(\n",
659
+ " separators=[\"\\n\\n\", \"\\n\", \".\", \" \", \"\"],\n",
660
+ " chunk_size=500,\n",
661
+ " chunk_overlap=100,\n",
662
+ " length_function=len\n",
663
+ ")\n",
664
+ "# text_splitter = LLMChunking()\n",
665
+ "\n",
666
+ "# Embeddings\n",
667
+ "embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-MiniLM-L6-v2\")\n",
668
+ "# embeddings = None\n",
669
+ "\n",
670
+ "\n",
671
+ "def initialize_rag(llm, embeddings, text_splitter):\n",
672
+ " \"\"\"\n",
673
+ " Initialize RAG vector stores and chains using modern LangChain (LCEL).\n",
674
+ " Args:\n",
675
+ " llm: The initialized ChatGroq (or other) LLM object.\n",
676
+ " embeddings: The initialized HuggingFaceEmbeddings object.\n",
677
+ " text_splitter: The initialized RecursiveCharacterTextSplitter object.\n",
678
+ " \"\"\"\n",
679
+ " vector_stores = {}\n",
680
+ " qa_chains = {}\n",
681
+ " \n",
682
+ " # 1. Define Prompt Template (Modern LCEL Format)\n",
683
+ " # Note: Modern chains typically look for \"context\" and \"input\" variables.\n",
684
+ " base_prompt_template = \"\"\"You are a {category} wellness expert. Provide helpful advice with specific actions:\n",
685
+ "\n",
686
+ "1. Start with a brief empathetic response to the user's concern\n",
687
+ "2. Offer 1-3 actionable suggestions with brief explanations\n",
688
+ "3. End with an open-ended question to continue conversation\n",
689
+ "\n",
690
+ "Guidelines:\n",
691
+ "- Keep responses conversational and supportive\n",
692
+ "- Avoid clinical jargon\n",
693
+ "- Focus on practical, implementable advice\n",
694
+ "- Maintain hopeful and encouraging tone\n",
695
+ "\n",
696
+ "Context:\n",
697
+ "{context}\n",
698
+ "\n",
699
+ "Question: {input}\n",
700
+ "\"\"\"\n",
701
+ " \n",
702
+ " for category in RAG_CATEGORIES:\n",
703
+ " persist_dir = f\"./chroma_db_{category}\"\n",
704
+ " vector_store = None \n",
705
+ "\n",
706
+ " # --- 2. Check/Load Existing Vector Store ---\n",
707
+ " if os.path.exists(persist_dir):\n",
708
+ " print(f\"Found existing vector store for {category}. Attempting to load...\")\n",
709
+ " try:\n",
710
+ " vector_store = Chroma(\n",
711
+ " persist_directory=persist_dir,\n",
712
+ " embedding_function=embeddings # UPDATED: 'embedding_function', not 'embedding'\n",
713
+ " )\n",
714
+ " vector_stores[category] = vector_store\n",
715
+ " except Exception as e:\n",
716
+ " print(f\"Error loading existing store {persist_dir}: {e}\")\n",
717
+ " print(\"Will delete and attempt to re-build.\")\n",
718
+ " shutil.rmtree(persist_dir)\n",
719
+ " \n",
720
+ " # --- 3. Create Vector Store if needed ---\n",
721
+ " if vector_store is None: \n",
722
+ " print(f\"No valid vector store for {category} found. Creating new one...\")\n",
723
+ " \n",
724
+ " dir_path = os.path.join(RAG_BASE_DIRECTORY, category)\n",
725
+ " docs = []\n",
726
+ " \n",
727
+ " if os.path.exists(dir_path):\n",
728
+ " for filename in os.listdir(dir_path):\n",
729
+ " if filename.endswith('.txt'):\n",
730
+ " file_path = os.path.join(dir_path, filename)\n",
731
+ " try:\n",
732
+ " with open(file_path, 'r', encoding='utf-8') as f:\n",
733
+ " text = f.read()\n",
734
+ " \n",
735
+ " chunks = text_splitter.split_text(text)\n",
736
+ " for chunk in chunks:\n",
737
+ " if chunk.strip():\n",
738
+ " metadata = {\n",
739
+ " \"source\": filename,\n",
740
+ " \"category\": category\n",
741
+ " }\n",
742
+ " docs.append(Document(\n",
743
+ " page_content=chunk.strip(),\n",
744
+ " metadata=metadata\n",
745
+ " ))\n",
746
+ " except Exception as e:\n",
747
+ " print(f\"Error processing {file_path}: {e}\")\n",
748
+ " \n",
749
+ " if docs:\n",
750
+ " # UPDATED: Use 'embedding_function' instead of 'embedding'\n",
751
+ " # UPDATED: Removed .persist() call (Auto-persists in new version)\n",
752
+ " vector_store = Chroma.from_documents(\n",
753
+ " documents=docs,\n",
754
+ " embedding=embeddings, \n",
755
+ " persist_directory=persist_dir\n",
756
+ " )\n",
757
+ " vector_stores[category] = vector_store\n",
758
+ " print(f\"Created new vector store for {category} with {len(docs)} documents.\")\n",
759
+ " else:\n",
760
+ " print(f\"No documents found for {category}. Skipping QA chain setup.\")\n",
761
+ " continue \n",
762
+ "\n",
763
+ " # --- 4. Create QA Chain (LCEL Style) ---\n",
764
+ " if vector_store:\n",
765
+ " # A. Create the Prompt\n",
766
+ " # We inject the specific category into the template string immediately\n",
767
+ " category_specific_template = base_prompt_template.replace(\"{category}\", category)\n",
768
+ " \n",
769
+ " prompt = PromptTemplate(\n",
770
+ " template=category_specific_template,\n",
771
+ " input_variables=[\"context\", \"input\"] # LCEL standard variables\n",
772
+ " )\n",
773
+ "\n",
774
+ " # B. Create the Document Chain (LLM + Prompt)\n",
775
+ " question_answer_chain = create_stuff_documents_chain(llm, prompt)\n",
776
+ "\n",
777
+ " # C. Create the Retrieval Chain (Retriever + Document Chain)\n",
778
+ " retriever = vector_store.as_retriever(search_kwargs={\"k\": 5})\n",
779
+ " rag_chain = create_retrieval_chain(retriever, question_answer_chain)\n",
780
+ "\n",
781
+ " qa_chains[category] = rag_chain\n",
782
+ " print(f\"Initialized {category} QA chain.\")\n",
783
+ "\n",
784
+ " return qa_chains\n",
785
+ "\n",
786
+ "qa_chains = initialize_rag(llm,embeddings,text_splitter)\n",
787
+ "\n",
788
+ "response = qa_chains['Yoga'].invoke({\"input\": \"HMy body aches every morning after wake up, what can i do?\"})\n",
789
+ "print(response['answer'])\n",
790
+ "\n"
791
+ ]
792
+ },
793
+ {
794
+ "cell_type": "code",
795
+ "execution_count": null,
796
+ "id": "69d2eb01",
797
+ "metadata": {},
798
+ "outputs": [
799
+ {
800
+ "name": "stdout",
801
+ "output_type": "stream",
802
+ "text": [
803
+ "Found existing vector store for Ayurveda. Attempting to load...\n",
804
+ "Initialized Ayurveda QA chain.\n",
805
+ "Found existing vector store for Lifestyle. Attempting to load...\n",
806
+ "Initialized Lifestyle QA chain.\n",
807
+ "Found existing vector store for psychology. Attempting to load...\n",
808
+ "Initialized psychology QA chain.\n",
809
+ "Found existing vector store for Yoga. Attempting to load...\n",
810
+ "Initialized Yoga QA chain.\n",
811
+ "Found existing vector store for Mental_health. Attempting to load...\n",
812
+ "Initialized Mental_health QA chain.\n"
813
+ ]
814
+ }
815
+ ],
816
+ "source": [
817
+ "# Example Usage:\n"
818
+ ]
819
+ },
820
+ {
821
+ "cell_type": "code",
822
+ "execution_count": null,
823
+ "id": "0b2e50be",
824
+ "metadata": {},
825
+ "outputs": [
826
+ {
827
+ "ename": "AuthenticationError",
828
+ "evalue": "Error code: 401 - {'error': {'message': 'Invalid API Key', 'type': 'invalid_request_error', 'code': 'invalid_api_key'}}",
829
+ "output_type": "error",
830
+ "traceback": [
831
+ "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
832
+ "\u001b[31mAuthenticationError\u001b[39m Traceback (most recent call last)",
833
+ "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m response = \u001b[43mqa_chains\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mYoga\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\u001b[43m{\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43minput\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mHMy body aches every morning after wake up, what can i do?\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 2\u001b[39m \u001b[38;5;28mprint\u001b[39m(response[\u001b[33m'\u001b[39m\u001b[33manswer\u001b[39m\u001b[33m'\u001b[39m])\n",
834
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/runnables/base.py:5691\u001b[39m, in \u001b[36mRunnableBindingBase.invoke\u001b[39m\u001b[34m(self, input, config, **kwargs)\u001b[39m\n\u001b[32m 5684\u001b[39m \u001b[38;5;129m@override\u001b[39m\n\u001b[32m 5685\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34minvoke\u001b[39m(\n\u001b[32m 5686\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 5689\u001b[39m **kwargs: Any | \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[32m 5690\u001b[39m ) -> Output:\n\u001b[32m-> \u001b[39m\u001b[32m5691\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mbound\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 5692\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 5693\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_merge_configs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5694\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43m{\u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5695\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
835
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/runnables/base.py:3153\u001b[39m, in \u001b[36mRunnableSequence.invoke\u001b[39m\u001b[34m(self, input, config, **kwargs)\u001b[39m\n\u001b[32m 3151\u001b[39m input_ = context.run(step.invoke, input_, config, **kwargs)\n\u001b[32m 3152\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m-> \u001b[39m\u001b[32m3153\u001b[39m input_ = \u001b[43mcontext\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstep\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minput_\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 3154\u001b[39m \u001b[38;5;66;03m# finish the root run\u001b[39;00m\n\u001b[32m 3155\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
836
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/runnables/passthrough.py:507\u001b[39m, in \u001b[36mRunnableAssign.invoke\u001b[39m\u001b[34m(self, input, config, **kwargs)\u001b[39m\n\u001b[32m 500\u001b[39m \u001b[38;5;129m@override\u001b[39m\n\u001b[32m 501\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34minvoke\u001b[39m(\n\u001b[32m 502\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 505\u001b[39m **kwargs: Any,\n\u001b[32m 506\u001b[39m ) -> \u001b[38;5;28mdict\u001b[39m[\u001b[38;5;28mstr\u001b[39m, Any]:\n\u001b[32m--> \u001b[39m\u001b[32m507\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_call_with_config\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_invoke\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
837
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/runnables/base.py:2060\u001b[39m, in \u001b[36mRunnable._call_with_config\u001b[39m\u001b[34m(self, func, input_, config, run_type, serialized, **kwargs)\u001b[39m\n\u001b[32m 2056\u001b[39m child_config = patch_config(config, callbacks=run_manager.get_child())\n\u001b[32m 2057\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m set_config_context(child_config) \u001b[38;5;28;01mas\u001b[39;00m context:\n\u001b[32m 2058\u001b[39m output = cast(\n\u001b[32m 2059\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mOutput\u001b[39m\u001b[33m\"\u001b[39m,\n\u001b[32m-> \u001b[39m\u001b[32m2060\u001b[39m \u001b[43mcontext\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 2061\u001b[39m \u001b[43m \u001b[49m\u001b[43mcall_func_with_variable_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# type: ignore[arg-type]\u001b[39;49;00m\n\u001b[32m 2062\u001b[39m \u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2063\u001b[39m \u001b[43m \u001b[49m\u001b[43minput_\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2064\u001b[39m \u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2065\u001b[39m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2066\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 2067\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m,\n\u001b[32m 2068\u001b[39m )\n\u001b[32m 2069\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[32m 2070\u001b[39m run_manager.on_chain_error(e)\n",
838
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/runnables/config.py:452\u001b[39m, in \u001b[36mcall_func_with_variable_args\u001b[39m\u001b[34m(func, input, config, run_manager, **kwargs)\u001b[39m\n\u001b[32m 450\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m run_manager \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m accepts_run_manager(func):\n\u001b[32m 451\u001b[39m kwargs[\u001b[33m\"\u001b[39m\u001b[33mrun_manager\u001b[39m\u001b[33m\"\u001b[39m] = run_manager\n\u001b[32m--> \u001b[39m\u001b[32m452\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
839
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/runnables/passthrough.py:493\u001b[39m, in \u001b[36mRunnableAssign._invoke\u001b[39m\u001b[34m(self, value, run_manager, config, **kwargs)\u001b[39m\n\u001b[32m 488\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33mThe input to RunnablePassthrough.assign() must be a dict.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 489\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(msg) \u001b[38;5;66;03m# noqa: TRY004\u001b[39;00m\n\u001b[32m 491\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m {\n\u001b[32m 492\u001b[39m **value,\n\u001b[32m--> \u001b[39m\u001b[32m493\u001b[39m **\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mmapper\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 494\u001b[39m \u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 495\u001b[39m \u001b[43m \u001b[49m\u001b[43mpatch_config\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget_child\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 496\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 497\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m,\n\u001b[32m 498\u001b[39m }\n",
840
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/runnables/base.py:3878\u001b[39m, in \u001b[36mRunnableParallel.invoke\u001b[39m\u001b[34m(self, input, config, **kwargs)\u001b[39m\n\u001b[32m 3872\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m get_executor_for_config(config) \u001b[38;5;28;01mas\u001b[39;00m executor:\n\u001b[32m 3873\u001b[39m futures = [\n\u001b[32m 3874\u001b[39m executor.submit(_invoke_step, step, \u001b[38;5;28minput\u001b[39m, config, key)\n\u001b[32m 3875\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m key, step \u001b[38;5;129;01min\u001b[39;00m steps.items()\n\u001b[32m 3876\u001b[39m ]\n\u001b[32m 3877\u001b[39m output = {\n\u001b[32m-> \u001b[39m\u001b[32m3878\u001b[39m key: \u001b[43mfuture\u001b[49m\u001b[43m.\u001b[49m\u001b[43mresult\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 3879\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m key, future \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(steps, futures, strict=\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[32m 3880\u001b[39m }\n\u001b[32m 3881\u001b[39m \u001b[38;5;66;03m# finish the root run\u001b[39;00m\n\u001b[32m 3882\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
841
+ "\u001b[36mFile \u001b[39m\u001b[32m~/.python/current/lib/python3.12/concurrent/futures/_base.py:456\u001b[39m, in \u001b[36mFuture.result\u001b[39m\u001b[34m(self, timeout)\u001b[39m\n\u001b[32m 454\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m CancelledError()\n\u001b[32m 455\u001b[39m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mself\u001b[39m._state == FINISHED:\n\u001b[32m--> \u001b[39m\u001b[32m456\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m__get_result\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 457\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 458\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTimeoutError\u001b[39;00m()\n",
842
+ "\u001b[36mFile \u001b[39m\u001b[32m~/.python/current/lib/python3.12/concurrent/futures/_base.py:401\u001b[39m, in \u001b[36mFuture.__get_result\u001b[39m\u001b[34m(self)\u001b[39m\n\u001b[32m 399\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m._exception:\n\u001b[32m 400\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m--> \u001b[39m\u001b[32m401\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;28mself\u001b[39m._exception\n\u001b[32m 402\u001b[39m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[32m 403\u001b[39m \u001b[38;5;66;03m# Break a reference cycle with the exception in self._exception\u001b[39;00m\n\u001b[32m 404\u001b[39m \u001b[38;5;28mself\u001b[39m = \u001b[38;5;28;01mNone\u001b[39;00m\n",
843
+ "\u001b[36mFile \u001b[39m\u001b[32m~/.python/current/lib/python3.12/concurrent/futures/thread.py:58\u001b[39m, in \u001b[36m_WorkItem.run\u001b[39m\u001b[34m(self)\u001b[39m\n\u001b[32m 55\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[32m 57\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m---> \u001b[39m\u001b[32m58\u001b[39m result = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 59\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[32m 60\u001b[39m \u001b[38;5;28mself\u001b[39m.future.set_exception(exc)\n",
844
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/runnables/base.py:3861\u001b[39m, in \u001b[36mRunnableParallel.invoke.<locals>._invoke_step\u001b[39m\u001b[34m(step, input_, config, key)\u001b[39m\n\u001b[32m 3855\u001b[39m child_config = patch_config(\n\u001b[32m 3856\u001b[39m config,\n\u001b[32m 3857\u001b[39m \u001b[38;5;66;03m# mark each step as a child run\u001b[39;00m\n\u001b[32m 3858\u001b[39m callbacks=run_manager.get_child(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mmap:key:\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkey\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m),\n\u001b[32m 3859\u001b[39m )\n\u001b[32m 3860\u001b[39m \u001b[38;5;28;01mwith\u001b[39;00m set_config_context(child_config) \u001b[38;5;28;01mas\u001b[39;00m context:\n\u001b[32m-> \u001b[39m\u001b[32m3861\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcontext\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 3862\u001b[39m \u001b[43m \u001b[49m\u001b[43mstep\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 3863\u001b[39m \u001b[43m \u001b[49m\u001b[43minput_\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 3864\u001b[39m \u001b[43m \u001b[49m\u001b[43mchild_config\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 3865\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
845
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/runnables/base.py:5691\u001b[39m, in \u001b[36mRunnableBindingBase.invoke\u001b[39m\u001b[34m(self, input, config, **kwargs)\u001b[39m\n\u001b[32m 5684\u001b[39m \u001b[38;5;129m@override\u001b[39m\n\u001b[32m 5685\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34minvoke\u001b[39m(\n\u001b[32m 5686\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 5689\u001b[39m **kwargs: Any | \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[32m 5690\u001b[39m ) -> Output:\n\u001b[32m-> \u001b[39m\u001b[32m5691\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mbound\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 5692\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 5693\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_merge_configs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5694\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43m{\u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5695\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
846
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/runnables/base.py:3153\u001b[39m, in \u001b[36mRunnableSequence.invoke\u001b[39m\u001b[34m(self, input, config, **kwargs)\u001b[39m\n\u001b[32m 3151\u001b[39m input_ = context.run(step.invoke, input_, config, **kwargs)\n\u001b[32m 3152\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m-> \u001b[39m\u001b[32m3153\u001b[39m input_ = \u001b[43mcontext\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstep\u001b[49m\u001b[43m.\u001b[49m\u001b[43minvoke\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minput_\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 3154\u001b[39m \u001b[38;5;66;03m# finish the root run\u001b[39;00m\n\u001b[32m 3155\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
847
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py:402\u001b[39m, in \u001b[36mBaseChatModel.invoke\u001b[39m\u001b[34m(self, input, config, stop, **kwargs)\u001b[39m\n\u001b[32m 388\u001b[39m \u001b[38;5;129m@override\u001b[39m\n\u001b[32m 389\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34minvoke\u001b[39m(\n\u001b[32m 390\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 395\u001b[39m **kwargs: Any,\n\u001b[32m 396\u001b[39m ) -> AIMessage:\n\u001b[32m 397\u001b[39m config = ensure_config(config)\n\u001b[32m 398\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m cast(\n\u001b[32m 399\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mAIMessage\u001b[39m\u001b[33m\"\u001b[39m,\n\u001b[32m 400\u001b[39m cast(\n\u001b[32m 401\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mChatGeneration\u001b[39m\u001b[33m\"\u001b[39m,\n\u001b[32m--> \u001b[39m\u001b[32m402\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mgenerate_prompt\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 403\u001b[39m \u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_convert_input\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 404\u001b[39m \u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 405\u001b[39m \u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mcallbacks\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 406\u001b[39m \u001b[43m \u001b[49m\u001b[43mtags\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtags\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 407\u001b[39m \u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmetadata\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 408\u001b[39m \u001b[43m \u001b[49m\u001b[43mrun_name\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mrun_name\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 409\u001b[39m \u001b[43m \u001b[49m\u001b[43mrun_id\u001b[49m\u001b[43m=\u001b[49m\u001b[43mconfig\u001b[49m\u001b[43m.\u001b[49m\u001b[43mpop\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mrun_id\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 410\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 411\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m.generations[\u001b[32m0\u001b[39m][\u001b[32m0\u001b[39m],\n\u001b[32m 412\u001b[39m ).message,\n\u001b[32m 413\u001b[39m )\n",
848
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py:1121\u001b[39m, in \u001b[36mBaseChatModel.generate_prompt\u001b[39m\u001b[34m(self, prompts, stop, callbacks, **kwargs)\u001b[39m\n\u001b[32m 1112\u001b[39m \u001b[38;5;129m@override\u001b[39m\n\u001b[32m 1113\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mgenerate_prompt\u001b[39m(\n\u001b[32m 1114\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 1118\u001b[39m **kwargs: Any,\n\u001b[32m 1119\u001b[39m ) -> LLMResult:\n\u001b[32m 1120\u001b[39m prompt_messages = [p.to_messages() \u001b[38;5;28;01mfor\u001b[39;00m p \u001b[38;5;129;01min\u001b[39;00m prompts]\n\u001b[32m-> \u001b[39m\u001b[32m1121\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mgenerate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mprompt_messages\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
849
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py:931\u001b[39m, in \u001b[36mBaseChatModel.generate\u001b[39m\u001b[34m(self, messages, stop, callbacks, tags, metadata, run_name, run_id, **kwargs)\u001b[39m\n\u001b[32m 928\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m i, m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(input_messages):\n\u001b[32m 929\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m 930\u001b[39m results.append(\n\u001b[32m--> \u001b[39m\u001b[32m931\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_generate_with_cache\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 932\u001b[39m \u001b[43m \u001b[49m\u001b[43mm\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 933\u001b[39m \u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 934\u001b[39m \u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrun_managers\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mrun_managers\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[32m 935\u001b[39m \u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 936\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 937\u001b[39m )\n\u001b[32m 938\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[32m 939\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m run_managers:\n",
850
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py:1233\u001b[39m, in \u001b[36mBaseChatModel._generate_with_cache\u001b[39m\u001b[34m(self, messages, stop, run_manager, **kwargs)\u001b[39m\n\u001b[32m 1231\u001b[39m result = generate_from_stream(\u001b[38;5;28miter\u001b[39m(chunks))\n\u001b[32m 1232\u001b[39m \u001b[38;5;28;01melif\u001b[39;00m inspect.signature(\u001b[38;5;28mself\u001b[39m._generate).parameters.get(\u001b[33m\"\u001b[39m\u001b[33mrun_manager\u001b[39m\u001b[33m\"\u001b[39m):\n\u001b[32m-> \u001b[39m\u001b[32m1233\u001b[39m result = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_generate\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 1234\u001b[39m \u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrun_manager\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\n\u001b[32m 1235\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 1236\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 1237\u001b[39m result = \u001b[38;5;28mself\u001b[39m._generate(messages, stop=stop, **kwargs)\n",
851
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/langchain_groq/chat_models.py:593\u001b[39m, in \u001b[36mChatGroq._generate\u001b[39m\u001b[34m(self, messages, stop, run_manager, **kwargs)\u001b[39m\n\u001b[32m 588\u001b[39m message_dicts, params = \u001b[38;5;28mself\u001b[39m._create_message_dicts(messages, stop)\n\u001b[32m 589\u001b[39m params = {\n\u001b[32m 590\u001b[39m **params,\n\u001b[32m 591\u001b[39m **kwargs,\n\u001b[32m 592\u001b[39m }\n\u001b[32m--> \u001b[39m\u001b[32m593\u001b[39m response = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mclient\u001b[49m\u001b[43m.\u001b[49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmessages\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmessage_dicts\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mparams\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 594\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m._create_chat_result(response, params)\n",
852
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/groq/resources/chat/completions.py:461\u001b[39m, in \u001b[36mCompletions.create\u001b[39m\u001b[34m(self, messages, model, citation_options, compound_custom, disable_tool_validation, documents, exclude_domains, frequency_penalty, function_call, functions, include_domains, include_reasoning, logit_bias, logprobs, max_completion_tokens, max_tokens, metadata, n, parallel_tool_calls, presence_penalty, reasoning_effort, reasoning_format, response_format, search_settings, seed, service_tier, stop, store, stream, temperature, tool_choice, tools, top_logprobs, top_p, user, extra_headers, extra_query, extra_body, timeout)\u001b[39m\n\u001b[32m 241\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mcreate\u001b[39m(\n\u001b[32m 242\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m 243\u001b[39m *,\n\u001b[32m (...)\u001b[39m\u001b[32m 300\u001b[39m timeout: \u001b[38;5;28mfloat\u001b[39m | httpx.Timeout | \u001b[38;5;28;01mNone\u001b[39;00m | NotGiven = not_given,\n\u001b[32m 301\u001b[39m ) -> ChatCompletion | Stream[ChatCompletionChunk]:\n\u001b[32m 302\u001b[39m \u001b[38;5;250m \u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m 303\u001b[39m \u001b[33;03m Creates a model response for the given chat conversation.\u001b[39;00m\n\u001b[32m 304\u001b[39m \n\u001b[32m (...)\u001b[39m\u001b[32m 459\u001b[39m \u001b[33;03m timeout: Override the client-level default timeout for this request, in seconds\u001b[39;00m\n\u001b[32m 460\u001b[39m \u001b[33;03m \"\"\"\u001b[39;00m\n\u001b[32m--> \u001b[39m\u001b[32m461\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_post\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 462\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43m/openai/v1/chat/completions\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m 463\u001b[39m \u001b[43m \u001b[49m\u001b[43mbody\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmaybe_transform\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 464\u001b[39m \u001b[43m \u001b[49m\u001b[43m{\u001b[49m\n\u001b[32m 465\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmessages\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmessages\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 466\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmodel\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 467\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mcitation_options\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mcitation_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 468\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mcompound_custom\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mcompound_custom\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 469\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mdisable_tool_validation\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mdisable_tool_validation\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 470\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mdocuments\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mdocuments\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 471\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mexclude_domains\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mexclude_domains\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 472\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mfrequency_penalty\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfrequency_penalty\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 473\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mfunction_call\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunction_call\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 474\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mfunctions\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunctions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 475\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43minclude_domains\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43minclude_domains\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 476\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43minclude_reasoning\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43minclude_reasoning\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 477\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mlogit_bias\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mlogit_bias\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 478\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mlogprobs\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mlogprobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 479\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmax_completion_tokens\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_completion_tokens\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 480\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmax_tokens\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_tokens\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 481\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmetadata\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mmetadata\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 482\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mn\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 483\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mparallel_tool_calls\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mparallel_tool_calls\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 484\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mpresence_penalty\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mpresence_penalty\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 485\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mreasoning_effort\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mreasoning_effort\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 486\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mreasoning_format\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mreasoning_format\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 487\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mresponse_format\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mresponse_format\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 488\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43msearch_settings\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43msearch_settings\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 489\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mseed\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mseed\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 490\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mservice_tier\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mservice_tier\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 491\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mstop\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 492\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mstore\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstore\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 493\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mstream\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 494\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtemperature\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtemperature\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 495\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtool_choice\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtool_choice\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 496\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtools\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtools\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 497\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtop_logprobs\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtop_logprobs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 498\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mtop_p\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mtop_p\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 499\u001b[39m \u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43muser\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43muser\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 500\u001b[39m \u001b[43m \u001b[49m\u001b[43m}\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 501\u001b[39m \u001b[43m \u001b[49m\u001b[43mcompletion_create_params\u001b[49m\u001b[43m.\u001b[49m\u001b[43mCompletionCreateParams\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 502\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 503\u001b[39m \u001b[43m \u001b[49m\u001b[43moptions\u001b[49m\u001b[43m=\u001b[49m\u001b[43mmake_request_options\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 504\u001b[39m \u001b[43m \u001b[49m\u001b[43mextra_headers\u001b[49m\u001b[43m=\u001b[49m\u001b[43mextra_headers\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_query\u001b[49m\u001b[43m=\u001b[49m\u001b[43mextra_query\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mextra_body\u001b[49m\u001b[43m=\u001b[49m\u001b[43mextra_body\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[43m=\u001b[49m\u001b[43mtimeout\u001b[49m\n\u001b[32m 505\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 506\u001b[39m \u001b[43m \u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m=\u001b[49m\u001b[43mChatCompletion\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 507\u001b[39m \u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstream\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[32m 508\u001b[39m \u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[43m=\u001b[49m\u001b[43mStream\u001b[49m\u001b[43m[\u001b[49m\u001b[43mChatCompletionChunk\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 509\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
853
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/groq/_base_client.py:1242\u001b[39m, in \u001b[36mSyncAPIClient.post\u001b[39m\u001b[34m(self, path, cast_to, body, options, files, stream, stream_cls)\u001b[39m\n\u001b[32m 1228\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mpost\u001b[39m(\n\u001b[32m 1229\u001b[39m \u001b[38;5;28mself\u001b[39m,\n\u001b[32m 1230\u001b[39m path: \u001b[38;5;28mstr\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 1237\u001b[39m stream_cls: \u001b[38;5;28mtype\u001b[39m[_StreamT] | \u001b[38;5;28;01mNone\u001b[39;00m = \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[32m 1238\u001b[39m ) -> ResponseT | _StreamT:\n\u001b[32m 1239\u001b[39m opts = FinalRequestOptions.construct(\n\u001b[32m 1240\u001b[39m method=\u001b[33m\"\u001b[39m\u001b[33mpost\u001b[39m\u001b[33m\"\u001b[39m, url=path, json_data=body, files=to_httpx_files(files), **options\n\u001b[32m 1241\u001b[39m )\n\u001b[32m-> \u001b[39m\u001b[32m1242\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m cast(ResponseT, \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mrequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcast_to\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mopts\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstream\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[43m=\u001b[49m\u001b[43mstream_cls\u001b[49m\u001b[43m)\u001b[49m)\n",
854
+ "\u001b[36mFile \u001b[39m\u001b[32m/workspaces/codespaces-blank/overenv/lib/python3.12/site-packages/groq/_base_client.py:1044\u001b[39m, in \u001b[36mSyncAPIClient.request\u001b[39m\u001b[34m(self, cast_to, options, stream, stream_cls)\u001b[39m\n\u001b[32m 1041\u001b[39m err.response.read()\n\u001b[32m 1043\u001b[39m log.debug(\u001b[33m\"\u001b[39m\u001b[33mRe-raising status error\u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m-> \u001b[39m\u001b[32m1044\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;28mself\u001b[39m._make_status_error_from_response(err.response) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[32m 1046\u001b[39m \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[32m 1048\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m response \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m, \u001b[33m\"\u001b[39m\u001b[33mcould not resolve response (should never happen)\u001b[39m\u001b[33m\"\u001b[39m\n",
855
+ "\u001b[31mAuthenticationError\u001b[39m: Error code: 401 - {'error': {'message': 'Invalid API Key', 'type': 'invalid_request_error', 'code': 'invalid_api_key'}}"
856
+ ]
857
+ }
858
+ ],
859
+ "source": []
860
+ },
861
+ {
862
+ "cell_type": "code",
863
+ "execution_count": null,
864
+ "id": "72c399bf",
865
+ "metadata": {},
866
  "outputs": [],
867
  "source": []
868
  }
869
  ],
870
  "metadata": {
871
  "kernelspec": {
872
+ "display_name": "overenv",
873
  "language": "python",
874
  "name": "python3"
875
  },
utils.py CHANGED
@@ -1,3 +1,4 @@
 
1
  # utils.py
2
  import os
3
  import json
@@ -8,6 +9,14 @@ from langchain_text_splitters import RecursiveCharacterTextSplitter
8
  # from langchain_huggingface import HuggingFaceEmbeddings
9
  # from langchain_community.vectorstores import Chroma
10
  # from langchain.prompts import PromptTemplate
 
 
 
 
 
 
 
 
11
  from configure import USER_DATA_PATH, RAG_BASE_DIRECTORY, RAG_CATEGORIES
12
  import shutil
13
  from dotenv import load_dotenv
@@ -15,7 +24,6 @@ from pymongo import MongoClient
15
  import certifi
16
  import re
17
 
18
-
19
  load_dotenv()
20
 
21
 
@@ -41,6 +49,7 @@ def LLMChunking():
41
 
42
  # LLM setup
43
  llm = ChatGroq(
 
44
  model="llama-3.3-70b-versatile",
45
  temperature=0,
46
  max_tokens=4000
@@ -56,8 +65,8 @@ text_splitter = RecursiveCharacterTextSplitter(
56
  # text_splitter = LLMChunking()
57
 
58
  # Embeddings
59
- # embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
60
- embeddings = None
61
 
62
  # Load user data
63
  def load_user_data():
@@ -75,114 +84,122 @@ def save_user_data(data):
75
  json.dump(data, f, indent=2)
76
 
77
  # Initialize RAG with per-category vector stores and QA chains
78
- # def initialize_rag():
79
- # vector_stores = {}
80
- # qa_chains = {}
81
-
82
- # # Updated prompt template for advice-focused responses
83
- # custom_prompt_template = """
84
- # You are a {category} wellness expert. Provide helpful advice with specific actions:
85
-
86
- # 1. Start with a brief empathetic response to the user's concern
87
- # 2. Offer 1-3 actionable suggestions with brief explanations
88
- # 3. End with an open-ended question to continue conversation
89
-
90
- # Guidelines:
91
- # - Keep responses conversational and supportive
92
- # - Avoid clinical jargon
93
- # - Focus on practical, implementable advice
94
- # - Maintain hopeful and encouraging tone
95
-
96
- # Context:
97
- # {context}
98
 
99
- # Question: {question}
100
- # """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
- # # Create vector stores and QA chains for each category
103
- # # Assume all your variables like RAG_CATEGORIES, embeddings, llm, etc.
104
- # # are defined above this block.
105
-
106
- # for category in RAG_CATEGORIES:
107
- # persist_dir = f"./chroma_db_{category}"
108
- # vector_store = None # Initialize vector_store to None
109
-
110
- # # --- 1. Check if Vector Store Already Exists ---
111
- # if os.path.exists(persist_dir):
112
- # print(f"Found existing vector store for {category}. Attempting to load...")
113
- # try:
114
- # # Load the existing vector store from disk
115
- # vector_store = Chroma(
116
- # persist_directory=persist_dir,
117
- # embedding_function=embeddings # Must provide the same embedding function
118
- # )
119
- # vector_stores[category] = vector_store
120
- # except Exception as e:
121
- # print(f"Error loading existing store {persist_dir}: {e}")
122
- # print("Will delete and attempt to re-build.")
123
- # shutil.rmtree(persist_dir)
124
 
125
- # # --- 2. Create Vector Store if it Doesn't Exist (or failed to load) ---
126
- # if vector_store is None:
127
- # print(f"No valid vector store for {category} found. Creating new one...")
 
 
 
128
 
129
- # # Process documents (Your original logic)
130
- # dir_path = os.path.join(RAG_BASE_DIRECTORY, category)
131
- # docs = []
132
- # if os.path.exists(dir_path):
133
- # for filename in os.listdir(dir_path):
134
- # if filename.endswith('.txt'):
135
- # file_path = os.path.join(dir_path, filename)
136
- # try:
137
- # with open(file_path, 'r', encoding='utf-8') as f:
138
- # text = f.read()
139
- # chunks = text_splitter.split_text(text)
140
- # for chunk in chunks:
141
- # if chunk.strip():
142
- # metadata = {
143
- # "source": filename,
144
- # "category": category
145
- # }
146
- # docs.append(Document(
147
- # page_content=chunk.strip(),
148
- # metadata=metadata
149
- # ))
150
- # except Exception as e:
151
- # print(f"Error processing {file_path}: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
- # # Create vector store (Your original logic)
154
- # if docs:
155
- # vector_store = Chroma.from_documents(
156
- # documents=docs,
157
- # embedding=embeddings,
158
- # persist_directory=persist_dir
159
- # )
160
- # vector_store.persist()
161
- # vector_stores[category] = vector_store
162
- # print(f"Created and persisted new vector store for {category} with {len(docs)} documents.")
163
- # else:
164
- # print(f"No documents found for {category}. Skipping QA chain setup.")
165
- # continue # Skip to the next category
166
-
167
- # # --- 3. Create QA Chain (if vector_store was loaded or created) ---
168
- # if vector_store:
169
- # # Create QA chain with updated prompt
170
- # prompt = PromptTemplate(
171
- # template=custom_prompt_template.replace("{category}", category),
172
- # input_variables=["context", "question"]
173
- # )
174
- # qa_chain = RetrievalQA.from_chain_type(
175
- # llm=llm,
176
- # chain_type="stuff",
177
- # retriever=vector_store.as_retriever(search_kwargs={"k": 5}),
178
- # chain_type_kwargs={"prompt": prompt},
179
- # return_source_documents=True
180
- # )
181
- # qa_chains[category] = qa_chain
182
- # print(f"Initialized {category} QA chain.")
183
-
184
- # # The rest of your code
185
- # return qa_chains
186
 
187
  # Classify question to category
188
  def classify_question_category(question):
@@ -195,18 +212,18 @@ def classify_question_category(question):
195
  print(response)
196
  return response.content.strip()
197
 
198
- # # Get RAG response using category-specific QA chain
199
- # def get_rag_response(question, qa_chains):
200
- # # Classify question
201
- # category = classify_question_category(question)
202
 
203
- # if category not in qa_chains:
204
- # # Fallback to first available chain
205
- # category = list(qa_chains.keys())[0]
206
 
207
- # # Get response
208
- # result = qa_chains[category].invoke({"query": question})
209
- # return result['result']
210
 
211
 
212
  # Classify user input
@@ -223,8 +240,9 @@ def classify_input(user_input):
223
  response = llm.invoke(prompt)
224
  return response.content.strip().lower()
225
 
226
- def parse_weird_json(text_data):
227
 
 
 
228
  fixed_json_string = re.sub(r'\]\s*\[', ', ', text_data.strip())
229
 
230
  # Step B: Load it as standard JSON
 
1
+
2
  # utils.py
3
  import os
4
  import json
 
9
  # from langchain_huggingface import HuggingFaceEmbeddings
10
  # from langchain_community.vectorstores import Chroma
11
  # from langchain.prompts import PromptTemplate
12
+
13
+ from langchain_core.documents import Document
14
+ from langchain_chroma import Chroma
15
+ from langchain_huggingface import HuggingFaceEmbeddings
16
+ from langchain_core.prompts import PromptTemplate
17
+ # from langchain.chains import RetrievalQA
18
+ from langchain_classic.chains import create_retrieval_chain
19
+ from langchain_classic.chains.combine_documents import create_stuff_documents_chain
20
  from configure import USER_DATA_PATH, RAG_BASE_DIRECTORY, RAG_CATEGORIES
21
  import shutil
22
  from dotenv import load_dotenv
 
24
  import certifi
25
  import re
26
 
 
27
  load_dotenv()
28
 
29
 
 
49
 
50
  # LLM setup
51
  llm = ChatGroq(
52
+ api_key="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",
53
  model="llama-3.3-70b-versatile",
54
  temperature=0,
55
  max_tokens=4000
 
65
  # text_splitter = LLMChunking()
66
 
67
  # Embeddings
68
+ embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
69
+ # embeddings = None
70
 
71
  # Load user data
72
  def load_user_data():
 
84
  json.dump(data, f, indent=2)
85
 
86
  # Initialize RAG with per-category vector stores and QA chains
87
+ def initialize_rag():
88
+ """
89
+ Initialize RAG vector stores and chains using modern LangChain (LCEL).
90
+ Args:
91
+ llm: The initialized ChatGroq (or other) LLM object.
92
+ embeddings: The initialized HuggingFaceEmbeddings object.
93
+ text_splitter: The initialized RecursiveCharacterTextSplitter object.
94
+ """
95
+ vector_stores = {}
96
+ qa_chains = {}
 
 
 
 
 
 
 
 
 
 
97
 
98
+ # 1. Define Prompt Template (Modern LCEL Format)
99
+ # Note: Modern chains typically look for "context" and "input" variables.
100
+ base_prompt_template = """You are a {category} wellness expert. Provide helpful advice with specific actions:
101
+
102
+ 1. Start with a brief empathetic response to the user's concern
103
+ 2. Offer 1-3 actionable suggestions with brief explanations
104
+ 3. End with an open-ended question to continue conversation
105
+
106
+ Guidelines:
107
+ - Keep responses conversational and supportive
108
+ - Avoid clinical jargon
109
+ - Focus on practical, implementable advice
110
+ - Maintain hopeful and encouraging tone
111
+
112
+ Context:
113
+ {context}
114
+
115
+ Question: {input}
116
+ """
117
 
118
+ for category in RAG_CATEGORIES:
119
+ persist_dir = f"./chroma_db_{category}"
120
+ vector_store = None
121
+
122
+ # --- 2. Check/Load Existing Vector Store ---
123
+ if os.path.exists(persist_dir):
124
+ print(f"Found existing vector store for {category}. Attempting to load...")
125
+ try:
126
+ vector_store = Chroma(
127
+ persist_directory=persist_dir,
128
+ embedding_function=embeddings # UPDATED: 'embedding_function', not 'embedding'
129
+ )
130
+ vector_stores[category] = vector_store
131
+ except Exception as e:
132
+ print(f"Error loading existing store {persist_dir}: {e}")
133
+ print("Will delete and attempt to re-build.")
134
+ shutil.rmtree(persist_dir)
 
 
 
 
 
135
 
136
+ # --- 3. Create Vector Store if needed ---
137
+ if vector_store is None:
138
+ print(f"No valid vector store for {category} found. Creating new one...")
139
+
140
+ dir_path = os.path.join(RAG_BASE_DIRECTORY, category)
141
+ docs = []
142
 
143
+ if os.path.exists(dir_path):
144
+ for filename in os.listdir(dir_path):
145
+ if filename.endswith('.txt'):
146
+ file_path = os.path.join(dir_path, filename)
147
+ try:
148
+ with open(file_path, 'r', encoding='utf-8') as f:
149
+ text = f.read()
150
+
151
+ chunks = text_splitter.split_text(text)
152
+ for chunk in chunks:
153
+ if chunk.strip():
154
+ metadata = {
155
+ "source": filename,
156
+ "category": category
157
+ }
158
+ docs.append(Document(
159
+ page_content=chunk.strip(),
160
+ metadata=metadata
161
+ ))
162
+ except Exception as e:
163
+ print(f"Error processing {file_path}: {e}")
164
+
165
+ if docs:
166
+ # UPDATED: Use 'embedding_function' instead of 'embedding'
167
+ # UPDATED: Removed .persist() call (Auto-persists in new version)
168
+ vector_store = Chroma.from_documents(
169
+ documents=docs,
170
+ embedding=embeddings,
171
+ persist_directory=persist_dir
172
+ )
173
+ vector_stores[category] = vector_store
174
+ print(f"Created new vector store for {category} with {len(docs)} documents.")
175
+ else:
176
+ print(f"No documents found for {category}. Skipping QA chain setup.")
177
+ continue
178
+
179
+ # --- 4. Create QA Chain (LCEL Style) ---
180
+ if vector_store:
181
+ # A. Create the Prompt
182
+ # We inject the specific category into the template string immediately
183
+ category_specific_template = base_prompt_template.replace("{category}", category)
184
 
185
+ prompt = PromptTemplate(
186
+ template=category_specific_template,
187
+ input_variables=["context", "input"] # LCEL standard variables
188
+ )
189
+
190
+ # B. Create the Document Chain (LLM + Prompt)
191
+ question_answer_chain = create_stuff_documents_chain(llm, prompt)
192
+
193
+ # C. Create the Retrieval Chain (Retriever + Document Chain)
194
+ retriever = vector_store.as_retriever(search_kwargs={"k": 5})
195
+ rag_chain = create_retrieval_chain(retriever, question_answer_chain)
196
+
197
+ qa_chains[category] = rag_chain
198
+ print(f"Initialized {category} QA chain.")
199
+
200
+ return qa_chains
201
+
202
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
 
204
  # Classify question to category
205
  def classify_question_category(question):
 
212
  print(response)
213
  return response.content.strip()
214
 
215
+ # Get RAG response using category-specific QA chain
216
+ def get_rag_response(question, qa_chains):
217
+ # Classify question
218
+ category = classify_question_category(question)
219
 
220
+ if category not in qa_chains:
221
+ # Fallback to first available chain
222
+ category = list(qa_chains.keys())[0]
223
 
224
+ # Get response
225
+ result = qa_chains[category].invoke({"input": question})
226
+ return result['answer']
227
 
228
 
229
  # Classify user input
 
240
  response = llm.invoke(prompt)
241
  return response.content.strip().lower()
242
 
 
243
 
244
+
245
+ def parse_weird_json(text_data):
246
  fixed_json_string = re.sub(r'\]\s*\[', ', ', text_data.strip())
247
 
248
  # Step B: Load it as standard JSON