Â
SECTION 1: LEARNING OBJECTIVES
By the end of this lesson, you will be able to:
-
Understand the evolution from BERT to GPTÂ and the paradigm shift in NLP.
-
Explain the core concepts of Generative AI – transformers, attention, and autoregressive generation.
-
Distinguish between classification models (BERT) and generative models (GPT, Claude, Gemini)Â .
-
Understand the capabilities and limitations of Large Language Models (LLMs)Â in financial applications.
-
Apply prompt engineering – zero-shot, few-shot, and chain-of-thought prompting – for financial tasks.
-
Implement retrieval-augmented generation (RAG)Â to provide LLMs with access to proprietary financial data.
-
Understand the risks of LLMs in finance – hallucinations, bias, compliance, and data privacy.
-
Use Python to interact with LLM APIs (e.g., OpenAI, Anthropic) for financial analysis.
-
Explore the business applications – summarisation, sentiment analysis, financial Q&A, document processing, and coding assistance.
SECTION 2: THE PARADIGM SHIFT – FROM BERT TO GPT
2.1 The Evolution
| Era | Model | Architecture | Task | Paradigm |
|---|---|---|---|---|
| 2018 | BERT | Encoder-only (bidirectional) | Classification, NER, QA | Pre-train → Fine-tune |
| 2019 | GPT-2 | Decoder-only (autoregressive) | Text generation | Pre-train → Fine-tune |
| 2020 | GPT-3 | Decoder-only (massive) | Text generation, few-shot | Pre-train → In-context learning |
| 2022 | ChatGPT | Instruction-tuned GPT-3.5 | Conversational AI | Reinforcement Learning from Human Feedback (RLHF) |
| 2023 | GPT-4, Claude, Gemini | Multi-modal, large-scale | Multi-task, reasoning | Emergent capabilities |
Key insight:Â The shift from “Pre-train + Fine-tune” (BERT) to “Pre-train + In-context Learning” (GPT) changed how we use AI. We no longer need to train task-specific models; we can prompt a general-purpose model to perform a wide range of tasks.
2.2 What is a Large Language Model (LLM)?
An LLM is a deep learning model trained on a massive corpus of text to predict the next token (word/subword) in a sequence.
Key characteristics:
-
Scale:Â Billions (or trillions) of parameters.
-
Data:Â Trained on hundreds of billions of tokens (the entire internet, books, academic papers).
-
Capabilities:Â Text generation, summarisation, translation, reasoning, coding, question answering.
-
Emergent abilities:Â At a certain scale, models exhibit abilities not present in smaller models (e.g., arithmetic, reasoning, chain-of-thought).
2.3 How LLMs Work (Simplified)
-
Tokenisation:Â Convert text into tokens (subwords).
-
Embedding:Â Convert tokens into dense vectors.
-
Transformer layers:Â Apply self-attention to learn relationships between tokens.
-
Output generation:Â At each step, predict the next token (autoregressive).
-
Decoding:Â Continue until a stop token is reached.
Temperature setting:Â Controls randomness.
-
Low temperature (0.0-0.3): Deterministic, focused.
-
High temperature (0.7-1.0): Creative, diverse.
SECTION 3: APPLYING LLMS TO FINANCE
3.1 What LLMs Can Do for Finance
| Application | Description | Example |
|---|---|---|
| Document Summarisation | Summarise long financial documents. | “Summarise this 10-K filing in 100 words.” |
| Sentiment Analysis | Extract sentiment from text. | “What is the sentiment of this earnings call transcript?” |
| Financial Q&A | Answer questions about financial documents. | “What was the company’s revenue growth in Q3 2024?” |
| Regulatory Compliance | Check documents for compliance issues. | “Does this prospectus comply with SEC regulations?” |
| Report Generation | Generate financial reports from data. | “Write a quarterly earnings report based on this data.” |
| Coding Assistance | Write or debug code for financial analysis. | “Write a Python script to calculate VaR for this portfolio.” |
| Data Extraction | Extract structured data from unstructured text. | “Extract all financial ratios from this annual report.” |
| Market Analysis | Provide insights on market conditions. | “What are the key risks and opportunities in the current market?” |
3.2 Prompt Engineering
The art of crafting prompts to get the desired output from an LLM.
| Technique | Description | Example |
|---|---|---|
| Zero-shot | No examples; just the instruction. | “Classify the sentiment of this text.” |
| Few-shot | Provide a few examples in the prompt. | “Sentiment: ‘Stock soars’ → Positive; ‘Markets tumble’ → Negative; Now classify: ‘…'” |
| Chain-of-Thought (CoT) | Ask the model to think step-by-step. | “Let’s think step-by-step. First, identify the key financial metrics. Then, assess their trends. Finally, provide a recommendation.” |
| Role Prompting | Assign a role to the model. | “Act as a senior financial analyst.” |
| Structured Output | Request output in a specific format (JSON, table). | “Return the output as a JSON object with fields: ‘sentiment’, ‘confidence’, ‘key_metrics’.” |
3.3 Retrieval-Augmented Generation (RAG)
Problem:Â LLMs are trained on public data up to a cut-off date. They don’t have access to proprietary data (e.g., internal financial documents, real-time market data).
Solution:Â RAG combines retrieval (search) with generation.
Workflow:
-
Index:Â Convert financial documents into embeddings (vectors) and store in a vector database.
-
Query:Â When a user asks a question, convert the question into an embedding.
-
Retrieve:Â Find the most relevant documents using similarity search (e.g., cosine similarity).
-
Augment:Â Inject the retrieved documents into the prompt.
-
Generate:Â The LLM answers the question using the retrieved context.
RAG ensures:
-
Up-to-date answers (using the latest data).
-
Accurate answers (grounded in retrieved documents).
-
Compliance (can trace answers back to source documents).
SECTION 4: IMPLEMENTATION IN PYTHON – USING LLM APIS
We will demonstrate:
-
Using OpenAI’s API for financial Q&A and sentiment analysis.
-
Implementing a simple RAG system.
-
Prompt engineering for financial tasks.
# =================================================================== # MODULE 6, LESSON 2: GENERATIVE AI AND LLMS FOR FINANCE # =================================================================== import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import json import re from typing import List, Dict import warnings warnings.filterwarnings('ignore') # Set style sns.set_style("whitegrid") print("="*70) print("GENERATIVE AI AND LARGE LANGUAGE MODELS (LLMS) FOR FINANCE") print("="*70) # ---------------------------------------------------------------- # PART A: SIMULATED LLM RESPONSES (NO API KEY REQUIRED) # ---------------------------------------------------------------- print("\n" + "-"*60) print("PART A: Simulated LLM Responses") print("-"*60) print(""" Note: To use actual LLM APIs (OpenAI, Anthropic, etc.), you need an API key. This demonstration uses simulated responses to illustrate concepts. To run with a real API: 1. Install: pip install openai 2. Set your API key: openai.api_key = "your-key" 3. Replace the simulate_llm_response function with actual API calls. """) def simulate_llm_response(prompt, model="simulated", temperature=0.7): """ Simulate an LLM response (for demonstration). In practice, replace this with an actual API call. """ # Keywords for demonstration if "summarise" in prompt.lower() or "summarize" in prompt.lower(): return """ Summary: The Federal Reserve's recent rate hike signals confidence in economic growth, though it has created volatility in equity markets. Technology stocks are particularly sensitive, while financials benefit from higher margins. The yield curve inversion suggests caution, but consumer spending remains resilient. """ elif "sentiment" in prompt.lower(): return """ Sentiment Analysis: Overall sentiment: Positive (0.72) - Positive indicators: record profits, strong demand, beating expectations - Negative indicators: inflation fears, layoffs, recession warnings - Neutral indicators: market volatility, interest rate changes Confidence: 85% """ elif "extract" in prompt.lower() or "json" in prompt.lower(): return """ { "company": "Bank of America", "quarter": "Q4 2024", "revenue": "$25.3B", "profit": "$7.8B", "growth_rate": "12%", "key_drivers": ["investment banking", "wealth management"], "risks": ["inflation", "regulatory scrutiny"] } """ elif "think step-by-step" in prompt.lower() or "reasoning" in prompt.lower(): return """ Step-by-Step Reasoning: 1. The company reported strong revenue growth (12% YoY) driven by investment banking. 2. Operating margins improved due to cost-cutting measures. 3. The balance sheet strengthened with increased capital reserves. 4. However, rising inflation poses a risk to consumer spending. 5. Recommendation: BUY with a target price of $45, based on a P/E of 15x. Confidence: 78% """ else: return "The Fed's actions have restored confidence in the banking sector, though challenges remain." # ---------------------------------------------------------------- # PART B: PROMPT ENGINEERING EXAMPLES # ---------------------------------------------------------------- print("\n" + "-"*60) print("PART B: Prompt Engineering for Financial Tasks") print("-"*60) # Define sample prompts prompts = { "Summarisation": "Summarise this earnings call transcript in 50 words: 'The company reported strong Q4 results with revenue of $25 billion, beating expectations. The CEO highlighted growth in digital banking and expansion into new markets. However, the company faces headwinds from rising interest rates and increased competition.'", "Sentiment Analysis": "Analyse the sentiment of this news headline: 'Fed rate hike sparks market rally as investors gain confidence.' Provide a score from -1 (negative) to +1 (positive).", "Extraction": "Extract the key financial metrics from this text and return as JSON: 'JPMorgan reported quarterly profit of $13.1 billion, up 8% from last year. Revenue reached $42.3 billion. The bank's CET1 ratio stood at 13.8%.'", "Chain-of-Thought": "Let's think step-by-step about whether to invest in Tesla. Consider: 1) Financial performance, 2) Market position, 3) Growth prospects, 4) Risks. Provide a recommendation.", "Role Prompting": "Act as a senior credit analyst. Assess the creditworthiness of a borrower with the following profile: revenue $5M, debt-to-income ratio 35%, credit score 620, industry: retail. Provide a recommendation." } # Simulate responses for task, prompt in prompts.items(): print(f"\n--- {task} ---") print(f"Prompt: {prompt[:100]}...") response = simulate_llm_response(prompt) print(f"Response: {response}") # ---------------------------------------------------------------- # PART C: RETRIEVAL-AUGMENTED GENERATION (RAG) – CONCEPTUAL # ---------------------------------------------------------------- print("\n" + "-"*60) print("PART C: Retrieval-Augmented Generation (RAG)") print("-"*60) # Sample financial documents (simulated) documents = [ {"id": 1, "text": "The Federal Reserve raised interest rates to 5.25%, the highest level in 22 years."}, {"id": 2, "text": "Bank of America reported Q3 earnings of $8.2 billion, beating analyst estimates."}, {"id": 3, "text": "Inflation eased to 3.2% in October, down from 3.7% in September."}, {"id": 4, "text": "The yield curve inverted, with the 10-year yield falling below the 2-year yield."}, {"id": 5, "text": "Goldman Sachs upgraded Apple to 'buy', with a target price of $250."} ] # Simulate embedding and retrieval (simple keyword matching) def simple_retrieve(query, documents): """Simple retrieval based on keyword matching.""" query_terms = query.lower().split() scored_docs = [] for doc in documents: score = sum(1 for term in query_terms if term in doc['text'].lower()) scored_docs.append((doc, score)) # Sort by score (descending) scored_docs.sort(key=lambda x: x[1], reverse=True) return [doc[0] for doc in scored_docs if doc[1] > 0][:3] # Top 3 # Sample query query = "What is the current interest rate and how does it affect banks?" # Retrieve relevant documents retrieved_docs = simple_retrieve(query, documents) print(f"Query: {query}") print(f"\nRetrieved Documents ({len(retrieved_docs)}):") for doc in retrieved_docs: print(f" - {doc['text']}") # Construct RAG prompt rag_prompt = f""" Context (from retrieved documents): {chr(10).join([doc['text'] for doc in retrieved_docs])} Question: {query} Answer the question using the context above. If the context doesn't contain enough information, say so. """ print("\nRAG Prompt:") print(rag_prompt) print("\nRAG Response (simulated):") print("Based on the available information, the Federal Reserve raised interest rates to 5.25%. This impacts banks by increasing their net interest margins (NIM), as they can earn more on loans. However, it also increases the cost of deposits and may lead to higher defaults. The inverted yield curve (10-year below 2-year) suggests market concerns about future economic growth, which could pressure bank profitability.") # ---------------------------------------------------------------- # PART D: RISKS AND LIMITATIONS OF LLMS IN FINANCE # ---------------------------------------------------------------- print("\n" + "-"*60) print("PART D: Risks and Limitations of LLMs in Finance") print("-"*60) risks = [ { "risk": "Hallucinations", "description": "LLMs can generate plausible but incorrect information.", "mitigation": "Use RAG with grounding in source documents; implement fact-checking; human-in-the-loop." }, { "risk": "Bias and Fairness", "description": "Models can amplify biases present in training data.", "mitigation": "Audit outputs for bias; use diverse training data; implement bias detection." }, { "risk": "Data Privacy", "description": "Sensitive financial data may be exposed to model providers.", "mitigation": "Use on-premise models (open-source); anonymise data; implement data governance." }, { "risk": "Lack of Explainability", "description": "LLMs are black boxes; it's difficult to understand why a decision was made.", "mitigation": "Use model interpretability tools; document decision-making; regulatory oversight." }, { "risk": "Regulatory Compliance", "description": "Regulators may not accept AI-generated outputs without human oversight.", "mitigation": "Maintain audit trails; human review for critical decisions; regulatory engagement." }, { "risk": "Jailbreaking", "description": "Malicious prompts can bypass safety measures.", "mitigation": "Implement prompt filtering; use content moderation; monitor for misuse." } ] risk_df = pd.DataFrame(risks) print(risk_df.to_string(index=False)) print("\nBest Practices for LLM Adoption in Banking:") print(""" 1. Start with small-scale pilots in non-critical areas. 2. Implement robust testing and validation frameworks. 3. Maintain human oversight for all LLM-generated outputs. 4. Document all prompts, inputs, and outputs for audit purposes. 5. Use RAG to ground responses in verified data sources. 6. Regularly update models and monitor for drift. 7. Engage with regulators early in the adoption process. 8. Develop internal guidelines for responsible LLM use. """) # ---------------------------------------------------------------- # PART E: OPEN-SOURCE LLMS FOR FINANCE # ---------------------------------------------------------------- print("\n" + "-"*60) print("PART E: Open-Source LLMs for Finance") print("-"*60) open_source_models = [ { "name": "Llama 2 / Llama 3", "developer": "Meta", "size": "7B-70B", "advantages": "Open weights; strong performance; customisable.", "use_case": "General-purpose financial analysis, document processing." }, { "name": "Mistral 7B", "developer": "Mistral AI", "size": "7B", "advantages": "Efficient; high performance; Apache 2.0 license.", "use_case": "Cost-effective deployment; high accuracy for size." }, { "name": "Falcon", "developer": "Technology Innovation Institute", "size": "7B-180B", "advantages": "Open weights; good for Arabic and multilingual.", "use_case": "Financial analytics in the Middle East and Africa." }, { "name": "BloombergGPT", "developer": "Bloomberg", "size": "50B", "advantages": "Trained on 363 billion tokens of financial data; domain-specific.", "use_case": "Financial sentiment, news analysis, regulatory filings." }, { "name": "FinGPT", "developer": "AI4Finance", "size": "Various", "advantages": "Open-source; specialised for finance; constantly updated.", "use_case": "Financial news summarisation, sentiment analysis, data extraction." } ] model_df = pd.DataFrame(open_source_models) print(model_df.to_string(index=False)) # ---------------------------------------------------------------- # PART F: BUSINESS APPLICATIONS AND IMPLEMENTATION ROADMAP # ---------------------------------------------------------------- print("\n" + "="*70) print("PART F: Business Applications and Implementation Roadmap") print("="*70) applications = { "Customer Service Automation": { "Description": "AI chatbots for customer queries, account information, and transactions.", "Implementation": "Use GPT-4 or open-source model with RAG on product documents.", "Timeline": "3-6 months" }, "Document Processing": { "Description": "Extract structured data from financial documents (invoices, contracts, filings).", "Implementation": "Use GPT-4 with structured output prompting or fine-tuned open-source model.", "Timeline": "2-4 months" }, "Research Assistance": { "Description": "Summarise earnings calls, analyst reports, and news for investment decisions.", "Implementation": "Use LLMs with RAG on internal research databases.", "Timeline": "1-3 months" }, "Regulatory Compliance": { "Description": "Check documents for compliance, identify regulatory risks.", "Implementation": "Use LLMs with fine-tuning on regulatory text and RAG on regulatory guidelines.", "Timeline": "6-12 months" }, "Trading Signals": { "Description": "Generate trading signals from news, social media, and alternative data.", "Implementation": "Use sentiment analysis (FinBERT) + generative AI for summarisation.", "Timeline": "6-12 months" }, "Code Generation": { "Description": "Assist data scientists and developers with code for financial analysis.", "Implementation": "Use GitHub Copilot, ChatGPT, or open-source code models.", "Timeline": "1-2 months" } } app_df = pd.DataFrame([ { "Application": app, "Description": details["Description"], "Implementation": details["Implementation"], "Timeline": details["Timeline"] } for app, details in applications.items() ]) print(app_df.to_string(index=False)) print("\nImplementation Roadmap:") print(""" Phase 1 (Months 1-3): Pilot Projects - Select 1-2 low-risk use cases (e.g., summarisation, code assistance). - Use API-based models (OpenAI, Anthropic) for speed. - Establish evaluation metrics and human oversight. Phase 2 (Months 4-6): Scale and Integrate - Expand to more use cases. - Implement RAG for proprietary data. - Begin evaluating open-source models for cost savings. Phase 3 (Months 7-12): Production and Governance - Deploy models in production with monitoring and auditing. - Establish internal policies for LLM use. - Engage with regulators for compliance. Phase 4 (Months 12+): Advanced and Proprietary - Fine-tune open-source models on proprietary data. - Develop in-house LLM capabilities (if justified). - Explore multi-modal capabilities (text + numbers + charts). """)
SECTION 5: COMPARISON – CLASSIFICATION VS GENERATIVE MODELS
| Aspect | Classification (BERT/FinBERT) | Generative (GPT/Claude) |
|---|---|---|
| Architecture | Encoder-only | Decoder-only |
| Training | Pre-train + Fine-tune | Pre-train + Instruction tuning |
| Primary Use | Classification, NER, QA | Generation, Q&A, reasoning |
| Speed | Fast (especially with GPUs) | Slower (autoregressive) |
| Explainability | Somewhat (attention weights) | Poor (black-box) |
| Cost | Lower (smaller models) | Higher (larger models, API costs) |
| Domain Customisation | Fine-tune on financial data | Few-shot prompting, RAG, fine-tuning |
| Regulatory Fit | Better (more interpretable) | Requires more oversight |
Recommended approach:Â Use classification models for specific, well-defined tasks (e.g., sentiment, NER). Use generative models for complex, open-ended tasks (e.g., summarisation, Q&A, report generation).
SECTION 6: SUMMARY FOR THE DATA PRACTITIONER
-
Generative AI (LLMs)Â represents a paradigm shift from task-specific models to general-purpose models.
-
LLMs (GPT-4, Claude, Llama) are capable of summarisation, question answering, reasoning, and code generation.
-
Prompt engineering (zero-shot, few-shot, chain-of-thought) is the primary way to interact with LLMs.
-
RAG (Retrieval-Augmented Generation)Â grounds LLM responses in proprietary or up-to-date data.
-
Risks include hallucinations, bias, data privacy, and regulatory compliance – requiring careful governance.
-
Open-source models (Llama, Mistral, FinGPT) offer alternatives to proprietary APIs for sensitive financial data.
-
In banking, LLMs can transform document processing, research, customer service, and compliance.
SECTION 7: RECOMMENDED NEXT STEPS
-
Sign up for an LLM API (OpenAI, Anthropic) and experiment with financial tasks.
-
Implement a simple RAG system using a vector database (e.g., Pinecone, Weaviate, or FAISS).
-
Explore open-source models (e.g., Llama 3 via Ollama or Hugging Face).
-
Learn about prompt engineering and few-shot learning in depth.
-
Study the regulatory landscape for AI in finance (e.g., EU AI Act, US Federal Reserve guidance).
-
Prepare for the next lesson on Explainable AI (XAI) for Regulatory Compliance.
[END OF LESSON 2 – MODULE 6]