{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "provenance": [],
   "machine_shape": "hm",
   "gpuType": "T4"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "gpuClass": "standard"
 },
 "cells": [
  {
   "cell_type": "code",
   "source": [
    "!apt-get install poppler-utils "
   ],
   "metadata": {
    "id": "fEo--VJph88f"
   },
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "id": "HF-QO_A9kcNW"
   },
   "outputs": [],
   "source": [
    "!pip install -Uqqq pip --progress-bar off\n",
    "!pip install -qqq langchain==0.0.173 --progress-bar off\n",
    "!pip install -qqq chromadb==0.3.23 --progress-bar off\n",
    "!pip install -qqq pypdf==3.8.1 --progress-bar off\n",
    "!pip install -qqq pygpt4all==1.1.0 --progress-bar off\n",
    "!pip install -qqq pdf2image==1.16.3 --progress-bar off"
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "!gdown 1DpFisoGXsQbpQJvijuvxkLW_pg-FUUMF"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "SZ10dc5_0G9h",
    "outputId": "dcf83cc1-2229-4ba2-9304-180412147c7a"
   },
   "execution_count": 2,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Downloading...\n",
      "From: https://drive.google.com/uc?id=1DpFisoGXsQbpQJvijuvxkLW_pg-FUUMF\n",
      "To: /content/ms-financial-statement.pdf\n",
      "\r  0% 0.00/29.1k [00:00<?, ?B/s]\r100% 29.1k/29.1k [00:00<00:00, 75.4MB/s]\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "!wget https://gpt4all.io/models/ggml-gpt4all-j-v1.3-groovy.bin"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "_CB5erWvXT8s",
    "outputId": "3cd2bc99-b840-4940-df87-2c500a0b9048"
   },
   "execution_count": 3,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "--2023-05-20 07:41:01--  https://gpt4all.io/models/ggml-gpt4all-j-v1.3-groovy.bin\n",
      "Resolving gpt4all.io (gpt4all.io)... 104.26.1.159, 104.26.0.159, 172.67.71.169, ...\n",
      "Connecting to gpt4all.io (gpt4all.io)|104.26.1.159|:443... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 3785248281 (3.5G)\n",
      "Saving to: ‘ggml-gpt4all-j-v1.3-groovy.bin’\n",
      "\n",
      "ggml-gpt4all-j-v1.3 100%[===================>]   3.52G  52.6MB/s    in 67s     \n",
      "\n",
      "2023-05-20 07:42:08 (53.6 MB/s) - ‘ggml-gpt4all-j-v1.3-groovy.bin’ saved [3785248281/3785248281]\n",
      "\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "from langchain.chains import RetrievalQA\n",
    "from langchain.document_loaders import PyPDFLoader\n",
    "from langchain.embeddings import HuggingFaceEmbeddings\n",
    "from langchain.llms import GPT4All\n",
    "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
    "from langchain.vectorstores import Chroma\n",
    "from pdf2image import convert_from_path"
   ],
   "metadata": {
    "id": "XtsFzzRUnOlS"
   },
   "execution_count": 19,
   "outputs": []
  },
  {
   "cell_type": "code",
   "source": [
    "images = convert_from_path(\"ms-financial-statement.pdf\", dpi=88)\n",
    "len(images)"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "-x1lI0qbh0nK",
    "outputId": "220e2649-53f1-480b-8681-e9458dd9b93e"
   },
   "execution_count": 46,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "metadata": {},
     "execution_count": 46
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "images[0]"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 986
    },
    "id": "_xDO61eWiFkj",
    "outputId": "32e085d9-abae-424e-f630-7addd20d3d9a"
   },
   "execution_count": 47,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "<PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=749x969 at 0x7EFF403D6740>"
      ],
      "image/png": "[encoded data, 84460 characters]\n"
     },
     "metadata": {},
     "execution_count": 47
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "images[1]"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 986
    },
    "id": "qVK2C-vTicPi",
    "outputId": "f89ea0d9-934a-4978-be69-eb058ccc945f"
   },
   "execution_count": 48,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "<PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=749x969 at 0x7EFF403D50C0>"
      ],
      "image/png": "[encoded data, 132352 characters]\n"
     },
     "metadata": {},
     "execution_count": 48
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Load Data"
   ],
   "metadata": {
    "id": "C3T21B3R0hHH"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "loader = PyPDFLoader(\"ms-financial-statement.pdf\")"
   ],
   "metadata": {
    "id": "BPMq8BWASZM0"
   },
   "execution_count": 3,
   "outputs": []
  },
  {
   "cell_type": "code",
   "source": [
    "documents = loader.load_and_split()"
   ],
   "metadata": {
    "id": "cEcersyxT_gl"
   },
   "execution_count": 4,
   "outputs": []
  },
  {
   "cell_type": "code",
   "source": [
    "len(documents)"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "SvmNQEFFLelF",
    "outputId": "b0adcb2c-a65d-421d-f687-47b48aef45d5"
   },
   "execution_count": 5,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "print(documents[0].page_content)"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Al0yUjGiUAgj",
    "outputId": "ec8f8e69-60e4-44b4-fe91-89e0d5a6b2a9"
   },
   "execution_count": 6,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "9   \n",
      "Dividends  \n",
      "Our Board of Directors declared the following dividends:  \n",
      "  \n",
      "Declaration Date  Record Date  Payment  Date  Dividend  \n",
      "Per Share  Amount    \n",
      "          \n",
      "Fiscal Year 2022        (In millions)  \n",
      "          \n",
      "September  14, 2021   November  18, 2021    December  9, 2021   $   0.62  $ 4,652   \n",
      "December  7, 2021   February  17, 2022    March  10, 2022    0.62   4,645   \n",
      "March  14, 2022   May 19, 2022    June  9, 2022    0.62   4,632   \n",
      "June  14, 2022   August  18, 2022    September  8, 2022    0.62   4,627     \n",
      "Total      $   2.48  $   18,556             \n",
      "          \n",
      "Fiscal Year 2021          \n",
      "          \n",
      "September  15, 2020   November  19, 2020    December  10, 2020   $ 0.56  $ 4,230   \n",
      "December  2, 2020   February  18, 2021    March  11, 2021    0.56   4,221   \n",
      "March  16, 2021   May 20, 2021    June  10, 2021    0.56   4,214   \n",
      "June  16, 2021   August  19, 2021    September  9, 2021    0.56   4,206     \n",
      "          \n",
      "Total      $ 2.24  $ 16,871             \n",
      "The dividend declared on June  14, 2022 was included in other current liabilities as of June  30, 2022.\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=64)\n",
    "texts = text_splitter.split_documents(documents)"
   ],
   "metadata": {
    "id": "UVpykqFoWiWb"
   },
   "execution_count": 7,
   "outputs": []
  },
  {
   "cell_type": "code",
   "source": [
    "len(texts)"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "ZoYnqSgCXKnp",
    "outputId": "13bf91f2-3bdd-466b-c60a-3f41c64dc2ac"
   },
   "execution_count": 8,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "metadata": {},
     "execution_count": 8
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "print(texts[0].page_content)"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "OiFYKMTPXNn_",
    "outputId": "4115112d-0cf0-47d0-ae8e-e8ac9dee70ed"
   },
   "execution_count": 9,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "9   \n",
      "Dividends  \n",
      "Our Board of Directors declared the following dividends:  \n",
      "  \n",
      "Declaration Date  Record Date  Payment  Date  Dividend  \n",
      "Per Share  Amount    \n",
      "          \n",
      "Fiscal Year 2022        (In millions)  \n",
      "          \n",
      "September  14, 2021   November  18, 2021    December  9, 2021   $   0.62  $ 4,652   \n",
      "December  7, 2021   February  17, 2022    March  10, 2022    0.62   4,645   \n",
      "March  14, 2022   May 19, 2022    June  9, 2022    0.62   4,632   \n",
      "June  14, 2022   August  18, 2022    September  8, 2022    0.62   4,627     \n",
      "Total      $   2.48  $   18,556             \n",
      "          \n",
      "Fiscal Year 2021          \n",
      "          \n",
      "September  15, 2020   November  19, 2020    December  10, 2020   $ 0.56  $ 4,230   \n",
      "December  2, 2020   February  18, 2021    March  11, 2021    0.56   4,221   \n",
      "March  16, 2021   May 20, 2021    June  10, 2021    0.56   4,214   \n",
      "June  16, 2021   August  19, 2021    September  9, 2021    0.56   4,206     \n",
      "          \n",
      "Total      $ 2.24  $ 16,871\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Create Embeddings"
   ],
   "metadata": {
    "id": "FGlS3t9K0kTA"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "embeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-MiniLM-L6-v2\")"
   ],
   "metadata": {
    "id": "bAcy1J0UXngi"
   },
   "execution_count": 10,
   "outputs": []
  },
  {
   "cell_type": "code",
   "source": [
    "db = Chroma.from_documents(texts, embeddings, persist_directory=\"db\")"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "pyM7ySBRaKgV",
    "outputId": "1ffe7795-6145-4e59-cd11-f43ecacf95a9"
   },
   "execution_count": 11,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stderr",
     "text": [
      "WARNING:chromadb:Using embedded DuckDB with persistence: data will be stored in: db\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Create Chain"
   ],
   "metadata": {
    "id": "LF1qakPS2MCD"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "model_n_ctx = 1000\n",
    "model_path = \"./ggml-gpt4all-j-v1.3-groovy.bin\"\n",
    "llm = GPT4All(model=model_path, n_ctx=1000, backend=\"gptj\", verbose=False)"
   ],
   "metadata": {
    "id": "qBZOUYGVbKPZ"
   },
   "execution_count": 12,
   "outputs": []
  },