Implementation:AI4Finance Foundation FinGPT query gpt4
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, NLP, Finance |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Concrete tool for generating GPT-4 analyst labels for financial data provided by the FinGPT Forecaster training pipeline.
Description
The query_gpt4 function performs knowledge distillation by sending constructed prompts (from market data) to GPT-4 and collecting its financial analysis as training labels. It iterates over a list of ticker symbols, constructs prompts via `get_all_prompts`, sends them to the OpenAI chat completions API with a system prompt defining the analyst persona, and saves results to per-symbol `_gpt-4.csv` files with `prompt` and `answer` columns.
The function supports resumption: if the output CSV already exists, it skips already-completed rows. It implements retry logic (up to 5 attempts) for API rate limits and transient errors.
Usage
Import this function when you need to generate GPT-4 analysis labels for training data:
- Knowledge distillation: Generating expert-level financial analysis as training labels for LoRA fine-tuning.
- Batch labeling: Processing multiple ticker symbols with progress tracking.
Code Reference
Source Location
- Repository: FinGPT
- File: fingpt/FinGPT_Forecaster/data.py
- Lines: L175-238
Signature
def query_gpt4(
symbol_list: list,
data_dir: str,
start_date: str,
end_date: str,
min_past_weeks: int = 1,
max_past_weeks: int = 3,
with_basics: bool = True,
with_market_sentiment: bool = False,
) -> None
Import
from data import query_gpt4
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| symbol_list | list | Yes | List of ticker symbols (e.g., ["AAPL", "MSFT"]) |
| data_dir | str | Yes | Directory containing per-symbol CSV files from prepare_data_for_symbol |
| start_date | str | Yes | Start date YYYY-MM-DD |
| end_date | str | Yes | End date YYYY-MM-DD |
| min_past_weeks | int | No | Minimum lookback weeks for prompt context (default 1) |
| max_past_weeks | int | No | Maximum lookback weeks for prompt context (default 3) |
| with_basics | bool | No | Must match prepare_data_for_symbol (default True) |
| with_market_sentiment | bool | No | Must match prepare_data_for_symbol (default False) |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | None | No return value |
| CSV files | files | Per-symbol `_gpt-4.csv` with columns: prompt, answer |
Usage Examples
Generate GPT-4 Labels for DOW30
import os
os.environ["OPENAI_KEY"] = "your_openai_key"
os.environ["FINNHUB_KEY"] = "your_finnhub_key"
from data import query_gpt4
from indices import DOW_30
# Generate GPT-4 analysis for all DOW30 stocks
query_gpt4(
symbol_list=DOW_30,
data_dir="./data",
start_date="2023-01-01",
end_date="2023-12-31",
min_past_weeks=1,
max_past_weeks=3,
with_basics=True,
)
# Results saved to ./data/{symbol}_gpt-4.csv