{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "0c2aff87", "metadata": {}, "outputs": [], "source": [ "import os\n", "import streamlit as st\n", "import pickle\n", "import time\n", "import langchain\n", "from langchain import OpenAI\n", "from langchain.chains import RetrievalQAWithSourcesChain\n", "from langchain.chains.qa_with_sources.loading import load_qa_with_sources_chain\n", "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", "from langchain.document_loaders import UnstructuredURLLoader\n", "from langchain.embeddings import OpenAIEmbeddings\n", "from langchain.vectorstores import FAISS" ] }, { "cell_type": "code", "execution_count": 6, "id": "80fc5e57", "metadata": {}, "outputs": [], "source": [ "#load openAI api key\n", "os.environ['OPENAI_API_KEY'] = 'sk-proj-YXQCFNWqx1cRLsNczXtST3BlbkFJvG1UVBNpEJTUvgb6zSrV'" ] }, { "cell_type": "code", "execution_count": 4, "id": "39e721c4", "metadata": {}, "outputs": [], "source": [ "# Initialise LLM with required params\n", "llm = OpenAI(temperature=0.9, max_tokens=500) " ] }, { "cell_type": "markdown", "id": "bd0c3ff7", "metadata": {}, "source": [ "### (1) Load data" ] }, { "cell_type": "code", "execution_count": 5, "id": "55fa0ef5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loaders = UnstructuredURLLoader(urls=[\n", " \"https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.html\",\n", " \"https://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\"\n", "])\n", "data = loaders.load() \n", "len(data)" ] }, { "cell_type": "markdown", "id": "9f51a5bd", "metadata": {}, "source": [ "### (2) Split data to create chunks" ] }, { "cell_type": "code", "execution_count": 7, "id": "054a6361", "metadata": {}, "outputs": [], "source": [ "text_splitter = RecursiveCharacterTextSplitter(\n", " chunk_size=1000,\n", " chunk_overlap=200\n", ")\n", "\n", "# As data is of type documents we can directly use split_documents over split_text in order to get the chunks.\n", "docs = text_splitter.split_documents(data)" ] }, { "cell_type": "code", "execution_count": 8, "id": "379e3d94", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "41" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(docs)" ] }, { "cell_type": "code", "execution_count": 9, "id": "637ee7ae", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "Document(page_content='English\\n\\nHindi\\n\\nGujarati\\n\\nSpecials\\n\\nTrending Stocks\\n\\nIRFC\\xa0INE053F01010, IRFC, 543257\\n\\nTata Power\\xa0INE245A01021, TATAPOWER, 500400\\n\\nRail Vikas\\xa0INE415G01027, RVNL, 542649\\n\\nJio Financial\\xa0INE758E01017, JIOFIN, 543940\\n\\nSuzlon Energy\\xa0INE040H01021, SUZLON, 532667\\n\\nCheck your Credit Score here!\\n\\nQuotes\\n\\nMutual Funds\\n\\nCommodities\\n\\nFutures & Options\\n\\nCurrency\\n\\nNews\\n\\nCryptocurrency\\n\\nForum\\n\\nNotices\\n\\nVideos\\n\\nGlossary\\n\\nAll\\n\\nHello, LoginHello, LoginLog-inor Sign-UpMy AccountMy Profile My PortfolioMy WatchlistMy Credit ScoreMy MessagesMy AlertsMy Profile My PROMy PortfolioMy WatchlistMy Credit ScoreMy MessagesMy AlertsLogoutChat with UsDownload AppFollow us on:\\n\\nUpgrade', metadata={'source': 'https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.html'})" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "docs[0]" ] }, { "cell_type": "markdown", "id": "9e35a876", "metadata": {}, "source": [ "### (3) Create embeddings for these chunks and save them to FAISS index" ] }, { "cell_type": "code", "execution_count": 12, "id": "c3d0a6dd", "metadata": {}, "outputs": [], "source": [ "# Create the embeddings of the chunks using openAIEmbeddings\n", "embeddings = OpenAIEmbeddings()\n", "\n", "# Pass the documents and embeddings inorder to create FAISS vector index\n", "vectorindex_openai = FAISS.from_documents(docs, embeddings)" ] }, { "cell_type": "code", "execution_count": 14, "id": "a9686c13", "metadata": {}, "outputs": [], "source": [ "# Storing vector index create in local\n", "file_path=\"vector_index.pkl\"\n", "with open(file_path, \"wb\") as f:\n", " pickle.dump(vectorindex_openai, f)" ] }, { "cell_type": "code", "execution_count": 15, "id": "688dc29b", "metadata": {}, "outputs": [], "source": [ "if os.path.exists(file_path):\n", " with open(file_path, \"rb\") as f:\n", " vectorIndex = pickle.load(f)" ] }, { "cell_type": "markdown", "id": "fbd96296", "metadata": {}, "source": [ "### (4) Retrieve similar embeddings for a given question and call LLM to retrieve final answer" ] }, { "cell_type": "code", "execution_count": 18, "id": "01f5e1e8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RetrievalQAWithSourcesChain(memory=None, callbacks=None, callback_manager=None, verbose=False, tags=None, metadata=None, combine_documents_chain=MapReduceDocumentsChain(memory=None, callbacks=None, callback_manager=None, verbose=False, tags=None, metadata=None, input_key='input_documents', output_key='output_text', llm_chain=LLMChain(memory=None, callbacks=None, callback_manager=None, verbose=False, tags=None, metadata=None, prompt=PromptTemplate(input_variables=['context', 'question'], output_parser=None, partial_variables={}, template='Use the following portion of a long document to see if any of the text is relevant to answer the question. \\nReturn any relevant text verbatim.\\n{context}\\nQuestion: {question}\\nRelevant text, if any:', template_format='f-string', validate_template=True), llm=OpenAI(cache=None, verbose=False, callbacks=None, callback_manager=None, tags=None, metadata=None, client=, model_name='text-davinci-003', temperature=0.9, max_tokens=500, top_p=1, frequency_penalty=0, presence_penalty=0, n=1, best_of=1, model_kwargs={}, openai_api_key='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', openai_api_base='', openai_organization='', openai_proxy='', batch_size=20, request_timeout=None, logit_bias={}, max_retries=6, streaming=False, allowed_special=set(), disallowed_special='all', tiktoken_model_name=None), output_key='text', output_parser=StrOutputParser(), return_final_only=True, llm_kwargs={}), reduce_documents_chain=ReduceDocumentsChain(memory=None, callbacks=None, callback_manager=None, verbose=False, tags=None, metadata=None, input_key='input_documents', output_key='output_text', combine_documents_chain=StuffDocumentsChain(memory=None, callbacks=None, callback_manager=None, verbose=False, tags=None, metadata=None, input_key='input_documents', output_key='output_text', llm_chain=LLMChain(memory=None, callbacks=None, callback_manager=None, verbose=False, tags=None, metadata=None, prompt=PromptTemplate(input_variables=['summaries', 'question'], output_parser=None, partial_variables={}, template='Given the following extracted parts of a long document and a question, create a final answer with references (\"SOURCES\"). \\nIf you don\\'t know the answer, just say that you don\\'t know. Don\\'t try to make up an answer.\\nALWAYS return a \"SOURCES\" part in your answer.\\n\\nQUESTION: Which state/country\\'s law governs the interpretation of the contract?\\n=========\\nContent: This Agreement is governed by English law and the parties submit to the exclusive jurisdiction of the English courts in relation to any dispute (contractual or non-contractual) concerning this Agreement save that either party may apply to any court for an injunction or other relief to protect its Intellectual Property Rights.\\nSource: 28-pl\\nContent: No Waiver. Failure or delay in exercising any right or remedy under this Agreement shall not constitute a waiver of such (or any other) right or remedy.\\n\\n11.7 Severability. The invalidity, illegality or unenforceability of any term (or part of a term) of this Agreement shall not affect the continuation in force of the remainder of the term (if any) and this Agreement.\\n\\n11.8 No Agency. Except as expressly stated otherwise, nothing in this Agreement shall create an agency, partnership or joint venture of any kind between the parties.\\n\\n11.9 No Third-Party Beneficiaries.\\nSource: 30-pl\\nContent: (b) if Google believes, in good faith, that the Distributor has violated or caused Google to violate any Anti-Bribery Laws (as defined in Clause 8.5) or that such a violation is reasonably likely to occur,\\nSource: 4-pl\\n=========\\nFINAL ANSWER: This Agreement is governed by English law.\\nSOURCES: 28-pl\\n\\nQUESTION: What did the president say about Michael Jackson?\\n=========\\nContent: Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \\n\\nLast year COVID-19 kept us apart. This year we are finally together again. \\n\\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \\n\\nWith a duty to one another to the American people to the Constitution. \\n\\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \\n\\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \\n\\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \\n\\nHe met the Ukrainian people. \\n\\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world. \\n\\nGroups of citizens blocking tanks with their bodies. Everyone from students to retirees teachers turned soldiers defending their homeland.\\nSource: 0-pl\\nContent: And we won’t stop. \\n\\nWe have lost so much to COVID-19. Time with one another. And worst of all, so much loss of life. \\n\\nLet’s use this moment to reset. Let’s stop looking at COVID-19 as a partisan dividing line and see it for what it is: A God-awful disease. \\n\\nLet’s stop seeing each other as enemies, and start seeing each other for who we really are: Fellow Americans. \\n\\nWe can’t change how divided we’ve been. But we can change how we move forward—on COVID-19 and other issues we must face together. \\n\\nI recently visited the New York City Police Department days after the funerals of Officer Wilbert Mora and his partner, Officer Jason Rivera. \\n\\nThey were responding to a 9-1-1 call when a man shot and killed them with a stolen gun. \\n\\nOfficer Mora was 27 years old. \\n\\nOfficer Rivera was 22. \\n\\nBoth Dominican Americans who’d grown up on the same streets they later chose to patrol as police officers. \\n\\nI spoke with their families and told them that we are forever in debt for their sacrifice, and we will carry on their mission to restore the trust and safety every community deserves.\\nSource: 24-pl\\nContent: And a proud Ukrainian people, who have known 30 years of independence, have repeatedly shown that they will not tolerate anyone who tries to take their country backwards. \\n\\nTo all Americans, I will be honest with you, as I’ve always promised. A Russian dictator, invading a foreign country, has costs around the world. \\n\\nAnd I’m taking robust action to make sure the pain of our sanctions is targeted at Russia’s economy. And I will use every tool at our disposal to protect American businesses and consumers. \\n\\nTonight, I can announce that the United States has worked with 30 other countries to release 60 Million barrels of oil from reserves around the world. \\n\\nAmerica will lead that effort, releasing 30 Million barrels from our own Strategic Petroleum Reserve. And we stand ready to do more if necessary, unified with our allies. \\n\\nThese steps will help blunt gas prices here at home. And I know the news about what’s happening can seem alarming. \\n\\nBut I want you to know that we are going to be okay.\\nSource: 5-pl\\nContent: More support for patients and families. \\n\\nTo get there, I call on Congress to fund ARPA-H, the Advanced Research Projects Agency for Health. \\n\\nIt’s based on DARPA—the Defense Department project that led to the Internet, GPS, and so much more. \\n\\nARPA-H will have a singular purpose—to drive breakthroughs in cancer, Alzheimer’s, diabetes, and more. \\n\\nA unity agenda for the nation. \\n\\nWe can do this. \\n\\nMy fellow Americans—tonight , we have gathered in a sacred space—the citadel of our democracy. \\n\\nIn this Capitol, generation after generation, Americans have debated great questions amid great strife, and have done great things. \\n\\nWe have fought for freedom, expanded liberty, defeated totalitarianism and terror. \\n\\nAnd built the strongest, freest, and most prosperous nation the world has ever known. \\n\\nNow is the hour. \\n\\nOur moment of responsibility. \\n\\nOur test of resolve and conscience, of history itself. \\n\\nIt is in this moment that our character is formed. Our purpose is found. Our future is forged. \\n\\nWell I know this nation.\\nSource: 34-pl\\n=========\\nFINAL ANSWER: The president did not mention Michael Jackson.\\nSOURCES:\\n\\nQUESTION: {question}\\n=========\\n{summaries}\\n=========\\nFINAL ANSWER:', template_format='f-string', validate_template=True), llm=OpenAI(cache=None, verbose=False, callbacks=None, callback_manager=None, tags=None, metadata=None, client=, model_name='text-davinci-003', temperature=0.9, max_tokens=500, top_p=1, frequency_penalty=0, presence_penalty=0, n=1, best_of=1, model_kwargs={}, openai_api_key='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', openai_api_base='', openai_organization='', openai_proxy='', batch_size=20, request_timeout=None, logit_bias={}, max_retries=6, streaming=False, allowed_special=set(), disallowed_special='all', tiktoken_model_name=None), output_key='text', output_parser=StrOutputParser(), return_final_only=True, llm_kwargs={}), document_prompt=PromptTemplate(input_variables=['page_content', 'source'], output_parser=None, partial_variables={}, template='Content: {page_content}\\nSource: {source}', template_format='f-string', validate_template=True), document_variable_name='summaries', document_separator='\\n\\n'), collapse_documents_chain=None, token_max=3000), document_variable_name='context', return_intermediate_steps=False), question_key='question', input_docs_key='docs', answer_key='answer', sources_answer_key='sources', return_source_documents=False, retriever=VectorStoreRetriever(tags=['FAISS'], metadata=None, vectorstore=, search_type='similarity', search_kwargs={}), reduce_k_below_max_tokens=False, max_tokens_limit=3375)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chain = RetrievalQAWithSourcesChain.from_llm(llm=llm, retriever=vectorIndex.as_retriever())\n", "chain" ] }, { "cell_type": "code", "execution_count": 26, "id": "8c2e228b", "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain] Entering Chain run with input:\n", "\u001b[0m{\n", " \"question\": \"what is the price of Tiago iCNG?\"\n", "}\n", "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain] Entering Chain run with input:\n", "\u001b[0m[inputs]\n", "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 4:chain:LLMChain] Entering Chain run with input:\n", "\u001b[0m{\n", " \"input_list\": [\n", " {\n", " \"context\": \"The company also said it has also introduced the twin-cylinder technology on its Tiago and Tigor models.\\n\\nThe Tiago iCNG is priced between Rs 6.55 lakh and Rs 8.1 lakh, while the Tigor iCNG comes at a price range of Rs 7.8 lakh to Rs 8.95 lakh.\\n\\nTata Motors Passenger Vehicles Ltd Head-Marketing, Vinay Pant said these introductions put together will make the company's CNG line up \\\"appealing, holistic, and stronger than ever\\\".\\n\\nPTI\\n\\nTags:\\n\\n#Business\\n\\n#Companies\\n\\nfirst published: Aug 4, 2023 02:17 pm\\n\\nbusiness news,\\n\\nSensex, and\\n\\nNifty updates. Obtain\\n\\nPersonal Finance insights, tax queries, and expert opinions on\\n\\nMoneycontrol or download the\\n\\nMoneycontrol App to stay updated!\\n\\nForum\\n\\nFacebook\\n\\nTwitter\\n\\nInstagram\\n\\nLinkedin\\n\\nRSS\\n\\nPortfolio\\n\\nMarkets\\n\\nWatchlist\\n\\nLive TV Show\\n\\nCurrencies\\n\\nCredit Score\\n\\nCommodities\\n\\nFixed Income\\n\\nPersonal Finance\\n\\nMutual Fund\\n\\nPre-Market\\n\\nIPO\\n\\nGlobal Market\\n\\nBudget 2023\\n\\nGold Rate\\n\\nBSE Sensex\\n\\nForum\\n\\nMC 30\\n\\nNews\\n\\nBusiness\\n\\nMarkets\\n\\nStocks\\n\\nEconomy\",\n", " \"question\": \"what is the price of Tiago iCNG?\"\n", " },\n", " {\n", " \"context\": \"Tata Motors launches Punch iCNG, price starts at Rs 7.1 lakh\\n\\nWatchlist\\n\\nPortfolio\\n\\nMessage\\n\\nSet Alert\\n\\nlive\\n\\nbselive\\n\\nnselive\\n\\nVolume \\n\\nTodays L/H \\n\\nMore\\n\\nTata Motors on Friday launched the CNG variant of its micro SUV Punch priced between Rs 7.1 lakh and Rs 9.68 lakh (ex-showroom, Delhi).\\n\\nThe Punch iCNG is equipped with the company's proprietary twin-cylinder technology with enhanced safety features like a micro-switch to keep the car switched off at the time of refuelling and thermal incident protection that cuts off CNG supply to the engine and releases gas into the atmosphere, Tata Motors said in a statement.\\n\\nIt is also equipped with other features such as voice assisted electric sunroof, automatic projector headlamps, LED DRLs, 16-inch diamond cut alloy wheels, 7-inch infotainment system by Harman that supports Android Auto and Apple Carplay connectivity, rain sensing wipers and height adjustable driver seat.\",\n", " \"question\": \"what is the price of Tiago iCNG?\"\n", " },\n", " {\n", " \"context\": \"Be a PRO\\n\\nBusiness\\n\\nMarkets\\n\\nStocks\\n\\nEconomy\\n\\nCompanies\\n\\nTrends\\n\\nIPO\\n\\nOpinion\\n\\nEV Special\\n\\nVisa Expert:\\n\\nGet instant updates on the latest news in the immigration world right at your fingertips with Visa Expert. Click Here!\\n\\nyou are here:\\n\\nHome\\n\\nNews\\n\\nBusiness\\n\\nTata Motors launches Punch iCNG, price starts at Rs 7.1 lakh\\n\\nThe Punch iCNG is equipped with the company's proprietary twin-cylinder technology with enhanced safety features like a micro-switch to keep the car switched off at the time of refuelling and thermal incident protection that cuts off CNG supply to the engine and releases gas into the atmosphere, Tata Motors said in a statement.\\n\\nPTI\\n\\nAugust 04, 2023 / 02:17 PM IST\\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\\nTata Motors launches Punch iCNG, price starts at Rs 7.1 lakh\\n\\nWatchlist\\n\\nPortfolio\\n\\nMessage\\n\\nSet Alert\\n\\nlive\\n\\nbselive\\n\\nnselive\\n\\nVolume \\n\\nTodays L/H \\n\\nMore\",\n", " \"question\": \"what is the price of Tiago iCNG?\"\n", " },\n", " {\n", " \"context\": \"English\\n\\nHindi\\n\\nGujarati\\n\\nSpecials\\n\\nTrending Stocks\\n\\nIRFC INE053F01010, IRFC, 543257\\n\\nTata Power INE245A01021, TATAPOWER, 500400\\n\\nRail Vikas INE415G01027, RVNL, 542649\\n\\nJio Financial INE758E01017, JIOFIN, 543940\\n\\nSuzlon Energy INE040H01021, SUZLON, 532667\\n\\nCheck your Credit Score here!\\n\\nQuotes\\n\\nMutual Funds\\n\\nCommodities\\n\\nFutures & Options\\n\\nCurrency\\n\\nNews\\n\\nCryptocurrency\\n\\nForum\\n\\nNotices\\n\\nVideos\\n\\nGlossary\\n\\nAll\\n\\nHello, LoginHello, LoginLog-inor Sign-UpMy AccountMy Profile My PortfolioMy WatchlistMy Credit ScoreMy MessagesMy AlertsMy Profile My PROMy PortfolioMy WatchlistMy Credit ScoreMy MessagesMy AlertsLogoutChat with UsDownload AppFollow us on:\\n\\nUpgrade\",\n", " \"question\": \"what is the price of Tiago iCNG?\"\n", " }\n", " ]\n", "}\n", "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 4:chain:LLMChain > 5:llm:OpenAI] Entering LLM run with input:\n", "\u001b[0m{\n", " \"prompts\": [\n", " \"Use the following portion of a long document to see if any of the text is relevant to answer the question. \\nReturn any relevant text verbatim.\\nThe company also said it has also introduced the twin-cylinder technology on its Tiago and Tigor models.\\n\\nThe Tiago iCNG is priced between Rs 6.55 lakh and Rs 8.1 lakh, while the Tigor iCNG comes at a price range of Rs 7.8 lakh to Rs 8.95 lakh.\\n\\nTata Motors Passenger Vehicles Ltd Head-Marketing, Vinay Pant said these introductions put together will make the company's CNG line up \\\"appealing, holistic, and stronger than ever\\\".\\n\\nPTI\\n\\nTags:\\n\\n#Business\\n\\n#Companies\\n\\nfirst published: Aug 4, 2023 02:17 pm\\n\\nbusiness news,\\n\\nSensex, and\\n\\nNifty updates. Obtain\\n\\nPersonal Finance insights, tax queries, and expert opinions on\\n\\nMoneycontrol or download the\\n\\nMoneycontrol App to stay updated!\\n\\nForum\\n\\nFacebook\\n\\nTwitter\\n\\nInstagram\\n\\nLinkedin\\n\\nRSS\\n\\nPortfolio\\n\\nMarkets\\n\\nWatchlist\\n\\nLive TV Show\\n\\nCurrencies\\n\\nCredit Score\\n\\nCommodities\\n\\nFixed Income\\n\\nPersonal Finance\\n\\nMutual Fund\\n\\nPre-Market\\n\\nIPO\\n\\nGlobal Market\\n\\nBudget 2023\\n\\nGold Rate\\n\\nBSE Sensex\\n\\nForum\\n\\nMC 30\\n\\nNews\\n\\nBusiness\\n\\nMarkets\\n\\nStocks\\n\\nEconomy\\nQuestion: what is the price of Tiago iCNG?\\nRelevant text, if any:\"\n", " ]\n", "}\n", "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 4:chain:LLMChain > 6:llm:OpenAI] Entering LLM run with input:\n", "\u001b[0m{\n", " \"prompts\": [\n", " \"Use the following portion of a long document to see if any of the text is relevant to answer the question. \\nReturn any relevant text verbatim.\\nTata Motors launches Punch iCNG, price starts at Rs 7.1 lakh\\n\\nWatchlist\\n\\nPortfolio\\n\\nMessage\\n\\nSet Alert\\n\\nlive\\n\\nbselive\\n\\nnselive\\n\\nVolume \\n\\nTodays L/H \\n\\nMore\\n\\nTata Motors on Friday launched the CNG variant of its micro SUV Punch priced between Rs 7.1 lakh and Rs 9.68 lakh (ex-showroom, Delhi).\\n\\nThe Punch iCNG is equipped with the company's proprietary twin-cylinder technology with enhanced safety features like a micro-switch to keep the car switched off at the time of refuelling and thermal incident protection that cuts off CNG supply to the engine and releases gas into the atmosphere, Tata Motors said in a statement.\\n\\nIt is also equipped with other features such as voice assisted electric sunroof, automatic projector headlamps, LED DRLs, 16-inch diamond cut alloy wheels, 7-inch infotainment system by Harman that supports Android Auto and Apple Carplay connectivity, rain sensing wipers and height adjustable driver seat.\\nQuestion: what is the price of Tiago iCNG?\\nRelevant text, if any:\"\n", " ]\n", "}\n", "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 4:chain:LLMChain > 7:llm:OpenAI] Entering LLM run with input:\n", "\u001b[0m{\n", " \"prompts\": [\n", " \"Use the following portion of a long document to see if any of the text is relevant to answer the question. \\nReturn any relevant text verbatim.\\nBe a PRO\\n\\nBusiness\\n\\nMarkets\\n\\nStocks\\n\\nEconomy\\n\\nCompanies\\n\\nTrends\\n\\nIPO\\n\\nOpinion\\n\\nEV Special\\n\\nVisa Expert:\\n\\nGet instant updates on the latest news in the immigration world right at your fingertips with Visa Expert. Click Here!\\n\\nyou are here:\\n\\nHome\\n\\nNews\\n\\nBusiness\\n\\nTata Motors launches Punch iCNG, price starts at Rs 7.1 lakh\\n\\nThe Punch iCNG is equipped with the company's proprietary twin-cylinder technology with enhanced safety features like a micro-switch to keep the car switched off at the time of refuelling and thermal incident protection that cuts off CNG supply to the engine and releases gas into the atmosphere, Tata Motors said in a statement.\\n\\nPTI\\n\\nAugust 04, 2023 / 02:17 PM IST\\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\\nTata Motors launches Punch iCNG, price starts at Rs 7.1 lakh\\n\\nWatchlist\\n\\nPortfolio\\n\\nMessage\\n\\nSet Alert\\n\\nlive\\n\\nbselive\\n\\nnselive\\n\\nVolume \\n\\nTodays L/H \\n\\nMore\\nQuestion: what is the price of Tiago iCNG?\\nRelevant text, if any:\"\n", " ]\n", "}\n", "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 4:chain:LLMChain > 8:llm:OpenAI] Entering LLM run with input:\n", "\u001b[0m{\n", " \"prompts\": [\n", " \"Use the following portion of a long document to see if any of the text is relevant to answer the question. \\nReturn any relevant text verbatim.\\nEnglish\\n\\nHindi\\n\\nGujarati\\n\\nSpecials\\n\\nTrending Stocks\\n\\nIRFC INE053F01010, IRFC, 543257\\n\\nTata Power INE245A01021, TATAPOWER, 500400\\n\\nRail Vikas INE415G01027, RVNL, 542649\\n\\nJio Financial INE758E01017, JIOFIN, 543940\\n\\nSuzlon Energy INE040H01021, SUZLON, 532667\\n\\nCheck your Credit Score here!\\n\\nQuotes\\n\\nMutual Funds\\n\\nCommodities\\n\\nFutures & Options\\n\\nCurrency\\n\\nNews\\n\\nCryptocurrency\\n\\nForum\\n\\nNotices\\n\\nVideos\\n\\nGlossary\\n\\nAll\\n\\nHello, LoginHello, LoginLog-inor Sign-UpMy AccountMy Profile My PortfolioMy WatchlistMy Credit ScoreMy MessagesMy AlertsMy Profile My PROMy PortfolioMy WatchlistMy Credit ScoreMy MessagesMy AlertsLogoutChat with UsDownload AppFollow us on:\\n\\nUpgrade\\nQuestion: what is the price of Tiago iCNG?\\nRelevant text, if any:\"\n", " ]\n", "}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 4:chain:LLMChain > 5:llm:OpenAI] [1.01s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", " \"text\": \" The Tiago iCNG is priced between Rs 6.55 lakh and Rs 8.1 lakh.\",\n", " \"generation_info\": {\n", " \"finish_reason\": \"stop\",\n", " \"logprobs\": null\n", " }\n", " }\n", " ]\n", " ],\n", " \"llm_output\": {\n", " \"token_usage\": {\n", " \"total_tokens\": 1343,\n", " \"prompt_tokens\": 1269,\n", " \"completion_tokens\": 74\n", " },\n", " \"model_name\": \"text-davinci-003\"\n", " },\n", " \"run\": null\n", "}\n", "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 4:chain:LLMChain > 6:llm:OpenAI] [1.01s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", " \"text\": \" Tata Motors on Friday launched the CNG variant of its micro SUV Punch priced between Rs 7.1 lakh and Rs 9.68 lakh (ex-showroom, Delhi).\",\n", " \"generation_info\": {\n", " \"finish_reason\": \"stop\",\n", " \"logprobs\": null\n", " }\n", " }\n", " ]\n", " ],\n", " \"llm_output\": {\n", " \"token_usage\": {},\n", " \"model_name\": \"text-davinci-003\"\n", " },\n", " \"run\": null\n", "}\n", "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 4:chain:LLMChain > 7:llm:OpenAI] [1.01s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", " \"text\": \" Tata Motors launches Punch iCNG, price starts at Rs 7.1 lakh\",\n", " \"generation_info\": {\n", " \"finish_reason\": \"stop\",\n", " \"logprobs\": null\n", " }\n", " }\n", " ]\n", " ],\n", " \"llm_output\": {\n", " \"token_usage\": {},\n", " \"model_name\": \"text-davinci-003\"\n", " },\n", " \"run\": null\n", "}\n", "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 4:chain:LLMChain > 8:llm:OpenAI] [1.01s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", " \"text\": \" None\",\n", " \"generation_info\": {\n", " \"finish_reason\": \"stop\",\n", " \"logprobs\": null\n", " }\n", " }\n", " ]\n", " ],\n", " \"llm_output\": {\n", " \"token_usage\": {},\n", " \"model_name\": \"text-davinci-003\"\n", " },\n", " \"run\": null\n", "}\n", "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 4:chain:LLMChain] [1.02s] Exiting Chain run with output:\n", "\u001b[0m{\n", " \"outputs\": [\n", " {\n", " \"text\": \" The Tiago iCNG is priced between Rs 6.55 lakh and Rs 8.1 lakh.\"\n", " },\n", " {\n", " \"text\": \" Tata Motors on Friday launched the CNG variant of its micro SUV Punch priced between Rs 7.1 lakh and Rs 9.68 lakh (ex-showroom, Delhi).\"\n", " },\n", " {\n", " \"text\": \" Tata Motors launches Punch iCNG, price starts at Rs 7.1 lakh\"\n", " },\n", " {\n", " \"text\": \" None\"\n", " }\n", " ]\n", "}\n", "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 9:chain:LLMChain] Entering Chain run with input:\n", "\u001b[0m{\n", " \"question\": \"what is the price of Tiago iCNG?\",\n", " \"summaries\": \"Content: The Tiago iCNG is priced between Rs 6.55 lakh and Rs 8.1 lakh.\\nSource: https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\\n\\nContent: Tata Motors on Friday launched the CNG variant of its micro SUV Punch priced between Rs 7.1 lakh and Rs 9.68 lakh (ex-showroom, Delhi).\\nSource: https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\\n\\nContent: Tata Motors launches Punch iCNG, price starts at Rs 7.1 lakh\\nSource: https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\\n\\nContent: None\\nSource: https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\"\n", "}\n", "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 9:chain:LLMChain > 10:llm:OpenAI] Entering LLM run with input:\n", "\u001b[0m{\n", " \"prompts\": [\n", " \"Given the following extracted parts of a long document and a question, create a final answer with references (\\\"SOURCES\\\"). \\nIf you don't know the answer, just say that you don't know. Don't try to make up an answer.\\nALWAYS return a \\\"SOURCES\\\" part in your answer.\\n\\nQUESTION: Which state/country's law governs the interpretation of the contract?\\n=========\\nContent: This Agreement is governed by English law and the parties submit to the exclusive jurisdiction of the English courts in relation to any dispute (contractual or non-contractual) concerning this Agreement save that either party may apply to any court for an injunction or other relief to protect its Intellectual Property Rights.\\nSource: 28-pl\\nContent: No Waiver. Failure or delay in exercising any right or remedy under this Agreement shall not constitute a waiver of such (or any other) right or remedy.\\n\\n11.7 Severability. The invalidity, illegality or unenforceability of any term (or part of a term) of this Agreement shall not affect the continuation in force of the remainder of the term (if any) and this Agreement.\\n\\n11.8 No Agency. Except as expressly stated otherwise, nothing in this Agreement shall create an agency, partnership or joint venture of any kind between the parties.\\n\\n11.9 No Third-Party Beneficiaries.\\nSource: 30-pl\\nContent: (b) if Google believes, in good faith, that the Distributor has violated or caused Google to violate any Anti-Bribery Laws (as defined in Clause 8.5) or that such a violation is reasonably likely to occur,\\nSource: 4-pl\\n=========\\nFINAL ANSWER: This Agreement is governed by English law.\\nSOURCES: 28-pl\\n\\nQUESTION: What did the president say about Michael Jackson?\\n=========\\nContent: Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \\n\\nLast year COVID-19 kept us apart. This year we are finally together again. \\n\\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \\n\\nWith a duty to one another to the American people to the Constitution. \\n\\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \\n\\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \\n\\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \\n\\nHe met the Ukrainian people. \\n\\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world. \\n\\nGroups of citizens blocking tanks with their bodies. Everyone from students to retirees teachers turned soldiers defending their homeland.\\nSource: 0-pl\\nContent: And we won’t stop. \\n\\nWe have lost so much to COVID-19. Time with one another. And worst of all, so much loss of life. \\n\\nLet’s use this moment to reset. Let’s stop looking at COVID-19 as a partisan dividing line and see it for what it is: A God-awful disease. \\n\\nLet’s stop seeing each other as enemies, and start seeing each other for who we really are: Fellow Americans. \\n\\nWe can’t change how divided we’ve been. But we can change how we move forward—on COVID-19 and other issues we must face together. \\n\\nI recently visited the New York City Police Department days after the funerals of Officer Wilbert Mora and his partner, Officer Jason Rivera. \\n\\nThey were responding to a 9-1-1 call when a man shot and killed them with a stolen gun. \\n\\nOfficer Mora was 27 years old. \\n\\nOfficer Rivera was 22. \\n\\nBoth Dominican Americans who’d grown up on the same streets they later chose to patrol as police officers. \\n\\nI spoke with their families and told them that we are forever in debt for their sacrifice, and we will carry on their mission to restore the trust and safety every community deserves.\\nSource: 24-pl\\nContent: And a proud Ukrainian people, who have known 30 years of independence, have repeatedly shown that they will not tolerate anyone who tries to take their country backwards. \\n\\nTo all Americans, I will be honest with you, as I’ve always promised. A Russian dictator, invading a foreign country, has costs around the world. \\n\\nAnd I’m taking robust action to make sure the pain of our sanctions is targeted at Russia’s economy. And I will use every tool at our disposal to protect American businesses and consumers. \\n\\nTonight, I can announce that the United States has worked with 30 other countries to release 60 Million barrels of oil from reserves around the world. \\n\\nAmerica will lead that effort, releasing 30 Million barrels from our own Strategic Petroleum Reserve. And we stand ready to do more if necessary, unified with our allies. \\n\\nThese steps will help blunt gas prices here at home. And I know the news about what’s happening can seem alarming. \\n\\nBut I want you to know that we are going to be okay.\\nSource: 5-pl\\nContent: More support for patients and families. \\n\\nTo get there, I call on Congress to fund ARPA-H, the Advanced Research Projects Agency for Health. \\n\\nIt’s based on DARPA—the Defense Department project that led to the Internet, GPS, and so much more. \\n\\nARPA-H will have a singular purpose—to drive breakthroughs in cancer, Alzheimer’s, diabetes, and more. \\n\\nA unity agenda for the nation. \\n\\nWe can do this. \\n\\nMy fellow Americans—tonight , we have gathered in a sacred space—the citadel of our democracy. \\n\\nIn this Capitol, generation after generation, Americans have debated great questions amid great strife, and have done great things. \\n\\nWe have fought for freedom, expanded liberty, defeated totalitarianism and terror. \\n\\nAnd built the strongest, freest, and most prosperous nation the world has ever known. \\n\\nNow is the hour. \\n\\nOur moment of responsibility. \\n\\nOur test of resolve and conscience, of history itself. \\n\\nIt is in this moment that our character is formed. Our purpose is found. Our future is forged. \\n\\nWell I know this nation.\\nSource: 34-pl\\n=========\\nFINAL ANSWER: The president did not mention Michael Jackson.\\nSOURCES:\\n\\nQUESTION: what is the price of Tiago iCNG?\\n=========\\nContent: The Tiago iCNG is priced between Rs 6.55 lakh and Rs 8.1 lakh.\\nSource: https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\\n\\nContent: Tata Motors on Friday launched the CNG variant of its micro SUV Punch priced between Rs 7.1 lakh and Rs 9.68 lakh (ex-showroom, Delhi).\\nSource: https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\\n\\nContent: Tata Motors launches Punch iCNG, price starts at Rs 7.1 lakh\\nSource: https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\\n\\nContent: None\\nSource: https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\\n=========\\nFINAL ANSWER:\"\n", " ]\n", "}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 9:chain:LLMChain > 10:llm:OpenAI] [2.88s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", " \"text\": \" The Tiago iCNG is priced between Rs 6.55 lakh and Rs 8.1 lakh.\\nSOURCES: https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\",\n", " \"generation_info\": {\n", " \"finish_reason\": \"stop\",\n", " \"logprobs\": null\n", " }\n", " }\n", " ]\n", " ],\n", " \"llm_output\": {\n", " \"token_usage\": {\n", " \"total_tokens\": 2093,\n", " \"prompt_tokens\": 1976,\n", " \"completion_tokens\": 117\n", " },\n", " \"model_name\": \"text-davinci-003\"\n", " },\n", " \"run\": null\n", "}\n", "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain > 9:chain:LLMChain] [2.88s] Exiting Chain run with output:\n", "\u001b[0m{\n", " \"text\": \" The Tiago iCNG is priced between Rs 6.55 lakh and Rs 8.1 lakh.\\nSOURCES: https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\"\n", "}\n", "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain > 3:chain:MapReduceDocumentsChain] [3.90s] Exiting Chain run with output:\n", "\u001b[0m{\n", " \"output_text\": \" The Tiago iCNG is priced between Rs 6.55 lakh and Rs 8.1 lakh.\\nSOURCES: https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\"\n", "}\n", "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:RetrievalQAWithSourcesChain] [4.09s] Exiting Chain run with output:\n", "\u001b[0m{\n", " \"answer\": \" The Tiago iCNG is priced between Rs 6.55 lakh and Rs 8.1 lakh.\\n\",\n", " \"sources\": \"https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html\"\n", "}\n" ] }, { "data": { "text/plain": [ "{'answer': ' The Tiago iCNG is priced between Rs 6.55 lakh and Rs 8.1 lakh.\\n',\n", " 'sources': 'https://www.moneycontrol.com/news/business/markets/wall-street-rises-as-tesla-soars-on-ai-optimism-11351111.htmlhttps://www.moneycontrol.com/news/business/tata-motors-launches-punch-icng-price-starts-at-rs-7-1-lakh-11098751.html'}" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query = \"what is the price of Tiago iCNG?\"\n", "# query = \"what are the main features of punch iCNG?\"\n", "\n", "langchain.debug=True\n", "\n", "chain({\"question\": query}, return_only_outputs=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11" } }, "nbformat": 4, "nbformat_minor": 5 }