{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from langchain_community.vectorstores import Qdrant" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from qdrant_client import QdrantClient, models\n", "\n", "client = QdrantClient(path=\"Qdrant_db\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from langchain_openai.embeddings import OpenAIEmbeddings\n", "\n", "embedding_model = OpenAIEmbeddings(model=\"text-embedding-3-small\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "collection_name = \"Meta info 400\"\n", "qdrant = Qdrant(client, collection_name, embedding_model)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "qdrant_retriever = qdrant.as_retriever()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Document(page_content='these risks and uncertainties, readers are cautioned not to place undue reliance on such forward‑looking statements.\\nUnless expressly indicated or the context requires otherwise, the terms \"Meta,\" \"company,\" \"we,\" \"us,\" and \"our\" in this document refer to Meta', metadata={'source': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'file_path': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'page': 3, 'total_pages': 147, 'format': 'PDF 1.4', 'title': '0001326801-24-000012', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', 'subject': 'Form 10-K filed on 2024-02-02 for the period ending 2023-12-31', 'keywords': '0001326801-24-000012; ; 10-K', 'creator': 'EDGAR Filing HTML Converter', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'creationDate': \"D:20240202060356-05'00'\", 'modDate': \"D:20240202060413-05'00'\", 'trapped': '', 'encryption': 'Standard V2 R3 128-bit RC4', '_id': '77ee8ea68d4940c7a649688a4b4dc095', '_collection_name': 'Meta info 400'}),\n", " Document(page_content='Table of Contents\\nItem 1B. Unresolved Staff Comments\\nNone.\\nItem 1C. Cybersecurity\\nAt Meta, cybersecurity risk management is an important part of our overall risk management efforts. Our industry is prone to cybersecurity threats and\\nattacks, and we regularly experience cybersecurity incidents of varying degrees. We believe we are a particularly attractive target as a result of our prominence', metadata={'source': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'file_path': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'page': 51, 'total_pages': 147, 'format': 'PDF 1.4', 'title': '0001326801-24-000012', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', 'subject': 'Form 10-K filed on 2024-02-02 for the period ending 2023-12-31', 'keywords': '0001326801-24-000012; ; 10-K', 'creator': 'EDGAR Filing HTML Converter', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'creationDate': \"D:20240202060356-05'00'\", 'modDate': \"D:20240202060413-05'00'\", 'trapped': '', 'encryption': 'Standard V2 R3 128-bit RC4', '_id': '1e13afafca6c4cdeb2815c2b168a290c', '_collection_name': 'Meta info 400'}),\n", " Document(page_content='EXHIBIT\\xa032.1\\nCERTIFICATION PURSUANT TO\\n18 U.S.C. SECTION\\xa01350\\nAS ADOPTED PURSUANT TO SECTION\\xa0906\\nOF THE SARBANES-OXLEY ACT OF 2002\\nI, Mark Zuckerberg, Board Chair and Chief Executive Officer of Meta Platforms,\\xa0Inc. (Company), do hereby certify, pursuant to 18 U.S.C. Section\\xa01350, as\\nadopted pursuant to Section\\xa0906 of the Sarbanes-Oxley Act of 2002, that to the best of my knowledge:\\n•', metadata={'source': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'file_path': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'page': 141, 'total_pages': 147, 'format': 'PDF 1.4', 'title': '0001326801-24-000012', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', 'subject': 'Form 10-K filed on 2024-02-02 for the period ending 2023-12-31', 'keywords': '0001326801-24-000012; ; 10-K', 'creator': 'EDGAR Filing HTML Converter', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'creationDate': \"D:20240202060356-05'00'\", 'modDate': \"D:20240202060413-05'00'\", 'trapped': '', 'encryption': 'Standard V2 R3 128-bit RC4', '_id': 'a7443d4d59f84e89a1573e300c05b147', '_collection_name': 'Meta info 400'}),\n", " Document(page_content='We were incorporated in Delaware in July 2004. We completed our initial public offering in May 2012 and our Class\\xa0A common stock is currently listed\\non the Nasdaq Global Select Market under the symbol \"META.\" Our principal executive offices are located at 1 Meta Way, Menlo Park, California 94025, and\\nour telephone number is (650) 543-4800.', metadata={'source': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'file_path': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'page': 13, 'total_pages': 147, 'format': 'PDF 1.4', 'title': '0001326801-24-000012', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', 'subject': 'Form 10-K filed on 2024-02-02 for the period ending 2023-12-31', 'keywords': '0001326801-24-000012; ; 10-K', 'creator': 'EDGAR Filing HTML Converter', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'creationDate': \"D:20240202060356-05'00'\", 'modDate': \"D:20240202060413-05'00'\", 'trapped': '', 'encryption': 'Standard V2 R3 128-bit RC4', '_id': '74778dd733954d4ea9701cd7726d8968', '_collection_name': 'Meta info 400'})]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query = \"What did the meta president said\"\n", "qdrant_retriever.invoke(query)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from langchain_openai import ChatOpenAI\n", "\n", "openai_chat_model = ChatOpenAI(model=\"gpt-3.5-turbo\")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from langchain_core.prompts import ChatPromptTemplate" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [], "source": [ "system_message = \"You are a helpful assistante, experienced lawyer and an expert reading SECURITIES AND EXCHANGE COMMISSION documents\"" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "RAG_PROMPT = \"\"\"\n", "\n", "CONTEXT:\n", "{context}\n", "\n", "QUERY:\n", "{question}\n", "\n", "Answer the query above using the context provided. If you don't know the answer responde with: I don't know\n", "\"\"\"\n", "\n", "rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "from langchain.retrievers import MultiQueryRetriever\n", "\n", "advanced_retriever = MultiQueryRetriever.from_llm(retriever=qdrant_retriever, llm=openai_chat_model)\n" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ChatPromptTemplate(input_variables=['context', 'question', 'system_message'], messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['context', 'question', 'system_message'], template=\"\\n\\nSYSTEM:\\n{system_message}\\n\\nCONTEXT:\\n{context}\\n\\nQUERY:\\n{question}\\n\\nAnswer the query above only using the context provided. If you don't know the answer responde with: I don't know\\n\"))])" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rag_prompt" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "from operator import itemgetter\n", "from langchain.schema.output_parser import StrOutputParser\n", "from langchain.schema.runnable import RunnablePassthrough\n", "\n", "retrieval_augmented_qa_chain = (\n", " # INVOKE CHAIN WITH: {\"question\" : \"<>\"}\n", " # \"question\" : populated by getting the value of the \"question\" key\n", " # \"context\" : populated by getting the value of the \"question\" key and chaining it into the base_retriever\n", " {\"context\": itemgetter(\"question\") | advanced_retriever, \"question\": itemgetter(\"question\")}\n", " # \"context\" : is assigned to a RunnablePassthrough object (will not be called or considered in the next step)\n", " # by getting the value of the \"context\" key from the previous step\n", " | RunnablePassthrough.assign(context=itemgetter(\"context\"))\n", " # \"response\" : the \"context\" and \"question\" values are used to format our prompt object and then piped\n", " # into the LLM and stored in a key called \"response\"\n", " # \"context\" : populated by getting the value of the \"context\" key from the previous step\n", " | {\"response\": rag_prompt | openai_chat_model, \"context\": itemgetter(\"context\")}\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\"What was the total value of 'Cash and cash equivalents' as of December 31, 2023?\"\n", "\n", "\"Who are Meta's 'Directors' (i.e., members of the Board of Directors)?\"" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AIMessage(content=\"I don't know\", response_metadata={'token_usage': {'completion_tokens': 4, 'prompt_tokens': 49, 'total_tokens': 53}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, id='run-6ad6e2e1-23e4-4aa7-a17c-12beea722db8-0')" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "retr = (rag_prompt | openai_chat_model)\n", "resp = retr.invoke({\"question\" : \"What was the total value \", \"context\": \"sdfsfsdfs\", \"system_message\" : \"dfgdfg\"})\n", "resp" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Mark Zuckerberg is the Board Chair and Chief Executive Officer.'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response = retrieval_augmented_qa_chain.invoke({\"question\" : \"Who is the Board Chair and Chief Executive Officer?\"})\n", "response[\"response\"].content" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'response': AIMessage(content='Mark Zuckerberg', response_metadata={'token_usage': {'completion_tokens': 2, 'prompt_tokens': 1705, 'total_tokens': 1707}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, id='run-7a0deb8c-4266-486b-964d-1b55397557f1-0'),\n", " 'context': [Document(page_content='/s/ MARK ZUCKERBERG\\nMark Zuckerberg\\nBoard Chair and Chief Executive Officer\\n(Principal Executive Officer)', metadata={'source': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'file_path': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'page': 139, 'total_pages': 147, 'format': 'PDF 1.4', 'title': '0001326801-24-000012', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', 'subject': 'Form 10-K filed on 2024-02-02 for the period ending 2023-12-31', 'keywords': '0001326801-24-000012; ; 10-K', 'creator': 'EDGAR Filing HTML Converter', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'creationDate': \"D:20240202060356-05'00'\", 'modDate': \"D:20240202060413-05'00'\", 'trapped': '', 'encryption': 'Standard V2 R3 128-bit RC4', '_id': '158c21bab89a4a1daefec736f7a56529', '_collection_name': 'Meta info'}),\n", " Document(page_content='including our founder, Board Chair, and CEO, together hold a majority of the combined voting power of our outstanding capital stock, and therefore are able to', metadata={'source': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'file_path': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'page': 49, 'total_pages': 147, 'format': 'PDF 1.4', 'title': '0001326801-24-000012', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', 'subject': 'Form 10-K filed on 2024-02-02 for the period ending 2023-12-31', 'keywords': '0001326801-24-000012; ; 10-K', 'creator': 'EDGAR Filing HTML Converter', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'creationDate': \"D:20240202060356-05'00'\", 'modDate': \"D:20240202060413-05'00'\", 'trapped': '', 'encryption': 'Standard V2 R3 128-bit RC4', '_id': '2333d2cb1e694d289b31df16b623076f', '_collection_name': 'Meta info'}),\n", " Document(page_content='position as our CEO and his ability to control the election or, in some cases, the replacement of our directors. In the event of his death, the shares of our capital', metadata={'source': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'file_path': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'page': 38, 'total_pages': 147, 'format': 'PDF 1.4', 'title': '0001326801-24-000012', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', 'subject': 'Form 10-K filed on 2024-02-02 for the period ending 2023-12-31', 'keywords': '0001326801-24-000012; ; 10-K', 'creator': 'EDGAR Filing HTML Converter', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'creationDate': \"D:20240202060356-05'00'\", 'modDate': \"D:20240202060413-05'00'\", 'trapped': '', 'encryption': 'Standard V2 R3 128-bit RC4', '_id': '39917b8eaae34727a610993fa1bad328', '_collection_name': 'Meta info'}),\n", " Document(page_content='/s/ Sheryl K. Sandberg\\nDirector\\nFebruary 1, 2024\\nSheryl K. Sandberg\\n/s/ Tracey T. Travis\\nDirector\\nFebruary 1, 2024\\nTracey T. Travis\\n/s/ Tony Xu\\nDirector\\nFebruary 1, 2024\\nTony Xu\\n132', metadata={'source': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'file_path': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'page': 132, 'total_pages': 147, 'format': 'PDF 1.4', 'title': '0001326801-24-000012', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', 'subject': 'Form 10-K filed on 2024-02-02 for the period ending 2023-12-31', 'keywords': '0001326801-24-000012; ; 10-K', 'creator': 'EDGAR Filing HTML Converter', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'creationDate': \"D:20240202060356-05'00'\", 'modDate': \"D:20240202060413-05'00'\", 'trapped': '', 'encryption': 'Standard V2 R3 128-bit RC4', '_id': '2d4262b80e9748a299028faa5cb05970', '_collection_name': 'Meta info'}),\n", " Document(page_content='stockholders may be called only by a majority of our board of directors, the chairman of our board of directors, our chief executive officer or our president.\\n•', metadata={'source': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'file_path': 'Data/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf', 'page': 136, 'total_pages': 147, 'format': 'PDF 1.4', 'title': '0001326801-24-000012', 'author': 'EDGAR® Online LLC, a subsidiary of OTC Markets Group', 'subject': 'Form 10-K filed on 2024-02-02 for the period ending 2023-12-31', 'keywords': '0001326801-24-000012; ; 10-K', 'creator': 'EDGAR Filing HTML Converter', 'producer': 'EDGRpdf Service w/ EO.Pdf 22.0.40.0', 'creationDate': \"D:20240202060356-05'00'\", 'modDate': \"D:20240202060413-05'00'\", 'trapped': '', 'encryption': 'Standard V2 R3 128-bit RC4', '_id': '8188dace3340485e83414a35c710392c', '_collection_name': 'Meta info'})]}" ] }, "execution_count": 148, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Doc-Expert", "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.9.19" } }, "nbformat": 4, "nbformat_minor": 2 }