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

Implementation:AI4Finance Foundation FinGPT extract classification

From Leeroopedia
Knowledge Sources
Domains NLP, Sentiment_Analysis, Finance
Last Updated 2026-10-18 00:00 GMT

Overview

Concrete tool for classifying financial statement sentiment using the OpenAI chat completions API provided by the FinGPT RAG pipeline.

Description

The extract_classification function sends a financial statement with a classification prompt to the OpenAI chat completions API (`gpt-3.5-turbo`). The system prompt instructs the model to act as a financial analyst answering a classification task. The classification prompt asks the model to determine the sentiment as "negative," "neutral," or "positive" based on existing knowledge.

The GUI-driven script `sentiment_classification_by_external_LLMs.py` calls this function for each row in a CSV file, saving results as numeric codes (0=negative, 1=positive, 2=neutral) in a new column `openai_inferred_sentiment_from_RAG`.

Usage

Import this function when you need to classify financial text sentiment via external LLM:

  • RAG sentiment classification: Classifying statements with optional retrieved context.
  • Batch classification: Processing CSV files via the GUI script.

Code Reference

Source Location

  • Repository: FinGPT
  • File: fingpt/FinGPT_RAG/multisource_retrieval/external_LLMs/external_LLMs.py (L9-43), fingpt/FinGPT_RAG/multisource_retrieval/utils/sentiment_classification_by_external_LLMs.py (L1-58)

Signature

def extract_classification(
    text: str,
    classification_prompt: str,
) -> str

Import

from external_LLMs import extract_classification

I/O Contract

Inputs

Name Type Required Description
text str Yes Financial statement to classify
classification_prompt str Yes Sentiment instruction prompt

Outputs

Name Type Description
return value str Classification response ("negative", "neutral", or "positive")

Usage Examples

Classify Financial Statement Sentiment

import os
os.environ["OPENAI_API_KEY"] = "your_key"

from external_LLMs import extract_classification

text = "Apple reported record quarterly earnings, beating expectations."
prompt = '. For financial statement above, determine its sentiment (based on your existing knowledge). Your answer should be either "negative" or "neutral" or "positive"'

result = extract_classification(text, prompt)
print(result)  # "positive"

Related Pages

Implements Principle

Requires Environment

Page Connections

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