
  {
   "cell_type": "code",
   "source": [
    "qa = RetrievalQA.from_chain_type(\n",
    "    llm=llm,\n",
    "    chain_type=\"stuff\",\n",
    "    retriever=db.as_retriever(search_kwargs={\"k\": 3}),\n",
    "    return_source_documents=True,\n",
    "    verbose=False,\n",
    ")"
   ],
   "metadata": {
    "id": "POCnAlJAgstA"
   },
   "execution_count": 13,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Ask Questions"
   ],
   "metadata": {
    "id": "QaMXzvcPhOM7"
   }
  },
  {
   "cell_type": "code",
   "source": [
    "%%time\n",
    "res = qa(\n",
    "    \"How much is the dividend per share during during 2022? Extract it from the text.\"\n",
    ")"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "FyZ0zw_zgu9v",
    "outputId": "0c94f00b-6af9-42ba-c12a-5c507ce68de6"
   },
   "execution_count": 18,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "CPU times: user 7min 48s, sys: 9.76 s, total: 7min 58s\n",
      "Wall time: 2min 9s\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "res"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "TFpY1O989W12",
    "outputId": "52d0fb18-eafe-4e56-f39c-521a9dae1d71"
   },
   "execution_count": 19,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'query': 'How much is the dividend per share during during 2022? Extract it from the text.',\n",
       " 'result': ' The dividend per share during 2022 is $0.62.',\n",
       " 'source_documents': [Document(page_content='Total      $ 2.24  $ 16,871             \\nThe dividend declared on June  14, 2022 was included in other current liabilities as of June  30, 2022.', metadata={'source': 'ms-financial-statement.pdf', 'page': 0}),\n",
       "  Document(page_content='9   \\nDividends  \\nOur Board of Directors declared the following dividends:  \\n  \\nDeclaration Date  Record Date  Payment  Date  Dividend  \\nPer Share  Amount    \\n          \\nFiscal Year 2022        (In millions)  \\n          \\nSeptember  14, 2021   November  18, 2021    December  9, 2021   $   0.62  $ 4,652   \\nDecember  7, 2021   February  17, 2022    March  10, 2022    0.62   4,645   \\nMarch  14, 2022   May 19, 2022    June  9, 2022    0.62   4,632   \\nJune  14, 2022   August  18, 2022    September  8, 2022    0.62   4,627     \\nTotal      $   2.48  $   18,556             \\n          \\nFiscal Year 2021          \\n          \\nSeptember  15, 2020   November  19, 2020    December  10, 2020   $ 0.56  $ 4,230   \\nDecember  2, 2020   February  18, 2021    March  11, 2021    0.56   4,221   \\nMarch  16, 2021   May 20, 2021    June  10, 2021    0.56   4,214   \\nJune  16, 2021   August  19, 2021    September  9, 2021    0.56   4,206     \\n          \\nTotal      $ 2.24  $ 16,871', metadata={'source': 'ms-financial-statement.pdf', 'page': 0})]}"
      ]
     },
     "metadata": {},
     "execution_count": 19
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "print(res[\"result\"])"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "59ACA33dEmYz",
    "outputId": "dff01e34-7410-40cd-eb1d-14d498189987"
   },
   "execution_count": 21,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      " The dividend per share during 2022 is $0.62.\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "%%time\n",
    "prompt = f\"\"\"How much is the investment amount in Microsoft on 6/22? Extract the answer from the text.\"\"\"\n",
    "res = qa(prompt.strip())"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "y-oaTK7eMYFf",
    "outputId": "40c9e505-c027-46c6-e750-5725c0a39563"
   },
   "execution_count": 14,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "CPU times: user 12min 41s, sys: 9.47 s, total: 12min 51s\n",
      "Wall time: 3min 29s\n"
     ]
    }
   ]
  },
  {
   "cell_type": "code",
   "source": [
    "print(res[\"result\"])"
   ],
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Lgwzya6JP_JL",
    "outputId": "8c3f2fb4-4fdc-4904-fc41-a939bdbead02"
   },
   "execution_count": 15,
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      " The investment amount in Microsoft on 6/22 is $309.69.\n"
     ]
    }
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## References\n",
    "\n",
    "- [GPT4All](https://gpt4all.io/)\n",
    "- [MICROSOFT 2022 ANNUAL REPORT](https://www.microsoft.com/investor/reports/ar22/download-center/)"
   ],
   "metadata": {
    "id": "F-FofoJ5CCp0"
   }
  }
 ]
}