How to build a Financial Advisory Bot using RAG with GenAI

Atharva Mundke
Google Cloud - Community
7 min readAug 4, 2024

In today’s world, people have money but don’t always know where to invest it to ensure it grows. Traditional advisory services often fall short, being expensive, not readily available, and sometimes slow to incorporate real-time market trends.

This blog post will take you step-by-step through the fascinating process of developing a financial advisory bot that democratizes access to excellent financial advice by utilizing the power of generative AI and retrieval-augmented generation (RAG).

By the end of this blog the reader will be able to build his own bot using RAG with GenAI Using Google Cloud Services.

This blog is aimed at developers, data scientists, and financial technology enthusiasts with intermediate to advanced skills in Python, machine learning, and a basic understanding of financial concepts.

Design

Our application include two types of services, which are Financial Product Bot and Advisory, which will be discussed in detail, later in this blog.

BOB Financial Advisory
├── Financial ChatBot
│ └── Data Storage(Buckets)
│ └── Langchain (TextSplitter)
│ └── embedding-001
│ └── gemini-pro Api
│ └── Flask
├── Advisory
│ └── Vertexai
│ └── gemini-pro Api
│ └── Categories Data
│ └── Flask

Our Financial Advisory Bot utilizes a sophisticated architecture that combines several advanced technologies:

System Architecture

Our innovative design integrates cutting-edge AI and cloud technologies to provide a seamless and efficient financial advisory service. Here’s an overview of the high-level architecture:

1. Data Collection: The bot gathers financial information through web scraping and APIs, ensuring it has access to the latest data from banking websites and financial news sources.

2. Data Integration: The bot integrates data from multiple sources, including web scraping and financial APIs, to provide a comprehensive analysis. This data is stored in Google Cloud Storage(i.e. Buckets), ensuring efficient retrieval and processing.

3. Generative AI: Utilizing advanced generative models like Google Gemini, the bot can generate insightful and personalized financial advice based on the retrieved information and user queries. This allows for more nuanced and accurate recommendations.

4. Retrieval-Augmented Generation (RAG): The bot uses RAG to enhance the quality of responses by combining retrieval and generation techniques. This ensures that the advice provided is both contextually relevant and up-to-date with the latest market trends.

5. Query Processing: User queries are converted into vector embeddings, which are then used to retrieve relevant information from our database. The responses are generated using Google Gemini’s advanced multimodal AI model.

6. User Interface: A user-friendly interface built with Flask allows users to input their queries easily, this manages secure communication between the frontend and backend, ensuring a smooth and secure user experience.

System Workflow

This comprehensive design ensures that the Financial Advisory Bot not only delivers personalized and timely financial advice but also maintains a high level of transparency and adaptability to changing market conditions.

Prerequisite

Before we dive into the implementation of the Project ensure you have the following:

  1. IDE(Visual Studio Code Editor, Google Colab Account)
  2. Framework Flask
  3. Along with that you should also,
  • Access to the Vertex AI and the Gemini Pro model
  • Basic knowledge of Python, machine learning, and financial concepts
  • Familiarity with Google Cloud Platform

Step by Step Instruction

For the test case, information used in this project retrieves data from banks’ websites (Bank of Baroda) and relevant sites through web scraping and uses APIs to source current financial news. This information is used for Retrieval-Augmented Generation (RAG) to provide accurate and timely financial advice. The collected data is stored in a Google Cloud Storage bucket for easy access and processing, ensuring the chatbot leverages up-to-date financial information to deliver reliable and relevant advice to users.

  1. Financial ChatBot:

In this project we have used flask, generative ai and langchain to build a financial advisory chatbot in which user can give their financial queries and the bot will return the information from bucket where we have stored it and process it and provide a descriptive answer. this board helps users to access precise and relevant financial products information which helps to make them decision

Step 1: Set up your Google API key

os.environ['GOOGLE_API_KEY'] = 'YOUR_API_KEY'

Step 2: Start by loading all the necessary libraries.

import google.generativeai as genai
from langchain_core.prompts import PromptTemplate
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_community.retrievers import BM25Retriever
from langchain.retrievers.ensemble import EnsembleRetriever
from langchain.prompts import ChatPromptTemplate
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnablePassthrough
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_community.document_loaders import PyPDFDirectoryLoader
from langchain_community.vectorstores import Chroma

Step 3: Load The Data From Buckets

How to Create Bucket.

Step 4: After that use the langchain TextSplitter to split the document into chunks.

splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100)
chunks = splitter.split_documents(docs)

Step 5: Now, load an embedding model to convert text into numerical embeddings. Here, we are using “embedding-001” embedding model.

embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
Step 6: Create a Vector Store to store embeddings and text chunks. If you want you can save these embeddings for later use.
vectorstore = Chroma.from_documents(chunks, embeddings)

Step 7: As we are using Hybrid Search we are performing vector search and keyword search

vectorstore_retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
keyword_retriever = BM25Retriever.from_documents(chunks)
keyword_retriever.k = 3
ensemble_retriever = EnsembleRetriever(
retrievers=[vectorstore_retriever, keyword_retriever],
weights=[0.5, 0.5]
)

Step 8: Create a prompt Template As per your choice

Step 9: Load the gemini-1.5-pro-001 (LLM) to use for retrieval

model = ChatGoogleGenerativeAI(model="gemini-1.5-pro-001",
max_token="1024",
temperature=0.3)

Step 10: Now create a document chain.

chain = (
{"context": ensemble_retriever, "query": RunnablePassthrough()}
| prompt
| model
| output_parser
)

Step 11: Now you can create a Interface of your choice in flask and run the application

Interface for Financial ChatBot

2. Financial Advisory:

In this project we have used flask and vertex ai to create the financial advisory that provides personalized advice based on users financial detail which include various parameters like age occupation income etc. we are using vertex ai generative model to analyze the data and generate a descriptive and personalize advice.

Step 1: Set Your Environment

import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] ='Path to credential file'

Step 2: Start by installing and loading all the necessary libraries.

!pip install google-cloud-aiplatform
from google.cloud import aiplatform
from vertexai.preview.language_models import ChatModel, InputOutputTextPair
from vertexai.preview.generative_models import GenerativeModel, Content

Step 3: Authenticate and initialize Vertex AI

from google.colab import auth
auth.authenticate_user()

Step 4: Initialize Vertex AI

aiplatform.init(project='YOUR-PROJECT-ID',location='REGION')

Step 5: Define the prompt template and generation config

In this According to your need and requirement Modify the template

Prompt_template= """You are a financial advisor assistant. Based on the following user information and query, provide personalized financial advice:
User Query: {query}
Please provide detailed, actionable advice tailored to the user's specific situation and query.
"""
generation_config = {
"max_output_tokens": 1024,
"temperature": 0.2,
"top_p": 0.8,
"top_k": 40
}

Step 6: Create the user interface using flask of your own choice.

Following is the example of the interface

Interface for Financial Advisory

Step 7: Implement the advice generation function

def generate_advice(b):
with output:
output.clear_output()
print("Generating advice…")
prompt = prompt_template.format(
query=query_input.value
)
model = GenerativeModel("gemini-pro")
response = model.generate_content(
prompt,
generation_config=generation_config,
safety_settings=safety_settings
)
print("Financial Advice:")
print(response.text)

Result/Demo

By the end of this project, users will have access to a sophisticated financial advisory bot capable of providing real-time, personalized financial advice. The bot will leverage advanced AI to analyze market trends and individual financial data, ensuring that users receive accurate and transparent recommendations tailored to their specific needs.

Imagine being able to ask the bot for investment strategies based on the latest market trends or getting personalized advice on managing your retirement funds. The bot’s ability to process and analyze vast amounts of data in real-time ensures that the advice is not only accurate but also adaptable to changing market conditions.

How to Navigate around the prototype: Click Here

For working prototype video: Click Here

For GitHub Repo: Click Here

What’s Next?

The journey doesn’t end here. For those interested in further enhancing their skills and the capabilities of the Financial Advisory Bot, consider exploring the following:

  • Automated Insights: Develop functionalities for automatically generating insights and reports based on user data and market trends.
  • Security and Privacy: Enhance security with features like end-to-end encryption, multi-factor authentication, and regular audits.
  • Real-time Collaboration: Enable features for users to share insights and financial plans with advisors or peers, fostering a collaborative environment.

Call to Action

To learn more about Google Cloud services and to create impact for the work you do, get around to these steps right away:

--

--