{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 📱 Using RAG to perform analysis of AirBNB Quarterly Report! 🤖\n", "\n", "[AirBNB Quarterly Report For the quarterly period ended March 31, 2024](https://airbnb2020ipo.q4web.com/files/doc_financials/2024/q1/fdb60f7d-e616-43dc-86ef-e33d3a9bdd05.pdf)\n", "\n", "## Question 1\n", "- question: What is Airbnb's 'Description of Business'?\n", "- response: Airbnb's 'Description of Business' is operating a global platform for unique stays and experiences, connecting hosts and guests online or through mobile devices to book spaces and experiences around the world.\n", "- LangSmith trace: https://smith.langchain.com/public/ebdf5473-64ac-4f85-81ab-bd3c3d624969/r\n", "\n", "## Question 2\n", "- question: What was the total value of 'Cash and cash equivalents' as of December 31, 2023?\n", "- response: The total value of 'Cash and cash equivalents' as of December 31, 2023, is $2,369.\n", "- LangSmith trace: https://smith.langchain.com/public/b0f93487-c729-4ccf-93f9-0354078282d8/r\n", "\n", "## Question 3\n", "- question: What is the 'maximum number of shares to be sold under the 10b5-1 Trading plan' by Brian Chesky?\n", "- response: The maximum number of shares to be sold under the 10b5-1 Trading plan by Brian Chesky is 1,146,000.\n", "- LangSmith trace: https://smith.langchain.com/public/7fc4b549-2ea5-4b86-abf9-71d5e9a62738/r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Note: you may need to restart the kernel to use updated packages.\n", "Note: you may need to restart the kernel to use updated packages.\n", "Note: you may need to restart the kernel to use updated packages.\n", "Note: you may need to restart the kernel to use updated packages.\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install -qU pymupdf \n", "%pip install -qU langchain langchain-core langchain-community langchain-text-splitters \n", "%pip install -qU langchain-openai\n", "%pip install -qU langchain-groq\n", "%pip install -qU langchain-qdrant" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import os\n", "from langchain import hub\n", "from langchain_groq import ChatGroq\n", "\n", "llm = ChatGroq(model=\"llama3-70b-8192\", temperature=0.3)\n", "\n", "os.environ[\"GROQ_API_KEY\"] = os.getenv(\"GROQ_API_KEY\")\n", "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\")\n", "\n", "QDRANT_API_KEY = os.getenv(\"QDRANT_API_KEY\")\n", "QDRANT_API_URL = os.getenv(\"QDRANT_URL\")\n", "\n", "# LangSmith tracing and \n", "os.environ[\"LANGCHAIN_PROJECT\"] = \"AirBnB PDF Jun18\"\n", "os.environ[\"LANGCHAIN_ENDPOINT\"]=os.getenv(\"LANGCHAIN_ENDPOINT\")\n", "os.environ[\"LANGCHAIN_API_KEY\"]=os.getenv(\"LANGCHAIN_API_KEY\")\n", "os.environ[\"LANGCHAIN_TRACING_V2\"]=os.getenv(\"LANGCHAIN_TRACING_V2\")\n", "\n", "# Leverage a prompt from the LangChain hub\n", "prompt = hub.pull(\"rlm/rag-prompt-llama3\")\n", "# prompt = hub.pull(\"cracked-nut/securities-comm-llama3\")\n", "# prompt = hub.pull(\"cracked-nut/securities-comm-llama3-v2\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Parameterize some stuff\n", "\n", "LOAD_NEW_DATA = False # Set to True to load new data\n", "FILE_PATH = \"/home/donbr/aie3-bootcamp/AIE3/Week 3/Day 2/files/airbnb.pdf\" # Path to the source PDF file\n", "collection = \"airbnb_pdf_rec_1000_200_images\" # qdrant collection name\n", "\n", "# QUESTION = \"What is Airbnb's 'Description of Business'?\"\n", "QUESTION = \"What was the total value of 'Cash and cash equivalents' as of December 31, 2023?\"\n", "# QUESTION = \"What is the 'maximum number of shares to be sold under the 10b5-1 Trading plan' by Brian Chesky?\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from langchain_community.document_loaders import PyMuPDFLoader\n", "from langchain_text_splitters import RecursiveCharacterTextSplitter\n", "from langchain_qdrant import Qdrant" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from langchain_openai import OpenAIEmbeddings\n", "\n", "embedding = OpenAIEmbeddings(model=\"text-embedding-3-small\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# run loader if LOAD_NEW_DATA is True\n", "if LOAD_NEW_DATA:\n", " loader = PyMuPDFLoader(FILE_PATH, extract_images=True)\n", " docs = loader.load()\n", "\n", " text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)\n", " splits = text_splitter.split_documents(docs)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Store the chunks in Qdrant\n", "if LOAD_NEW_DATA:\n", " from_splits = Qdrant.from_documents(\n", " embedding=embedding,\n", " collection_name=collection,\n", " url=QDRANT_API_URL,\n", " api_key=QDRANT_API_KEY,\n", " prefer_grpc=True, \n", " documents=splits,\n", " )" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "qdrant = Qdrant.from_existing_collection(\n", " embedding=embedding,\n", " collection_name=collection,\n", " url=QDRANT_API_URL,\n", " api_key=QDRANT_API_KEY,\n", " prefer_grpc=True, \n", ")\n", "\n", "# retriever = qdrant.as_retriever(search_type=\"mmr\", search_kwargs={\"k\": 8})\n", "\n", "# retriever = qdrant.as_retriever(search_kwargs={\"k\": 5})\n", "\n", "retriever = qdrant.as_retriever(\n", " search_type=\"similarity_score_threshold\",\n", " search_kwargs={\"score_threshold\": 0.5, \"k\": 5}\n", ")\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "from operator import itemgetter\n", "from langchain.schema.runnable import RunnablePassthrough\n", "\n", "rag_chain = (\n", " {\"context\": itemgetter(\"question\") | retriever, \"question\": itemgetter(\"question\")}\n", " | RunnablePassthrough.assign(context=itemgetter(\"context\"))\n", " | {\"response\": prompt | llm, \"context\": itemgetter(\"context\")}\n", ")" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " +---------------------------------+ \n", " | ParallelInput | \n", " +---------------------------------+ \n", " **** **** \n", " **** *** \n", " ** **** \n", "+--------------------------------+ ** \n", "| Lambda(itemgetter('question')) | * \n", "+--------------------------------+ * \n", " * * \n", " * * \n", " * * \n", " +----------------------+ +--------------------------------+ \n", " | VectorStoreRetriever | | Lambda(itemgetter('question')) | \n", " +----------------------+ +--------------------------------+ \n", " **** **** \n", " **** **** \n", " ** ** \n", " +----------------------------------+ \n", " | ParallelOutput | \n", " +----------------------------------+ \n", " * \n", " * \n", " * \n", " +------------------------+ \n", " | ParallelInput | \n", " +------------------------+ \n", " *** *** \n", " *** *** \n", " ** ** \n", " +-------------------------------+ +-------------+ \n", " | Lambda(itemgetter('context')) | | Passthrough | \n", " +-------------------------------+ +-------------+ \n", " *** *** \n", " *** *** \n", " ** ** \n", " +-------------------------+ \n", " | ParallelOutput | \n", " +-------------------------+ \n", " * \n", " * \n", " * \n", " +---------------------------------+ \n", " | ParallelInput | \n", " +---------------------------------+ \n", " **** *** \n", " *** *** \n", " ** **** \n", " +--------------------+ ** \n", " | ChatPromptTemplate | * \n", " +--------------------+ * \n", " * * \n", " * * \n", " * * \n", " +----------+ +-------------------------------+ \n", " | ChatGroq | | Lambda(itemgetter('context')) | \n", " +----------+** +-------------------------------+ \n", " **** **** \n", " *** *** \n", " ** ** \n", " +----------------------------------+ \n", " | ParallelOutput | \n", " +----------------------------------+ \n" ] } ], "source": [ "print(rag_chain.get_graph().draw_ascii())" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "response = rag_chain.invoke({\"question\" : QUESTION})" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The total value of 'Cash and cash equivalents' as of December 31, 2023, is $2,369.\n" ] } ], "source": [ "# return the response. filter on the response key AIMessage content element\n", "print(response[\"response\"].content)\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Document(page_content='December 31, 2023\\nLevel\\xa01\\nLevel\\xa02\\nLevel\\xa03\\nTotal\\nAssets\\nCash and cash equivalents:\\nMoney market funds\\n$\\n2,018\\xa0 $\\n—\\xa0 $\\n—\\xa0 $\\n2,018\\xa0\\nCertificates of deposit\\n—\\xa0\\n1\\xa0\\n—\\xa0\\n1\\xa0\\nGovernment bonds\\n—\\xa0\\n115\\xa0\\n—\\xa0\\n115\\xa0\\nCommercial paper\\n—\\xa0\\n223\\xa0\\n—\\xa0\\n223\\xa0\\nCorporate debt securities\\n—\\xa0\\n12\\xa0\\n—\\xa0\\n12\\xa0\\n2,018\\xa0\\n351\\xa0\\n—\\xa0\\n2,369\\xa0\\nShort-term investments:\\nCertificates of deposit\\n—\\xa0\\n172\\xa0\\n—\\xa0\\n172\\xa0\\nGovernment bonds\\n—\\xa0\\n333\\xa0\\n—\\xa0\\n333\\xa0\\nCommercial paper\\n—\\xa0\\n366\\xa0\\n—\\xa0\\n366\\xa0\\nCorporate debt securities\\n—\\xa0\\n1,491\\xa0\\n—\\xa0\\n1,491\\xa0\\nMortgage-backed and asset-backed securities\\n—\\xa0\\n145\\xa0\\n—\\xa0\\n145\\xa0\\n—\\xa0\\n2,507\\xa0\\n—\\xa0\\n2,507\\xa0\\nFunds receivable and amounts held on behalf of customers:\\nMoney market funds\\n1,360\\xa0\\n—\\xa0\\n—\\xa0\\n1,360\\xa0\\nPrepaids and other current assets:\\nForeign exchange derivative assets\\n—\\xa0\\n27\\xa0\\n—\\xa0\\n27\\xa0\\nOther assets, noncurrent:\\nCorporate debt securities\\n—\\xa0\\n—\\xa0\\n4\\xa0\\n4\\xa0\\nTotal assets at fair value\\n$\\n3,378\\xa0 $\\n2,885\\xa0 $\\n4\\xa0 $\\n6,267\\xa0\\nLiabilities\\nAccrued expenses, accounts payable, and other current liabilities:\\nForeign exchange derivative liabilities\\n$\\n—\\xa0 $\\n55\\xa0 $\\n—\\xa0 $\\n55', metadata={'subject': 'Form 10-Q filed on 2024-05-08 for the period ending 2024-03-31', 'creator': 'EDGAR Filing HTML Converter', 'total_pages': 54, 'keywords': '0001559720-24-000017; ; 10-Q', 'modDate': \"D:20240508161807-04'00'\", 'encryption': 'Standard V2 R3 128-bit RC4', 'trapped': '', 'format': 'PDF 1.4', 'creationDate': \"D:20240508161757-04'00'\", 'source': '/home/donbr/aie3-bootcamp/AIE3/Week 3/Day 2/files/airbnb.pdf', 'file_path': '/home/donbr/aie3-bootcamp/AIE3/Week 3/Day 2/files/airbnb.pdf', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'page': 12, 'title': '0001559720-24-000017', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', '_id': '5e811a77-6780-4705-8052-062160087f10', '_collection_name': 'airbnb_pdf_rec_1000_200_images'}),\n", " Document(page_content='Our cash and cash equivalents are generally held at large global systemically important banks (or “G-SIBs”) which are subject to high capital requirements and are required to\\nregularly perform stringent stress tests related to their ability to absorb capital losses. Our cash, cash equivalents, and short-term investments held outside the United States may be\\nrepatriated, subject to certain limitations, and would be available to be used to fund our domestic operations. However, repatriation of such funds may result in additional tax\\nliabilities. We believe that our existing cash, cash equivalents, and short-term investments balances in the United States are sufficient to fund our working capital needs in the United\\nStates.\\nWe have access to $1.0 billion of commitments and a $200\\xa0million sub-limit for the issuance of letters of credit under the 2022 Credit Facility. As of March\\xa031, 2024, no amounts were', metadata={'subject': 'Form 10-Q filed on 2024-05-08 for the period ending 2024-03-31', 'creator': 'EDGAR Filing HTML Converter', 'total_pages': 54, 'keywords': '0001559720-24-000017; ; 10-Q', 'modDate': \"D:20240508161807-04'00'\", 'encryption': 'Standard V2 R3 128-bit RC4', 'trapped': '', 'format': 'PDF 1.4', 'source': '/home/donbr/aie3-bootcamp/AIE3/Week 3/Day 2/files/airbnb.pdf', 'creationDate': \"D:20240508161757-04'00'\", 'file_path': '/home/donbr/aie3-bootcamp/AIE3/Week 3/Day 2/files/airbnb.pdf', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'page': 28, 'title': '0001559720-24-000017', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', '_id': '9c9e6975-1d72-4c4d-9b9e-c129dfe56ddb', '_collection_name': 'airbnb_pdf_rec_1000_200_images'}),\n", " Document(page_content='2023 and March\\xa031, 2024, respectively. A total of $283 million and $479 million of these securities, with unrealized losses of $14 million and $16 million, were in a continuous\\nunrealized loss position for more than twelve months as of December\\xa031, 2023 and March\\xa031, 2024, respectively.\\nThe following table summarizes the contractual maturities of the Company’s available-for-sale debt securities (in millions):\\nMarch 31, 2024\\nAmortized\\nCost\\nEstimated\\nFair Value\\nDue within one year\\n$\\n1,489\\xa0 $\\n1,489\\xa0\\nDue after one year through five years\\n957\\xa0\\n947\\xa0\\nDue after five years\\n96\\xa0\\n92\\xa0\\nTotal\\n$\\n2,542\\xa0 $\\n2,528\\xa0\\nNote 5. Fair Value Measurements and Financial Instruments\\nThe following table summarizes the Company’s financial assets and liabilities measured at fair value on a recurring basis (in millions):\\nDecember 31, 2023\\nLevel\\xa01\\nLevel\\xa02\\nLevel\\xa03\\nTotal\\nAssets\\nCash and cash equivalents:\\nMoney market funds\\n$\\n2,018\\xa0 $\\n—\\xa0 $\\n—\\xa0 $\\n2,018\\xa0\\nCertificates of deposit\\n—\\xa0\\n1\\xa0\\n—\\xa0\\n1\\xa0\\nGovernment bonds\\n—\\xa0\\n115\\xa0\\n—\\xa0\\n115', metadata={'subject': 'Form 10-Q filed on 2024-05-08 for the period ending 2024-03-31', 'creator': 'EDGAR Filing HTML Converter', 'total_pages': 54, 'keywords': '0001559720-24-000017; ; 10-Q', 'trapped': '', 'encryption': 'Standard V2 R3 128-bit RC4', 'modDate': \"D:20240508161807-04'00'\", 'format': 'PDF 1.4', 'creationDate': \"D:20240508161757-04'00'\", 'file_path': '/home/donbr/aie3-bootcamp/AIE3/Week 3/Day 2/files/airbnb.pdf', 'source': '/home/donbr/aie3-bootcamp/AIE3/Week 3/Day 2/files/airbnb.pdf', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'page': 12, 'title': '0001559720-24-000017', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', '_id': '8e24fa1f-628b-4773-9011-e5ba6e97d830', '_collection_name': 'airbnb_pdf_rec_1000_200_images'}),\n", " Document(page_content='$\\n12,667\\xa0 $\\n16,529\\xa0\\nSupplemental disclosures of balance sheet information\\nSupplemental balance sheet information consisted of the following (in millions):\\nDecember 31,\\n2023\\nMarch 31,\\n2024\\nOther assets, noncurrent:\\nProperty and equipment, net\\n$\\n160\\xa0 $\\n171\\xa0\\nOperating lease right-of-use assets\\n119\\xa0\\n111\\xa0\\nOther\\n184\\xa0\\n190\\xa0\\nOther assets, noncurrent\\n$\\n463\\xa0 $\\n472\\xa0\\nAccrued expenses, accounts payable, and other current liabilities:\\nIndirect taxes payable and withholding tax reserves\\n$\\n1,119\\xa0 $\\n1,455\\xa0\\nCompensation and employee benefits\\n436\\xa0\\n346\\xa0\\nAccounts payable\\n141\\xa0\\n184\\xa0\\nOperating lease liabilities, current\\n61\\xa0\\n61\\xa0\\nOther\\n897\\xa0\\n922\\xa0\\nAccrued expenses, accounts payable, and other current liabilities\\n$\\n2,654\\xa0 $\\n2,968\\xa0\\nOther liabilities, noncurrent:\\nOperating lease liabilities, noncurrent\\n$\\n252\\xa0 $\\n237\\xa0\\nOther liabilities, noncurrent\\n287\\xa0\\n273\\xa0\\nOther liabilities, noncurrent\\n$\\n539\\xa0 $\\n510\\xa0\\nPayments to Customers', metadata={'subject': 'Form 10-Q filed on 2024-05-08 for the period ending 2024-03-31', 'creator': 'EDGAR Filing HTML Converter', 'total_pages': 54, 'keywords': '0001559720-24-000017; ; 10-Q', 'modDate': \"D:20240508161807-04'00'\", 'encryption': 'Standard V2 R3 128-bit RC4', 'trapped': '', 'format': 'PDF 1.4', 'source': '/home/donbr/aie3-bootcamp/AIE3/Week 3/Day 2/files/airbnb.pdf', 'file_path': '/home/donbr/aie3-bootcamp/AIE3/Week 3/Day 2/files/airbnb.pdf', 'creationDate': \"D:20240508161757-04'00'\", 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'page': 10, 'title': '0001559720-24-000017', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', '_id': '28a8a595-2736-403b-a5c3-8411353cc470', '_collection_name': 'airbnb_pdf_rec_1000_200_images'}),\n", " Document(page_content='Table of Contents\\nAirbnb, Inc.\\nNotes to Condensed Consolidated Financial Statements (unaudited)\\nNote 3. Supplemental Financial Statement Information\\nCash, Cash Equivalents, and Restricted Cash\\nThe following table reconciles cash, cash equivalents, and restricted cash reported on the Company’s unaudited condensed consolidated balance sheets to the total amount\\npresented in the unaudited condensed consolidated statements of cash flows (in millions):\\nDecember 31,\\n2023\\nMarch 31,\\n2024\\nCash and cash equivalents\\n$\\n6,874\\xa0 $\\n7,829\\xa0\\nCash and cash equivalents included in funds receivable and amounts held on behalf of customers\\n5,769\\xa0\\n8,665\\xa0\\nRestricted cash included in prepaids and other current assets\\n24\\xa0\\n35\\xa0\\nTotal cash, cash equivalents, and restricted cash presented in the unaudited condensed consolidated statements of cash flows\\n$\\n12,667\\xa0 $\\n16,529\\xa0\\nSupplemental disclosures of balance sheet information\\nSupplemental balance sheet information consisted of the following (in millions):\\nDecember 31,', metadata={'subject': 'Form 10-Q filed on 2024-05-08 for the period ending 2024-03-31', 'creator': 'EDGAR Filing HTML Converter', 'total_pages': 54, 'keywords': '0001559720-24-000017; ; 10-Q', 'trapped': '', 'encryption': 'Standard V2 R3 128-bit RC4', 'modDate': \"D:20240508161807-04'00'\", 'format': 'PDF 1.4', 'creationDate': \"D:20240508161757-04'00'\", 'file_path': '/home/donbr/aie3-bootcamp/AIE3/Week 3/Day 2/files/airbnb.pdf', 'source': '/home/donbr/aie3-bootcamp/AIE3/Week 3/Day 2/files/airbnb.pdf', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'page': 10, 'title': '0001559720-24-000017', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', '_id': 'fc69f02a-bdc5-443a-8e3e-8e2254b6012a', '_collection_name': 'airbnb_pdf_rec_1000_200_images'})]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response[\"context\"]" ] } ], "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.11.8" } }, "nbformat": 4, "nbformat_minor": 2 }