Created
February 3, 2025 18:08
-
-
Save mtanco/0090292cf99c84236633548d3081b91d to your computer and use it in GitHub Desktop.
h2oGPTe Banking RAG Demo - Intelligent Document Processing for Financial Services This demo showcases how to leverage h2oGPTe's powerful RAG capabilities for banking and financial services use cases. It demonstrates:
- Automated knowledge base creation from banking documentation
- Intelligent Q&A using RAG
- Best practices for financial documen…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
h2oGPTe Banking RAG Demo | |
Demonstrates using h2oGPTe for intelligent document processing in financial services | |
This demo shows how to: | |
1. Crawl relevant banking documentation | |
2. Create a knowledge base | |
3. Perform intelligent Q&A using RAG | |
""" | |
import h2ogpte | |
from h2ogpte import H2OGPTE | |
import requests | |
from typing import List | |
import logging | |
# Setup logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
class BankingKnowledgeBase: | |
def __init__(self, api_key: str): | |
"""Initialize the banking knowledge base with h2oGPTe""" | |
self.client = H2OGPTE(api_key=api_key) | |
self.sources = [] | |
def add_wikipedia_sources(self) -> None: | |
"""Add relevant banking Wikipedia pages to the knowledge base""" | |
banking_pages = [ | |
"Federal_Reserve", | |
"Investment_banking", | |
"Retail_banking", | |
"Financial_technology" | |
] | |
for page in banking_pages: | |
url = f"https://en.wikipedia.org/wiki/{page}" | |
try: | |
# Add document to h2oGPTe knowledge base | |
self.client.add_document( | |
url, | |
metadata={"source": "wikipedia", "topic": page} | |
) | |
self.sources.append(url) | |
logger.info(f"Added {url} to knowledge base") | |
except Exception as e: | |
logger.error(f"Error adding {url}: {e}") | |
def ask_question(self, question: str) -> str: | |
"""Query the knowledge base using RAG""" | |
try: | |
response = self.client.query( | |
question, | |
search_options={"sources": self.sources} | |
) | |
return response.text | |
except Exception as e: | |
logger.error(f"Error querying knowledge base: {e}") | |
return None | |
def main(): | |
"""Main demo function""" | |
# Initialize with your API key | |
api_key = "YOUR_H2OGPTE_API_KEY" | |
kb = BankingKnowledgeBase(api_key) | |
# Build knowledge base | |
logger.info("Building banking knowledge base...") | |
kb.add_wikipedia_sources() | |
# Example questions | |
questions = [ | |
"What are the key differences between retail and investment banking?", | |
"How is fintech impacting traditional banking services?", | |
"What role does the Federal Reserve play in the US banking system?" | |
] | |
# Demonstrate RAG capabilities | |
logger.info("Demonstrating RAG capabilities...") | |
for question in questions: | |
logger.info(f"\nQuestion: {question}") | |
answer = kb.ask_question(question) | |
logger.info(f"Answer: {answer}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment