๐ Guide to Local Financial Analysis with DeepSeek R1, Llama, and Ollama using RAG
๐ Learn How DeepSeek R1 & Llama Analyze Financial Reports Locally Using AI-Powered RAG โ A Study
๐ Introduction
Quarterly Financial Results offer valuable insights, but manually extracting them is time-consuming. Businesses, investors, and analysts often find it challenging to scan large reports for key trends, risks, and growth indicators.
This guide shows how to set up a Retrieval-Augmented Generation (RAG) system for analyzing financial reports using DeepSeek R1 and Llama, running locally with Ollama, LangChain, and Streamlit.
This setup will allow you to:
โ
Extract financial insights efficiently, including revenue trends, profitability, and risk factors
โ
Compare DeepSeek R1 vs. Llama to evaluate different financial analysis approaches
โ
Run everything offline, eliminating cloud dependencies and reducing operational costs
โ
Enhance response time with local AI models for faster and more efficient financial analysis
By the end of this guide, youโll have a fully functional, interactive AI-powered tool to analyze financial reports securely and efficiently.
๐ก๏ธ Why Use DeepSeek & Llama Locally?
๐น Data Privacy: Keep your financial data local.
๐น No Cloud Costs: Save on expensive cloud API calls. Works Offline.
๐น Fast Processing: Local models reduce network delays and speed up responses, depending on your laptop's hardware.
๐น Customizable Insights: Tailor the RAG system to your financial needs.
๐ผ๏ธ System Architecture Overview
The diagram below illustrates the flow of data and processing in the local financial report analysis system using Streamlit, LangChain, FAISS, DeepSeek R1, Llama, and Ollama.
๐ Key Components in the Architecture:
User Interface โ Streamlit lets users upload PDFs and interact with the system.
Preprocessing โ The file is processed with PDFPlumberLoader, chunked, and embedded for retrieval.
RAG Pipeline โ FAISS and LangChain retrieve relevant content for analysis.
LLM Selection โ Choose between DeepSeek R1 and Llama, running locally via Ollama.
Prompt & Response โ The system sends a structured prompt to the chosen LLM for financial insights.
๐ ๏ธ Step 1: Download Ollama, Pull DeepSeek & Llama Models
Install Ollama: Download and install Ollama for your OS.
Pull DeepSeek & Llama Models
ollama pull deepseek-r1:1.5b
ollama pull llama3:2b
ollama list
๐ ๏ธ Step 2: Install Prerequisites Python Packages
requirements.txt will have the dependencies
streamlit
numpy
langchain
langchain-community
langchain-experimental
pdfplumber
faiss-cpu
sentence-transformers
huggingface-hub
ollama
pip install -r requirements.txt
This will install all the packages listed in requirements.txt
๐ ๏ธ Step 3: Coding the Financial Analysis System
๐ Understanding the Code
This Streamlit-based Financial Report Analysis Tool lets users upload PDF financial reports, process them with semantic chunking & embeddings, and generate insights using DeepSeek R1 or Llama.
Key Components of the code:
File Upload & Processing: Extracts text from PDFs with PDFPlumberLoader.
Chunking & Embeddings: Splits documents for better retrieval.
Vector Storage: Uses FAISS for efficient document embedding storage and search.
LLM Selection: Users can choose deepseek-r1 or llama for analysis.
RAG with LangChain:
LangChain handles embedding, storage, and retrieval.
RetrievalQA Chain fetches relevant financial data.
LLMChain formats and sends content to the model.
Interactive Q&A: Users can select suggested questions or enter custom queries.
With LangChain, the tool ensures structured processing, efficient retrieval, and accurate AI-generated financial insights.
๐The Importance of the Prompt
A well-structured prompt is critical for generating precise and useful financial analysis. The prompt used in this project ensures:
โ
Clear and structured responses rather than long, ambiguous text.
โ
Focused financial analysis by enforcing categories like key metrics, trends, risks, and recommendations.
โ
Concise insights with a limit of 5 key points per section to avoid overwhelming the user.
โ
Proper financial formatting, ensuring numbers and financial terms are correctly represented.
import streamlit as st
import numpy as np
import re
from datetime import datetime
from langchain_community.document_loaders import PDFPlumberLoader
from langchain_experimental.text_splitter import SemanticChunker
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.llms import Ollama
from langchain.prompts import PromptTemplate
from langchain.chains.llm import LLMChain
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
from langchain.chains import RetrievalQA
# Streamlit UI setup
st.set_page_config(page_title="Financial Report Analyzer", layout="wide")
st.title("๐ Financial Report Analysis ")
# Sidebar settings
with st.sidebar:
st.header("Analysis Configuration")
model_choice = st.selectbox("Select LLM Model", ["deepseek-r1:1.5b", "llama3.2:1b"], help="Choose the language model for analysis")
analysis_types = st.multiselect("Analysis Focus", ["Financial Metrics", "Risk Analysis", "Market Analysis", "Performance Indicators", "Cash Flow Analysis", "Balance Sheet Analysis", "Ratio Analysis"], default=["Financial Metrics"])
# File upload and analysis
uploaded_file = st.file_uploader("Upload Financial Report (PDF)", type="pdf")
if uploaded_file:
with open("temp.pdf", "wb") as f:
f.write(uploaded_file.getvalue())
with st.spinner("Processing document..."):
loader = PDFPlumberLoader("temp.pdf")
docs = loader.load()
full_text = " ".join([doc.page_content for doc in docs])
# Document-based Q&A system
text_splitter = SemanticChunker(HuggingFaceEmbeddings())
documents = text_splitter.split_documents(docs)
embedder = HuggingFaceEmbeddings()
vector = FAISS.from_documents(documents, embedder)
retriever = vector.as_retriever(search_type="similarity", search_kwargs={"k": 5})
llm = Ollama(model=model_choice)
# Enhanced Q&A prompt template
financial_prompt = """
You are a financial analyst. Provide a structured, clean, and concise analysis with the following sections:
### **Analysis Structure:**
1. **Key Financial Metrics** (Revenue, Profit, Expenses, Cash Flow)
2. **Trends & Changes** (Year-over-Year & Quarter-over-Quarter)
3. **Risks & Challenges** (Debt, Market Risks, Operational Issues)
4. **Actionable Insights & Recommendations** (Steps to improve financial health)
### **Response Guidelines:**
- **Avoid unnecessary repetition.**
- **Ensure proper formatting of financial figures.**
- **Keep responses within 200 words.**
### **Context:**
{context}
### **Question:**
{question}
### **Generate a structured financial analysis:**
"""
QA_PROMPT = PromptTemplate.from_template(financial_prompt)
llm_chain = LLMChain(llm=llm, prompt=QA_PROMPT)
combine_documents_chain = StuffDocumentsChain(llm_chain=llm_chain, document_variable_name="context")
qa = RetrievalQA(combine_documents_chain=combine_documents_chain, retriever=retriever)
# Interactive Q&A
st.header("๐ก Interactive Analysis")
suggested_questions = {
"Financial Metrics": ["What are the key revenue trends?", "How has profitability evolved?", "What are the main cost drivers?"],
"Risk Analysis": ["What are the primary risk factors?", "How is the company managing market risks?", "What are the key regulatory risks?"],
"Market Analysis": ["What is the company's market position?", "How does it compare to competitors?", "What are the market opportunities?"]
}
all_suggested_questions = [q for analysis_type in analysis_types for q in suggested_questions.get(analysis_type, [])]
selected_question = st.selectbox("Suggested Questions:", [""] + all_suggested_questions)
user_input = st.text_input("Or ask your own question:", value=selected_question)
# Display selected model before analysis
st.subheader(f"Using Model: {model_choice}")
if user_input:
with st.spinner("Analyzing..."):
response = qa(user_input)["result"]
# Remove <think> artifacts and extra spaces
clean_response = re.sub(r"<think>|</think>", "", response).strip()
# Ensure numbers are formatted correctly
clean_response = re.sub(r'(\d)([A-Za-z])', r'\1 \2', clean_response) # Space between numbers and text
clean_response = re.sub(r'(\d{1,3})(\d{3})', r'\1,\2', clean_response) # Add commas to large numbers
st.subheader("๐ Analysis Results")
st.markdown(f"- {clean_response}")
if st.button("Export Analysis"):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
export_data = {"question": user_input, "analysis": response}
st.download_button(label="Download Analysis", data=str(export_data), file_name=f"financial_analysis_{timestamp}.txt", mime="text/plain")
else:
st.info("Please upload a financial report (PDF) to begin analysis.")
๐ ๏ธ Step 4: Launch the Streamlit App
Run the following command
streamlit run '.\financial-report-analyzer.py'
This will launch the application in your browser. We can upload the Quaterly result in PDF
๐ ๏ธ Step 5: Upload Financial Reports and Generate Insights
For this , we used quarterly results from:
1๏ธโฃ Upload the quarterly financial reports (e.g., NVIDIA & Meta, or any).
2๏ธโฃ Select the LLM model (DeepSeek R1 or Llama).
3๏ธโฃ Ask financial questions and analyze the generated insights.
๐ก Example Questions:
Analyze the key financial metrics from Q1 FY25 and explain their significance.
What financial risks does the company face?
Compare META's performance across different business segments.
Explain NVIDIA's AI strategy based on the announcements in this report
๐ Output from Models
Question asked here : What financial risks does the company face?
๐ข DeepSeek Output Screenshot
๐ต Llama Output Screenshot
๐ These responses was analyzed using both DeepSeek R1 and Llama for comparison. Check some output screenshots at the end to see the model comparisons in action!
Comparing DeepSeek R1 and Llama for Financial Analysis
Aspect | DeepSeek R1 | Llama 3.2 |
Logical Flow | Step-by-step analysis with reasoning, linking revenue, expenses, and risks. | Lists risks without clear cause-effect reasoning. |
Financial Depth | Analyzes GAAP vs. non-GAAP, tax rates, margins, and cash flow. | Provides financial figures but lacks deep analysis. |
Risk Identification | Identifies financial risks and suggests mitigation strategies. | Highlights general risks without detailed modeling. |
Best For | Analysts and investors needing detailed insights and reasoning. | Executives and stakeholders needing quick summaries. |
๐ Conclusion
By following this guide, you have successfully built a local AI-powered financial analysis tool using DeepSeek R & Llama 3.2 with Retrieval-Augmented Generation (RAG). This setup allows you to:
โ
Analyze financial reports securely without relying on cloud-based models.
โ
Extract key financial insights such as revenue trends, profitability, and risks.
โ
Compare DeepSeek R1.5 and Llama 3.2 to understand their strengths in financial analysis.
โ
Run everything locally, ensuring speed, cost efficiency, and data privacy.
Final Takeaway:
๐น DeepSeek-R1 delivers detailed, structured, and data-driven financial analysis with strong reasoning capabilities.
๐น Llama 3.2 offers concise, high-level summaries with strategic insights.
๐ก Note: This comparison is based on deepseek-r1 1.5b and llama3.2 1b. Higher-billion parameter models might provide enhanced reasoning, accuracy, and depth in financial analysis. Performance may vary based on model size and hardware capabilities.
From Ollama Site - deepseek-r1 , llama3.2
๐ป System Requirements
Minimum Requirements
CPU: Intel Core i7 / AMD Ryzen 7 (4000 series)
RAM: 16GB
GPU: NVIDIA RTX 2060 / AMD RX 6600 (4GB VRAM minimum)
Note: I used AMD Ryzen 9 5900HX, 16GB RAM, NVIDIA GeForce RTX 3050.
๐ธ Some more output responses and analysis
๐น Q : Compare META's performance across different business segments.
Model : Llama
It lists the key metrics from the document in a concise manner with a high-level summary.
๐น Q : How might NVIDIA be impacted by DeepSeekโs advancements in AI?
Model : DeepSeek-R1
First, it performs reasoning based on the question, then it provides a detailed, structured, and data-driven financial analysis.
๐น Q : How has profitability evolved?
Model : DeepSeek-R1
Again, the reasoning capability for financial analysis excels!
Letโs Connect!
If you found this useful, feel free to connect with me on LinkedIn-https://www.linkedin.com/in/sridharsampath89/
Repo : https://github.com/SridharSampath/financial-report-analyzer.git