Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Environment:AI4Finance Foundation FinGPT OpenAI API

From Leeroopedia
Knowledge Sources
Domains LLMs, API
Last Updated 2026-10-18 00:00 GMT

Overview

OpenAI API access for GPT-4 label distillation and GPT-3.5-turbo sentiment classification via the `openai` Python SDK.

Description

This environment provides access to OpenAI's chat completion and completion APIs. The Forecaster pipeline uses GPT-4 as a teacher model to generate training labels (knowledge distillation), while the RAG multisource retrieval module uses GPT-3.5-turbo for sentiment classification and `text-davinci-003` for keyword extraction. The `openai` Python package (>=0.28.1 in RAG, latest for Forecaster) is required.

Usage

Use this environment for Knowledge Distillation (GPT-4 label generation), Sentiment Classification (GPT-3.5-turbo), and Keyword Extraction (text-davinci-003). Required by `query_gpt4`, `extract_keywords`, and `extract_classification`.

System Requirements

Category Requirement Notes
OS Any HTTP API
Hardware CPU sufficient No GPU needed
Network Internet Required for API calls

Dependencies

Python Packages

  • `openai` >= 0.28.1 (RAG); latest for Forecaster (`from openai import OpenAI`)
  • `requests` >= 2.31.0 (for direct API calls in RAG)
  • `python-dotenv` >= 1.0.0

Credentials

The following environment variables must be set:

  • `OPENAI_API_KEY` (or `OPENAI_KEY`): OpenAI API key. Must not be placeholder `"your openai key"`.
  • `OPENAI_API_URL` (optional): Custom API endpoint (used by RAG keyword extraction).

Quick Install

pip install openai python-dotenv

Code Evidence

GPT-4 client initialization from `fingpt/FinGPT_Forecaster/data.py:22`:

client = OpenAI(api_key=os.environ.get("OPENAI_KEY"))

GPT-4 completion with retry logic from `fingpt/FinGPT_Forecaster/data.py:223-237`:

cnt = 0
while cnt < 5:
    try:
        completion = client.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": prompt}
            ]
        )
        break
    except Exception:
        cnt += 1
        print(f'retry cnt {cnt}')

GPT-3.5-turbo classification from `fingpt/FinGPT_RAG/multisource_retrieval/external_LLMs/external_LLMs.py:19-29`:

payload = {
    'model': 'gpt-3.5-turbo',
    "messages": [
        {"role": "system", "content": "You are a financial analyst..."},
        {"role": "user", "content": "..." + text + '"' + classification_prompt}
    ],
}

Common Errors

Error Message Cause Solution
`OPENAI_API_KEY is not set or is still placeholder` Key not configured Set `OPENAI_API_KEY` or `OPENAI_KEY` environment variable
`RateLimitError` Too many requests Code retries up to 5 times; add exponential backoff
`Model not found: text-davinci-003` Deprecated model Use newer completion model; update `chatGPT_tokenization.py`

Compatibility Notes

  • Variable name inconsistency: Forecaster `data.py` uses `OPENAI_KEY`; RAG scripts and `test_api_keys.py` use `OPENAI_API_KEY`.
  • Deprecated models: `text-davinci-003` is deprecated by OpenAI; RAG keyword extraction may need migration to newer models.
  • SDK versions: Forecaster uses `from openai import OpenAI` (v1.x SDK); RAG pins `openai==0.28.1` (v0.x SDK).

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment