{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "! pip install -q --upgrade google-generativeai langchain-google-genai gradio" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/vasim/.local/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "import os\n", "from langchain.document_loaders import (\n", " PyPDFLoader,\n", " TextLoader,\n", " Docx2txtLoader\n", ")\n", "\n", "from langchain.text_splitter import CharacterTextSplitter\n", "# from PyPDF2 import PdfReader\n", "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", "from langchain_google_genai import GoogleGenerativeAIEmbeddings\n", "import google.generativeai as genai\n", "from langchain.vectorstores import FAISS\n", "from langchain_google_genai import ChatGoogleGenerativeAI\n", "from langchain.chains.question_answering import load_qa_chain\n", "from langchain.prompts import PromptTemplate\n", "from dotenv import load_dotenv" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "load_dotenv()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0.0026024636,\n", " -0.00046470974,\n", " -0.06524466,\n", " 0.0037671768,\n", " 0.050909057,\n", " 0.03566457,\n", " 0.004729628,\n", " -0.02265792,\n", " -0.019636517,\n", " 0.041511342,\n", " -0.0032873638,\n", " 0.009839665,\n", " 0.0022297094,\n", " 0.011045753,\n", " -0.0029112806,\n", " 0.00036380324,\n", " 0.013318236,\n", " 0.008650382,\n", " 0.040087987,\n", " 0.003234754,\n", " 0.02216253,\n", " 0.027707517,\n", " 0.0015880043,\n", " -0.011375762,\n", " 0.027686268,\n", " -0.03158842,\n", " 0.013077853,\n", " -0.02646762,\n", " -0.03462742,\n", " -0.010597595,\n", " -0.053662993,\n", " 0.033564012,\n", " -0.025191225,\n", " 0.008060145,\n", " 0.006509568,\n", " -0.065666765,\n", " -0.003321127,\n", " 0.015929354,\n", " 0.009666688,\n", " 0.017954187,\n", " 0.003692957,\n", " -0.025450539,\n", " -0.019842725,\n", " 0.0074209156,\n", " -0.0038040788,\n", " -0.013353295,\n", " -0.06744818,\n", " 0.02687267,\n", " 0.00036143005,\n", " -0.04220317,\n", " 0.03936105,\n", " 0.038491815,\n", " 0.044381935,\n", " -0.04167377,\n", " -0.017151065,\n", " -0.027833307,\n", " 0.0400076,\n", " -0.0050078775,\n", " 0.0017342605,\n", " 0.033973645,\n", " -0.0035528678,\n", " -0.008506744,\n", " 0.050232697,\n", " 0.0047968724,\n", " -0.022322817,\n", " -0.075537644,\n", " -0.014450601,\n", " 0.0054708333,\n", " 0.034601416,\n", " -0.01907222,\n", " -0.013576153,\n", " -0.07363731,\n", " 0.058990918,\n", " 0.0051348056,\n", " -0.009838986,\n", " -0.155085,\n", " -0.0028395671,\n", " 0.04909831,\n", " 0.0138770975,\n", " 0.011540807,\n", " -0.0018861439,\n", " -0.01755376,\n", " -0.08361732,\n", " -0.039391823,\n", " -0.077364445,\n", " 0.0204261,\n", " -0.05680118,\n", " -0.009028887,\n", " -0.015683021,\n", " 0.061899763,\n", " -0.026810465,\n", " -0.021239934,\n", " 0.03995343,\n", " -0.06467655,\n", " 0.011940644,\n", " 0.063081965,\n", " -0.03472896,\n", " -0.020098649,\n", " 0.03046696,\n", " 0.0026317302,\n", " -0.0033308691,\n", " 0.0076804003,\n", " -0.07220898,\n", " 0.03097491,\n", " 0.02099598,\n", " 0.015009643,\n", " 0.0027554797,\n", " 0.056863576,\n", " 0.016525302,\n", " 0.044575516,\n", " -0.07853067,\n", " 0.01992914,\n", " 0.02582012,\n", " 0.024094418,\n", " 0.03533265,\n", " -0.021520808,\n", " -0.033198,\n", " 0.07007376,\n", " 0.020927606,\n", " 0.03658496,\n", " 0.021251578,\n", " -0.025971107,\n", " 0.09621473,\n", " -0.023366872,\n", " 0.003256949,\n", " -0.022166384,\n", " -0.038614567,\n", " 0.02979776,\n", " 0.039533786,\n", " 0.012638491,\n", " -0.0055726334,\n", " -0.05542629,\n", " -0.024121152,\n", " 0.03178174,\n", " 0.052707106,\n", " 0.104821995,\n", " 0.019577984,\n", " -0.013310535,\n", " 0.044787172,\n", " -0.024393918,\n", " 0.009802365,\n", " 0.02994997,\n", " -0.0008475685,\n", " 0.04228906,\n", " -0.006219175,\n", " 0.04402614,\n", " -0.040632106,\n", " 0.009029098,\n", " 0.08250455,\n", " -0.008027798,\n", " -0.012396638,\n", " -0.005741472,\n", " -0.06530075,\n", " -0.0076336395,\n", " 0.056545228,\n", " 0.008156076,\n", " -0.031731352,\n", " 0.025516897,\n", " 0.013671465,\n", " 0.007437407,\n", " 0.022746652,\n", " -0.0021193696,\n", " 0.01611938,\n", " 0.018748892,\n", " 0.0031200994,\n", " 0.0031527125,\n", " 0.0005562957,\n", " 0.017112905,\n", " -0.03922318,\n", " 0.002402639,\n", " -0.041061036,\n", " -0.005380344,\n", " -0.036544673,\n", " -0.03292972,\n", " -0.010599688,\n", " -0.033042144,\n", " 0.021467226,\n", " -0.011730441,\n", " -0.040189914,\n", " -0.0038518396,\n", " 0.006088003,\n", " -0.05208495,\n", " 0.026408045,\n", " 0.04386243,\n", " -0.01699642,\n", " -0.05508133,\n", " 0.060988806,\n", " -0.021120366,\n", " -0.048851084,\n", " 0.011562087,\n", " 0.0046992013,\n", " 0.0029417663,\n", " -0.030239712,\n", " -0.044726174,\n", " -0.036426347,\n", " 0.058376063,\n", " 0.007909313,\n", " 0.0082651945,\n", " 0.005973673,\n", " -0.08589435,\n", " -0.04365921,\n", " 0.079266064,\n", " 0.010272922,\n", " -0.00077904004,\n", " 0.027783131,\n", " 0.009019264,\n", " 0.05524787,\n", " -0.048576605,\n", " -0.04641576,\n", " 0.04849984,\n", " -0.031302325,\n", " 0.04504644,\n", " -0.014899033,\n", " 0.02830163,\n", " 0.012142052,\n", " 0.0027500011,\n", " 0.0037108215,\n", " 0.024816774,\n", " 0.034425203,\n", " -0.025865829,\n", " -0.0023688357,\n", " -0.004570346,\n", " -0.036537673,\n", " 0.00785959,\n", " -0.006369414,\n", " 0.05036734,\n", " -0.0013942671,\n", " -0.0066736704,\n", " -0.007213406,\n", " -0.02938604,\n", " -0.005098042,\n", " 0.069885835,\n", " 0.039996464,\n", " -0.0014599679,\n", " 0.052899357,\n", " 0.012151934,\n", " 0.0045691393,\n", " -0.0169914,\n", " 0.023379533,\n", " 0.054388337,\n", " -0.046915285,\n", " 0.012740782,\n", " 0.008226091,\n", " 0.0650389,\n", " -0.053086124,\n", " -0.035921216,\n", " -0.047861494,\n", " 0.03646698,\n", " 0.02124863,\n", " 0.07875475,\n", " 0.041897457,\n", " -0.059685115,\n", " -0.005645064,\n", " -0.038765974,\n", " -0.082096376,\n", " 0.019661527,\n", " -0.048235465,\n", " 0.032961316,\n", " -0.015816065,\n", " 0.01349209,\n", " 0.030743608,\n", " 0.015003568,\n", " -0.028993933,\n", " -0.01690658,\n", " -0.035253666,\n", " -0.010791926,\n", " 0.0042820405,\n", " -0.06907554,\n", " -0.0014160916,\n", " -0.012969035,\n", " 0.043451488,\n", " -0.01587785,\n", " 0.038347695,\n", " 0.012478342,\n", " -0.012291408,\n", " 0.018989006,\n", " -0.012111724,\n", " 0.043280758,\n", " 0.00065698626,\n", " -0.03504554,\n", " 0.06592344,\n", " 0.03193109,\n", " 0.026153369,\n", " -0.050498523,\n", " 0.013383252,\n", " -0.008368444,\n", " -0.041243635,\n", " -0.04379342,\n", " -0.007160138,\n", " -0.041415907,\n", " -0.009043778,\n", " -0.06227678,\n", " 0.04495307,\n", " -0.012437426,\n", " -0.005763928,\n", " -0.01721329,\n", " 0.0021767728,\n", " 0.036614556,\n", " 0.014989671,\n", " -0.02297016,\n", " 0.017203707,\n", " -0.068807654,\n", " 0.028532282,\n", " -0.077031314,\n", " -0.003550955,\n", " -0.016694745,\n", " -0.024186041,\n", " -0.051172636,\n", " 0.023041002,\n", " 0.03607617,\n", " -0.006545797,\n", " 0.026791858,\n", " -0.050831117,\n", " -0.0060527464,\n", " 0.040906418,\n", " 0.030260349,\n", " -0.03622405,\n", " 0.03427033,\n", " -0.0076176235,\n", " 0.04239094,\n", " -0.007758525,\n", " 0.08181105,\n", " -0.002424102,\n", " -0.008245623,\n", " -0.024848122,\n", " 0.052394852,\n", " 0.019161373,\n", " 0.00611725,\n", " -0.020502884,\n", " 0.026944518,\n", " -0.010385731,\n", " 0.041430656,\n", " -0.072016835,\n", " -0.0066381455,\n", " -0.0046330006,\n", " 0.05215405,\n", " -0.08320881,\n", " 0.019687334,\n", " -0.047417916,\n", " -0.03933844,\n", " 0.023311257,\n", " -0.0010838164,\n", " -0.016675808,\n", " -0.053388983,\n", " 0.002350658,\n", " -0.018954204,\n", " -0.021903217,\n", " 0.028829457,\n", " 0.068412215,\n", " 0.04974716,\n", " 0.015097642,\n", " 0.084202014,\n", " 0.02282603,\n", " 0.06366213,\n", " -0.026193192,\n", " -0.01595,\n", " 0.044413194,\n", " -0.053779442,\n", " 0.008965999,\n", " -0.03272141,\n", " 0.004915969,\n", " 0.007829177,\n", " 0.002712625,\n", " -0.036152992,\n", " -0.04032615,\n", " -0.010541384,\n", " -0.034215618,\n", " -0.025328316,\n", " -0.014646692,\n", " 0.029056,\n", " 0.02925028,\n", " 0.008913267,\n", " 0.0019940082,\n", " -0.06695999,\n", " -0.0010260289,\n", " 0.011302115,\n", " -0.03292385,\n", " 0.00042418708,\n", " 0.017628789,\n", " -0.00475448,\n", " 0.043151293,\n", " 0.006198079,\n", " 0.08446194,\n", " 0.0038840906,\n", " 0.029208006,\n", " -0.03733759,\n", " 0.035057925,\n", " -0.025195966,\n", " -0.022711853,\n", " 0.069107376,\n", " -0.056606133,\n", " 0.07434411,\n", " 0.101236925,\n", " -0.0047446825,\n", " 0.0071048927,\n", " -0.011161651,\n", " 0.012175554,\n", " -0.037840243,\n", " 0.036562636,\n", " 0.057127528,\n", " -0.014619162,\n", " -0.045825053,\n", " -0.027700488,\n", " -0.0059357225,\n", " -0.050302368,\n", " -0.00026914122,\n", " 0.019348465,\n", " -0.013578195,\n", " -0.042193584,\n", " -0.009891802,\n", " 0.017196594,\n", " -0.025658505,\n", " 0.006126944,\n", " -0.0873973,\n", " -0.043111254,\n", " -0.03861637,\n", " 0.040047407,\n", " 0.004773531,\n", " -0.0055007017,\n", " 0.020156063,\n", " -0.04213769,\n", " -0.0020724083,\n", " 0.012001859,\n", " -0.05014639,\n", " -0.08432724,\n", " -0.04858163,\n", " 0.03317245,\n", " 0.009808364,\n", " -0.011328112,\n", " 0.034310147,\n", " 0.033897907,\n", " 0.0014663753,\n", " -0.006445559,\n", " -0.0244833,\n", " -0.01675865,\n", " -0.019391444,\n", " -0.0033584565,\n", " 0.02279385,\n", " -0.008156112,\n", " 0.0020341233,\n", " 0.064902686,\n", " -0.029238507,\n", " 0.014163741,\n", " -0.0075179366,\n", " -0.049113605,\n", " -0.0025780846,\n", " -0.014767654,\n", " -0.034390695,\n", " 0.009971777,\n", " -0.06279843,\n", " 0.04299513,\n", " -0.08961137,\n", " -0.02808285,\n", " -0.041206528,\n", " -0.04893974,\n", " -0.039606616,\n", " -0.0077889636,\n", " 0.08335686,\n", " -0.010121534,\n", " -0.0057770414,\n", " -0.029257368,\n", " -0.020732552,\n", " -0.0035506056,\n", " -0.103099026,\n", " 0.021422477,\n", " -0.018300131,\n", " -0.0013129099,\n", " -0.020269357,\n", " 0.007500347,\n", " 0.054774307,\n", " 0.0024568313,\n", " -0.0438022,\n", " -0.015716508,\n", " 0.0008172836,\n", " -0.046375576,\n", " -0.03338143,\n", " -0.091609284,\n", " 0.062183857,\n", " -0.020281738,\n", " -0.025984671,\n", " 0.055528525,\n", " 0.016999535,\n", " -0.028604554,\n", " 0.026752898,\n", " -0.009096473,\n", " 0.022438003,\n", " -0.017217133,\n", " -0.025918938,\n", " -0.021384709,\n", " 0.04131251,\n", " 0.011413672,\n", " -0.03978722,\n", " 0.003499603,\n", " -0.03932664,\n", " -0.03266107,\n", " -0.0037615069,\n", " 0.01098813,\n", " 0.055706598,\n", " 0.04374345,\n", " 0.018282231,\n", " -0.011001723,\n", " -0.026331998,\n", " 0.008415885,\n", " -0.0017321053,\n", " 0.07693357,\n", " -0.08920055,\n", " -0.022223707,\n", " 0.012738715,\n", " 0.01656477,\n", " -0.023257954,\n", " -0.0056636413,\n", " 0.009431777,\n", " -0.0063169277,\n", " 0.04679692,\n", " 0.054250635,\n", " -0.0041032853,\n", " 0.0056171734,\n", " 0.018150132,\n", " -0.00058200443,\n", " 0.019457081,\n", " 0.0018458135,\n", " 0.0003532362,\n", " -0.08229051,\n", " -8.8678644e-05,\n", " 0.010146113,\n", " -0.050217737,\n", " -0.018734543,\n", " -0.0039246683,\n", " -0.048076928,\n", " 0.03604184,\n", " -0.027944451,\n", " 0.07513914,\n", " -0.05666047,\n", " -0.03619383,\n", " 0.049743947,\n", " 0.029366102,\n", " 0.017087461,\n", " 0.01768675,\n", " -0.015412814,\n", " -0.023675736,\n", " 0.014236046,\n", " -0.0353288,\n", " 0.034391064,\n", " 0.009070227,\n", " -0.07759078,\n", " 0.017592117,\n", " -0.00289023,\n", " -0.06718024,\n", " 0.0136539275,\n", " -0.0040624915,\n", " -0.023015171,\n", " 0.033067793,\n", " 0.038277265,\n", " -0.0037099298,\n", " 0.004821567,\n", " -0.0043556388,\n", " 0.010389023,\n", " -0.011538616,\n", " 0.017592786,\n", " -0.0029306181,\n", " -0.008503525,\n", " -0.012787438,\n", " 0.013798478,\n", " 0.034812994,\n", " 0.06886472,\n", " -0.0009630421,\n", " -0.013648257,\n", " 0.008538377,\n", " 0.04826577,\n", " -0.0047196406,\n", " 0.013498656,\n", " -0.0005008069,\n", " 0.036062807,\n", " 0.01667458,\n", " 0.04128509,\n", " 0.00669686,\n", " -0.03196692,\n", " 0.00046054774,\n", " -0.009966674,\n", " -0.019439934,\n", " 0.04555503,\n", " -0.0172578,\n", " 0.0064148423,\n", " 0.04498466,\n", " -0.029169412,\n", " 0.050785944,\n", " 0.06723971,\n", " 0.047408525,\n", " 0.021920597,\n", " 0.014741846,\n", " -0.044936806,\n", " 0.0051182187,\n", " -0.04394078,\n", " -0.019925192,\n", " 0.015369175,\n", " 0.0011553997,\n", " -0.00030811407,\n", " -0.040543456,\n", " -0.027933566,\n", " -0.033180367,\n", " 0.05718386,\n", " -0.027550489,\n", " 0.023995027,\n", " -0.05262063,\n", " 0.03024302,\n", " 0.04645257,\n", " -0.04900795,\n", " 0.0077397116,\n", " 0.027761744,\n", " 0.05983705,\n", " -0.020184644,\n", " -0.0199132,\n", " 0.01580453,\n", " -0.04299223,\n", " -0.050547145,\n", " -0.0519957,\n", " 0.08249654,\n", " 0.0058217077,\n", " -0.021394847,\n", " -0.049588665,\n", " -0.04656881,\n", " -0.010261212,\n", " -0.0025667087,\n", " -0.03484151,\n", " -0.02142792,\n", " 0.0061094393,\n", " -0.009446063,\n", " -0.0042138724,\n", " 0.055291895,\n", " 0.03617863,\n", " 0.06034423,\n", " 0.06481362,\n", " -0.03290425,\n", " 0.029245012,\n", " -0.048802678,\n", " -0.0037805268,\n", " -0.045257535,\n", " 0.036172822,\n", " 0.038555525,\n", " -0.02758126,\n", " -0.06875568,\n", " 0.0017989068,\n", " -0.008342789,\n", " -0.01680573,\n", " -0.0112027405,\n", " 0.062149644,\n", " 0.027881822,\n", " -0.106920265,\n", " -0.064146906,\n", " -0.02093536,\n", " -0.033114824,\n", " 0.015528122,\n", " 0.00026822102,\n", " -0.008216998,\n", " 0.012704465,\n", " 0.030706486,\n", " -0.05130113,\n", " -0.050231606,\n", " 0.034715306,\n", " 0.005949386,\n", " -0.05149062,\n", " -0.03999013,\n", " 0.019164404,\n", " 0.013223384,\n", " 0.00029772808,\n", " -0.023228012,\n", " -0.03363084,\n", " -0.06697605,\n", " -0.00289148,\n", " 0.065958,\n", " -0.082492866,\n", " 0.018354924,\n", " 0.03967476,\n", " -0.029593993,\n", " 0.049283735,\n", " -0.010077968,\n", " -0.007118481,\n", " 0.061920356,\n", " 0.02018739,\n", " -0.0039915326,\n", " 0.03894082,\n", " 0.012183999,\n", " -0.030113503,\n", " 0.014517147,\n", " 0.007991418,\n", " 0.024015212,\n", " 0.009208566,\n", " -0.020164741,\n", " -0.020583002,\n", " -0.039123856,\n", " 0.00087153394,\n", " -0.03082177,\n", " 0.0020127937,\n", " 0.06228645,\n", " 0.026857367,\n", " 0.0368351,\n", " -0.027293308,\n", " -0.026962344,\n", " 0.008827768,\n", " 0.014945436,\n", " -0.029614218,\n", " -0.031657815,\n", " -0.007631079,\n", " -0.0076884576,\n", " 0.04339069,\n", " -0.0026196744,\n", " 0.013788046,\n", " 0.011783524,\n", " 0.026193634,\n", " 0.07127412,\n", " 0.034598272,\n", " -0.035599194,\n", " -0.025256252,\n", " 0.09411122,\n", " 0.010242209,\n", " -0.017384086,\n", " 0.019750888,\n", " 0.043792516,\n", " -0.020661648,\n", " 0.070129156,\n", " 0.049977932,\n", " 0.023388589,\n", " 0.034094717,\n", " -0.048965782,\n", " -0.03520176,\n", " 0.060515266,\n", " -0.032321338,\n", " 0.019958243,\n", " -0.02175526,\n", " -0.04276059,\n", " 0.09902725,\n", " -0.012637526,\n", " 0.01423612,\n", " 0.033305667,\n", " -0.017869117,\n", " 0.07474199,\n", " 0.03466648,\n", " 0.050615326,\n", " 0.010753818,\n", " -0.08773687,\n", " -0.033456173,\n", " -0.018937487,\n", " -0.0038408504,\n", " 0.03892539,\n", " 0.01966001,\n", " 0.00027581758,\n", " 0.0057093804,\n", " -0.03970555,\n", " -0.0064289654,\n", " -0.014644212,\n", " -0.0068154233,\n", " 0.007386573,\n", " -0.03209175,\n", " 0.043366488,\n", " 0.04398344,\n", " 0.0070169214,\n", " -0.015283345,\n", " -0.005414628,\n", " -0.026633335,\n", " 0.022585975,\n", " -0.025980968,\n", " -0.0075632525,\n", " -0.024178095,\n", " -0.0019861956,\n", " 0.033296216,\n", " 0.014306449,\n", " 0.011111949,\n", " 0.013801798]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "embeddings = GoogleGenerativeAIEmbeddings(model = \"models/embedding-001\")\n", "embeddings.embed_query(\"I am good\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 2 }