{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "190b630b-a119-45d8-b389-98dadaeb7dfd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain import PromptTemplate\n",
    "from langchain.chains import RetrievalQA\n",
    "from langchain.embeddings import HuggingFaceEmbeddings\n",
    "from langchain.vectorstores import Chroma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f96c95be-4c37-490f-959d-451ea30ad7b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.document_loaders import PyPDFLoader,DirectoryLoader\n",
    "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.llms import CTransformers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1fc7efb6-324b-4178-85e2-98b3932f1ee0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_pdf(data):\n",
    "    loader=DirectoryLoader(data,glob=\"*.pdf\",loader_cls=PyPDFLoader)\n",
    "    documents=loader.load()\n",
    "    return documents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9af3480e-12cf-4816-9573-96b79bc2f336",
   "metadata": {},
   "outputs": [],
   "source": [
    "extracted_data=load_pdf(\"data/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66f63c86-5b80-4f7b-bb03-687de3d03714",
   "metadata": {},
   "outputs": [],
   "source": [
    "#extracted_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10a566fd-9ee5-4851-8566-cac6826bc2d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "text_splitter=RecursiveCharacterTextSplitter(chunk_size=2000,chunk_overlap=80)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ad3fb0c-46ed-4fd1-9f6f-d4d67524571c",
   "metadata": {},
   "outputs": [],
   "source": [
    "text_chunks=text_splitter.split_documents(extracted_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "195b0b99-4390-4ffb-aee5-8db8f51c417e",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(text_chunks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8e166d17-cd1e-4830-a18e-aab609feaea5",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\gyanb\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:139: LangChainDeprecationWarning: The class `HuggingFaceEmbeddings` was deprecated in LangChain 0.2.2 and will be removed in 0.3.0. An updated version of the class exists in the langchain-huggingface package and should be used instead. To use it run `pip install -U langchain-huggingface` and import as `from langchain_huggingface import HuggingFaceEmbeddings`.\n",
      "  warn_deprecated(\n",
      "C:\\Users\\gyanb\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\sentence_transformers\\cross_encoder\\CrossEncoder.py:11: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
      "  from tqdm.autonotebook import tqdm, trange\n",
      "C:\\Users\\gyanb\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\huggingface_hub\\file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "embeddings=HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-MiniLM-L6-v2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1ed3add3-a8c6-4d6c-a055-b48b009eaad1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "HuggingFaceEmbeddings(client=SentenceTransformer(\n",
       "  (0): Transformer({'max_seq_length': 256, 'do_lower_case': False}) with Transformer model: BertModel \n",
       "  (1): Pooling({'word_embedding_dimension': 384, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False, 'include_prompt': True})\n",
       "  (2): Normalize()\n",
       "), model_name='sentence-transformers/all-MiniLM-L6-v2', cache_folder=None, model_kwargs={}, encode_kwargs={}, multi_process=False, show_progress=False)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "3605119d-59b5-4b53-88dc-309a9a77af3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "query_result=embeddings.embed_query(\"Hello World\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "bdbe509c-bc65-4af7-98e2-4f448eb601b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "384"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(query_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bc89af76-fc32-4d82-a5fd-6bdab9942115",
   "metadata": {},
   "outputs": [],
   "source": [
    "persist_directory='db'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "beddc393-844b-46db-8326-e3a9338aa5fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "vectordb=Chroma.from_documents(documents=text_chunks,embedding=embeddings,persist_directory=persist_directory)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "47b5d65b-c719-4dbc-b48d-d0fb4f9e4d11",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\gyanb\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\langchain_core\\_api\\deprecation.py:139: LangChainDeprecationWarning: Since Chroma 0.4.x the manual persistence method is no longer supported as docs are automatically persisted.\n",
      "  warn_deprecated(\n"
     ]
    }
   ],
   "source": [
    "vectordb.persist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "56592917-115a-4b7f-a75e-392555dd2ca8",
   "metadata": {},
   "outputs": [],
   "source": [
    "vectordb=None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f45f9c09-45dd-4f9a-9b9e-1c0146b6df95",
   "metadata": {},
   "outputs": [],
   "source": [
    "vectordb=Chroma(persist_directory=persist_directory,embedding_function=embeddings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "03c6e919-23e7-4698-b24d-5fa1f2928641",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<langchain_community.vectorstores.chroma.Chroma at 0x25f64898e10>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vectordb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "81188bcb-0922-44ec-8c04-626707285a4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever=vectordb.as_retriever()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a2c356fe-fa11-47e4-b642-bdc3e62aec0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "retriever=vectordb.as_retriever(search_kwargs={\"k\":2})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "09b74205-9f14-4782-a2dd-7f8f8b58435a",
   "metadata": {},
   "outputs": [],
   "source": [
    "docs=retriever.invoke(\"What are allergies\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "936fd2f9-c924-4d27-8e7d-d1f609ef06c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Document(page_content='Allergic React ions \\nLisa R. Palivos, MD \\nKey Points \\n• Epinephrine is the first-line medication for the treat\\xad\\nment of anaphylaxis. Second-line treatment consists of \\nantihistamines and corticosteroids. \\n• Urticaria may be the first sign of what might progress to \\nangioedema or anaphylaxis. \\nINTRODUCTION \\nAn allergic reaction is the body\\'s way of responding to \\nforeign substances that come in contact with the skin, nose, \\neyes, respiratory tract or gastrointestinal tract. Examples of \\nallergens are dust, pollen, plants, medications, foods, latex, \\nand insect bites. Anything can be an allergen. Allergic \\nreactions can range from mild local urticarial eruptions \\nto severe and life-threatening airway obstruction, respira \\xad\\ntory failure, and circulatory colla pse. Urticaria, or \"hives;\\' \\nis an immunoglobulin E (IgE)-m ediated hypersensitivity \\nreaction to an allergen resulting in red, raised wheals that \\nitch and sting. Circulating antibodies bind the allergen and \\nIgE receptors on mast cells. In response, mast cells release \\ninflammatory substances (histamine, bradyki nin), which \\nresults in increased vascular permea bility. Urticaria is one \\nof the most common skin lesions seen in the emergency \\ndepartment (ED) in both young and older patients. About \\n20% of the population experiences at least 1 attack of \\nurticaria in their lifetim e. \\nAngioedema is nonpitting edema of the deeper layers of \\nthe skin owing to a loss of vascular integrity caused by inflam\\xad\\nmatory mediators. It is not pruritic but can cause burning, \\nnumbness, or pain, generally in the face or neck. Approx\\xad\\nimately 94% of the cases of angioedema presenting to \\nthe ED are drug-induced. Most drug-induced angioedema \\noccurs in patients taking angiotensin-converting enzyme • For patients with respiratory symptoms or throat swell\\xad\\ning, perform a rapid assessment of the airway and \\nintubate early. \\n• Attempt to determine and then discontinue the inciting \\nagent.', metadata={'page': 441, 'source': 'data\\\\Clinical Emergency Medicine ( PDFDrive.com ).pdf'}),\n",
       " Document(page_content='KEY TERMS\\nAllergen —Any substance that irritates only those\\nwho are sensitive (allergic) to it.\\nAsthma —Wheezing (labored breathing) due to\\nallergies or irritation of the lungs.\\nDecongestant —Medicines that shrink blood ves-\\nsels and consequently mucus membranes. Pseu-doephedrine, phenylephrine, and phenylpropano-lamine are the most common.\\nSinus —Air-filled cavities surrounding the eyes and\\nnose are lined with mucus-producing membranes.They cleanse the nose, add resonance to the voice,and partially determine the structure of the face.\\nSinuses are lined with mucus membranes, just like the\\nnose. Polyps can easily obstruct the drainage of mucusfrom the sinuses. When any fluid in the body is trappedso it cannot flow freely, it becomes infected. The result,sinusitis , is a common complication of allergic rhinitis .\\nCauses and symptoms\\nSome people who are allergic to aspirin develop\\nboth asthma and nasal polyps.\\nNasal polyps often plug the nose, usually one side at\\na time. People with allergic rhinitis are so used to havinga stopped up nose they may not notice the differencewhen a polyp develops. Other polyps may be closer to asinus opening, so airflow is not obstructed, but mucusbecomes trapped in the sinus. In this case, there is a feel-ing of fullness in the head, no sense of smell, and per-haps a headache . The trapped mucus will eventually get\\ninfected, adding pain , fever, and perhaps bloody dis-\\ncharge from the nose.\\nDiagnosis\\nA physical examination will identify most polyps.\\nSmall polyps located higher up or further back may behidden from view, but they will be detected with moresophisticated medical instruments. The otorhinolaryngol-ogist is equipped to diagnose nasal polyps. In order toperform the exam, medicine must be applied to decon-gest the membranes. Cotton balls soaked with one ofthese agents and left in the nostrils for a few minutes pro-vide adequate shrinkage.\\nTreatment\\nMost polyps can be removed by the head and neck', metadata={'page': 22, 'source': 'data\\\\Gale Encyclopedia of Medicine Vol. 4 (N-S).pdf'})]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "docs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ec2caea3-0536-4196-b0e4-3bcc07cb5c0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_template='''\n",
    "Use the following pieces of information to answer the user's question.\n",
    "If you don't know the answer,just say that you don't know,don't try to makeup the answer.\n",
    "Context: {context}\n",
    "Question: {question}\n",
    "\n",
    "Only return the helpful aswer below and nothing else.\n",
    "Helpful Answer:\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bd7c332d-5b5f-47ac-afce-f38f079a9024",
   "metadata": {},
   "outputs": [],
   "source": [
    "PROMPT=PromptTemplate(template=prompt_template,input_variales=[\"context\",\"question\"])\n",
    "chain_type_kwargs={\"prompt\":PROMPT}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "84c36238-1ac2-4a09-b5dc-77ce79450a3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm=CTransformers(model=\"model/llama-2-7b-chat.ggmlv3.q4_0.bin\",model_type=\"llama\",config={'max_new_tokens':2048,'context_length' : 1024,'temperature':0.8})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b562786d-0cfe-43ff-8add-5576ca021007",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa=RetrievalQA.from_chain_type(llm=llm,chain_type=\"stuff\",retriever=vectordb.as_retriever(search_kwargs={\"k\":2}),return_source_documents=True,chain_type_kwargs=chain_type_kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1596abaf-3a93-4ebd-89fe-a9ac6e4f70dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings \n",
    "warnings.filterwarnings('ignore') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "aa30e298-d248-4723-a3a7-4b37a6745e80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Input Prompt: what ia achne\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Response:  An answer to this question is not possible without more context or information about the specific person being referred to. Personality disorders are complex mental health conditions that can manifest in a variety of ways, and it would be impossible to accurately identify the type of personality disorder someone has based solely on the information provided.\n",
      "\n",
      "To provide a proper diagnosis and treatment plan, a qualified mental health professional would need to conduct a comprehensive evaluation of the individual's symptoms, behaviors, and medical history. This would involve several sessions of psychotherapy and may also include psychological testing and assessment tools.\n",
      "\n",
      "In summary, while you may have some general knowledge about personality disorders, it is not possible to accurately identify the specific type of disorder someone has without a thorough evaluation by a qualified mental health professional.\n"
     ]
    }
   ],
   "source": [
    " user_input=input(f\"Input Prompt:\")\n",
    "result=qa({\"query\":user_input})\n",
    "print(\"Response: \",result[\"result\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "328f2e08-f3bd-459b-bea6-18da1885968c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d6dceeb0-3fdb-4faf-b6da-9afeda53d23f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Input Prompt: what is allergies\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Response:  Allergy is an abnormal immune response to a substance that is typically harmless but can cause an exaggerated reaction in some people. Allergic reactions occur when the body's immune system mistakenly identifies a harmless substance as a threat and attempts to fight it off, leading to inflammation and damage. Common allergy triggers include pollen, dust mites, mold, pet dander, and certain foods like nuts or shellfish. Symptoms of an allergic reaction can range from mild discomfort to life-threatening reactions, depending on the severity of the reaction. Treatment options include medication, immunotherapy, and avoidance of the allergen.\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Input Prompt: what is malaria\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Response:  Malaria is a serious disease caused by a parasitic infection, typically transmitted through the bite of an infected mosquito. The parasites that cause malaria are known as Plasmodium species, and there are several different species that can cause the disease, including P. falciparum, P. vivax, and P. ovale. Malaria can cause a wide range of symptoms, including fever, chills, headache, muscle and joint pain, fatigue, and in severe cases, it can lead to organ failure and death. Diagnosis of malaria is typically made through a combination of medical history, physical examination, and laboratory tests, such as microscopy or rapid diagnostic tests (RDTs) that detect the presence of the parasite in the blood. Treatment for malaria usually involves antimalarial drugs, which can help to eliminate the parasite from the body and alleviate symptoms. Prevention measures for malaria include using insecticide-treated bed nets, wearing protective clothing, and using anti-mosquito agents when traveling to areas where the disease is common.\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Input Prompt: Exit\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    user_input=input(f\"Input Prompt:\")\n",
    "    if user_input==\"Exit\":\n",
    "        break\n",
    "    result=qa({\"query\":user_input})\n",
    "    print(\"Response: \",result[\"result\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65a8da67-d3c8-4a8b-987e-520ecac7cdbb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
