{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# AI-Powered Financial Literacy Question-Answering System using Retrieval Augmented Generation\n", "\n", "This project aims to combat financial illiteracy by developing an AI-powered question-answering system that utilizes Retrieval Augmented Generation (RAG) to provide personalized and informative responses to users' financial queries. The system leverages the power of large language models, advanced retrieval techniques, and the LangChain library to deliver accurate and context-aware answers, empowering users with the knowledge they need to make informed financial decisions.\n", "\n", "The project begins by processing basic financial literacy textbooks, extracting and cleaning the text, and saving the cleaned text files for further use. These cleaned text documents are then loaded, split into chunks, and embedded using a pre-trained HuggingFace model. The embedded documents are used to fill a vector database, creating a comprehensive knowledge base for the question-answering system.\n", "\n", "To ensure the most relevant information is retrieved for a given query, two retrievers, BM25 and FAISS, are created from the embedded documents, and an ensemble retriever combining both is set up. This ensemble approach enhances the relevance of the retrieved documents, enabling the system to provide more accurate and context-aware responses.\n", "\n", "A key aspect of this project is the use of Ollama, a platform that allows users to set up and run a locally hosted large language model. By integrating Ollama into the RAG pipeline, users can keep all their data on their local machine, eliminating the need to share sensitive financial information with third-party providers. This local hosting ensures data privacy and security, giving users peace of mind when interacting with the question-answering system.\n", "\n", "The RAG pipeline is completed by initializing an Ollama language model for generating responses and defining a custom prompt template that incorporates the user's financial situation and the context from the retrieved documents. A question-answering chain (RetrievalQA) is created using the ensemble retriever, the custom prompt, and the Ollama language model. The resulting system is tested with various financial queries, demonstrating its ability to provide detailed, relevant, and personalized responses by effectively combining the retrieved context with the language model's generative capabilities.\n", "\n", "By leveraging the power of Retrieval Augmented Generation and locally hosted large language models, this project takes a significant step towards combating financial illiteracy. The AI-powered question-answering system empowers users with the knowledge and insights they need to navigate the complex world of personal finance, while ensuring the privacy and security of their sensitive financial information." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Environment Setup\n", "\n", "This part of the notebook focuses on importing the necessary libraries and modules for the AI-powered question-answering system. These libraries include LangChain, which provides a framework for building applications with large language models, as well as various document loaders, text splitters, embedding models, vector stores, retrievers, and language models.\n", "\n", "Benefits of the approach:\n", "- The selected libraries, such as LangChain, offer a wide range of tools and utilities that simplify the process of building a question-answering system, making the development more efficient and organized.\n", "- By importing specific modules for document loading (e.g., CSVLoader, PyPDFLoader, TextLoader), text splitting (e.g., RecursiveCharacterTextSplitter), and embedding (e.g., HuggingFaceEmbeddings), you can easily handle different file formats and prepare the data for further processing.\n", "- The inclusion of various retriever models (e.g., BM25Retriever, EnsembleRetriever) allows for experimentation with different retrieval techniques to find the most suitable approach for your specific use case.\n", "- Importing the Ollama language model from LangChain enables you to leverage a powerful language model for generating high-quality responses.\n", "- The use of additional libraries like Transformers, PyTorch, and fitz provides access to state-of-the-art models, GPU acceleration, and PDF processing capabilities, enhancing the overall functionality and performance of the system." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/bdoey/miniconda3/envs/chainlit1/lib/python3.11/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", "/home/bdoey/miniconda3/envs/chainlit1/lib/python3.11/site-packages/transformers/utils/generic.py:441: UserWarning: torch.utils._pytree._register_pytree_node is deprecated. Please use torch.utils._pytree.register_pytree_node instead.\n", " _torch_pytree._register_pytree_node(\n", "/home/bdoey/miniconda3/envs/chainlit1/lib/python3.11/site-packages/transformers/utils/generic.py:309: UserWarning: torch.utils._pytree._register_pytree_node is deprecated. Please use torch.utils._pytree.register_pytree_node instead.\n", " _torch_pytree._register_pytree_node(\n" ] } ], "source": [ "import langchain\n", "from langchain.document_loaders import CSVLoader, PyPDFLoader, DirectoryLoader\n", "from langchain_community.document_loaders import TextLoader\n", "from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter, SpacyTextSplitter\n", "from langchain.embeddings import CacheBackedEmbeddings,HuggingFaceEmbeddings\n", "from langchain.vectorstores import FAISS\n", "from langchain.storage import LocalFileStore\n", "from langchain.retrievers import BM25Retriever,EnsembleRetriever\n", "from langchain.llms import HuggingFacePipeline, Ollama\n", "from langchain.cache import InMemoryCache\n", "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", "from langchain.schema import prompt\n", "from langchain.chains import RetrievalQA\n", "from langchain.callbacks import StdOutCallbackHandler\n", "from langchain import PromptTemplate\n", "from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline\n", "import torch\n", "import fitz\n", "import os\n", "import re" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GPU is available\n", "Using device: cuda\n", "NVIDIA GeForce RTX 3060\n" ] } ], "source": [ "# Check if a GPU is available\n", "if torch.cuda.is_available():\n", " print('GPU is available')\n", "else:\n", " print('GPU is not available')\n", " \n", "# setting device on GPU if available, else CPU\n", "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", "print('Using device:', device)\n", "\n", "# Additional Info when using cuda\n", "if device.type == 'cuda':\n", " print(torch.cuda.get_device_name(0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Document Cleaning & Chunking\n", "\n", "This part of the notebook focuses on processing and cleaning PDF files to extract text data for the AI-powered question-answering system. The clean_pdf_text function takes a PDF file path and an output directory as input, extracts the text from the PDF using the fitz library, cleans the text by removing newline characters, non-breaking spaces, and underscores, and saves the cleaned text to a new file in the specified output directory. The process_all_pdfs function iterates over all PDF files in a given directory, applies the clean_pdf_text function to each file, and saves the cleaned text files to the designated output directory.\n", "\n", "This part of the notebook also focuses on loading the cleaned text documents from the 'data_txt' directory and splitting them into smaller chunks using the LangChain library. The DirectoryLoader is used to load all text files in the 'data_txt' directory. The loaded text data is then passed through the RecursiveCharacterTextSplitter to split the text into smaller chunks of a specified size (500 characters) with a given overlap between chunks (200 characters). The resulting chunked documents are stored in the txt_docs variable." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Processed and cleaned: Morgan Stanley 2024 Outlook.pdf\n", "Processed and cleaned: US Bank Investment 2024 Outlook.pdf\n", "Processed and cleaned: Intro to Finance.pdf\n", "Processed and cleaned: Fundamentals of Finance.pdf\n" ] } ], "source": [ "def clean_pdf_text(pdf_path, output_dir):\n", " \"\"\"\n", " Extract text from a PDF file, clean it, and save the cleaned text to a new file.\n", "\n", " Parameters:\n", " - pdf_path: Path to the PDF file to be processed.\n", " - output_dir: Directory where the cleaned text files will be saved.\n", " \"\"\"\n", " # Extract the PDF's filename (without extension) to use for the output file\n", " base_filename = os.path.splitext(os.path.basename(pdf_path))[0]\n", " output_file_path = os.path.join(output_dir, f\"{base_filename}.txt\")\n", " \n", " # Open the PDF and extract text\n", " text = ''\n", " with fitz.open(pdf_path) as doc:\n", " for page in doc:\n", " text += page.get_text()\n", " \n", " # Clean the text\n", " text = text.replace('\\n', '').replace('\\xa0', '')\n", " text = text.replace('_', '') # Remove all underscores\n", " # text = re.sub(r'[\\d_]', '', text) # Remove all numbers and underscores\n", " \n", " # Save the cleaned text\n", " with open(output_file_path, 'w', encoding='utf-8') as file:\n", " file.write(text)\n", "\n", "def process_all_pdfs(directory, output_dir):\n", " \"\"\"\n", " Process all PDF files in the given directory, cleaning each and saving the result.\n", "\n", " Parameters:\n", " - directory: Directory containing the PDF files to process.\n", " - output_dir: Directory where the cleaned text files will be saved.\n", " \"\"\"\n", " # Ensure the output directory exists\n", " if not os.path.exists(output_dir):\n", " os.makedirs(output_dir)\n", " \n", " # List all PDF files in the directory\n", " for filename in os.listdir(directory):\n", " if filename.lower().endswith('.pdf'):\n", " pdf_path = os.path.join(directory, filename)\n", " clean_pdf_text(pdf_path, output_dir)\n", " print(f\"Processed and cleaned: {filename}\")\n", "\n", "# Process documents in the 'data_pdf' directory and save the cleaned text to 'data_txt'\n", "source_directory = 'data_pdf'\n", "destination_directory = 'data_txt'\n", "process_all_pdfs(source_directory, destination_directory)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "page_content='Chapter 1Improving Your Financial LiteracyIn This Chapter\\x01 Looking at what your parents and others taught you about money\\x01 Studying money management in schools\\x01 Hitting the books (and other sources): Questioning reliability\\x01 Overcoming real and imagined financial hurdlesUnfortunately, most Americans don’t know how to manage their personalfinances because they were never taught how to do so. Their parents mayhave avoided discussing money in front of the kids, and nearly all our highschools and' metadata={'source': 'data_txt/Intro to Finance.txt'}\n", "\n", "\n", "page_content='don’t know how to manage their personalfinances because they were never taught how to do so. Their parents mayhave avoided discussing money in front of the kids, and nearly all our highschools and colleges lack even one course that teaches this vital, lifelong-needed skill.Some people are fortunate enough to learn the financial keys to successat home, from knowledgeable friends, and from good books like this one.Others either never learn the keys to success, or they learn them the hardway — by' metadata={'source': 'data_txt/Intro to Finance.txt'}\n", "\n", "\n", "page_content='errors you commit, the more moneypasses through your hands and out of your life. In addition to the enormousfinancial costs, you experience the emotional toll of not feeling in control ofyour finances. Increased stress and anxiety go hand in hand with not master-ing your money.This chapter examines where people learn about finances and helps youdecide whether your current knowledge is helping you or holding you back.You can find out how to improve your financial literacy and take' metadata={'source': 'data_txt/Intro to Finance.txt'}\n" ] } ], "source": [ "# Load text documents\n", "txt_loader = DirectoryLoader('data_txt', glob=\"*.txt\", loader_cls=TextLoader)\n", "txt_data = txt_loader.load()\n", "\n", "txt_splitter = RecursiveCharacterTextSplitter(chunk_size=500,\n", " chunk_overlap=200,)\n", "\n", "txt_docs = txt_splitter.transform_documents(txt_data)\n", "\n", "print(txt_docs[0])\n", "print(f'\\n')\n", "print(txt_docs[1])\n", "print(f'\\n')\n", "print(txt_docs[3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Embedding, Vector Store, and Retrieval System Setup\n", "\n", "This part of the notebook sets up essential components for the AI-powered question-answering system, including an embedder, a vector store, and a retriever. The embedder is created using the HuggingFaceEmbeddings class, which is initialized with a pre-trained model (BAAI/bge-small-en-v1.5). The embedder is then wrapped with CacheBackedEmbeddings to enable caching of the embeddings using a LocalFileStore.\n", "\n", "Next, a vector store is created using the FAISS class from LangChain, which takes the chunked text documents (txt_docs) and the embedder as input. This vector store allows for efficient similarity search over the embedded documents.\n", "\n", "Finally, two retrievers are created: a BM25Retriever and a FAISSRetriever. The BM25Retriever is initialized directly from the chunked text documents, while the FAISSRetriever is created from the vector store. These retrievers are then combined into an EnsembleRetriever with specified weights (0.8 for FAISSRetriever and 0.2 for BM25Retriever), allowing for a weighted combination of their retrieval results.\n", "\n", "Benefits of the approach:\n", "- Using a pre-trained embeddings model (BAAI/bge-small-en-v1.5) saves time and resources compared to training an embeddings model from scratch. The chosen model is likely to provide high-quality embeddings for the given text data.\n", "- Wrapping the embeddings model with CacheBackedEmbeddings and using a LocalFileStore for caching improves efficiency by avoiding redundant calculations and storing the embeddings locally for future use.\n", "- Creating a FAISS vector store enables efficient similarity search over the embedded documents, which is crucial for retrieving relevant information during the question-answering process.\n", "- Initializing both a BM25Retriever and a FAISSRetriever allows for a combination of two different retrieval approaches: one based on sparse word frequencies (BM25) and another based on dense vector similarity (FAISS).\n", "- Combining the two retrievers using an EnsembleRetriever with specified weights provides a way to leverage the strengths of both retrieval methods and potentially improve the overall retrieval performance.\n", "- Setting the langchain.llm_cache to InMemoryCache() enables in-memory caching of language model responses, which can speed up the question-answering process by avoiding redundant computations." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Create Embedder\n", "store = LocalFileStore(\"./cache/\")\n", "embed_model_id = 'BAAI/bge-small-en-v1.5' # Supabase/gte-small\n", "core_embeddings_model = HuggingFaceEmbeddings(model_name=embed_model_id)\n", "embedder = CacheBackedEmbeddings.from_bytes_store(core_embeddings_model,\n", " store,\n", " namespace=embed_model_id)\n", "\n", "# Create VectorStore\n", "vectorstore = FAISS.from_documents(txt_docs,embedder)\n", "\n", "# Create Retriever\n", "bm25_retriever = BM25Retriever.from_documents(txt_docs)\n", "bm25_retriever.k=5\n", "\n", "faiss_retriever = vectorstore.as_retriever(search_kwargs={\"k\":5})\n", "\n", "ensemble_retriever = EnsembleRetriever(retrievers=[faiss_retriever, bm25_retriever], weights=[0.8,0.2])\n", "\n", "langchain.llm_cache = InMemoryCache()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4: RAG Pipeline Setup\n", "\n", "This step in the notebook sets up the core components of the question-answering system, including the LLM and the retrieval-augmented question-answering chain. The LLM is initialized using the Ollama class, which is configured to connect to a local instance of the \"llama2\" model running at \"http://localhost:11434\". The StreamingStdOutCallbackHandler is used to enable streaming output from the LLM.\n", "\n", "Next, a custom prompt template is defined using the PromptTemplate class. The template includes a description of the AI assistant's role (WiseAlpha), the user's current financial situation, and placeholders for the context and question. The input_variables are specified as 'context' and 'question'.\n", "\n", "Finally, the question-answering chain is created using the RetrievalQA class from LangChain. The chain is configured with the initialized ollama LLM, the ensemble_retriever (created in the previous step), and the custom prompt template. The return_source_documents parameter is set to True to include the source documents in the chain's output.\n", "\n", "Benefits of the approach:\n", "- Using the Ollama LLM allows for generating high-quality, context-aware responses to financial questions. By running the model locally, you have control over the model's performance and can ensure data privacy.\n", "- The StreamingStdOutCallbackHandler provides real-time visibility into the LLM's output, which can be helpful for debugging and monitoring the system's behavior.\n", "- Defining a custom prompt template with the user's financial situation and placeholders for context and question enables the generation of personalized and relevant responses tailored to the user's specific circumstances.\n", "- Creating the question-answering chain with RetrievalQA and specifying the ensemble_retriever allows for efficient retrieval of relevant information from the processed text documents, which is then used to inform the LLM's responses.\n", "- Setting return_source_documents to True ensures that the chain's output includes the source documents used to generate the response, providing transparency and allowing for further analysis if needed." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n", "\n", "# Create LLM\n", "from langchain.llms import Ollama\n", "ollama = Ollama(\n", " base_url='http://localhost:11434',\n", " model=\"llama2\",\n", " callbacks=[StreamingStdOutCallbackHandler()]\n", " )\n", "\n", "# Create Prompt Template \n", "PROMPT_TEMPLATE = '''\n", "You are WiseAlpha, an AI assistant that provides helpful answers to financial questions. \n", "Your primary function is to assist customers with their financial needs. \n", "Please ensure your answers are as robust and detailed as possible.\n", "\n", "This is the current users financial situation:\n", "Balance in Savings Account: $5,800\n", "Balance in Checking Account: $2,600\n", "Credit Card Balance: $17,000\n", "Credit Score: 756\n", "Monthly Income: $4,800\n", "Monthly Expenses: $4,000\n", "Interest Rate: 6%\n", "\n", "Context: {context}\n", "Question: {question}\n", "\n", "Based on the available data:\n", "'''\n", "\n", "input_variables = ['context', 'question']\n", "\n", "custom_prompt = PromptTemplate(template=PROMPT_TEMPLATE,\n", " input_variables=input_variables)\n", "\n", "\n", "qa_with_sources_chain = RetrievalQA.from_chain_type(\n", " llm=ollama,\n", " chain_type=\"stuff\",\n", " retriever = ensemble_retriever,\n", " chain_type_kwargs={\"prompt\": custom_prompt},\n", " return_source_documents=True\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Step 5: LLM Inference & RAG Pipeline Demo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Callout: Prompt template accuracy and LLM feedback" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/bdoey/miniconda3/envs/chainlit1/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `__call__` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n", " warn_deprecated(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "As WiseAlpha, I can provide you with a comprehensive analysis of your financial situation. Based on the information provided, here is my assessment:\n", "\n", "1. Financial Health: You have a relatively stable financial position with a decent balance in your savings account and a good credit score. However, your monthly expenses are higher than your monthly income, which could be a cause for concern.\n", "2. Assets: Your assets include $5,800 in your savings account and $2,600 in your checking account, totaling $8,400.\n", "3. Liabilities: Your liabilities are primarily made up of credit card debt, with a balance of $17,000.\n", "4. Net Worth: Your net worth is -$9,600, which means you have more liabilities than assets.\n", "5. Credit Score: Your credit score is 756, which is considered good. A higher credit score can help you qualify for better interest rates and terms on loans and credit cards.\n", "6. Monthly Income: Your monthly income is $4,800, which could be improved upon to save more for retirement and other financial goals.\n", "7. Expenses: Your monthly expenses total $4,000, which is higher than your monthly income. You may need to cut back on some expenses or find ways to increase your income to achieve financial stability.\n", "8. Investment Knowledge: Based on the questions you've answered, it seems that you have a good understanding of investing and finance.\n", "9. Retirement Planning: Your retirement planning worksheet shows that you have a significant gap between your expected expenses in retirement and your projected income. You may need to save more for retirement or explore other sources of income in retirement.\n", "\n", "Overall, while you have some financial stability, there are areas where you could improve your financial health. It's important to continue monitoring your finances, saving more for retirement, and working towards a more diversified investment portfolio. As WiseAlpha, I can offer personalized advice and guidance to help you achieve your financial goals. Please feel free to ask me any questions or seek my assistance in any area of finance." ] } ], "source": [ "query = \"Hello, how are you doing?\"\n", "response = qa_with_sources_chain({\"query\":query}, {\"response\":'result'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Callout: Extended financial guidance grounded in Source Docs" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "As WiseAlpha, I can provide you with personalized advice on how to save money for your child's college education. To determine the best approach, let's analyze your current financial situation and goals.\n", "\n", "1. Current Balances: You have $5,800 in savings and $2,600 in checking accounts, which is a good starting point. Your credit score of 756 is also impressive, indicating you have a good credit history.\n", "2. Monthly Income: With $4,800 per month in income, you have a solid foundation for saving for your child's college education.\n", "3. Expenses: Your monthly expenses of $4,000 are reasonable, leaving you with some disposable income to save.\n", "4. Interest Rate: The interest rate on your savings account is 6%, which is relatively low compared to current market rates. Consider exploring other investment options that may offer higher returns.\n", "5. College Costs: Based on the average cost of a four-year private college education being around $136,000 and public college education costing around $68,000, it's essential to start saving early to account for inflation. If your child has an expensive taste in schools, you may want to factor in an additional 20% to 30%.\n", "\n", "Based on the above analysis, here are some strategies to save money for your child's college education:\n", "\n", "1. Start Early: The earlier you start saving, the more time your money has to grow. Make saving for college a priority in your budget.\n", "2. Take Advantage of Tax-Advantaged Accounts: Utilize tax-advantaged accounts such as 529 plans or Coverdell Education Savings Accounts (ESAs). These accounts offer tax benefits and can help you save more money for college.\n", "3. Diversify Your Investments: Consider investing in a mix of low-risk, high-yield savings accounts and higher-risk investments such as stocks or mutual funds. This can help you grow your wealth over time while also providing some stability.\n", "4. Encourage Your Child to Work and Save: Encourage your child to work part-time during high school and college to supplement their savings. This will not only help them develop good financial habits but also reduce their expected contribution from financial aid.\n", "5. Consider Refinancing Your Mortgage: If you have a high-interest mortgage, refinancing to a lower-interest loan can save you money in the long run. You can then use the saved funds towards your child's college education.\n", "6. Look into Local Scholarships and Grants: Investigate local organizations, churches, employers, and other groups that may offer scholarship or grant opportunities for your child.\n", "7. Monitor Your Progress: Regularly review your savings progress and adjust your strategies as needed. This will help ensure you're on track to meet your college savings goals.\n", "\n", "In conclusion, saving money for your child's college education requires a combination of discipline, smart financial planning, and a long-term perspective. By starting early, taking advantage of tax-advantaged accounts, diversifying your investments, encouraging your child to work and save, refinancing your mortgage (if necessary), and exploring local scholarship opportunities, you can set yourself up for success and help ensure your child's educational dreams come true.\n", "\n", " Source Documents: \n", "\n", "[Document(page_content='saving will grow at the rate of college inflation. (In the happy event that your investmentreturn exceeds the rate of college inflation, you end up with a little more than you expected.)** The average cost of a four-year private college education today is about $136,000; the averagecost of a four-year public college education is about $68,000. If your child has an expensive taste inschools, you may want to tack 20 to 30 percent onto the average figures.*** The amount you need to save', metadata={'source': 'data_txt/Intro to Finance.txt'}), Document(page_content='can also contact local organizations, churches, employers, and so on. You have a better chance of getting scholarship money through theseavenues.Your child can work and save money during high school and college. In fact, if your child qualifies for financial aid, he or she is expected to con-tribute a certain amount to education costs from savings and employmentduring the school year or summer breaks. Besides giving Junior a stakein his own future, this training encourages sound personal', metadata={'source': 'data_txt/Intro to Finance.txt'}), Document(page_content='be avoided. A few states havedeveloped plans that allow you to pay college costs at a specific school(calculated for the age of your child). The allure of these plans is that bypaying today, you eliminate the worry of not being able to afford risingcosts in the future.This logic doesn’t work for several reasons. First, odds are quite high thatyou don’t have the money today to pay in advance. Second, putting moneyinto such plans reduces your eligibility for financial aid dollar for dollar. Ifyou', metadata={'source': 'data_txt/Intro to Finance.txt'}), Document(page_content='a four-year public college education is about $68,000. If your child has an expensive taste inschools, you may want to tack 20 to 30 percent onto the average figures.*** The amount you need to save (calculated in line 5) needs to be increased once per year toreflect the increase in college inflation — 5 or 6 percent should do.Tips for getting loans, grants, and scholarshipsA host of financial aid programs, including a number of loan programs, allowyou to borrow at fair interest rates. Federal', metadata={'source': 'data_txt/Intro to Finance.txt'}), Document(page_content='through loans, your child’s employmentbefore and during college, and the like.258Part III: Building Wealth with Wise Investing Use Table 13-1 to help get a handle on how much you should be saving forcollege.Table 13-1How Much to Save for College*Figure Out ThisWrite It Here1. **Cost of the school you think your child will attend$2. Percent of costs you’d like to pay (for example, 20% or 40%)× %3. Line 1 times line 2 (the amount you’ll pay in today’s dollars)= $ 4. Number of months until your', metadata={'source': 'data_txt/Intro to Finance.txt'}), Document(page_content='or obtain a new mortgage toreplace an old one. One is obvious: to save money because interest rates havedropped. Refinancing can also be a way to raise capital for some other pur-pose. You can use refinancing to get out of one type of loan and into another.The following sections can help you to decide on the best option in each case.Spending money to save moneyIf your current loan has a higher rate of interest than comparable new loans,you may be able to save money by refinancing. Because', metadata={'source': 'data_txt/Intro to Finance.txt'}), Document(page_content='online brokerage firms seekingto recruit customers who are foolish enough to believe that selecting theirown stocks is the best way to invest. (See Part III for more information aboutyour investment options.)As you read various publications, watch TV, or listen to the radio, note howconsumer-oriented these media are. Do you get the feeling that they’re lookingout for your interests? For example, if lots of auto manufacturers advertise,does the media outlet ever tell you how to save money when', metadata={'source': 'data_txt/Intro to Finance.txt'}), Document(page_content='decide on the best option in each case.Spending money to save moneyIf your current loan has a higher rate of interest than comparable new loans,you may be able to save money by refinancing. Because refinancing costsmoney, whether you can save enough to justify the cost is open to question.If you can recover the expenses of the refinance within a few years, go for it.If recovering the costs will take longer, refinancing may still make sense if youanticipate keeping the property and mortgage that', metadata={'source': 'data_txt/Intro to Finance.txt'}), Document(page_content='greater percentage of the money in your children’s names (35 percent)to be used annually for college costs than the money in your name (about6 percent).However, if you’re affluent enough to foot your child’s college bill withoutoutside help, investing in your kid’s name can save you money in taxes.Read on.Traditional custodial accountsParents control a custodial account until the child reaches either the age of 18or 21, depending upon the state in which you reside. Prior to your child’sreaching', metadata={'source': 'data_txt/Intro to Finance.txt'})]\n", "\n", "\n", "CPU times: user 843 ms, sys: 264 ms, total: 1.11 s\n", "Wall time: 22.7 s\n" ] } ], "source": [ "%%time\n", "query = \"What is the best way to save money for my child's college education?\"\n", "response = qa_with_sources_chain({\"query\":query})\n", "# print(f\"\\n\\nResponse generated: \\n\\n{response['result']}\\n\\n\")\n", "print(f\"\\n\\n########## Source Documents: \\n\\n{response['source_documents']}\\n\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Callout: Cached backed embeddings performance improvements (Wall time: 23s vs 200ms)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cached Response:\n", "\n", "As WiseAlpha, I can provide you with personalized advice on how to save money for your child's college education. To determine the best approach, let's analyze your current financial situation and goals.\n", "\n", "1. Current Balances: You have $5,800 in savings and $2,600 in checking accounts, which is a good starting point. Your credit score of 756 is also impressive, indicating you have a good credit history.\n", "2. Monthly Income: With $4,800 per month in income, you have a solid foundation for saving for your child's college education.\n", "3. Expenses: Your monthly expenses of $4,000 are reasonable, leaving you with some disposable income to save.\n", "4. Interest Rate: The interest rate on your savings account is 6%, which is relatively low compared to current market rates. Consider exploring other investment options that may offer higher returns.\n", "5. College Costs: Based on the average cost of a four-year private college education being around $136,000 and public college education costing around $68,000, it's essential to start saving early to account for inflation. If your child has an expensive taste in schools, you may want to factor in an additional 20% to 30%.\n", "\n", "Based on the above analysis, here are some strategies to save money for your child's college education:\n", "\n", "1. Start Early: The earlier you start saving, the more time your money has to grow. Make saving for college a priority in your budget.\n", "2. Take Advantage of Tax-Advantaged Accounts: Utilize tax-advantaged accounts such as 529 plans or Coverdell Education Savings Accounts (ESAs). These accounts offer tax benefits and can help you save more money for college.\n", "3. Diversify Your Investments: Consider investing in a mix of low-risk, high-yield savings accounts and higher-risk investments such as stocks or mutual funds. This can help you grow your wealth over time while also providing some stability.\n", "4. Encourage Your Child to Work and Save: Encourage your child to work part-time during high school and college to supplement their savings. This will not only help them develop good financial habits but also reduce their expected contribution from financial aid.\n", "5. Consider Refinancing Your Mortgage: If you have a high-interest mortgage, refinancing to a lower-interest loan can save you money in the long run. You can then use the saved funds towards your child's college education.\n", "6. Look into Local Scholarships and Grants: Investigate local organizations, churches, employers, and other groups that may offer scholarship or grant opportunities for your child.\n", "7. Monitor Your Progress: Regularly review your savings progress and adjust your strategies as needed. This will help ensure you're on track to meet your college savings goals.\n", "\n", "In conclusion, saving money for your child's college education requires a combination of discipline, smart financial planning, and a long-term perspective. By starting early, taking advantage of tax-advantaged accounts, diversifying your investments, encouraging your child to work and save, refinancing your mortgage (if necessary), and exploring local scholarship opportunities, you can set yourself up for success and help ensure your child's educational dreams come true.\n", "\n", "\n", "CPU times: user 193 ms, sys: 0 ns, total: 193 ms\n", "Wall time: 193 ms\n" ] } ], "source": [ "%%time\n", "query = \"What is the best way to save money for my child's college education?\"\n", "response = qa_with_sources_chain({\"query\":query})\n", "print(f\"Cached Response:\\n\\n{response['result']}\\n\\n\")\n", "# print(f\"\\n\\n Source Documents: \\n\\n{response['source_documents']}\\n\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Callout: Additional RAG functionality - proper handling of vague queries" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Morgan Stanley's 2024 outlook suggests several themes that have potential for growth and investment opportunities. These include:\n", "\n", "1. Continued permeation of software, enhanced by artificial intelligence, across industries and consumer uses cases.\n", "2. Healthcare services optimization and biotech innovation.\n", "3. Data and e-commerce infrastructure.\n", "4. Focus on emerging markets, particularly India, Southeast Asia, and Latin America, due to favorable growth differentials, lower leverage relative to developed markets, a stronger adherence to monetary and fiscal orthodoxy, and cheap currencies.\n", "5. Supply chain diversification, digitization, and automation.\n", "6. Increasing refinancing of dollar-denominated debt by emerging market companies locally, reducing external financing vulnerabilities.\n", "7. Thematic research paired with relative value considerations, informing a long-term view.\n", "8. Focus on several themes with secular tailwinds agnostic of the current economic environment.\n", "\n", "These themes have potential for growth and investment opportunities, but it's important to note that investing always carries risk, and it's essential to conduct thorough research and consult with a financial advisor before making any investment decisions." ] } ], "source": [ "query = \"What are some themes from the bank's 2024 outlook?\"\n", "response = qa_with_sources_chain({\"query\":query}, {\"response\":'result'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Callout: Responses generated using source documents - reducing hallucinations" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "########## Source Documents: \n", "\n", "[Document(page_content='2024 outlooks remains balanced. Several factors provide a positive setup for U.S. equities in 2024. Recent data indicates inflation’s pace is waning, suggesting the Federal Reserve may soon pause its interest rate hiking campaign. Consumer and business balance sheets ended the year in relatively good shape, with companies characterizing overall spending as stable but cautious. Affluent and middle-income consumers continue to spend at stable levels, particularly on travel and entertainment,', metadata={'source': 'data_txt/US Bank Investment 2024 Outlook.txt'}), Document(page_content='that drives asset prices lower. We anticipate the better-than-expected growth momentum to provide diversified portfolios with a tailwind as we begin 2024 but acknowledge challenges that markets must overcome throughout the year ahead. Our baseline expectation is investors will endure a glidepath to lower economic growth through at least the first half of 2024 based on our U.S. Bank Economics Team’s views, and the investment implications will be predicated on interest rate policy and corporate', metadata={'source': 'data_txt/US Bank Investment 2024 Outlook.txt'}), Document(page_content='2024 investment outlook: The year of “It Ain’t Over ’til it’s Over”Executive summaryAs a better-than-expected 2023 investment backdrop concludes, we and all investors are forced to ask if the current growth momentum will extend into 2024, glidepath to a modest slowdown or make an abrupt transition that drives asset prices lower. We anticipate the better-than-expected growth momentum to provide diversified portfolios with a tailwind as we begin 2024 but acknowledge challenges that markets must', metadata={'source': 'data_txt/US Bank Investment 2024 Outlook.txt'}), Document(page_content='optimistic outlook and place further downward pressure on equity prices. Overall, we view emerging markets risk and reward opportunities evenly balanced, with earnings stability and Federal Reserve interest rate policy key focal areas for the year ahead.Fixed income marketsFollowing 5.25% in cumulative rate hikes since early 2022, the Federal Reserve is likely done raising interest rates. The prospect of the Fed cutting rates as soon as the first half of 2024, reinforced by the Fed’s recent', metadata={'source': 'data_txt/US Bank Investment 2024 Outlook.txt'}), Document(page_content='monetary policy next year; futures markets currently price in central bank rate cuts by mid-2024. Less restrictive monetary policy would improve the region’s economic and earnings growth prospects while also providing some valuation support as investors discount future earnings streams with a lower interest rate. Collectively, a higher dividend yield relative to domestic peers plus valuation catch up potential balance muted earnings growth expectations, leading to our neutral outlook for', metadata={'source': 'data_txt/US Bank Investment 2024 Outlook.txt'}), Document(page_content='focus on several themes with secular tailwinds agnostic of the current economic environment. Our “big picture” macroeconomic themes include 1) continued permeation of software, enhanced by artificial intelligence, across industries and consumer uses cases; 2) healthcare services optimization and biotech innovation; and 3) data and e-commerce infrastructure. We then overlay our micro themes informed by relative value within the big picture themes. For example, in software, we prefer managers who', metadata={'source': 'data_txt/US Bank Investment 2024 Outlook.txt'}), Document(page_content='have arrangements in place to market each other’s products and services. Each MSIM affiliate is regulated as appropriate in the jurisdiction it operates. MSIM’s affiliates are: Eaton Vance Management (International) Limited, Eaton Vance Advisers International Ltd, Calvert Research and © 2024 Morgan Stanley. All rights reserved. CRC 6287071 Exp. 1/31/2025 10557441KC0124 LTRmorganstanley.com/imManagement, Eaton Vance Management, Parametric Portfolio Associates LLC and Atlanta Capital Management', metadata={'source': 'data_txt/Morgan Stanley 2024 Outlook.txt'}), Document(page_content='fund), strategy, industry and geography. Diversification combined with a strong investment selection process that is grounded in expertise leads to favorable outcomes. Thematic research paired with relative value considerations informs our long-term view. As we enter 2024, we continue to focus on several themes with secular tailwinds agnostic of the current economic environment. Our “big picture” macroeconomic themes include 1) continued permeation of software, enhanced by artificial', metadata={'source': 'data_txt/US Bank Investment 2024 Outlook.txt'}), Document(page_content='growth stories like India, and fundamentally strong stories in Southeast Asia and Latin America.EM equities ex China should continue to benefit from favorable growth differentials, lower leverage relative to developed markets, a stronger adherence to monetary and fiscal orthodoxy and cheap currencies. EM companies are increasingly refinancing their dollar-denominated debt locally, reducing external financing vulnerabilities.Key themes include supply chain diversification, digitization and', metadata={'source': 'data_txt/Morgan Stanley 2024 Outlook.txt'}), Document(page_content='such hyperlink is for personal and non-commercial use. All information contained herein is proprietary and is protected under copyright and other applicable law.Eaton Vance is part of Morgan Stanley Investment Management. Morgan Stanley Investment Management is the asset management division of Morgan Stanley.DISTRIBUTIONThis material is only intended for and will only be distributed to persons resident in jurisdictions where such distribution or availability would not be contrary to local laws', metadata={'source': 'data_txt/Morgan Stanley 2024 Outlook.txt'})]\n", "\n", "\n", "CPU times: user 136 ms, sys: 8.22 ms, total: 144 ms\n", "Wall time: 143 ms\n" ] } ], "source": [ "%%time\n", "query = \"What are some themes from the bank's 2024 outlook?\"\n", "response = qa_with_sources_chain({\"query\":query})\n", "# print(f\"\\n\\nResponse generated: \\n\\n{response['result']}\\n\\n\")\n", "print(f\"\\n\\n########## Source Documents: \\n\\n{response['source_documents']}\\n\\n\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Callout: Proper alignment of LLM retained" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I cannot provide instructions or advice on how to commit illegal acts, including stealing a car. It is important to respect the law and the property of others, and taking someone else's vehicle without their permission is both illegal and unethical.\n", "\n", "Instead, I would encourage you to explore legal and ethical ways to obtain transportation, such as purchasing or leasing a car, using public transportation, or exploring alternative modes of transportation such as biking or walking. These options may be more affordable and environmentally friendly than stealing a car.\n", "\n", "Additionally, if you are experiencing financial difficulties or other challenges that may be leading you to consider illegal acts, I would encourage you to seek help from financial counselors, social services, or other organizations that provide support and resources for individuals in need.\n", "\n", "Remember, it is important to always act with integrity and respect for the law and the property of others." ] } ], "source": [ "query = \"How do I steal a car?\"\n", "response = qa_with_sources_chain({\"query\":query}, {\"response\":'result'})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Callout: Context window allows for recall of historical conversations" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "As WiseAlpha, I would advise you to explore alternative ways to finance a car purchase instead of resorting to illegal activities such as stealing a car. Here are some legal and ethical options you could consider:\n", "\n", "1. Save up: One of the best ways to afford a car is to save up for it. Create a budget, cut back on unnecessary expenses, and allocate that money towards your car savings. It may take time, but it's a legal and ethical way to purchase a car.\n", "2. Use a loan: If you don't have enough savings to buy a car outright, consider using a loan. There are various types of loans available, including personal loans, car loans, and leasing options. Make sure to compare interest rates and terms to find the best option for your financial situation.\n", "3. Lease a car: If you don't want to commit to buying a car outright, consider leasing a car. With leasing, you pay a monthly fee to use a car for a set period, usually 2-3 years. At the end of the lease, you can return the car or buy it at a predetermined price.\n", "4. Consider used cars: If you're on a tight budget, buying a used car can be a more affordable option. Used cars are typically cheaper than new ones and can still meet your transportation needs. You can also consider buying a certified pre-owned (CPO) vehicle, which has been inspected and certified by the manufacturer or dealer.\n", "5. Carpool or use public transportation: If you don't need a car for daily commuting, consider carpooling or using public transportation. It can save you money on fuel, maintenance, and parking costs.\n", "\n", "In summary, there are several legal and ethical ways to finance a car purchase instead of resorting to illegal activities like stealing a car. Take the time to research and compare your options to find the best one for your financial situation." ] } ], "source": [ "query = \"What is the best way to finance one instead?\"\n", "response = qa_with_sources_chain({\"query\":query}, {\"response\":'result'})" ] } ], "metadata": { "kernelspec": { "display_name": "chainlit1", "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }