Skip to main content

Building a RAG System with Gemini LLM for Financial forecasting

In this article, we will be building a RAG system with Gemini LLM for financial forecasting along with Weights & Biases integration.
Created on May 20|Last edited on July 23


Financial forecasting plays a key role in every business. It is a process of analyzing historical data, market trends, and other influencing factors to predict future financial performance. With effective forecasting, you can pave the way for enhanced financial outcomes, stabilized cash flow, and improved access to the necessary credit and investments that propel business growth. By embracing robust forecasting techniques, companies can better navigate the complexities of economic fluctuation and position themselves for sustained success.
Now, let’s say you want your LLM to make financial predictions. The best way to do that is to integrate a RAG system, which when combined with an LLM's generative capabilities, can help the system produce more accurate and precise predictions. A great LLM to use within a RAG system would be Gemini LLM as it can be fine-tuned or adapted to specific financial forecasting tasks, providing high-quality base capabilities for generating responses based on retrieved information.
To complete the package, you can also use Weights & Biases to help you track different experiments where Gemini LLM is used within a RAG system, documenting everything from model parameters to performance metrics. This is crucial for optimizing the RAG system’s configuration for specific types of financial predictions.

Understanding RAG Systems

Image By Author
To put it simply, a RAG (Retriever-Augmented Generation) is a technique that enhances the reliability and accuracy of a “generator” (LLM) by adding to it an additional component called the “retriever”. The “retriever” searches a large database of documents or data to find information that is relevant to the input query. It acts like a search engine, pulling in the most pertinent facts or figures based on the question it receives.
Then based on the information retrieved in the first step, the “generator” constructs an answer or output. It uses the context provided by the retriever to generate coherent and contextually appropriate responses.
Overall, a RAG system combines the capabilities of information retrieval and text generation to create answers that are not only relevant but also informed by up-to-date and specific data. This makes RAG systems particularly useful for applications requiring detailed and accurate information, such as answering complex questions, providing explanations, or making predictions based on a wide range of data.
So, when it comes to financial forecasting, the retriever component of a RAG system can access a wide array of external financial data, documents, and reports, pulling in the most relevant and up-to-date information related to the financial prediction query. This means the LLM is not just relying on the data it was trained on, which might be outdated or limited, but is also utilizing current market data, trends, and analyses. Furthermore, by using a broad and diverse set of data sources, RAG systems can help reduce the biases inherent in any single source or dataset, potentially decreasing the error rate in financial predictions.
Let’s take market trends analysis for example, RAG systems can analyze vast amounts of financial news, market reports, and real-time data feeds to identify and predict market trends. By retrieving the most relevant information and generating comprehensive insights, these systems can help analysts understand how different factors might influence the market.

Key Components: Gemini LLM and W&B

Google’s Gemini marks a major advancement in AI technology, demonstrating Google's dedication to pushing the boundaries of Artificial Intelligence. According to Google’s blog about the model, Gemini has gone through extensive testing and showed outstanding performance on a wide range of tasks such as understanding natural images, audio, and video, as well as mathematical reasoning. So, this means that it could quite possibly excel in understanding complex financial terminology and context, enabling it to accurately interpret financial documents, reports, and news.
Additionally, Gemini is capable of handling vast amounts of data, allowing it to scale to meet the needs of large financial institutions or detailed analyses. Another important feature to mention is its ability to process real-time data, which can be crucial for timely financial forecasting and decision-making.
Next, we have Weights & Biases, which offers tools to track and visualize model performance over time, thus aiding in the identification of trends and pinpointing areas of improvement. It also streamlines experiment management, allowing users to run and compare multiple model configurations and understand the best setups for specific financial forecasting tasks. So, by monitoring ongoing performance and facilitating iterative testing, W&B ensures that RAG systems remain at the cutting edge, continuously improving in accuracy and efficiency.
Overall, the combination of Gemini LLM's advanced language capabilities and the robust monitoring and management tools provided by W&B creates a powerful collaboration that enhances RAG systems, particularly in complex and data-intensive fields like financial forecasting.

Building a RAG System for Financial Forecasting

In this section, we will building a RAG system using Gemini and S&P 500 stock data
dataset along with implementing W&B supervision. We will also be using the FAISS vector database which is the vector database that will store our vectorized data.
Here’s how the dataset given to the model looks like.


Step 1: Installing the Necessary Packages

!pip install git+https://github.com/LucknowAI/Lucknow-LLM
!pip install numpy pandas scikit-learn
!pip install langchain tiktoken faiss-cpu transformers pandas torch openai

Step 2: Importing Libraries and Initialize Models

import os
import pandas as pd
import numpy as np
import wandb
from lucknowllm import GeminiModel
import faiss
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import FAISS
from langchain.prompts import PromptTemplate
“os”, “pandas”, and “numpy” are standard Python libraries for file handling, data manipulation, and numerical computations. “wandb” library is for logging experiments to Weights & Biases. “from lucknowllm import GeminiModel” is importing the Gemini model from Lucknow LLM.
“faiss” is a library for efficient similarity search and clustering of dense vectors. “OpenAIEmbeddings”, “FAISS”, and “PromptTemplate” are classes from the LangChain library for working with embeddings, vector stores, and prompt templates.

Step 3: Initializing the API Keys

Here, we initialize the API keys for the Gemini and OpenAI models:
# Initialize the Gemini model (replace 'YOUR_GEMINI_API_KEY' with your actual Gemini API key)
gemini_api_key = "YOUR_GEMINI_API_KEY"
Gemini = GeminiModel(api_key=gemini_api_key, model_name="gemini-1.0-pro")

# Initialize the OpenAI API key
openai_api_key = "YOUR_OPENAI_API_KEY"
Logging in to Weights & Biases
wandb.login()
Initializing a new W&B run
wandb.init(project="rag_with_gemini")

Step 4: Defining the Helper Functions

Loading and preprocessing CSV file
def load_and_process_csv(file_path):
data = pd.read_csv(file_path)
data['text'] = data.apply(lambda row: f"Stock {row['Name']} on date {row['date']} opening price {row['open']} closing price {row['close']}.", axis=1)
texts = data['text'].tolist()
return texts
This function loads a CSV file, processes it to create textual descriptions of the stock data, and returns the list of texts.
Creating and Storing embeddings
def create_and_store_embeddings(texts, openai_api_key):
embeddings = OpenAIEmbeddings(api_key=openai_api_key)
vectors = embeddings.embed_documents(texts)
vector_store = FAISS.from_texts(texts, embeddings)
return vector_store
This function creates embeddings for the texts using OpenAI's embeddings and stores them in a FAISS vector store.
Defining the “retrieve” function
def retrieve(query, vector_store, k=20):
return vector_store.similarity_search(query, k=k)
This function retrieves the most relevant documents from the vector store based on the query.
Defining the “ask_question” function
def ask_question(question, vector_store):
top_docs = retrieve(question, vector_store)
top_contexts = [doc.page_content for doc in top_docs]
top_context = " ".join(top_contexts)
prompt_template = PromptTemplate(
input_variables=["context", "question"],
template="""You are an expert question answering system. I'll give you a question and context, and you'll return the answer.
Context: {context}
Query: {question}"""
)
argumented_prompt = prompt_template.format(context=top_context, question=question)
model_output = Gemini.generate_content(argumented_prompt)
table = wandb.Table(columns=["Question", "Retrieved Data", "Response"])
table.add_data(question, "\n".join(top_contexts), model_output)
wandb.log({"Q&A Log": table})
return model_output
This function handles the retrieval of relevant documents, generates a response using the Gemini model, and logs the results to W&B in a table format.

Step 5: Load and process data

Specifying the path to the CSV file and processing it to create embeddings
file_path = "/kaggle/input/sandp500/individual_stocks_5yr/individual_stocks_5yr/AAL_data.csv"

texts = load_and_process_csv(file_path)

vector_store = create_and_store_embeddings(texts, openai_api_key)
Asking a question and logging the response
file_path = "/kaggle/input/sandp500/individual_stocks_5yr/individual_stocks_5yr/AAL_data.csv"

texts = load_and_process_csv(file_path)

vector_store = create_and_store_embeddings(texts, openai_api_key)
Finishing the W&B run
wandb.finish()
Let’s quickly summarize the code. First, we have “Installation” where we Install all required libraries and packages. Then “Imports and Initialization” where we import necessary libraries and initialize the Gemini and OpenAI models with their respective API keys.
Then comes the four helper functions which are:
  1. “load_and_process_csv”: Loads and processes the CSV file to generate textual descriptions.
  2. “Create_and_store_embeddings”: Creates embeddings for the textual descriptions and stores them in a FAISS vector store.
  3. “Retrieve”: Retrieves the most relevant documents from the vector store based on the query.
  4. “Ask_question”: Retrieves relevant documents, then generates a response using the Gemini model and logs the question, retrieved data, and response to W&B in a table format.
Next, we have “Data processing” where we load and process the CSV file to create embeddings. Finally, the last step is asking the model a question, which then retrieves relevant data, generates a response, and logs everything to W&B.

Using the RAG System

After building the RAG system it is now time to ask the model a few questions about the stock market data. We have selected AAL(American Airlines Group Inc.) as the company that we will ask the model questions about.
Here are the logged questions, data retrieved, and responses generated.
Asking about yearly performance.


As you can see here, the model started by mentioning the stock prices at the start of the year in January then stated that afterwards the stock prices fluctuated. The model then proceeded to give insights into the highest and lowest prices recorded during that year as well as the overall trend that year.
Asking about Quarterly trends.



Rest of the response.

As you can see here, the model mentioned the initial and final price of the stock in each quarter, the overall direction of the stock price during each quarter, and any significant changes in stock prices within each quarter, indicating the percentage gain or loss.
Asking about monthly performance.

Rest of the response.




Here the model gave the opening and closing prices of each month along with an indication of the percentage gain or loss in each month.
Asking the model to compare between the performance of the company in two different periods.

Here the model compared the opening and closing prices of both periods by indicating the percentage difference between each one. Then the model proceeded to highlight the key differences between the stock prices in both periods as well as mentioning the factors that could have caused this difference.
So, now that we have asked questions covering various types of analyses, including yearly, quarterly, monthly, and comparative analyses, we hope you see how efficient the RAG system is at performing these types of financial analyses.

Conclusion

Using a Retrieval-Augmented Generation (RAG) system with the Gemini LLM for financial forecasting offers numerous benefits. This approach combines the strengths of information retrieval and natural language generation, providing comprehensive insights based on historical data.
The Gemini LLM can efficiently process vast amounts of financial texts, extracting relevant information and generating detailed reports, which aids in understanding market trends and historical performance. Additionally, the RAG system's ability to provide contextually relevant information enhances decision-making processes for financial analysts and investors.
The future of RAG systems in finance holds exciting potential for improvements and applications. Integration with real-time data sources and advanced analytics could enhance the accuracy and timeliness of the information provided. Incorporating machine learning models tailored for financial forecasting can further refine predictions and risk assessments.
Moreover, expanding the capabilities of RAG systems to include multi-modal data such as text, numerical data, and visualizations can offer a more holistic view of financial markets. As technology advances, RAG systems with LLMs like Gemini could become indispensable tools in finance, supporting everything from portfolio management to regulatory compliance and strategic planning.
Iterate on AI agents and models faster. Try Weights & Biases today.