Principle:AI4Finance Foundation FinGPT Retrieval Augmented Generation
| Knowledge Sources | |
|---|---|
| Domains | NLP, Information_Retrieval, Finance |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
A technique that enhances language model responses by retrieving relevant external context (news articles, financial reports) and incorporating it into the model's input prompt before generation.
Description
Retrieval-Augmented Generation (RAG) addresses the limitation that LLMs have fixed, potentially outdated knowledge. In FinGPT's RAG sentiment analysis pipeline, the system: (1) scrapes financial news from multiple sources (Yahoo Finance, CNBC, Google, MarketScreener, Seeking Alpha), (2) extracts keywords from financial statements using an LLM, (3) retrieves relevant articles based on similarity matching, (4) augments the input with retrieved context, and (5) classifies sentiment using an instruction-tuned LLM.
The retrieval step uses a lightweight word-overlap similarity score to filter relevant articles. Retrieved context is appended to the financial statement before classification, giving the model additional information to make more accurate sentiment predictions.
Usage
Use this principle when:
- External knowledge is needed: Financial statements require context from recent news for accurate sentiment analysis.
- Knowledge is time-sensitive: Financial markets change rapidly; RAG provides up-to-date context.
- Multi-source aggregation: Combining information from Yahoo, CNBC, Google, MarketScreener, and Seeking Alpha.
Theoretical Basis
RAG combines retrieval and generation in a two-stage process:
Where is a retrieved document and is the retrieval probability. In the FinGPT implementation, retrieval is deterministic (keyword-based similarity) rather than learned:
Failed to parse (syntax error): {\displaystyle \text{sim}(a, b) = \frac{|\text{matching\_words}(a, b)|}{\min(|\text{words}(a)|, |\text{words}(b)|)} }
If similarity exceeds a threshold (0.8 for most sources, 0.75 for Google), the article context is appended to the input.
Pipeline structure:
# Abstract: RAG for financial sentiment
keywords = llm.extract_keywords(statement) # LLM-based keyword extraction
articles = scrape_news(keywords) # Multi-source news scraping
relevant = filter_by_similarity(articles, keywords, threshold=0.8)
context = relevant[0].text # Append context
sentiment = llm.classify(statement + context)
Practical Guide
- Set API keys (`OPENAI_API_KEY`) before running the pipeline.
- Adjust `similarity_threshold` per source (0.8 default, 0.75 for Google).
- The `scraping_by_url` function dispatches to source-specific scrapers based on URL domain.
- Use `easygui` for interactive CSV file selection in the classification and verification scripts.
Related Pages
Implemented By
- Implementation:AI4Finance_Foundation_FinGPT_scraping_by_url
- Implementation:AI4Finance_Foundation_FinGPT_extract_keywords
- Implementation:AI4Finance_Foundation_FinGPT_extract_classification
- Implementation:AI4Finance_Foundation_FinGPT_similarity_score