SECTION 1: LEARNING OBJECTIVES

By the end of this lesson, you will be able to:

  • Understand the evolution from traditional AI to generative AI in banking.

  • Identify key use cases for generative AI in banking.

  • Apply Large Language Models (LLMs) for customer service, document processing, and content generation.

  • Implement Retrieval-Augmented Generation (RAG) for domain-specific financial applications.

  • Understand the risks of generative AI in banking – hallucinations, bias, and data privacy.

  • Measure generative AI performance using appropriate metrics.

  • Develop a generative AI strategy for a bank.

  • Implement prompt engineering for financial applications.


SECTION 2: THE EVOLUTION OF AI IN BANKING

2.1 From Predictive to Generative AI
 
 
Era AI Type Capabilities Banking Application
AI 1.0 Predictive AI Classification, regression, anomaly detection. Fraud detection, credit scoring, churn prediction.
AI 2.0 Generative AI Content creation, summarisation, Q&A, code generation. Document processing, customer service, report generation.
AI 3.0 Autonomous AI Self-optimising, decision-making. Autonomous operations, AI-driven strategy.
2.2 Why Generative AI in Banking?
 
 
Benefit Description
Customer Service 24/7 intelligent chatbots and virtual assistants.
Document Processing Extract, summarise, and generate financial documents.
Report Generation Automate regulatory and internal reporting.
Code Generation Accelerate software development and data analysis.
Content Creation Generate marketing, educational, and customer content.
Knowledge Management Access and synthesise organisational knowledge.
Personalisation Generate personalised customer communications.

SECTION 3: GENERATIVE AI USE CASES IN BANKING

3.1 Customer Service
 
 
Use Case Description Example
Intelligent Chatbots AI-powered conversational agents. 24/7 support for customer queries.
Email Generation Automated email responses. Drafting replies to customer inquiries.
Summarisation Summarise customer interactions. Call summaries, chat logs.
Sentiment Analysis Analyse customer sentiment. Real-time sentiment detection.
Voice Banking Voice-activated banking. Natural language voice commands.
3.2 Document Processing
 
 
Use Case Description Example
Document Summarisation Summarise long financial documents. Annual reports, prospectuses.
KYC Automation Extract information from KYC documents. Identity verification.
Contract Analysis Analyse legal and financial contracts. Risk assessment, compliance.
Report Generation Generate regulatory reports. Automated report writing.
Document Classification Classify financial documents. Invoices, statements, applications.
3.3 Internal Operations
 
 
Use Case Description Example
Code Generation Generate code for data analysis. Accelerated development.
Knowledge Retrieval Access organisational knowledge. Internal Q&A systems.
Meeting Summaries Summarise meetings and decisions. Automated minutes.
Training Materials Generate training content. Onboarding, compliance training.
Risk Analysis Generate risk reports and analysis. Automated risk assessments.

SECTION 4: RETRIEVAL-AUGMENTED GENERATION (RAG)

4.1 What is RAG?

Retrieval-Augmented Generation (RAG) is a technique that combines a retrieval system with a generative model to produce more accurate, relevant, and factually grounded responses by retrieving relevant documents from a knowledge base.

4.2 RAG Architecture
text
┌─────────────────────────────────────────────────────────────────────────────┐
│                    RAG ARCHITECTURE                                       │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │                    USER QUERY                                       │   │
│  │  (Customer question, document request)                              │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │                    RETRIEVAL SYSTEM                                 │   │
│  │  (Vector search, keyword search)                                   │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │                    KNOWLEDGE BASE                                   │   │
│  │  (Documents, policies, product information, customer data)         │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │                    CONTEXT ENRICHMENT                               │   │
│  │  (Add retrieved documents to prompt)                               │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │                    GENERATIVE MODEL                                 │   │
│  │  (LLM generates response based on context)                         │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                    │                                        │
│                                    v                                        │
│  ┌──────────────────────────────────────────────────────────────────────┐   │
│  │                    RESPONSE                                         │   │
│  │  (Accurate, grounded, relevant)                                    │   │
│  └──────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
4.3 RAG Benefits
 
 
Benefit Description
Accuracy Grounded in retrieved documents.
Up-to-Date Uses current information.
Transparent Sources can be cited.
Secure Data stays in controlled environment.
Cost-Effective Uses smaller models with retrieval.

SECTION 5: PROMPT ENGINEERING

5.1 Key Prompt Engineering Techniques
 
 
Technique Description Financial Example
Zero-Shot No examples, just instruction. “Summarise this annual report.”
Few-Shot Provide examples in the prompt. “Summarise like this example.”
Chain-of-Thought Step-by-step reasoning. “Let’s think step-by-step…”
Role Prompting Assign a role to the model. “Act as a financial analyst.”
Structured Output Request specific format. “Return JSON with these fields.”
5.2 Prompt Engineering Best Practices
 
 
Practice Description Example
Be Specific Clearly state what you want. “Summarise in 3 bullet points.”
Provide Context Give background information. “This is a loan application document.”
Set Constraints Define limits and requirements. “Max 100 words, professional tone.”
Use Examples Show desired output format. “Like this example: …”
Iterate Refine prompts based on results. Test and improve.

SECTION 6: IMPLEMENTATION IN PYTHON – GENERATIVE AI

python
# ===================================================================
# MODULE 4, LESSON 6: GENERATIVE AI AND LLMS IN BANKING
# ===================================================================

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import json
import hashlib
from datetime import datetime
import warnings
warnings.filterwarnings('ignore')

print("="*70)
print("GENERATIVE AI AND LARGE LANGUAGE MODELS (LLMS) IN BANKING")
print("="*70)

# ----------------------------------------------------------------
# PART A: GENERATIVE AI USE CASES
# ----------------------------------------------------------------

print("\n" + "-"*60)
print("PART A: Generative AI Use Cases in Banking")
print("-"*60)

use_cases = pd.DataFrame({
    'Use Case': [
        'Customer Service Chatbot',
        'Document Summarisation',
        'KYC Document Processing',
        'Report Generation',
        'Contract Analysis',
        'Code Generation',
        'Knowledge Management',
        'Email Generation',
        'Meeting Summaries'
    ],
    'Impact (1-5)': [5, 4, 5, 4, 4, 3, 4, 3, 3],
    'Complexity (1-5)': [3, 2, 4, 3, 4, 3, 3, 2, 2],
    'Maturity (1-5)': [4, 4, 3, 3, 2, 4, 3, 4, 4],
    'Priority': ['High', 'High', 'High', 'Medium', 'Medium', 'Medium', 'Medium', 'Medium', 'Low']
})

print("Generative AI Use Cases:")
print(use_cases.to_string(index=False))

# ----------------------------------------------------------------
# PART B: SIMULATED LLM RESPONSES
# ----------------------------------------------------------------

print("\n" + "-"*60)
print("PART B: Simulated LLM Responses")
print("-"*60)

class FinancialLLM:
    """Simulate a financial LLM for banking applications."""
    
    def __init__(self):
        self.prompt_history = []
    
    def generate_response(self, prompt, context=""):
        """Generate a simulated LLM response."""
        self.prompt_history.append({
            'prompt': prompt,
            'context': context,
            'timestamp': datetime.now().isoformat()
        })
        
        # Simulate different types of responses
        if "summarise" in prompt.lower() or "summarize" in prompt.lower():
            return "This document outlines the bank's Q4 2024 financial performance. Revenue increased by 12% to $1.2B, driven by growth in digital banking and lending. Operating costs decreased by 8% due to automation. The bank is well-positioned for 2025 with strong capital ratios and a robust digital strategy."
        
        elif "contract" in prompt.lower():
            return "The contract contains 15 clauses covering: payment terms (net 30), service level agreements (99.9% uptime), confidentiality obligations, termination conditions (30-day notice), and liability limitations. Key risks: indemnification clause, IP ownership, and dispute resolution (arbitration in London)."
        
        elif "kyc" in prompt.lower() or "verify" in prompt.lower():
            return "KYC verification completed. Customer identity verified using government ID and biometric check. PEP screening clear. Sanctions check clear. Risk rating: Low. Recommended: Proceed with account opening."
        
        elif "loan" in prompt.lower():
            return "Based on the customer's profile (income: $85,000, credit score: 720, DTI: 28%), the loan application is assessed as low risk. Recommended: Approve $250,000 mortgage at 5.5% APR with 30-year term."
        
        elif "code" in prompt.lower():
            return "```python\nimport pandas as pd\n\ndef calculate_roi(investment, returns):\n    return (returns / investment) * 100\n\n# Example usage\ndf = pd.read_csv('investment_data.csv')\ndf['roi'] = df['returns'].apply(lambda x: calculate_roi(100000, x))\n```"
        
        else:
            return "I understand your query. As your financial assistant, I can help with account information, transaction history, product recommendations, and financial guidance. How can I assist you today?"
    
    def rag_response(self, query, documents):
        """Simulate RAG-enhanced response."""
        # Simulate retrieval
        retrieved = documents[:3] if len(documents) > 0 else []
        context = " ".join(retrieved)
        return self.generate_response(query, context)

# Create financial LLM
llm = FinancialLLM()

# Test different use cases
print("1. Document Summarisation:")
print(llm.generate_response("Summarise this annual report for me"))

print("\n2. Contract Analysis:")
print(llm.generate_response("Analyse this contract for key risks"))

print("\n3. KYC Verification:")
print(llm.generate_response("Verify KYC documents for customer"))

print("\n4. Loan Assessment:")
print(llm.generate_response("Assess this loan application"))

print("\n5. Code Generation:")
print(llm.generate_response("Write Python code to calculate ROI"))

# ----------------------------------------------------------------
# PART C: RETRIEVAL-AUGMENTED GENERATION (RAG) SIMULATION
# ----------------------------------------------------------------

print("\n" + "-"*60)
print("PART C: RAG Simulation")
print("-"*60)

# Simulate a knowledge base
knowledge_base = [
    "Our mortgage products include fixed-rate (3.5-5.5% APR) and variable-rate (2.5-4.5% APR) options.",
    "Customer service hours: Monday-Friday 8am-8pm, Saturday 9am-5pm, Sunday closed.",
    "Credit card offers: Standard (0% APR for 12 months), Premium (2% cashback), Travel (3x points on travel).",
    "Savings accounts: Regular (2.5% APY), High-Yield (4.0% APY), and Money Market (3.5% APY).",
    "Loan application process: Complete online form → Submit documents → Review (2-3 days) → Decision.",
    "Digital banking features: Mobile check deposit, bill pay, P2P transfers, budgeting tools, credit score monitoring.",
    "Security: Biometric authentication, real-time fraud monitoring, 24/7 account security, zero-liability fraud protection.",
    "Investment options: Stocks, ETFs, mutual funds, bonds, and advisory services."
]

def rag_query(query, knowledge_base, llm):
    """Simulate a RAG query."""
    # Simple retrieval: keyword matching
    retrieved = [doc for doc in knowledge_base if any(word in doc.lower() for word in query.lower().split())]
    
    if not retrieved:
        retrieved = ["No specific information found. Please contact customer service for assistance."]
    
    print(f"Query: {query}")
    print(f"\nRetrieved Documents ({len(retrieved)}):")
    for doc in retrieved:
        print(f"  • {doc}")
    
    print("\nGenerated Response:")
    response = llm.rag_response(query, retrieved)
    print(f"  {response}")
    
    return response

# Test RAG
print("RAG Query Example:")
rag_query("What mortgage products do you offer?", knowledge_base, llm)
print("\n" + "-"*40)
rag_query("What are your customer service hours?", knowledge_base, llm)

# ----------------------------------------------------------------
# PART D: PROMPT ENGINEERING EXAMPLES
# ----------------------------------------------------------------

print("\n" + "-"*60)
print("PART D: Prompt Engineering Examples")
print("-"*60)

prompt_examples = {
    "Zero-Shot": {
        "Prompt": "Summarise this loan agreement in 3 bullet points.",
        "Response": llm.generate_response("Summarise this loan agreement in 3 bullet points")
    },
    "Few-Shot": {
        "Prompt": "Example 1: 'High revenue growth' → Positive.\nExample 2: 'Declining market share' → Negative.\nClassify: 'Strong digital adoption'",
        "Response": llm.generate_response("Classify: 'Strong digital adoption'")
    },
    "Chain-of-Thought": {
        "Prompt": "Let's think step-by-step. A customer has income $80k, debt $20k, and wants to borrow $50k. Is this likely to be approved?",
        "Response": llm.generate_response("Analyse this loan application step-by-step")
    },
    "Role Prompting": {
        "Prompt": "Act as a senior credit analyst. Assess the risk of a borrower with income $60k, credit score 650, DTI 35%.",
        "Response": llm.generate_response("Assess this borrower as a senior credit analyst")
    },
    "Structured Output": {
        "Prompt": "Return as JSON: customer name, account balance, and transaction history.",
        "Response": llm.generate_response("Return customer data as JSON")
    }
}

for technique, data in prompt_examples.items():
    print(f"\n{technique}:")
    print(f"  Prompt: {data['Prompt']}")
    print(f"  Response: {data['Response']}")

# ----------------------------------------------------------------
# PART E: GENERATIVE AI RISKS AND MITIGATIONS
# ----------------------------------------------------------------

print("\n" + "-"*60)
print("PART E: Generative AI Risks and Mitigations")
print("-"*60)

risks = pd.DataFrame({
    'Risk': [
        'Hallucinations',
        'Bias',
        'Data Privacy',
        'Security',
        'Regulatory Compliance',
        'Intellectual Property',
        'Model Drift',
        'Cost'
    ],
    'Likelihood (1-5)': [4, 3, 4, 3, 4, 2, 3, 3],
    'Impact (1-5)': [5, 4, 5, 5, 5, 3, 3, 3],
    'Mitigation': [
        'RAG, fact-checking, human review',
        'Diverse training data, bias testing',
        'On-premise deployment, data anonymisation',
        'Access controls, encryption',
        'Governance framework, regular audits',
        'Clear usage policies, licensing review',
        'Regular monitoring, retraining',
        'Cost-benefit analysis, optimised models'
    ]
})

print("Generative AI Risks and Mitigations:")
print(risks.to_string(index=False))

# ----------------------------------------------------------------
# PART F: GENERATIVE AI ROADMAP
# ----------------------------------------------------------------

print("\n" + "-"*60)
print("PART F: Generative AI Roadmap")
print("-"*60)

roadmap = {
    "Phase 1 (0-6 months) – Foundation": {
        "Focus": "Build foundational capabilities.",
        "Activities": [
            "Identify high-impact use cases (customer service, document processing).",
            "Build RAG pipeline for domain-specific knowledge.",
            "Develop prompt engineering capabilities.",
            "Implement governance and security framework."
        ],
        "Success Metrics": ["2+ use cases piloted", "RAG pipeline operational"]
    },
    "Phase 2 (6-12 months) – Scaling": {
        "Focus": "Scale generative AI across the organisation.",
        "Activities": [
            "Deploy intelligent chatbots across customer touchpoints.",
            "Automate document processing and report generation.",
            "Implement knowledge management system.",
            "Enable self-service generative AI for employees."
        ],
        "Success Metrics": ["CSAT > 80%", "Document processing time reduced by 70%"]
    },
    "Phase 3 (12-24 months) – Advanced": {
        "Focus": "Advanced generative AI applications.",
        "Activities": [
            "Implement AI-driven decision support.",
            "Build personalised customer communications.",
            "Enable generative AI for code and data analysis.",
            "Develop industry-leading capabilities."
        ],
        "Success Metrics": ["AI-driven decisions > 50%", "Revenue uplift > 10%"]
    },
    "Phase 4 (24+ months) – Innovation": {
        "Focus": "Innovate with generative AI.",
        "Activities": [
            "Explore multi-modal AI (text, voice, image).",
            "Build autonomous AI agents.",
            "Develop custom fine-tuned models.",
            "Achieve industry leadership in generative AI."
        ],
        "Success Metrics": ["Industry-leading generative AI", "Continuous innovation"]
    }
}

for phase, details in roadmap.items():
    print(f"\n{phase}:")
    print(f"  Focus: {details['Focus']}")
    print("  Activities:")
    for activity in details['Activities']:
        print(f"    • {activity}")
    print("  Success Metrics:")
    for metric in details['Success Metrics']:
        print(f"    • {metric}")

# ----------------------------------------------------------------
# PART G: GENERATIVE AI METRICS
# ----------------------------------------------------------------

print("\n" + "-"*60)
print("PART G: Generative AI Metrics")
print("-"*60)

genai_metrics = pd.DataFrame({
    'Metric': [
        'Response Accuracy',
        'Response Relevance',
        'Customer Satisfaction (CSAT)',
        'Task Completion Rate',
        'Hallucination Rate',
        'Response Time (ms)',
        'Cost per Interaction',
        'Adoption Rate'
    ],
    'Current Value': [
        '82%',
        '78%',
        '72%',
        '65%',
        '8%',
        '450ms',
        '$0.25',
        '35%'
    ],
    'Target Value': [
        '> 90%',
        '> 85%',
        '> 80%',
        '> 80%',
        '< 2%',
        '< 300ms',
        '< $0.10',
        '> 70%'
    ],
    'Status': ['🟡', '🟡', '🟡', '🔴', '🔴', '🟡', '🟡', '🔴']
})

print("Generative AI Metrics:")
print(genai_metrics.to_string(index=False))

# ----------------------------------------------------------------
# PART H: SUMMARY AND RECOMMENDATIONS
# ----------------------------------------------------------------

print("\n" + "="*70)
print("PART H: Summary and Recommendations")
print("="*70)

print("""
Generative AI and LLMs in Banking – Key Takeaways:

1. Generative AI creates new content, summarises, and generates code.
2. Key use cases: customer service, document processing, report generation, contract analysis.
3. RAG (Retrieval-Augmented Generation) improves accuracy by grounding responses in documents.
4. Prompt engineering: zero-shot, few-shot, chain-of-thought, role prompting, structured output.
5. Risks: hallucinations, bias, data privacy, security, regulatory compliance.
6. Key metrics: accuracy, relevance, CSAT, hallucination rate, response time.
7. Roadmap: foundation → scaling → advanced → innovation.

Recommendations:
  - Start with high-impact use cases (customer service, document summarisation).
  - Implement RAG for domain-specific applications.
  - Develop prompt engineering capabilities.
  - Implement governance and security framework.
  - Measure and optimise generative AI performance.
  - Continuously improve with feedback and fine-tuning.
""")

print("="*70)
print("END OF LESSON 6 – MODULE 4")
print("="*70)

SECTION 7: SUMMARY FOR THE DATA PRACTITIONER

  • Generative AI creates new content, summarises documents, generates code, and answers questions.

  • Key use cases include customer service chatbots, document processing, report generation, contract analysis, and knowledge management.

  • Retrieval-Augmented Generation (RAG) improves accuracy by grounding responses in retrieved documents.

  • Prompt engineering techniques include zero-shot, few-shot, chain-of-thought, role prompting, and structured output.

  • Risks include hallucinations, bias, data privacy, security, and regulatory compliance.

  • Key metrics include response accuracy, relevance, CSAT, hallucination rate, and response time.

  • Roadmap progresses from foundation to scaling, advanced, and innovation phases.


SECTION 8: RECOMMENDED NEXT STEPS

  1. Start with high-impact use cases (customer service, document summarisation).

  2. Implement RAG for domain-specific applications.

  3. Develop prompt engineering capabilities.

  4. Implement governance and security framework.

  5. Measure and optimise generative AI performance.

  6. Continuously improve with feedback and fine-tuning.

  7. Prepare for Lesson 7: Credit Risk Modelling with AI.