{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "11d5ae50",
   "metadata": {},
   "source": [
    "# Experimenting with HyDE\n",
    "\n",
    "Using this to explore query rewrites\\\n",
    "August 2025"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "813f8b1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import (\n",
    "    StorageContext,\n",
    "    load_index_from_storage,\n",
    "    ServiceContext,\n",
    "    Settings,\n",
    ")\n",
    "from llama_index.embeddings.huggingface import HuggingFaceEmbedding\n",
    "from llama_index.llms.ollama import Ollama\n",
    "from llama_index.core.prompts import PromptTemplate\n",
    "from llama_index.core.indices.query.query_transform import HyDEQueryTransform\n",
    "from llama_index.core.query_engine.transform_query_engine import TransformQueryEngine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f3d65589",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm=\"llama3.1:8B\"\n",
    "\n",
    "# Use a local model to generate\n",
    "Settings.llm = Ollama(\n",
    "    model=llm,    # First model tested\n",
    "    request_timeout=360.0,\n",
    "    context_window=8000,\n",
    "    temperature=0.7,\n",
    "    )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "afd593ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load embedding model (same as used for vector store)\n",
    "embed_model = HuggingFaceEmbedding(model_name=\"all-mpnet-base-v2\")\n",
    "Settings.embed_model = embed_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "04c702a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original query: Find entries with strong feelings of depression.\n",
      "HyDE-generated query (used for embedding):\n",
      " Find entries with strong feelings of depression.\n"
     ]
    }
   ],
   "source": [
    "#Initial query\n",
    "initial_query = \"Find entries with strong feelings of depression.\"\n",
    "\n",
    "# Define a custom HyDE prompt (this is fully supported)\n",
    "hyde_prompt = PromptTemplate(\n",
    "    \"You are a helpful assistant. Generate a detailed hypothetical answer to the user query below.\\n\\nQuery: {query_str}\\n\\nAnswer:\"\n",
    ")\n",
    "\n",
    "hyde_transform = HyDEQueryTransform(llm=Settings.llm,hyde_prompt=hyde_prompt,include_original=False)\n",
    "\n",
    "# Run the transform manually\n",
    "hyde_query = hyde_transform.run(initial_query)\n",
    "\n",
    "# Print the result\n",
    "print(\"Original query:\", initial_query)\n",
    "print(\"HyDE-generated query (used for embedding):\\n\", hyde_query.query_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "3b211daf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There are many important feelings that people experience in their lives. Here are some examples:\n",
      "\n",
      "1. **Love**: A strong affection or attachment to someone, which can be romantic, familial, or platonic.\n",
      "2. **Happiness**: A positive emotional state characterized by a sense of joy, contentment, and satisfaction.\n",
      "3. **Empathy**: The ability to understand and share the feelings of others, which is essential for building strong relationships and fostering compassion.\n",
      "4. **Gratitude**: Feeling thankful or appreciative for something or someone in one's life, which can cultivate a positive outlook and well-being.\n",
      "5. **Compassion**: A feeling of concern and kindness towards others who are suffering or struggling, which can inspire acts of service and support.\n",
      "6. **Confidence**: A sense of self-assurance and faith in one's abilities, which is essential for personal growth and achievement.\n",
      "7. **Respect**: Feeling admiration or esteem for someone or something, which is necessary for building strong relationships and social bonds.\n",
      "8. **Forgiveness**: The ability to let go of negative emotions and forgive oneself or others for past mistakes or hurtful actions.\n",
      "9. **Excitement**: A feeling of enthusiasm and eagerness, often accompanied by a sense of anticipation or adventure.\n",
      "10. **Serenity**: A state of calmness and peace, which can be cultivated through mindfulness and self-reflection.\n",
      "\n",
      "These feelings are essential for human well-being and relationships, and they play important roles in shaping our experiences and interactions with others.\n",
      "\n",
      "Would you like me to expand on any of these feelings or explore other emotions?\n"
     ]
    }
   ],
   "source": [
    "# Check that the LLM is working\n",
    "# confirmed that this generates different responses each time\n",
    "response = Settings.llm.complete(\"What are several important feelings?\")\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "9db5c9c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HyDE output:\n",
      " Find entries with strong feelings of depression.\n"
     ]
    }
   ],
   "source": [
    "# Test for silent errors. Output verifies working.\n",
    "try:\n",
    "    hyde_result = hyde_transform.run(initial_query)\n",
    "    print(\"HyDE output:\\n\", hyde_result)\n",
    "except Exception as e:\n",
    "    print(\"LLM error:\", e)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5add1ed",
   "metadata": {},
   "source": [
    "## Testing HyDE based on llamaindex documentation\n",
    "\n",
    "https://docs.llamaindex.ai/en/stable/examples/query_transformations/HyDEQueryTransformDemo/#querying-without-transformation-yields-reasonable-answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "90381bc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[\"Here is a passage that includes several key details about depression:\\n\\n**The Descent into Darkness**\\n\\nAs I lay in bed, staring blankly at the ceiling, I felt an overwhelming sense of hopelessness wash over me. The darkness seemed to close in around me, suffocating me with its crushing weight. Every thought felt like a burden, every decision a chore. I couldn't bear the idea of getting out of bed, of facing another day filled with anxiety and despair.\\n\\nI had been struggling with depression for what felt like an eternity. The symptoms had started slowly, a nagging feeling that something was off, but I had tried to brush it aside as mere exhaustion or stress. But as time went on, the feelings intensified, until they became all-consuming. I felt like I was drowning in a sea of sadness, unable to find a lifeline.\\n\\nThe smallest things would set me off - a harsh word from a loved one, a missed deadline at work, even just getting out of bed and facing another day. The world seemed too much for me to handle, and I retreated into my own private hell of despair. I couldn't eat, couldn't sleep, couldn't find any joy in the things that used to bring me happiness.\\n\\nAs I looked back on the past few months, I realized that this wasn't just a passing phase or a normal response to stress. Depression had taken hold, and it was suffocating me. I knew I needed help, but the thought of seeking treatment seemed daunting, even terrifying. What if they couldn't help me? What if I was stuck in this pit forever?\\n\\nI felt like I was losing myself, bit by bit, as depression consumed me. I longed for a glimmer of hope, a spark of light to guide me through the darkness. But it seemed elusive, always just out of reach.\\n\\nThis passage includes several key details about depression, including:\\n\\n* **Overwhelming feelings of sadness and hopelessness**: The protagonist feels an intense sense of despair that is difficult to shake.\\n* **Loss of motivation**: They feel like they can't get out of bed or face another day filled with anxiety and despair.\\n* **Withdrawal from activities**: They have lost interest in things that used to bring them joy, and are unable to eat or sleep.\\n* **Social isolation**: They retreat into their own private hell, feeling disconnected from others.\\n* **Loss of identity**: They feel like they are losing themselves as depression consumes them.\\n* **Fear of seeking help**: The protagonist is afraid to seek treatment, fearing that it won't work or that they will be stuck in this state forever.\",\n",
       " 'Find entries with strong feelings of depression.']"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hyde = HyDEQueryTransform(llm=Settings.llm,include_original=True)\n",
    "query_str = \"Find entries with strong feelings of depression.\"\n",
    "query_bundle = hyde(query_str)\n",
    "hyde_doc = query_bundle.embedding_strs\n",
    "hyde_doc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "08e7eca4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "[\"Here is a passage that includes several key details about depression:\\n\\n**The Descent into Darkness**\\n\\nAs I lay in bed, staring blankly at the ceiling, I felt an overwhelming sense of hopelessness wash over me. The darkness seemed to close in around me, suffocating me with its crushing weight. Every thought felt like a burden, every decision a chore. I couldn't bear the idea of getting out of bed, of facing another day filled with anxiety and despair.\\n\\nI had been struggling with depression for what felt like an eternity. The symptoms had started slowly, a nagging feeling that something was off, but I had tried to brush it aside as mere exhaustion or stress. But as time went on, the feelings intensified, until they became all-consuming. I felt like I was drowning in a sea of sadness, unable to find a lifeline.\\n\\nThe smallest things would set me off - a harsh word from a loved one, a missed deadline at work, even just getting out of bed and facing another day. The world seemed too much for me to handle, and I retreated into my own private hell of despair. I couldn't eat, couldn't sleep, couldn't find any joy in the things that used to bring me happiness.\\n\\nAs I looked back on the past few months, I realized that this wasn't just a passing phase or a normal response to stress. Depression had taken hold, and it was suffocating me. I knew I needed help, but the thought of seeking treatment seemed daunting, even terrifying. What if they couldn't help me? What if I was stuck in this pit forever?\\n\\nI felt like I was losing myself, bit by bit, as depression consumed me. I longed for a glimmer of hope, a spark of light to guide me through the darkness. But it seemed elusive, always just out of reach.\\n\\nThis passage includes several key details about depression, including:\\n\\n* **Overwhelming feelings of sadness and hopelessness**: The protagonist feels an intense sense of despair that is difficult to shake.\\n* **Loss of motivation**: They feel like they can't get out of bed or face another day filled with anxiety and despair.\\n* **Withdrawal from activities**: They have lost interest in things that used to bring them joy, and are unable to eat or sleep.\\n* **Social isolation**: They retreat into their own private hell, feeling disconnected from others.\\n* **Loss of identity**: They feel like they are losing themselves as depression consumes them.\\n* **Fear of seeking help**: The protagonist is afraid to seek treatment, fearing that it won't work or that they will be stuck in this state forever.\", 'Find entries with strong feelings of depression.']"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import Markdown, display\n",
    "display(Markdown(f\"{hyde_doc}\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ca50f9d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
