{ "cells": [ { "cell_type": "code", "execution_count": 50, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "f7gr5j8iDEva", "outputId": "ab697b5e-e2ca-460f-c652-a6ee865f3057" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Requirement already satisfied: sentence_transformers in /usr/local/lib/python3.10/dist-packages (2.6.1)\n", "Requirement already satisfied: openai in /usr/local/lib/python3.10/dist-packages (1.16.2)\n", "Requirement already satisfied: unstructured in /usr/local/lib/python3.10/dist-packages (0.13.2)\n", "Requirement already satisfied: transformers<5.0.0,>=4.32.0 in /usr/local/lib/python3.10/dist-packages (from sentence_transformers) (4.38.2)\n", "Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from sentence_transformers) (4.66.2)\n", "Requirement already satisfied: torch>=1.11.0 in /usr/local/lib/python3.10/dist-packages (from sentence_transformers) (2.2.1+cu121)\n", "Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from sentence_transformers) (1.25.2)\n", "Requirement already satisfied: scikit-learn in /usr/local/lib/python3.10/dist-packages (from sentence_transformers) (1.2.2)\n", "Requirement already satisfied: scipy in /usr/local/lib/python3.10/dist-packages (from sentence_transformers) (1.11.4)\n", "Requirement already satisfied: huggingface-hub>=0.15.1 in /usr/local/lib/python3.10/dist-packages (from sentence_transformers) (0.20.3)\n", "Requirement already satisfied: Pillow in /usr/local/lib/python3.10/dist-packages (from sentence_transformers) (9.4.0)\n", "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai) (3.7.1)\n", "Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai) (1.7.0)\n", "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from openai) (0.27.0)\n", "Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from openai) (2.6.4)\n", "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai) (1.3.1)\n", "Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from openai) (4.10.0)\n", "Requirement already satisfied: chardet in /usr/local/lib/python3.10/dist-packages (from unstructured) (5.2.0)\n", "Requirement already satisfied: filetype in /usr/local/lib/python3.10/dist-packages (from unstructured) (1.2.0)\n", "Requirement already satisfied: python-magic in /usr/local/lib/python3.10/dist-packages (from unstructured) (0.4.27)\n", "Requirement already satisfied: lxml in /usr/local/lib/python3.10/dist-packages (from unstructured) (4.9.4)\n", "Requirement already satisfied: nltk in /usr/local/lib/python3.10/dist-packages (from unstructured) (3.8.1)\n", "Requirement already satisfied: tabulate in /usr/local/lib/python3.10/dist-packages (from unstructured) (0.9.0)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from unstructured) (2.31.0)\n", "Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.10/dist-packages (from unstructured) (4.12.3)\n", "Requirement already satisfied: emoji in /usr/local/lib/python3.10/dist-packages (from unstructured) (2.11.0)\n", "Requirement already satisfied: dataclasses-json in /usr/local/lib/python3.10/dist-packages (from unstructured) (0.6.4)\n", "Requirement already satisfied: python-iso639 in /usr/local/lib/python3.10/dist-packages (from unstructured) (2024.2.7)\n", "Requirement already satisfied: langdetect in /usr/local/lib/python3.10/dist-packages (from unstructured) (1.0.9)\n", "Requirement already satisfied: rapidfuzz in /usr/local/lib/python3.10/dist-packages (from unstructured) (3.8.0)\n", "Requirement already satisfied: backoff in /usr/local/lib/python3.10/dist-packages (from unstructured) (2.2.1)\n", "Requirement already satisfied: unstructured-client<=0.18.0 in /usr/local/lib/python3.10/dist-packages (from unstructured) (0.18.0)\n", "Requirement already satisfied: wrapt in /usr/local/lib/python3.10/dist-packages (from unstructured) (1.14.1)\n", "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai) (3.6)\n", "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai) (1.2.0)\n", "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai) (2024.2.2)\n", "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai) (1.0.5)\n", "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0)\n", "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.15.1->sentence_transformers) (3.13.3)\n", "Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.15.1->sentence_transformers) (2023.6.0)\n", "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.15.1->sentence_transformers) (6.0.1)\n", "Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.15.1->sentence_transformers) (23.2)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.16.3 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->openai) (2.16.3)\n", "Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (1.12)\n", "Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (3.2.1)\n", "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (3.1.3)\n", "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (12.1.105)\n", "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (12.1.105)\n", "Requirement already satisfied: nvidia-cudnn-cu12==8.9.2.26 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (8.9.2.26)\n", "Requirement already satisfied: nvidia-cublas-cu12==12.1.3.1 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (12.1.3.1)\n", "Requirement already satisfied: nvidia-cufft-cu12==11.0.2.54 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (11.0.2.54)\n", "Requirement already satisfied: nvidia-curand-cu12==10.3.2.106 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (10.3.2.106)\n", "Requirement already satisfied: nvidia-cusolver-cu12==11.4.5.107 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (11.4.5.107)\n", "Requirement already satisfied: nvidia-cusparse-cu12==12.1.0.106 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (12.1.0.106)\n", "Requirement already satisfied: nvidia-nccl-cu12==2.19.3 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (2.19.3)\n", "Requirement already satisfied: nvidia-nvtx-cu12==12.1.105 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (12.1.105)\n", "Requirement already satisfied: triton==2.2.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.11.0->sentence_transformers) (2.2.0)\n", "Requirement already satisfied: nvidia-nvjitlink-cu12 in /usr/local/lib/python3.10/dist-packages (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.11.0->sentence_transformers) (12.4.127)\n", "Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers<5.0.0,>=4.32.0->sentence_transformers) (2023.12.25)\n", "Requirement already satisfied: tokenizers<0.19,>=0.14 in /usr/local/lib/python3.10/dist-packages (from transformers<5.0.0,>=4.32.0->sentence_transformers) (0.15.2)\n", "Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.10/dist-packages (from transformers<5.0.0,>=4.32.0->sentence_transformers) (0.4.2)\n", "Requirement already satisfied: charset-normalizer>=3.2.0 in /usr/local/lib/python3.10/dist-packages (from unstructured-client<=0.18.0->unstructured) (3.3.2)\n", "Requirement already satisfied: dataclasses-json-speakeasy>=0.5.11 in /usr/local/lib/python3.10/dist-packages (from unstructured-client<=0.18.0->unstructured) (0.5.11)\n", "Requirement already satisfied: jsonpath-python>=1.0.6 in /usr/local/lib/python3.10/dist-packages (from unstructured-client<=0.18.0->unstructured) (1.0.6)\n", "Requirement already satisfied: marshmallow>=3.19.0 in /usr/local/lib/python3.10/dist-packages (from unstructured-client<=0.18.0->unstructured) (3.21.1)\n", "Requirement already satisfied: mypy-extensions>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from unstructured-client<=0.18.0->unstructured) (1.0.0)\n", "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from unstructured-client<=0.18.0->unstructured) (2.8.2)\n", "Requirement already satisfied: six>=1.16.0 in /usr/local/lib/python3.10/dist-packages (from unstructured-client<=0.18.0->unstructured) (1.16.0)\n", "Requirement already satisfied: typing-inspect>=0.9.0 in /usr/local/lib/python3.10/dist-packages (from unstructured-client<=0.18.0->unstructured) (0.9.0)\n", "Requirement already satisfied: urllib3>=1.26.18 in /usr/local/lib/python3.10/dist-packages (from unstructured-client<=0.18.0->unstructured) (2.0.7)\n", "Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4->unstructured) (2.5)\n", "Requirement already satisfied: click in /usr/local/lib/python3.10/dist-packages (from nltk->unstructured) (8.1.7)\n", "Requirement already satisfied: joblib in /usr/local/lib/python3.10/dist-packages (from nltk->unstructured) (1.3.2)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn->sentence_transformers) (3.4.0)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=1.11.0->sentence_transformers) (2.1.5)\n", "Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch>=1.11.0->sentence_transformers) (1.3.0)\n", "Requirement already satisfied: plotly in /usr/local/lib/python3.10/dist-packages (5.15.0)\n", "Requirement already satisfied: tenacity>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from plotly) (8.2.3)\n", "Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from plotly) (23.2)\n", "Requirement already satisfied: langchain in /usr/local/lib/python3.10/dist-packages (0.1.14)\n", "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (6.0.1)\n", "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.0.29)\n", "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (3.9.3)\n", "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (4.0.3)\n", "Requirement already satisfied: dataclasses-json<0.7,>=0.5.7 in /usr/local/lib/python3.10/dist-packages (from langchain) (0.6.4)\n", "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.33)\n", "Requirement already satisfied: langchain-community<0.1,>=0.0.30 in /usr/local/lib/python3.10/dist-packages (from langchain) (0.0.31)\n", "Requirement already satisfied: langchain-core<0.2.0,>=0.1.37 in /usr/local/lib/python3.10/dist-packages (from langchain) (0.1.40)\n", "Requirement already satisfied: langchain-text-splitters<0.1,>=0.0.1 in /usr/local/lib/python3.10/dist-packages (from langchain) (0.0.1)\n", "Requirement already satisfied: langsmith<0.2.0,>=0.1.17 in /usr/local/lib/python3.10/dist-packages (from langchain) (0.1.40)\n", "Requirement already satisfied: numpy<2,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.25.2)\n", "Requirement already satisfied: pydantic<3,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.6.4)\n", "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.31.0)\n", "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (8.2.3)\n", "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n", "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.2.0)\n", "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.5)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)\n", "Requirement already satisfied: marshmallow<4.0.0,>=3.18.0 in /usr/local/lib/python3.10/dist-packages (from dataclasses-json<0.7,>=0.5.7->langchain) (3.21.1)\n", "Requirement already satisfied: typing-inspect<1,>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from dataclasses-json<0.7,>=0.5.7->langchain) (0.9.0)\n", "Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.10/dist-packages (from jsonpatch<2.0,>=1.33->langchain) (2.4)\n", "Requirement already satisfied: packaging<24.0,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2.0,>=0.1.37->langchain) (23.2)\n", "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /usr/local/lib/python3.10/dist-packages (from langsmith<0.2.0,>=0.1.17->langchain) (3.10.0)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.16.3 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (2.16.3)\n", "Requirement already satisfied: typing-extensions>=4.6.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (4.10.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.6)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2.0.7)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2024.2.2)\n", "Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.10/dist-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.3)\n", "Requirement already satisfied: mypy-extensions>=0.3.0 in /usr/local/lib/python3.10/dist-packages (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain) (1.0.0)\n", "Requirement already satisfied: tiktoken in /usr/local/lib/python3.10/dist-packages (0.6.0)\n", "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from tiktoken) (2023.12.25)\n", "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.10/dist-packages (from tiktoken) (2.31.0)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken) (3.6)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken) (2.0.7)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken) (2024.2.2)\n", "Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (3.7.1)\n", "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.2.0)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (0.12.1)\n", "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (4.50.0)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.4.5)\n", "Requirement already satisfied: numpy>=1.20 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.25.2)\n", "Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (23.2)\n", "Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (9.4.0)\n", "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (3.1.2)\n", "Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (2.8.2)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)\n" ] } ], "source": [ "!pip install sentence_transformers openai unstructured\n", "!pip install plotly\n", "!pip install langchain\n", "!pip install tiktoken\n", "!pip install matplotlib\n", "%pip install -Uqqq rich openai tiktoken wandb langchain unstructured tabulate pdf2image chromadb gradio faiss-gpu\n" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "eCDxucPBGp5g", "outputId": "acfa7783-f1b7-49c4-bb75-46bfc1ca2e78" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Requirement already satisfied: linkify-it-py in /usr/local/lib/python3.10/dist-packages (2.0.3)\n", "Requirement already satisfied: uc-micro-py in /usr/local/lib/python3.10/dist-packages (from linkify-it-py) (1.0.3)\n" ] } ], "source": [ "!pip install linkify-it-py" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "id": "u4zmuvSp7WVI" }, "outputs": [], "source": [ "import numpy as np\n", "import os, random\n", "from pathlib import Path\n", "import tiktoken\n", "from getpass import getpass\n", "from rich.markdown import Markdown\n", "from langchain.chains.qa_with_sources import load_qa_with_sources_chain" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "EVxVIwh87WVJ", "outputId": "d3269f75-96b5-4532-e01f-72e0c28e6e3a" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "9223372036854775807" ] }, "metadata": {}, "execution_count": 53 } ], "source": [ "import torch\n", "import sys\n", "import csv\n", "\n", "csv.field_size_limit(sys.maxsize)\n", "\n", "# torch.has_mps" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "_1AdkW1kFB20" }, "outputs": [], "source": [ "# os.environ[\"OPENAI_API_KEY\"] = getpass(\"\")\n", "#os.environ[\"OPENAI_API_KEY\"] = “206143cbdb8d41ec94c654dec0eed59f”" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "5SuSwwcx7WVK", "outputId": "26865351-e76b-4c8e-c637-dbda76d2fb78" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "OpenAI API key configured\n" ] } ], "source": [ "\n", "if os.getenv(\"OPENAI_API_KEY\") is None:\n", " if any(['VSCODE' in x for x in os.environ.keys()]):\n", " print('Please enter password in the VS Code prompt at the top of your VS Code window!')\n", " os.environ[\"OPENAI_API_KEY\"] = getpass(\"\")\n", "\n", "assert os.getenv(\"OPENAI_API_KEY\", \"\").startswith(\"sk-\"), \"This doesn't look like a valid OpenAI API key\"\n", "print(\"OpenAI API key configured\")" ] }, { "cell_type": "markdown", "metadata": { "id": "6fLvsQcLEW0K" }, "source": [ "#Chunking and RAG" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "id": "Qgn9BzjX7WVK" }, "outputs": [], "source": [ "from langchain.embeddings.openai import OpenAIEmbeddings\n", "from langchain.text_splitter import CharacterTextSplitter\n", "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", "from langchain.vectorstores import Chroma\n", "from langchain.document_loaders import TextLoader\n", "from langchain.chat_models import ChatOpenAI\n", "from langchain import PromptTemplate\n", "from langchain.chains import LLMChain\n", "from langchain.chains.qa_with_sources import load_qa_with_sources_chain\n", "from langchain.llms import OpenAI\n", "from langchain.vectorstores import FAISS" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "id": "cgh0f__69Qse" }, "outputs": [], "source": [ "MODEL_NAME = \"text-embedding-3-small\"\n", "# MODEL_NAME = \"gpt-4\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "28-76KUp9TdA" }, "outputs": [], "source": [ "# from langchain.document_loaders import DirectoryLoader\n", "\n", "# def find_md_files(directory):\n", "# \"Find all markdown files in a directory and return a LangChain Document\"\n", "# dl = DirectoryLoader(directory, \"**/*.txt\")\n", "# return dl.load()\n", "\n", "# documents = find_md_files('data')\n", "# len(documents)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "MLiFbXx1-hAv" }, "outputs": [], "source": [ "# documents" ] }, { "cell_type": "markdown", "metadata": { "id": "FznW3HMpD4f_" }, "source": [ "#upload files" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "id": "wtENHSIWEf9K" }, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "id": "c3Edx6229xs3" }, "outputs": [], "source": [ "from langchain.document_loaders.csv_loader import CSVLoader\n", "# from langchain.docstore.document import Document\n", "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", "from langchain.document_loaders import DirectoryLoader" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "id": "logMHfgiSen2" }, "outputs": [], "source": [ "url='https://drive.google.com/file/d/1r2lol3qUPSu9l6cb2qbRTlw9IZM9O-C_/view?usp=sharing'\n", "url='https://drive.google.com/uc?id=' + url.split('/')[-2]\n", "df = pd.read_csv(url)\n" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "id": "1jr0ZaWqvSPQ", "outputId": "2f707ab4-cf35-45bc-c784-17a6e5a4b4c1" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " question \\\n", "0 What is (are) Glaucoma ? \n", "1 What causes Glaucoma ? \n", "2 What are the symptoms of Glaucoma ? \n", "3 What are the treatments for Glaucoma ? \n", "4 What is (are) Glaucoma ? \n", "\n", " answer source \\\n", "0 Glaucoma is a group of diseases that can damag... NIHSeniorHealth \n", "1 Nearly 2.7 million people have glaucoma, a lea... NIHSeniorHealth \n", "2 Symptoms of Glaucoma Glaucoma can develop in ... NIHSeniorHealth \n", "3 Although open-angle glaucoma cannot be cured, ... NIHSeniorHealth \n", "4 Glaucoma is a group of diseases that can damag... NIHSeniorHealth \n", "\n", " focus_area \n", "0 Glaucoma \n", "1 Glaucoma \n", "2 Glaucoma \n", "3 Glaucoma \n", "4 Glaucoma " ], "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
questionanswersourcefocus_area
0What is (are) Glaucoma ?Glaucoma is a group of diseases that can damag...NIHSeniorHealthGlaucoma
1What causes Glaucoma ?Nearly 2.7 million people have glaucoma, a lea...NIHSeniorHealthGlaucoma
2What are the symptoms of Glaucoma ?Symptoms of Glaucoma Glaucoma can develop in ...NIHSeniorHealthGlaucoma
3What are the treatments for Glaucoma ?Although open-angle glaucoma cannot be cured, ...NIHSeniorHealthGlaucoma
4What is (are) Glaucoma ?Glaucoma is a group of diseases that can damag...NIHSeniorHealthGlaucoma
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "
\n", "
\n" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "variable_name": "df", "summary": "{\n \"name\": \"df\",\n \"rows\": 16412,\n \"fields\": [\n {\n \"column\": \"question\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 14984,\n \"samples\": [\n \"How many people are affected by 8p11 myeloproliferative syndrome ?\",\n \"What is (are) caudal regression syndrome ?\",\n \"What is the outlook for Cephalic Disorders ?\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"answer\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 15782,\n \"samples\": [\n \"Mercury poisoning is a condition that occurs in people who are exposed to toxic levels of the element, mercury. There are three different forms of mercury that can cause health problems: Elemental mercury (also known as liquid mercury or quicksilver) can be found in glass thermometers, electrical switches, dental fillings and fluorescent light bulbs. This form of mercury is generally only harmful when small droplets become airborne and are inhaled. If this occurs, signs and symptoms of poisoning may include metallic taste, vomiting, difficulty breathing, coughing, and/or swollen, bleeding gums. In severe cases, long-term brain damage, permanent lung damage and even death may occur. Inorganic mercury is found in batteries, chemistry labs, and some disinfectants. This form of mercury is harmful when swallowed. Signs and symptoms of inorganic mercury poisoning vary based on the amount consumed, but may include burning in the stomach and throat; vomiting; and/or bloody diarrhea. Inorganic mercury can also affect the kidneys and brain if it enters the blood stream. Organic mercury can be found in fish. Some organisms convert fumes from burning coal into organic mercury. This form of mercury is harmful if inhaled, eaten, or placed on the skin for long periods of time. Long-term exposure to organic mercury may result in skin numbness or pain; tremor; inability to walk well; blindness; double vision; memory problems; seizures; or even death. Treatment is generally supportive and based on the signs and symptoms present in each person. Medications called chelators, which remove mercury and heavy metals from the body, are generally prescribed.\",\n \"Leishmaniasis is found in people in focal areas of more than 90 countries in the tropics, subtropics, and southern Europe. The ecologic settings range from rain forests to deserts. Leishmaniasis usually is more common in rural than in urban areas, but it is found in the outskirts of some cities. Climate and other environmental changes have the potential to expand the geographic range of the sand fly vectors and the areas in the world where leishmaniasis is found.\\n \\nLeishmaniasis is found on every continent except Australia and Antarctica.\\n \\n - In the Old World (the Eastern Hemisphere), leishmaniasis is found in some parts of Asia, the Middle East, Africa (particularly in the tropical region and North Africa, with some cases elsewhere), and southern Europe. It is not found in Australia or the Pacific islands.\\n - In the New World (the Western Hemisphere), it is found in some parts of Mexico, Central America, and South America. It is not found in Chile or Uruguay. Occasional cases of cutaneous leishmaniasis have been acquired in Texas and Oklahoma.\\n \\n \\nThe number of new cases per year is not known with certainty. For cutaneous leishmaniasis, estimates of the number of cases range from approximately 0.7 million (700,000) to 1.2 million (1,200,000). For visceral leishmaniasis, estimates of the number of cases range from approximately 0.2 million (200,000) to 0.4 million (400,000). The cases of leishmaniasis evaluated in the United States reflect travel and immigration patterns. For example, many of the cases of cutaneous leishmaniasis in U.S. civilian travelers have been acquired in common tourist destinations in Latin America, such as in Costa Rica.\\n \\nOverall, infection in people is caused by more than 20 species (types) of Leishmania parasites, which are spread by about 30 species of phlebotomine sand flies; particular species of the parasite are spread by particular sand flies. The sand fly vectors generally are the most active during twilight, evening, and night-time hours (from dusk to dawn).\\n \\nIn many geographic areas where leishmaniasis is found in people, infected people are not needed to maintain the transmission cycle of the parasite in nature; infected animals (such as rodents or dogs), along with sand flies, maintain the cycle. However, in some parts of the world, infected people are needed to maintain the cycle; this type of transmission (human\\u2014sand fly\\u2014human) is called anthroponotic. In areas with anthroponotic transmission, effective treatment of individual patients can help control the spread of the parasite.\",\n \"What are the signs and symptoms of Trichotillomania? The Human Phenotype Ontology provides the following list of signs and symptoms for Trichotillomania. If the information is available, the table below includes how often the symptom is seen in people with this condition. You can use the MedlinePlus Medical Dictionary to look up the definitions for these medical terms. Signs and Symptoms Approximate number of patients (when available) Alopecia - Autosomal dominant inheritance - Hair-pulling - Multifactorial inheritance - Obsessive-compulsive behavior - The Human Phenotype Ontology (HPO) has collected information on how often a sign or symptom occurs in a condition. Much of this information comes from Orphanet, a European rare disease database. The frequency of a sign or symptom is usually listed as a rough estimate of the percentage of patients who have that feature. The frequency may also be listed as a fraction. The first number of the fraction is how many people had the symptom, and the second number is the total number of people who were examined in one study. For example, a frequency of 25/25 means that in a study of 25 people all patients were found to have that symptom. Because these frequencies are based on a specific study, the fractions may be different if another group of patients are examined. Sometimes, no information on frequency is available. In these cases, the sign or symptom may be rare or common.\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"source\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 9,\n \"samples\": [\n \"GHR\",\n \"CancerGov\",\n \"NHLBI\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"focus_area\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 5126,\n \"samples\": [\n \"Adams-Oliver syndrome\",\n \"Occupational Health\",\n \"What I need to know about Diverticular Disease\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" } }, "metadata": {}, "execution_count": 60 } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "l-Rjki2Yvq01", "outputId": "b10e7032-65cf-4abc-fcee-4058974dbc1d" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(16412, 4)" ] }, "metadata": {}, "execution_count": 61 } ], "source": [ "df.shape" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lA4AoU3f4dJH", "outputId": "ff25084e-55f6-4444-b612-ec7620de4367" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "mkdir: cannot create directory ‘data’: File exists\n" ] } ], "source": [ "!mkdir data" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "id": "nsvUBcPJCV_m" }, "outputs": [], "source": [ "df[:100].to_csv('data/df_embed.csv',index=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "Kwy9kXLzYEu7" }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "id": "5WYH_AC18qes" }, "source": [ "1. Step 1: Read the data" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 122 }, "id": "T6LiGVc4LREH", "outputId": "5cd717bc-22cb-4512-9dec-38ea73321b28" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Nearly 2.7 million people have glaucoma, a leading cause of blindness in the United States. Although anyone can get glaucoma, some people are at higher risk. They include - African-Americans over age 40 - everyone over age 60, especially Hispanics/Latinos - people with a family history of glaucoma. African-Americans over age 40 everyone over age 60, especially Hispanics/Latinos people with a family history of glaucoma. In addition to age, eye pressure is a risk factor. Whether you develop glaucoma depends on the level of pressure your optic nerve can tolerate without being damaged. This level is different for each person. Thats why a comprehensive dilated eye exam is very important. It can help your eye care professional determine what level of eye pressure is normal for you. Another risk factor for optic nerve damage relates to blood pressure. Thus, it is important to also make sure that your blood pressure is at a proper level for your body by working with your medical doctor. (Watch the animated video to learn more about the causes of glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.)'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 64 } ], "source": [ "df.answer\t[1]" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 206 }, "id": "QV58YlrRYKIS", "outputId": "af676e54-dd44-49a7-9be7-ec74cc9cd3e2" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " question \\\n", "0 What is (are) Glaucoma ? \n", "1 What causes Glaucoma ? \n", "2 What are the symptoms of Glaucoma ? \n", "3 What are the treatments for Glaucoma ? \n", "4 What is (are) Glaucoma ? \n", "\n", " answer source \\\n", "0 Glaucoma is a group of diseases that can damag... NIHSeniorHealth \n", "1 Nearly 2.7 million people have glaucoma, a lea... NIHSeniorHealth \n", "2 Symptoms of Glaucoma Glaucoma can develop in ... NIHSeniorHealth \n", "3 Although open-angle glaucoma cannot be cured, ... NIHSeniorHealth \n", "4 Glaucoma is a group of diseases that can damag... NIHSeniorHealth \n", "\n", " focus_area \n", "0 Glaucoma \n", "1 Glaucoma \n", "2 Glaucoma \n", "3 Glaucoma \n", "4 Glaucoma " ], "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
questionanswersourcefocus_area
0What is (are) Glaucoma ?Glaucoma is a group of diseases that can damag...NIHSeniorHealthGlaucoma
1What causes Glaucoma ?Nearly 2.7 million people have glaucoma, a lea...NIHSeniorHealthGlaucoma
2What are the symptoms of Glaucoma ?Symptoms of Glaucoma Glaucoma can develop in ...NIHSeniorHealthGlaucoma
3What are the treatments for Glaucoma ?Although open-angle glaucoma cannot be cured, ...NIHSeniorHealthGlaucoma
4What is (are) Glaucoma ?Glaucoma is a group of diseases that can damag...NIHSeniorHealthGlaucoma
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "
\n", "
\n" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "variable_name": "df", "summary": "{\n \"name\": \"df\",\n \"rows\": 16412,\n \"fields\": [\n {\n \"column\": \"question\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 14984,\n \"samples\": [\n \"How many people are affected by 8p11 myeloproliferative syndrome ?\",\n \"What is (are) caudal regression syndrome ?\",\n \"What is the outlook for Cephalic Disorders ?\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"answer\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 15782,\n \"samples\": [\n \"Mercury poisoning is a condition that occurs in people who are exposed to toxic levels of the element, mercury. There are three different forms of mercury that can cause health problems: Elemental mercury (also known as liquid mercury or quicksilver) can be found in glass thermometers, electrical switches, dental fillings and fluorescent light bulbs. This form of mercury is generally only harmful when small droplets become airborne and are inhaled. If this occurs, signs and symptoms of poisoning may include metallic taste, vomiting, difficulty breathing, coughing, and/or swollen, bleeding gums. In severe cases, long-term brain damage, permanent lung damage and even death may occur. Inorganic mercury is found in batteries, chemistry labs, and some disinfectants. This form of mercury is harmful when swallowed. Signs and symptoms of inorganic mercury poisoning vary based on the amount consumed, but may include burning in the stomach and throat; vomiting; and/or bloody diarrhea. Inorganic mercury can also affect the kidneys and brain if it enters the blood stream. Organic mercury can be found in fish. Some organisms convert fumes from burning coal into organic mercury. This form of mercury is harmful if inhaled, eaten, or placed on the skin for long periods of time. Long-term exposure to organic mercury may result in skin numbness or pain; tremor; inability to walk well; blindness; double vision; memory problems; seizures; or even death. Treatment is generally supportive and based on the signs and symptoms present in each person. Medications called chelators, which remove mercury and heavy metals from the body, are generally prescribed.\",\n \"Leishmaniasis is found in people in focal areas of more than 90 countries in the tropics, subtropics, and southern Europe. The ecologic settings range from rain forests to deserts. Leishmaniasis usually is more common in rural than in urban areas, but it is found in the outskirts of some cities. Climate and other environmental changes have the potential to expand the geographic range of the sand fly vectors and the areas in the world where leishmaniasis is found.\\n \\nLeishmaniasis is found on every continent except Australia and Antarctica.\\n \\n - In the Old World (the Eastern Hemisphere), leishmaniasis is found in some parts of Asia, the Middle East, Africa (particularly in the tropical region and North Africa, with some cases elsewhere), and southern Europe. It is not found in Australia or the Pacific islands.\\n - In the New World (the Western Hemisphere), it is found in some parts of Mexico, Central America, and South America. It is not found in Chile or Uruguay. Occasional cases of cutaneous leishmaniasis have been acquired in Texas and Oklahoma.\\n \\n \\nThe number of new cases per year is not known with certainty. For cutaneous leishmaniasis, estimates of the number of cases range from approximately 0.7 million (700,000) to 1.2 million (1,200,000). For visceral leishmaniasis, estimates of the number of cases range from approximately 0.2 million (200,000) to 0.4 million (400,000). The cases of leishmaniasis evaluated in the United States reflect travel and immigration patterns. For example, many of the cases of cutaneous leishmaniasis in U.S. civilian travelers have been acquired in common tourist destinations in Latin America, such as in Costa Rica.\\n \\nOverall, infection in people is caused by more than 20 species (types) of Leishmania parasites, which are spread by about 30 species of phlebotomine sand flies; particular species of the parasite are spread by particular sand flies. The sand fly vectors generally are the most active during twilight, evening, and night-time hours (from dusk to dawn).\\n \\nIn many geographic areas where leishmaniasis is found in people, infected people are not needed to maintain the transmission cycle of the parasite in nature; infected animals (such as rodents or dogs), along with sand flies, maintain the cycle. However, in some parts of the world, infected people are needed to maintain the cycle; this type of transmission (human\\u2014sand fly\\u2014human) is called anthroponotic. In areas with anthroponotic transmission, effective treatment of individual patients can help control the spread of the parasite.\",\n \"What are the signs and symptoms of Trichotillomania? The Human Phenotype Ontology provides the following list of signs and symptoms for Trichotillomania. If the information is available, the table below includes how often the symptom is seen in people with this condition. You can use the MedlinePlus Medical Dictionary to look up the definitions for these medical terms. Signs and Symptoms Approximate number of patients (when available) Alopecia - Autosomal dominant inheritance - Hair-pulling - Multifactorial inheritance - Obsessive-compulsive behavior - The Human Phenotype Ontology (HPO) has collected information on how often a sign or symptom occurs in a condition. Much of this information comes from Orphanet, a European rare disease database. The frequency of a sign or symptom is usually listed as a rough estimate of the percentage of patients who have that feature. The frequency may also be listed as a fraction. The first number of the fraction is how many people had the symptom, and the second number is the total number of people who were examined in one study. For example, a frequency of 25/25 means that in a study of 25 people all patients were found to have that symptom. Because these frequencies are based on a specific study, the fractions may be different if another group of patients are examined. Sometimes, no information on frequency is available. In these cases, the sign or symptom may be rare or common.\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"source\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 9,\n \"samples\": [\n \"GHR\",\n \"CancerGov\",\n \"NHLBI\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"focus_area\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 5126,\n \"samples\": [\n \"Adams-Oliver syndrome\",\n \"Occupational Health\",\n \"What I need to know about Diverticular Disease\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" } }, "metadata": {}, "execution_count": 65 } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "id": "QSOiVXVxekH_" }, "outputs": [], "source": [ "path= 'data/df_embed.csv'\n", "loader = CSVLoader(file_path=path,source_column=\"answer\")\n", "\n", "data = loader.load()" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Q2dWBLgyEy_n", "outputId": "2fe0f42e-9011-456d-9388-6f15487ed4a0" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "page_content=\"question: What is (are) Glaucoma ?\\nanswer: Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. While glaucoma can strike anyone, the risk is much greater for people over 60. How Glaucoma Develops There are several different types of glaucoma. Most of these involve the drainage system within the eye. At the front of the eye there is a small space called the anterior chamber. A clear fluid flows through this chamber and bathes and nourishes the nearby tissues. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too slowly out of the eye. As the fluid builds up, the pressure inside the eye rises. Unless this pressure is controlled, it may cause damage to the optic nerve and other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of glaucoma is called open-angle glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result. There is no cure for glaucoma. Vision lost from the disease cannot be restored. However, there are treatments that may save remaining vision. That is why early diagnosis is important. See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\\nsource: NIHSeniorHealth\\nfocus_area: Glaucoma\" metadata={'source': \"Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. While glaucoma can strike anyone, the risk is much greater for people over 60. How Glaucoma Develops There are several different types of glaucoma. Most of these involve the drainage system within the eye. At the front of the eye there is a small space called the anterior chamber. A clear fluid flows through this chamber and bathes and nourishes the nearby tissues. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too slowly out of the eye. As the fluid builds up, the pressure inside the eye rises. Unless this pressure is controlled, it may cause damage to the optic nerve and other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of glaucoma is called open-angle glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result. There is no cure for glaucoma. Vision lost from the disease cannot be restored. However, there are treatments that may save remaining vision. That is why early diagnosis is important. See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\", 'row': 0}\n" ] } ], "source": [ "print(data[0])" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9OlS2Kx8FlVx", "outputId": "beb1403f-83e8-4ac9-9a90-bea176f66d10" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "You have 100 document(s) in your data\n", "There are 1938 words in your document\n" ] } ], "source": [ "print (f'You have {len(data)} document(s) in your data')\n", "print (f'There are {len(data[0].page_content)} words in your document')" ] }, { "cell_type": "markdown", "metadata": { "id": "Vm0lO--f80NZ" }, "source": [ "2. Breakdown data - Chunking Process" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "id": "FOUcWZF3Gk8n" }, "outputs": [], "source": [ "text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)\n", "texts = text_splitter.split_documents(data)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "xdJDpvcBGnp3", "outputId": "dad7ddc1-190b-4aea-bafa-94701b283d7a" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "329" ] }, "metadata": {}, "execution_count": 70 } ], "source": [ "len(texts)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "jaRsK0qaZ9Fk", "outputId": "ec9f2a8a-5f2b-4662-d01e-6e49a0d6d256" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "You now have 329 document(s) in your data\n", "There are 996 characters in your document\n" ] } ], "source": [ "print (f'You now have {len(texts)} document(s) in your data')\n", "print (f'There are {len(texts[1].page_content)} characters in your document')" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "qaFGDzkjMRJG", "outputId": "af98fdf2-17f5-4af8-cfcb-ad35721c460c" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "Document(page_content='measures your side or peripheral vision. It helps your eye care professional tell if you have lost side vision, a sign of glaucoma. In a dilated eye exam, drops are placed in your eyes to widen, or dilate, the pupils. Your eye care professional uses a special magnifying lens to examine your retina and optic nerve for signs of damage and other eye problems. After the exam, your close-up vision may remain blurred for several hours. In tonometry, an instrument measures the pressure inside the eye. Numbing drops may be applied to your eye for this test. With pachymetry, a numbing drop is applied to your eye. Your eye care professional uses an ultrasonic wave instrument to measure the thickness of your cornea.', metadata={'source': 'Symptoms of Glaucoma Glaucoma can develop in one or both eyes. The most common type of glaucoma, open-angle glaucoma, has no symptoms at first. It causes no pain, and vision seems normal. Without treatment, people with glaucoma will slowly lose their peripheral, or side vision. They seem to be looking through a tunnel. Over time, straight-ahead vision may decrease until no vision remains. Tests for Glaucoma Glaucoma is detected through a comprehensive eye exam that includes a visual acuity test, visual field test, dilated eye exam, tonometry, and pachymetry. (Watch the animated video to learn more about testing for glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) A visual acuity test uses an eye chart test to measure how well you see at various distances. A visual field test measures your side or peripheral vision. It helps your eye care professional tell if you have lost side vision, a sign of glaucoma. In a dilated eye exam, drops are placed in your eyes to widen, or dilate, the pupils. Your eye care professional uses a special magnifying lens to examine your retina and optic nerve for signs of damage and other eye problems. After the exam, your close-up vision may remain blurred for several hours. In tonometry, an instrument measures the pressure inside the eye. Numbing drops may be applied to your eye for this test. With pachymetry, a numbing drop is applied to your eye. Your eye care professional uses an ultrasonic wave instrument to measure the thickness of your cornea.', 'row': 2})" ] }, "metadata": {}, "execution_count": 72 } ], "source": [ "texts[10]" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "O-acByeSuScf", "outputId": "e85f0c06-7a5c-4719-a3f1-1138b75260d6" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Requirement already satisfied: langchain-openai in /usr/local/lib/python3.10/dist-packages (0.1.1)\n", "Requirement already satisfied: langchain-core<0.2.0,>=0.1.33 in /usr/local/lib/python3.10/dist-packages (from langchain-openai) (0.1.40)\n", "Requirement already satisfied: openai<2.0.0,>=1.10.0 in /usr/local/lib/python3.10/dist-packages (from langchain-openai) (1.16.2)\n", "Requirement already satisfied: tiktoken<1,>=0.5.2 in /usr/local/lib/python3.10/dist-packages (from langchain-openai) (0.6.0)\n", "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2.0,>=0.1.33->langchain-openai) (6.0.1)\n", "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2.0,>=0.1.33->langchain-openai) (1.33)\n", "Requirement already satisfied: langsmith<0.2.0,>=0.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2.0,>=0.1.33->langchain-openai) (0.1.40)\n", "Requirement already satisfied: packaging<24.0,>=23.2 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2.0,>=0.1.33->langchain-openai) (23.2)\n", "Requirement already satisfied: pydantic<3,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2.0,>=0.1.33->langchain-openai) (2.6.4)\n", "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain-core<0.2.0,>=0.1.33->langchain-openai) (8.2.3)\n", "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.10.0->langchain-openai) (3.7.1)\n", "Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai<2.0.0,>=1.10.0->langchain-openai) (1.7.0)\n", "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.10.0->langchain-openai) (0.27.0)\n", "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.10.0->langchain-openai) (1.3.1)\n", "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.10.0->langchain-openai) (4.66.2)\n", "Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from openai<2.0.0,>=1.10.0->langchain-openai) (4.10.0)\n", "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from tiktoken<1,>=0.5.2->langchain-openai) (2023.12.25)\n", "Requirement already satisfied: requests>=2.26.0 in /usr/local/lib/python3.10/dist-packages (from tiktoken<1,>=0.5.2->langchain-openai) (2.31.0)\n", "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.10.0->langchain-openai) (3.6)\n", "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.10.0->langchain-openai) (1.2.0)\n", "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.10.0->langchain-openai) (2024.2.2)\n", "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.10.0->langchain-openai) (1.0.5)\n", "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.10.0->langchain-openai) (0.14.0)\n", "Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.10/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.2.0,>=0.1.33->langchain-openai) (2.4)\n", "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /usr/local/lib/python3.10/dist-packages (from langsmith<0.2.0,>=0.1.0->langchain-core<0.2.0,>=0.1.33->langchain-openai) (3.10.0)\n", "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain-core<0.2.0,>=0.1.33->langchain-openai) (0.6.0)\n", "Requirement already satisfied: pydantic-core==2.16.3 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain-core<0.2.0,>=0.1.33->langchain-openai) (2.16.3)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken<1,>=0.5.2->langchain-openai) (3.3.2)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.26.0->tiktoken<1,>=0.5.2->langchain-openai) (2.0.7)\n" ] } ], "source": [ "!pip install -U langchain-openai\n", "from langchain_openai import OpenAIEmbeddings\n", "from langchain_openai import ChatOpenAI" ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "id": "ZV4m9sie7WVV" }, "outputs": [], "source": [ "embeddings = OpenAIEmbeddings()\n", "db = FAISS.from_documents(texts, embeddings)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "id": "dezKUIttCb0h" }, "outputs": [], "source": [ "db.save_local(\"faiss_index\")\n" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "id": "k7fz-NgvChtr" }, "outputs": [], "source": [ "db_loaded = FAISS.load_local(\"faiss_index\", embeddings,allow_dangerous_deserialization=True)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "id": "AMkkNB3U7WVV" }, "outputs": [], "source": [ "query = \"\"\"What are the causes of glaucoma\"\"\"" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "id": "tCaQyBkz7WVV" }, "outputs": [], "source": [ "def get_response_from_query(db, query, k=10):\n", " \"\"\"\n", "\n", " \"\"\"\n", "\n", " docs = db.similarity_search(query, k=k) # extremely important\n", "\n", " docs_page_content = \" \".join([d.page_content for d in docs])\n", "\n", " # llm = BardLLM()\n", " llm = ChatOpenAI(model_name=\"gpt-3.5-turbo-16k\",temperature=0)\n", "\n", " prompt = PromptTemplate(\n", " input_variables=[\"question\", \"docs\"],\n", " template=\"\"\"\n", " You are a bot that is open to discussions about different cultural, philosophical and political exchanges. I will use do different analysis to the articles provided to me. Stay truthful and if you weren't provided any resources give your oppinion only.\n", " Answer the following question: {question}\n", " By searching the following articles: {docs}\n", "\n", " Only use the factual information from the documents. Make sure to mention key phrases from the articles.\n", "\n", " If you feel like you don't have enough information to answer the question, say \"I don't know\".\n", "\n", " \"\"\",\n", " )\n", "\n", " chain = LLMChain(llm=llm, prompt=prompt)\n", " # chain = RetrievalQAWithSourcesChain.from_chain_type(llm=llm, prompt=prompt,\n", " # chain_type=\"stuff\", retriever=db.as_retriever(), return_source_documents=True)\n", "\n", " response = chain.run(question=query, docs=docs_page_content,return_source_documents=True)\n", " r_text = str(response)\n", "\n", " ##evaluation part\n", "\n", " prompt_eval = PromptTemplate(\n", " input_variables=[\"answer\", \"docs\"],\n", " template=\"\"\"\n", " You job is to evaluate if the response to a given context is faithful.\n", "\n", " for the following: {answer}\n", " By searching the following article: {docs}\n", "\n", " Give a reason why they are similar or not, start with a Yes or a No.\n", "\n", " \"\"\",\n", " )\n", "\n", " chain_part_2 = LLMChain(llm=llm, prompt=prompt_eval)\n", "\n", " evals = chain_part_2.run(answer=r_text, docs=docs_page_content)\n", "\n", "\n", " return response,docs,evals" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "id": "twZ_ILhJ7WVV" }, "outputs": [], "source": [ "answer,sources,evals=get_response_from_query(db_loaded,query,15)" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Xaa84xr109s5", "outputId": "b3629efd-c62b-4cea-a69a-d5d6b44d8bec" }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[Document(page_content='question: What causes Glaucoma ?', metadata={'source': 'Nearly 2.7 million people have glaucoma, a leading cause of blindness in the United States. Although anyone can get glaucoma, some people are at higher risk. They include - African-Americans over age 40 - everyone over age 60, especially Hispanics/Latinos - people with a family history of glaucoma. African-Americans over age 40 everyone over age 60, especially Hispanics/Latinos people with a family history of glaucoma. In addition to age, eye pressure is a risk factor. Whether you develop glaucoma depends on the level of pressure your optic nerve can tolerate without being damaged. This level is different for each person. Thats why a comprehensive dilated eye exam is very important. It can help your eye care professional determine what level of eye pressure is normal for you. Another risk factor for optic nerve damage relates to blood pressure. Thus, it is important to also make sure that your blood pressure is at a proper level for your body by working with your medical doctor. (Watch the animated video to learn more about the causes of glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.)', 'row': 1}),\n", " Document(page_content=\"answer: Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. While glaucoma can strike anyone, the risk is much greater for people over 60. How Glaucoma Develops There are several different types of glaucoma. Most of these involve the drainage system within the eye. At the front of the eye there is a small space called the anterior chamber. A clear fluid flows through this chamber and bathes and nourishes the nearby tissues. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too slowly out of the eye. As the fluid builds up, the pressure inside the eye rises. Unless this pressure is controlled, it may cause damage to the optic nerve and other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of\", metadata={'source': \"Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. While glaucoma can strike anyone, the risk is much greater for people over 60. How Glaucoma Develops There are several different types of glaucoma. Most of these involve the drainage system within the eye. At the front of the eye there is a small space called the anterior chamber. A clear fluid flows through this chamber and bathes and nourishes the nearby tissues. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too slowly out of the eye. As the fluid builds up, the pressure inside the eye rises. Unless this pressure is controlled, it may cause damage to the optic nerve and other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of glaucoma is called open-angle glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result. There is no cure for glaucoma. Vision lost from the disease cannot be restored. However, there are treatments that may save remaining vision. That is why early diagnosis is important. See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\", 'row': 0}),\n", " Document(page_content='other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of glaucoma is called open-angle glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result. There is no cure for glaucoma. Vision lost from the disease cannot be restored. However, there are treatments that may save remaining vision. That is why early diagnosis is important. See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.', metadata={'source': \"Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. While glaucoma can strike anyone, the risk is much greater for people over 60. How Glaucoma Develops There are several different types of glaucoma. Most of these involve the drainage system within the eye. At the front of the eye there is a small space called the anterior chamber. A clear fluid flows through this chamber and bathes and nourishes the nearby tissues. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too slowly out of the eye. As the fluid builds up, the pressure inside the eye rises. Unless this pressure is controlled, it may cause damage to the optic nerve and other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of glaucoma is called open-angle glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result. There is no cure for glaucoma. Vision lost from the disease cannot be restored. However, there are treatments that may save remaining vision. That is why early diagnosis is important. See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\", 'row': 0}),\n", " Document(page_content='question: What is (are) Glaucoma ?\\nanswer: Open-angle glaucoma is the most common form of glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When the fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result.\\nsource: NIHSeniorHealth\\nfocus_area: Glaucoma', metadata={'source': 'Open-angle glaucoma is the most common form of glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When the fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result.', 'row': 6}),\n", " Document(page_content='answer: Nearly 2.7 million people have glaucoma, a leading cause of blindness in the United States. Although anyone can get glaucoma, some people are at higher risk. They include - African-Americans over age 40 - everyone over age 60, especially Hispanics/Latinos - people with a family history of glaucoma. African-Americans over age 40 everyone over age 60, especially Hispanics/Latinos people with a family history of glaucoma. In addition to age, eye pressure is a risk factor. Whether you develop glaucoma depends on the level of pressure your optic nerve can tolerate without being damaged. This level is different for each person. Thats why a comprehensive dilated eye exam is very important. It can help your eye care professional determine what level of eye pressure is normal for you. Another risk factor for optic nerve damage relates to blood pressure. Thus, it is important to also make sure that your blood pressure is at a proper level for your body by working with your medical', metadata={'source': 'Nearly 2.7 million people have glaucoma, a leading cause of blindness in the United States. Although anyone can get glaucoma, some people are at higher risk. They include - African-Americans over age 40 - everyone over age 60, especially Hispanics/Latinos - people with a family history of glaucoma. African-Americans over age 40 everyone over age 60, especially Hispanics/Latinos people with a family history of glaucoma. In addition to age, eye pressure is a risk factor. Whether you develop glaucoma depends on the level of pressure your optic nerve can tolerate without being damaged. This level is different for each person. Thats why a comprehensive dilated eye exam is very important. It can help your eye care professional determine what level of eye pressure is normal for you. Another risk factor for optic nerve damage relates to blood pressure. Thus, it is important to also make sure that your blood pressure is at a proper level for your body by working with your medical doctor. (Watch the animated video to learn more about the causes of glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.)', 'row': 1}),\n", " Document(page_content='question: What are the symptoms of Glaucoma ?', metadata={'source': 'Symptoms of Glaucoma Glaucoma can develop in one or both eyes. The most common type of glaucoma, open-angle glaucoma, has no symptoms at first. It causes no pain, and vision seems normal. Without treatment, people with glaucoma will slowly lose their peripheral, or side vision. They seem to be looking through a tunnel. Over time, straight-ahead vision may decrease until no vision remains. Tests for Glaucoma Glaucoma is detected through a comprehensive eye exam that includes a visual acuity test, visual field test, dilated eye exam, tonometry, and pachymetry. (Watch the animated video to learn more about testing for glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) A visual acuity test uses an eye chart test to measure how well you see at various distances. A visual field test measures your side or peripheral vision. It helps your eye care professional tell if you have lost side vision, a sign of glaucoma. In a dilated eye exam, drops are placed in your eyes to widen, or dilate, the pupils. Your eye care professional uses a special magnifying lens to examine your retina and optic nerve for signs of damage and other eye problems. After the exam, your close-up vision may remain blurred for several hours. In tonometry, an instrument measures the pressure inside the eye. Numbing drops may be applied to your eye for this test. With pachymetry, a numbing drop is applied to your eye. Your eye care professional uses an ultrasonic wave instrument to measure the thickness of your cornea.', 'row': 2}),\n", " Document(page_content='question: What is (are) Glaucoma ?', metadata={'source': \"Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. While glaucoma can strike anyone, the risk is much greater for people over 60. How Glaucoma Develops There are several different types of glaucoma. Most of these involve the drainage system within the eye. At the front of the eye there is a small space called the anterior chamber. A clear fluid flows through this chamber and bathes and nourishes the nearby tissues. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too slowly out of the eye. As the fluid builds up, the pressure inside the eye rises. Unless this pressure is controlled, it may cause damage to the optic nerve and other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of glaucoma is called open-angle glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result. There is no cure for glaucoma. Vision lost from the disease cannot be restored. However, there are treatments that may save remaining vision. That is why early diagnosis is important. See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\", 'row': 0}),\n", " Document(page_content='question: What are the treatments for Glaucoma ?', metadata={'source': 'Although open-angle glaucoma cannot be cured, it can usually be controlled. While treatments may save remaining vision, they do not improve sight already lost from glaucoma. The most common treatments for glaucoma are medication and surgery. Medications Medications for glaucoma may be either in the form of eye drops or pills. Some drugs reduce pressure by slowing the flow of fluid into the eye. Others help to improve fluid drainage. (Watch the video to learn more about coping with glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) For most people with glaucoma, regular use of medications will control the increased fluid pressure. But, these drugs may stop working over time. Or, they may cause side effects. If a problem occurs, the eye care professional may select other drugs, change the dose, or suggest other ways to deal with the problem. Read or listen to ways some patients are coping with glaucoma. Surgery Laser surgery is another treatment for glaucoma. During laser surgery, a strong beam of light is focused on the part of the anterior chamber where the fluid leaves the eye. This results in a series of small changes that makes it easier for fluid to exit the eye. Over time, the effect of laser surgery may wear off. Patients who have this form of surgery may need to keep taking glaucoma drugs. Researching Causes and Treatments Through studies in the laboratory and with patients, NEI is seeking better ways to detect, treat, and prevent vision loss in people with glaucoma. For example, researchers have discovered genes that could help explain how glaucoma damages the eye. NEI also is supporting studies to learn more about who is likely to get glaucoma, when to treat people who have increased eye pressure, and which treatment to use first.', 'row': 3}),\n", " Document(page_content=\"question: What is (are) Glaucoma ?\\nanswer: Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. The most common form of the disease is open-angle glaucoma. With early treatment, you can often protect your eyes against serious vision loss. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\\nsource: NIHSeniorHealth\\nfocus_area: Glaucoma\", metadata={'source': \"Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. The most common form of the disease is open-angle glaucoma. With early treatment, you can often protect your eyes against serious vision loss. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\", 'row': 4}),\n", " Document(page_content='change the dose, or suggest other ways to deal with the problem. Read or listen to ways some patients are coping with glaucoma. Surgery Laser surgery is another treatment for glaucoma. During laser surgery, a strong beam of light is focused on the part of the anterior chamber where the fluid leaves the eye. This results in a series of small changes that makes it easier for fluid to exit the eye. Over time, the effect of laser surgery may wear off. Patients who have this form of surgery may need to keep taking glaucoma drugs. Researching Causes and Treatments Through studies in the laboratory and with patients, NEI is seeking better ways to detect, treat, and prevent vision loss in people with glaucoma. For example, researchers have discovered genes that could help explain how glaucoma damages the eye. NEI also is supporting studies to learn more about who is likely to get glaucoma, when to treat people who have increased eye pressure, and which treatment to use first.', metadata={'source': 'Although open-angle glaucoma cannot be cured, it can usually be controlled. While treatments may save remaining vision, they do not improve sight already lost from glaucoma. The most common treatments for glaucoma are medication and surgery. Medications Medications for glaucoma may be either in the form of eye drops or pills. Some drugs reduce pressure by slowing the flow of fluid into the eye. Others help to improve fluid drainage. (Watch the video to learn more about coping with glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) For most people with glaucoma, regular use of medications will control the increased fluid pressure. But, these drugs may stop working over time. Or, they may cause side effects. If a problem occurs, the eye care professional may select other drugs, change the dose, or suggest other ways to deal with the problem. Read or listen to ways some patients are coping with glaucoma. Surgery Laser surgery is another treatment for glaucoma. During laser surgery, a strong beam of light is focused on the part of the anterior chamber where the fluid leaves the eye. This results in a series of small changes that makes it easier for fluid to exit the eye. Over time, the effect of laser surgery may wear off. Patients who have this form of surgery may need to keep taking glaucoma drugs. Researching Causes and Treatments Through studies in the laboratory and with patients, NEI is seeking better ways to detect, treat, and prevent vision loss in people with glaucoma. For example, researchers have discovered genes that could help explain how glaucoma damages the eye. NEI also is supporting studies to learn more about who is likely to get glaucoma, when to treat people who have increased eye pressure, and which treatment to use first.', 'row': 3}),\n", " Document(page_content='answer: Symptoms of Glaucoma Glaucoma can develop in one or both eyes. The most common type of glaucoma, open-angle glaucoma, has no symptoms at first. It causes no pain, and vision seems normal. Without treatment, people with glaucoma will slowly lose their peripheral, or side vision. They seem to be looking through a tunnel. Over time, straight-ahead vision may decrease until no vision remains. Tests for Glaucoma Glaucoma is detected through a comprehensive eye exam that includes a visual acuity test, visual field test, dilated eye exam, tonometry, and pachymetry. (Watch the animated video to learn more about testing for glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) A visual acuity test uses an eye chart test to measure how well you see at various distances. A visual field test measures your side or peripheral vision. It helps your eye care professional tell if you have lost', metadata={'source': 'Symptoms of Glaucoma Glaucoma can develop in one or both eyes. The most common type of glaucoma, open-angle glaucoma, has no symptoms at first. It causes no pain, and vision seems normal. Without treatment, people with glaucoma will slowly lose their peripheral, or side vision. They seem to be looking through a tunnel. Over time, straight-ahead vision may decrease until no vision remains. Tests for Glaucoma Glaucoma is detected through a comprehensive eye exam that includes a visual acuity test, visual field test, dilated eye exam, tonometry, and pachymetry. (Watch the animated video to learn more about testing for glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) A visual acuity test uses an eye chart test to measure how well you see at various distances. A visual field test measures your side or peripheral vision. It helps your eye care professional tell if you have lost side vision, a sign of glaucoma. In a dilated eye exam, drops are placed in your eyes to widen, or dilate, the pupils. Your eye care professional uses a special magnifying lens to examine your retina and optic nerve for signs of damage and other eye problems. After the exam, your close-up vision may remain blurred for several hours. In tonometry, an instrument measures the pressure inside the eye. Numbing drops may be applied to your eye for this test. With pachymetry, a numbing drop is applied to your eye. Your eye care professional uses an ultrasonic wave instrument to measure the thickness of your cornea.', 'row': 2}),\n", " Document(page_content='source: NIHSeniorHealth\\nfocus_area: Glaucoma', metadata={'source': 'Symptoms of Glaucoma Glaucoma can develop in one or both eyes. The most common type of glaucoma, open-angle glaucoma, has no symptoms at first. It causes no pain, and vision seems normal. Without treatment, people with glaucoma will slowly lose their peripheral, or side vision. They seem to be looking through a tunnel. Over time, straight-ahead vision may decrease until no vision remains. Tests for Glaucoma Glaucoma is detected through a comprehensive eye exam that includes a visual acuity test, visual field test, dilated eye exam, tonometry, and pachymetry. (Watch the animated video to learn more about testing for glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) A visual acuity test uses an eye chart test to measure how well you see at various distances. A visual field test measures your side or peripheral vision. It helps your eye care professional tell if you have lost side vision, a sign of glaucoma. In a dilated eye exam, drops are placed in your eyes to widen, or dilate, the pupils. Your eye care professional uses a special magnifying lens to examine your retina and optic nerve for signs of damage and other eye problems. After the exam, your close-up vision may remain blurred for several hours. In tonometry, an instrument measures the pressure inside the eye. Numbing drops may be applied to your eye for this test. With pachymetry, a numbing drop is applied to your eye. Your eye care professional uses an ultrasonic wave instrument to measure the thickness of your cornea.', 'row': 2}),\n", " Document(page_content='source: NIHSeniorHealth\\nfocus_area: Glaucoma', metadata={'source': 'Nearly 2.7 million people have glaucoma, a leading cause of blindness in the United States. Although anyone can get glaucoma, some people are at higher risk. They include - African-Americans over age 40 - everyone over age 60, especially Hispanics/Latinos - people with a family history of glaucoma. African-Americans over age 40 everyone over age 60, especially Hispanics/Latinos people with a family history of glaucoma. In addition to age, eye pressure is a risk factor. Whether you develop glaucoma depends on the level of pressure your optic nerve can tolerate without being damaged. This level is different for each person. Thats why a comprehensive dilated eye exam is very important. It can help your eye care professional determine what level of eye pressure is normal for you. Another risk factor for optic nerve damage relates to blood pressure. Thus, it is important to also make sure that your blood pressure is at a proper level for your body by working with your medical doctor. (Watch the animated video to learn more about the causes of glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.)', 'row': 1}),\n", " Document(page_content='source: NIHSeniorHealth\\nfocus_area: Glaucoma', metadata={'source': 'Although open-angle glaucoma cannot be cured, it can usually be controlled. While treatments may save remaining vision, they do not improve sight already lost from glaucoma. The most common treatments for glaucoma are medication and surgery. Medications Medications for glaucoma may be either in the form of eye drops or pills. Some drugs reduce pressure by slowing the flow of fluid into the eye. Others help to improve fluid drainage. (Watch the video to learn more about coping with glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) For most people with glaucoma, regular use of medications will control the increased fluid pressure. But, these drugs may stop working over time. Or, they may cause side effects. If a problem occurs, the eye care professional may select other drugs, change the dose, or suggest other ways to deal with the problem. Read or listen to ways some patients are coping with glaucoma. Surgery Laser surgery is another treatment for glaucoma. During laser surgery, a strong beam of light is focused on the part of the anterior chamber where the fluid leaves the eye. This results in a series of small changes that makes it easier for fluid to exit the eye. Over time, the effect of laser surgery may wear off. Patients who have this form of surgery may need to keep taking glaucoma drugs. Researching Causes and Treatments Through studies in the laboratory and with patients, NEI is seeking better ways to detect, treat, and prevent vision loss in people with glaucoma. For example, researchers have discovered genes that could help explain how glaucoma damages the eye. NEI also is supporting studies to learn more about who is likely to get glaucoma, when to treat people who have increased eye pressure, and which treatment to use first.', 'row': 3}),\n", " Document(page_content='source: NIHSeniorHealth\\nfocus_area: Glaucoma', metadata={'source': \"Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. While glaucoma can strike anyone, the risk is much greater for people over 60. How Glaucoma Develops There are several different types of glaucoma. Most of these involve the drainage system within the eye. At the front of the eye there is a small space called the anterior chamber. A clear fluid flows through this chamber and bathes and nourishes the nearby tissues. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too slowly out of the eye. As the fluid builds up, the pressure inside the eye rises. Unless this pressure is controlled, it may cause damage to the optic nerve and other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of glaucoma is called open-angle glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result. There is no cure for glaucoma. Vision lost from the disease cannot be restored. However, there are treatments that may save remaining vision. That is why early diagnosis is important. See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\", 'row': 0})]" ] }, "metadata": {}, "execution_count": 80 } ], "source": [ "sources" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "K7P30pL57WVV", "outputId": "be41f8f0-a201-494c-bfe8-f2d2752d7b35" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "\n", "> Question:\n", "'What are the causes of glaucoma'\n", "\n", "> Answer:\n", "(\"Glaucoma is a group of diseases that can damage the eye's optic nerve and \"\n", " 'result in vision loss and blindness. The most common form of glaucoma is '\n", " 'open-angle glaucoma, where the clear fluid in the eye drains too slowly, '\n", " 'causing pressure to build up and potentially damage the optic nerve. The '\n", " 'risk of glaucoma is higher for people over 60, African-Americans over age '\n", " '40, Hispanics/Latinos over age 60, and those with a family history of '\n", " 'glaucoma. Glaucoma typically has no symptoms at first, but without '\n", " 'treatment, it can lead to the loss of peripheral vision and eventually '\n", " 'complete vision loss. While there is no cure for glaucoma, early diagnosis '\n", " 'and treatment can help protect remaining vision. Treatment options include '\n", " 'medication, laser surgery, and ongoing research to improve detection, '\n", " 'treatment, and prevention of glaucoma.')\n", "\n", "> Eval:\n", "('No, the response is not faithful. The response does not accurately answer '\n", " 'the question of what causes glaucoma.')\n", "----------------------------------SOURCE DOCUMENTS---------------------------\n", "\n", "> Nearly 2.7 million people have glaucoma, a leading cause of blindness in the United States. Although anyone can get glaucoma, some people are at higher risk. They include - African-Americans over age 40 - everyone over age 60, especially Hispanics/Latinos - people with a family history of glaucoma. African-Americans over age 40 everyone over age 60, especially Hispanics/Latinos people with a family history of glaucoma. In addition to age, eye pressure is a risk factor. Whether you develop glaucoma depends on the level of pressure your optic nerve can tolerate without being damaged. This level is different for each person. Thats why a comprehensive dilated eye exam is very important. It can help your eye care professional determine what level of eye pressure is normal for you. Another risk factor for optic nerve damage relates to blood pressure. Thus, it is important to also make sure that your blood pressure is at a proper level for your body by working with your medical doctor. (Watch the animated video to learn more about the causes of glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.)\n", "'question: What causes Glaucoma ?'\n", "\n", "> Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. While glaucoma can strike anyone, the risk is much greater for people over 60. How Glaucoma Develops There are several different types of glaucoma. Most of these involve the drainage system within the eye. At the front of the eye there is a small space called the anterior chamber. A clear fluid flows through this chamber and bathes and nourishes the nearby tissues. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too slowly out of the eye. As the fluid builds up, the pressure inside the eye rises. Unless this pressure is controlled, it may cause damage to the optic nerve and other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of glaucoma is called open-angle glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result. There is no cure for glaucoma. Vision lost from the disease cannot be restored. However, there are treatments that may save remaining vision. That is why early diagnosis is important. See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\n", "(\"answer: Glaucoma is a group of diseases that can damage the eye's optic \"\n", " 'nerve and result in vision loss and blindness. While glaucoma can strike '\n", " 'anyone, the risk is much greater for people over 60. How Glaucoma Develops '\n", " 'There are several different types of glaucoma. Most of these involve the '\n", " 'drainage system within the eye. At the front of the eye there is a small '\n", " 'space called the anterior chamber. A clear fluid flows through this chamber '\n", " 'and bathes and nourishes the nearby tissues. (Watch the video to learn more '\n", " 'about glaucoma. To enlarge the video, click the brackets in the lower '\n", " 'right-hand corner. To reduce the video, press the Escape (Esc) button on '\n", " 'your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too '\n", " 'slowly out of the eye. As the fluid builds up, the pressure inside the eye '\n", " 'rises. Unless this pressure is controlled, it may cause damage to the optic '\n", " 'nerve and other parts of the eye and result in loss of vision. Open-angle '\n", " 'Glaucoma The most common type of')\n", "\n", "> Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. While glaucoma can strike anyone, the risk is much greater for people over 60. How Glaucoma Develops There are several different types of glaucoma. Most of these involve the drainage system within the eye. At the front of the eye there is a small space called the anterior chamber. A clear fluid flows through this chamber and bathes and nourishes the nearby tissues. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too slowly out of the eye. As the fluid builds up, the pressure inside the eye rises. Unless this pressure is controlled, it may cause damage to the optic nerve and other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of glaucoma is called open-angle glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result. There is no cure for glaucoma. Vision lost from the disease cannot be restored. However, there are treatments that may save remaining vision. That is why early diagnosis is important. See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\n", "('other parts of the eye and result in loss of vision. Open-angle Glaucoma The '\n", " 'most common type of glaucoma is called open-angle glaucoma. In the normal '\n", " 'eye, the clear fluid leaves the anterior chamber at the open angle where the '\n", " 'cornea and iris meet. When fluid reaches the angle, it flows through a '\n", " 'spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid '\n", " 'reaches the angle, it passes too slowly through the meshwork drain, causing '\n", " 'the pressure inside the eye to build. If the pressure damages the optic '\n", " 'nerve, open-angle glaucoma -- and vision loss -- may result. There is no '\n", " 'cure for glaucoma. Vision lost from the disease cannot be restored. However, '\n", " 'there are treatments that may save remaining vision. That is why early '\n", " 'diagnosis is important. See this graphic for a quick overview of glaucoma, '\n", " 'including how many people it affects, whos at risk, what to do if you have '\n", " 'it, and how to learn more. See a glossary of glaucoma terms.')\n", "\n", "> Open-angle glaucoma is the most common form of glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When the fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result.\n", "('question: What is (are) Glaucoma ?\\n'\n", " 'answer: Open-angle glaucoma is the most common form of glaucoma. In the '\n", " 'normal eye, the clear fluid leaves the anterior chamber at the open angle '\n", " 'where the cornea and iris meet. When the fluid reaches the angle, it flows '\n", " 'through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when '\n", " 'the fluid reaches the angle, it passes too slowly through the meshwork '\n", " 'drain, causing the pressure inside the eye to build. If the pressure damages '\n", " 'the optic nerve, open-angle glaucoma -- and vision loss -- may result.\\n'\n", " 'source: NIHSeniorHealth\\n'\n", " 'focus_area: Glaucoma')\n", "\n", "> Nearly 2.7 million people have glaucoma, a leading cause of blindness in the United States. Although anyone can get glaucoma, some people are at higher risk. They include - African-Americans over age 40 - everyone over age 60, especially Hispanics/Latinos - people with a family history of glaucoma. African-Americans over age 40 everyone over age 60, especially Hispanics/Latinos people with a family history of glaucoma. In addition to age, eye pressure is a risk factor. Whether you develop glaucoma depends on the level of pressure your optic nerve can tolerate without being damaged. This level is different for each person. Thats why a comprehensive dilated eye exam is very important. It can help your eye care professional determine what level of eye pressure is normal for you. Another risk factor for optic nerve damage relates to blood pressure. Thus, it is important to also make sure that your blood pressure is at a proper level for your body by working with your medical doctor. (Watch the animated video to learn more about the causes of glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.)\n", "('answer: Nearly 2.7 million people have glaucoma, a leading cause of '\n", " 'blindness in the United States. Although anyone can get glaucoma, some '\n", " 'people are at higher risk. They include - African-Americans over age 40 - '\n", " 'everyone over age 60, especially Hispanics/Latinos - people with a family '\n", " 'history of glaucoma. African-Americans over age 40 everyone over age 60, '\n", " 'especially Hispanics/Latinos people with a family history of glaucoma. In '\n", " 'addition to age, eye pressure is a risk factor. Whether you develop glaucoma '\n", " 'depends on the level of pressure your optic nerve can tolerate without being '\n", " 'damaged. This level is different for each person. Thats why a comprehensive '\n", " 'dilated eye exam is very important. It can help your eye care professional '\n", " 'determine what level of eye pressure is normal for you. Another risk factor '\n", " 'for optic nerve damage relates to blood pressure. Thus, it is important to '\n", " 'also make sure that your blood pressure is at a proper level for your body '\n", " 'by working with your medical')\n", "\n", "> Symptoms of Glaucoma Glaucoma can develop in one or both eyes. The most common type of glaucoma, open-angle glaucoma, has no symptoms at first. It causes no pain, and vision seems normal. Without treatment, people with glaucoma will slowly lose their peripheral, or side vision. They seem to be looking through a tunnel. Over time, straight-ahead vision may decrease until no vision remains. Tests for Glaucoma Glaucoma is detected through a comprehensive eye exam that includes a visual acuity test, visual field test, dilated eye exam, tonometry, and pachymetry. (Watch the animated video to learn more about testing for glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) A visual acuity test uses an eye chart test to measure how well you see at various distances. A visual field test measures your side or peripheral vision. It helps your eye care professional tell if you have lost side vision, a sign of glaucoma. In a dilated eye exam, drops are placed in your eyes to widen, or dilate, the pupils. Your eye care professional uses a special magnifying lens to examine your retina and optic nerve for signs of damage and other eye problems. After the exam, your close-up vision may remain blurred for several hours. In tonometry, an instrument measures the pressure inside the eye. Numbing drops may be applied to your eye for this test. With pachymetry, a numbing drop is applied to your eye. Your eye care professional uses an ultrasonic wave instrument to measure the thickness of your cornea.\n", "'question: What are the symptoms of Glaucoma ?'\n", "\n", "> Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. While glaucoma can strike anyone, the risk is much greater for people over 60. How Glaucoma Develops There are several different types of glaucoma. Most of these involve the drainage system within the eye. At the front of the eye there is a small space called the anterior chamber. A clear fluid flows through this chamber and bathes and nourishes the nearby tissues. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too slowly out of the eye. As the fluid builds up, the pressure inside the eye rises. Unless this pressure is controlled, it may cause damage to the optic nerve and other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of glaucoma is called open-angle glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result. There is no cure for glaucoma. Vision lost from the disease cannot be restored. However, there are treatments that may save remaining vision. That is why early diagnosis is important. See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\n", "'question: What is (are) Glaucoma ?'\n", "\n", "> Although open-angle glaucoma cannot be cured, it can usually be controlled. While treatments may save remaining vision, they do not improve sight already lost from glaucoma. The most common treatments for glaucoma are medication and surgery. Medications Medications for glaucoma may be either in the form of eye drops or pills. Some drugs reduce pressure by slowing the flow of fluid into the eye. Others help to improve fluid drainage. (Watch the video to learn more about coping with glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) For most people with glaucoma, regular use of medications will control the increased fluid pressure. But, these drugs may stop working over time. Or, they may cause side effects. If a problem occurs, the eye care professional may select other drugs, change the dose, or suggest other ways to deal with the problem. Read or listen to ways some patients are coping with glaucoma. Surgery Laser surgery is another treatment for glaucoma. During laser surgery, a strong beam of light is focused on the part of the anterior chamber where the fluid leaves the eye. This results in a series of small changes that makes it easier for fluid to exit the eye. Over time, the effect of laser surgery may wear off. Patients who have this form of surgery may need to keep taking glaucoma drugs. Researching Causes and Treatments Through studies in the laboratory and with patients, NEI is seeking better ways to detect, treat, and prevent vision loss in people with glaucoma. For example, researchers have discovered genes that could help explain how glaucoma damages the eye. NEI also is supporting studies to learn more about who is likely to get glaucoma, when to treat people who have increased eye pressure, and which treatment to use first.\n", "'question: What are the treatments for Glaucoma ?'\n", "\n", "> Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. The most common form of the disease is open-angle glaucoma. With early treatment, you can often protect your eyes against serious vision loss. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\n", "('question: What is (are) Glaucoma ?\\n'\n", " \"answer: Glaucoma is a group of diseases that can damage the eye's optic \"\n", " 'nerve and result in vision loss and blindness. The most common form of the '\n", " 'disease is open-angle glaucoma. With early treatment, you can often protect '\n", " 'your eyes against serious vision loss. (Watch the video to learn more about '\n", " 'glaucoma. To enlarge the video, click the brackets in the lower right-hand '\n", " 'corner. To reduce the video, press the Escape (Esc) button on your '\n", " 'keyboard.) See this graphic for a quick overview of glaucoma, including how '\n", " 'many people it affects, whos at risk, what to do if you have it, and how to '\n", " 'learn more. See a glossary of glaucoma terms.\\n'\n", " 'source: NIHSeniorHealth\\n'\n", " 'focus_area: Glaucoma')\n", "\n", "> Although open-angle glaucoma cannot be cured, it can usually be controlled. While treatments may save remaining vision, they do not improve sight already lost from glaucoma. The most common treatments for glaucoma are medication and surgery. Medications Medications for glaucoma may be either in the form of eye drops or pills. Some drugs reduce pressure by slowing the flow of fluid into the eye. Others help to improve fluid drainage. (Watch the video to learn more about coping with glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) For most people with glaucoma, regular use of medications will control the increased fluid pressure. But, these drugs may stop working over time. Or, they may cause side effects. If a problem occurs, the eye care professional may select other drugs, change the dose, or suggest other ways to deal with the problem. Read or listen to ways some patients are coping with glaucoma. Surgery Laser surgery is another treatment for glaucoma. During laser surgery, a strong beam of light is focused on the part of the anterior chamber where the fluid leaves the eye. This results in a series of small changes that makes it easier for fluid to exit the eye. Over time, the effect of laser surgery may wear off. Patients who have this form of surgery may need to keep taking glaucoma drugs. Researching Causes and Treatments Through studies in the laboratory and with patients, NEI is seeking better ways to detect, treat, and prevent vision loss in people with glaucoma. For example, researchers have discovered genes that could help explain how glaucoma damages the eye. NEI also is supporting studies to learn more about who is likely to get glaucoma, when to treat people who have increased eye pressure, and which treatment to use first.\n", "('change the dose, or suggest other ways to deal with the problem. Read or '\n", " 'listen to ways some patients are coping with glaucoma. Surgery Laser surgery '\n", " 'is another treatment for glaucoma. During laser surgery, a strong beam of '\n", " 'light is focused on the part of the anterior chamber where the fluid leaves '\n", " 'the eye. This results in a series of small changes that makes it easier for '\n", " 'fluid to exit the eye. Over time, the effect of laser surgery may wear off. '\n", " 'Patients who have this form of surgery may need to keep taking glaucoma '\n", " 'drugs. Researching Causes and Treatments Through studies in the laboratory '\n", " 'and with patients, NEI is seeking better ways to detect, treat, and prevent '\n", " 'vision loss in people with glaucoma. For example, researchers have '\n", " 'discovered genes that could help explain how glaucoma damages the eye. NEI '\n", " 'also is supporting studies to learn more about who is likely to get '\n", " 'glaucoma, when to treat people who have increased eye pressure, and which '\n", " 'treatment to use first.')\n", "\n", "> Symptoms of Glaucoma Glaucoma can develop in one or both eyes. The most common type of glaucoma, open-angle glaucoma, has no symptoms at first. It causes no pain, and vision seems normal. Without treatment, people with glaucoma will slowly lose their peripheral, or side vision. They seem to be looking through a tunnel. Over time, straight-ahead vision may decrease until no vision remains. Tests for Glaucoma Glaucoma is detected through a comprehensive eye exam that includes a visual acuity test, visual field test, dilated eye exam, tonometry, and pachymetry. (Watch the animated video to learn more about testing for glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) A visual acuity test uses an eye chart test to measure how well you see at various distances. A visual field test measures your side or peripheral vision. It helps your eye care professional tell if you have lost side vision, a sign of glaucoma. In a dilated eye exam, drops are placed in your eyes to widen, or dilate, the pupils. Your eye care professional uses a special magnifying lens to examine your retina and optic nerve for signs of damage and other eye problems. After the exam, your close-up vision may remain blurred for several hours. In tonometry, an instrument measures the pressure inside the eye. Numbing drops may be applied to your eye for this test. With pachymetry, a numbing drop is applied to your eye. Your eye care professional uses an ultrasonic wave instrument to measure the thickness of your cornea.\n", "('answer: Symptoms of Glaucoma Glaucoma can develop in one or both eyes. The '\n", " 'most common type of glaucoma, open-angle glaucoma, has no symptoms at first. '\n", " 'It causes no pain, and vision seems normal. Without treatment, people with '\n", " 'glaucoma will slowly lose their peripheral, or side vision. They seem to be '\n", " 'looking through a tunnel. Over time, straight-ahead vision may decrease '\n", " 'until no vision remains. Tests for Glaucoma Glaucoma is detected through a '\n", " 'comprehensive eye exam that includes a visual acuity test, visual field '\n", " 'test, dilated eye exam, tonometry, and pachymetry. (Watch the animated video '\n", " 'to learn more about testing for glaucoma. To enlarge the video, click the '\n", " 'brackets in the lower right-hand corner. To reduce the video, press the '\n", " 'Escape (Esc) button on your keyboard.) A visual acuity test uses an eye '\n", " 'chart test to measure how well you see at various distances. A visual field '\n", " 'test measures your side or peripheral vision. It helps your eye care '\n", " 'professional tell if you have lost')\n", "\n", "> Symptoms of Glaucoma Glaucoma can develop in one or both eyes. The most common type of glaucoma, open-angle glaucoma, has no symptoms at first. It causes no pain, and vision seems normal. Without treatment, people with glaucoma will slowly lose their peripheral, or side vision. They seem to be looking through a tunnel. Over time, straight-ahead vision may decrease until no vision remains. Tests for Glaucoma Glaucoma is detected through a comprehensive eye exam that includes a visual acuity test, visual field test, dilated eye exam, tonometry, and pachymetry. (Watch the animated video to learn more about testing for glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) A visual acuity test uses an eye chart test to measure how well you see at various distances. A visual field test measures your side or peripheral vision. It helps your eye care professional tell if you have lost side vision, a sign of glaucoma. In a dilated eye exam, drops are placed in your eyes to widen, or dilate, the pupils. Your eye care professional uses a special magnifying lens to examine your retina and optic nerve for signs of damage and other eye problems. After the exam, your close-up vision may remain blurred for several hours. In tonometry, an instrument measures the pressure inside the eye. Numbing drops may be applied to your eye for this test. With pachymetry, a numbing drop is applied to your eye. Your eye care professional uses an ultrasonic wave instrument to measure the thickness of your cornea.\n", "'source: NIHSeniorHealth\\nfocus_area: Glaucoma'\n", "\n", "> Nearly 2.7 million people have glaucoma, a leading cause of blindness in the United States. Although anyone can get glaucoma, some people are at higher risk. They include - African-Americans over age 40 - everyone over age 60, especially Hispanics/Latinos - people with a family history of glaucoma. African-Americans over age 40 everyone over age 60, especially Hispanics/Latinos people with a family history of glaucoma. In addition to age, eye pressure is a risk factor. Whether you develop glaucoma depends on the level of pressure your optic nerve can tolerate without being damaged. This level is different for each person. Thats why a comprehensive dilated eye exam is very important. It can help your eye care professional determine what level of eye pressure is normal for you. Another risk factor for optic nerve damage relates to blood pressure. Thus, it is important to also make sure that your blood pressure is at a proper level for your body by working with your medical doctor. (Watch the animated video to learn more about the causes of glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.)\n", "'source: NIHSeniorHealth\\nfocus_area: Glaucoma'\n", "\n", "> Although open-angle glaucoma cannot be cured, it can usually be controlled. While treatments may save remaining vision, they do not improve sight already lost from glaucoma. The most common treatments for glaucoma are medication and surgery. Medications Medications for glaucoma may be either in the form of eye drops or pills. Some drugs reduce pressure by slowing the flow of fluid into the eye. Others help to improve fluid drainage. (Watch the video to learn more about coping with glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) For most people with glaucoma, regular use of medications will control the increased fluid pressure. But, these drugs may stop working over time. Or, they may cause side effects. If a problem occurs, the eye care professional may select other drugs, change the dose, or suggest other ways to deal with the problem. Read or listen to ways some patients are coping with glaucoma. Surgery Laser surgery is another treatment for glaucoma. During laser surgery, a strong beam of light is focused on the part of the anterior chamber where the fluid leaves the eye. This results in a series of small changes that makes it easier for fluid to exit the eye. Over time, the effect of laser surgery may wear off. Patients who have this form of surgery may need to keep taking glaucoma drugs. Researching Causes and Treatments Through studies in the laboratory and with patients, NEI is seeking better ways to detect, treat, and prevent vision loss in people with glaucoma. For example, researchers have discovered genes that could help explain how glaucoma damages the eye. NEI also is supporting studies to learn more about who is likely to get glaucoma, when to treat people who have increased eye pressure, and which treatment to use first.\n", "'source: NIHSeniorHealth\\nfocus_area: Glaucoma'\n", "\n", "> Glaucoma is a group of diseases that can damage the eye's optic nerve and result in vision loss and blindness. While glaucoma can strike anyone, the risk is much greater for people over 60. How Glaucoma Develops There are several different types of glaucoma. Most of these involve the drainage system within the eye. At the front of the eye there is a small space called the anterior chamber. A clear fluid flows through this chamber and bathes and nourishes the nearby tissues. (Watch the video to learn more about glaucoma. To enlarge the video, click the brackets in the lower right-hand corner. To reduce the video, press the Escape (Esc) button on your keyboard.) In glaucoma, for still unknown reasons, the fluid drains too slowly out of the eye. As the fluid builds up, the pressure inside the eye rises. Unless this pressure is controlled, it may cause damage to the optic nerve and other parts of the eye and result in loss of vision. Open-angle Glaucoma The most common type of glaucoma is called open-angle glaucoma. In the normal eye, the clear fluid leaves the anterior chamber at the open angle where the cornea and iris meet. When fluid reaches the angle, it flows through a spongy meshwork, like a drain, and leaves the eye. Sometimes, when the fluid reaches the angle, it passes too slowly through the meshwork drain, causing the pressure inside the eye to build. If the pressure damages the optic nerve, open-angle glaucoma -- and vision loss -- may result. There is no cure for glaucoma. Vision lost from the disease cannot be restored. However, there are treatments that may save remaining vision. That is why early diagnosis is important. See this graphic for a quick overview of glaucoma, including how many people it affects, whos at risk, what to do if you have it, and how to learn more. See a glossary of glaucoma terms.\n", "'source: NIHSeniorHealth\\nfocus_area: Glaucoma'\n", "----------------------------------SOURCE DOCUMENTS---------------------------\n" ] } ], "source": [ "from pprint import pprint\n", "\n", "# Print question, answer, and evaluations\n", "print(\"\\n\\n> Question:\")\n", "pprint(query)\n", "print(\"\\n> Answer:\")\n", "pprint(answer)\n", "print(\"\\n> Eval:\")\n", "pprint(evals)\n", "\n", "# Print the relevant sources used for the answer\n", "print(\"----------------------------------SOURCE DOCUMENTS---------------------------\")\n", "for document in sources:\n", " print(\"\\n> \" + document.metadata[\"source\"])\n", " pprint(document.page_content[:1000])\n", "print(\"----------------------------------SOURCE DOCUMENTS---------------------------\")\n" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "id": "UV7_OQHg7WVW" }, "outputs": [], "source": [ "import gradio as gr" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "id": "n1wKmRl37WVW" }, "outputs": [], "source": [ "def get_response_from_query(db, query, k=3):\n", "\n", "\n", " docs = db.similarity_search(query, k=k)\n", "\n", " docs_page_content = \" \".join([d.page_content for d in docs])\n", "\n", " # llm = BardLLM()\n", " llm = ChatOpenAI(model_name=\"gpt-3.5-turbo-16k\",temperature=0)\n", "\n", " prompt = PromptTemplate(\n", " input_variables=[\"question\", \"docs\"],\n", " template=\"\"\"\n", " A bot that is open to discussions about different cultural, philosophical and political exchanges. I will use do different analysis to the articles provided to me. Stay truthful and if you weren't provided any resources give your oppinion only.\n", " Answer the following question: {question}\n", " By searching the following articles: {docs}\n", "\n", " Only use the factual information from the documents. Make sure to mention key phrases from the articles.\n", "\n", " If you feel like you don't have enough information to answer the question, say \"I don't know\".\n", "\n", " \"\"\",\n", " )\n", "\n", " chain = LLMChain(llm=llm, prompt=prompt)\n", " # chain = RetrievalQAWithSourcesChain.from_chain_type(llm=llm, prompt=prompt,\n", " # chain_type=\"stuff\", retriever=db.as_retriever(), return_source_documents=True)\n", "\n", " response = chain.run(question=query, docs=docs_page_content,return_source_documents=True)\n", " r_text = str(response)\n", "\n", " ##evaluation part\n", "\n", " prompt_eval = PromptTemplate(\n", " input_variables=[\"answer\", \"docs\"],\n", " template=\"\"\"\n", " You job is to evaluate if the response to a given context is faithful.\n", "\n", " for the following: {answer}\n", " By searching the following article: {docs}\n", "\n", " Give a reason why they are similar or not, start with a Yes or a No.\n", " \"\"\",\n", " )\n", "\n", " chain_part_2 = LLMChain(llm=llm, prompt=prompt_eval)\n", "\n", "\n", " evals = chain_part_2.run(answer=r_text, docs=docs_page_content)\n", "\n", " return response,docs,evals" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "id": "7VMW3kDRG2lM" }, "outputs": [], "source": [ "from linkify_it import LinkifyIt" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "background_save": true }, "id": "IdKyZMdyjQ1g" }, "outputs": [], "source": [ "#!pip list" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 591 }, "id": "YqMJNvrl7WVW", "outputId": "b10c0577-0bed-4ff0-f08d-c0cd637e0f4b" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n", "Running on public URL: https://0e10f7835c64fcfb57.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "
" ] }, "metadata": {} }, { "output_type": "execute_result", "data": { "text/plain": [] }, "metadata": {}, "execution_count": 85 } ], "source": [ "def greet(query):\n", "\n", " answer,sources,evals = get_response_from_query(db,query,2)\n", " return answer,sources,evals\n", "examples = [\n", " [\"How to be happy\"],\n", " [\"Climate Change Challenges in Europe\"],\n", " [\"Philosophy in the world of Minimalism\"],\n", " [\"Hate Speech vs Freedom of Speech\"],\n", " [\"Articles by Noam Chomsky on US Politics\"],\n", " [\"The importance of values and reflection\"]\n", " ]\n", "demo = gr.Interface(fn=greet, title=\"cicero-semantic-search\", inputs=\"text\",\n", " outputs=[gr.components.Textbox(lines=3, label=\"Response\"),\n", " gr.components.Textbox(lines=3, label=\"Source\"),\n", " gr.components.Textbox(lines=3, label=\"Evaluation\")],\n", " examples=examples)\n", "\n", "demo.launch(share=True)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UnjSpVxc7WVW", "outputId": "40178747-3931-4cfd-9197-89d66c3fe355" }, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:88: UserWarning: \n", "The secret `HF_TOKEN` does not exist in your Colab secrets.\n", "To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n", "You will be able to reuse this secret in all of your notebooks.\n", "Please note that authentication is recommended but still optional to access public models or datasets.\n", " warnings.warn(\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Loaded as API: https://0e10f7835c64fcfb57.gradio.live/ ✔\n", "(\"I apologize, but as an AI language model, I don't have direct access to search the internet or browse articles. However, I can provide you with some general information about diagnosing Paget's Disease of Bone.\\n\\nTo diagnose Paget's Disease of Bone, doctors typically use a combination of medical history, physical examination, imaging tests, and blood tests. Here are some key phrases related to the diagnosis of Paget's Disease of Bone:\\n\\n1. Medical History: Doctors may ask about symptoms, family history, and any previous bone conditions or fractures.\\n\\n2. Physical Examination: During a physical exam, doctors may check for bone deformities, enlarged bones, or areas of tenderness.\\n\\n3. Imaging Tests: X-rays, bone scans, and sometimes CT or MRI scans can help identify affected bones, detect bone abnormalities, and assess the extent of the disease.\\n\\n4. Blood Tests: Blood tests can measure specific markers, such as alkaline phosphatase (ALP) and collagen type I cross-linked N-telopeptide (NTx), which are often elevated in Paget's Disease of Bone.\\n\\nIt's important to consult with a healthcare professional for an accurate diagnosis and appropriate treatment options.\", '[Document(page_content=\"question: How to diagnose Paget\\'s Disease of Bone ?\", metadata={\\'source\\': \"An Underdiagnosed Disease Experts believe that Paget\\'s disease is underdiagnosed; people with a mild case and no symptoms may never know they have the disease. Or, they may receive a diagnosis by accident when x-rays or other laboratory tests done for another reason reveal Paget\\'s disease. When symptoms do occur, they usually appear gradually and, in the early stages, may be confused with those of arthritis or other medical problems. Sometimes a person may not receive a clear diagnosis until the disease progresses and complications develop. Diagnostic Tests X-rays are almost always used to diagnose Paget\\'s disease, but the disease may be discovered using one of three tests: - x-rays - an alkaline phosphatase blood test - or a bone scan. x-rays an alkaline phosphatase blood test or a bone scan. Bones affected by Paget\\'s disease have a distinctive appearance on x-rays, which may show increased bone density, an abnormal bone structure, bowing, and enlargement. X-rays of leg bones may show very tiny fractures called microfractures. The enzyme alkaline phosphatase is involved in the normal growth of new bone. Having higher-than-normal levels of this chemical in the blood, however, may be a sign of Paget\\'s disease. The alkaline phosphatase blood test measures the level of this substance. A bone scan provides a picture of the affected bones that doctors can use to see how far the disease has progressed. If a bone scan done for another reason suggests Paget\\'s disease, the doctor can order x-rays to confirm the diagnosis. If the Disease Runs in the Family Early diagnosis and treatment of Paget\\'s disease is important. Because Paget\\'s disease can be hereditary, some experts recommend that the brothers, sisters, and children of anyone with the disease talk to their doctor about having an alkaline phosphatase blood test every 2 to 3 years after about age 40.\", \\'row\\': 35}), Document(page_content=\"question: What are the treatments for Paget\\'s Disease of Bone ?\", metadata={\\'source\\': \"Early Diagnosis is Important Although there is no cure for Paget\\'s disease of bone, it is treatable. Treatment is most effective when the disease is diagnosed early, before it causes major changes in the affected bones. The goal of treatment is to relieve bone pain and prevent the disease from progressing. Medications Are Available The Food and Drug Administration has approved several medications that can stop or slow down the progression of the disease and reduce pain and other symptoms. These medications fall into two categories: bisphosphonates and calcitonin. Both medications work by stopping or reducing the excessive breakdown of old bone that leads to excessive formation of new, but weaker, bone. People with Paget\\'s disease should talk to their doctors about which medication is right for them. Bisphosphonates Six bisphosphonates are currently available for patients with Paget\\'s disease. Doctors most commonly recommend the strongest ones, which include - risedronate - alendronate - pamidronate - zoledronic acid - tiludronate and etidronate are not as strong but may be appropriate for some patients. risedronate alendronate pamidronate zoledronic acid tiludronate and etidronate are not as strong but may be appropriate for some patients. Some of the bisphosphonates approved for the treatment of Paget\\'s disease, including risedronate and alendronate, are also approved for the treatment of osteoporosis. However, people with Paget\\'s disease must take higher dosages of these medicines for shorter periods of time than people with osteoporosis. Calcitonin Doctors also may prescribe calcitonin to treat Paget\\'s disease in some people, although it has been found to be less effective than bisphosphonates. Calcitonin is a naturally occurring hormone made by the thyroid gland. Your doctor may recommend that you repeat calcitonin treatments with brief rest periods in between treatments. The nasal spray form of calcitonin is not recommended or approved to treat Paget\\'s disease. Surgery Surgery may be a treatment option for some people. Hip or knee replacement surgery may help people with severe arthritis. Surgery can also realign affected leg bones to reduce pain or help broken bones heal in a better position. Nutrition and Exercise Good nutrition and exercise are important for bone health, and that is true for people with Paget\\'s disease as well. Women over age 50 should consume 1,200 milligrams (mg) of calcium daily. Men between the ages of 51 and 70 should consume 1,000 mg of calcium a day, and men over 70 should consume 1,200 mg per day. People ages 51 to 70 should consume at least 600 international units (IU) of vitamin D daily. People over age 70 should consume at least 800 IUs daily. Calcium keeps bones strong, and vitamin D helps the body absorb calcium. Exercise is very important in maintaining bone health, avoiding weight gain, and keeping joints mobile. However, people with Paget\\'s disease need to avoid putting too much stress on affected bones. They should discuss their exercise program with their doctor to make sure it is a good one for them. Finding New Treatments Recently, there have been major advances in the treatment of Paget\\'s disease of bone. Research into new treatments continues. Some researchers are trying to identify the genetic and viral causes of the disease. Other researchers are learning more about bone biology to better understand how the body breaks down old bone and replaces it with new bone.\", \\'row\\': 36})]', 'No, the response is not faithful. The response states that the AI language model does not have direct access to search the internet or browse articles, but then it mentions searching for an article to answer the given questions.')\n" ] } ], "source": [ "from gradio_client import Client\n", "\n", "client = Client(\"https://0e10f7835c64fcfb57.gradio.live\")\n", "result = client.predict(\n", "\t\t\"How to diagnose Paget's Disease of Bone ?\",\t# str in 'query' Textbox component\n", "\t\tapi_name=\"/predict\"\n", ")\n", "print(result)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "KcuKWqwj_AgQ" }, "outputs": [], "source": [] } ], "metadata": { "accelerator": "GPU", "colab": { "gpuType": "T4", "provenance": [], "toc_visible": true }, "environment": { "kernel": "python3", "name": "pytorch-gpu.1-13.m108", "type": "gcloud", "uri": "gcr.io/deeplearning-platform-release/pytorch-gpu.1-13:m108" }, "kernelspec": { "display_name": "Python 3", "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.10.10" } }, "nbformat": 4, "nbformat_minor": 0 }