# FinGPT

> Open-source financial large language models you can fine-tune for under $300

- Category: Training & Fine-Tuning / Fine-Tuning Frameworks
- Language: Jupyter Notebook
- Licence: MIT
- Repository: https://github.com/AI4Finance-Foundation/FinGPT
- Website: https://fingpt.io
- Canonical page: https://ai-tldr.dev/tools/fingpt/

## In short

FinGPT, from the AI4Finance Foundation, is an open-source effort to build financial large language models that anyone can run and adapt. It sits in the Fine-Tuning Frameworks category of the Training & Fine-Tuning tools and is open-source, written in Jupyter Notebook and licensed MIT. To get started: install the libraries.

## Overview

FinGPT, from the AI4Finance Foundation, is an open-source effort to build financial large language models that anyone can run and adapt. Instead of training a costly model from scratch like BloombergGPT, FinGPT takes strong open base models and fine-tunes them with the lightweight LoRA method, so a fresh model can be produced for less than $300 per run.

The project ships ready-to-use models on Hugging Face for tasks such as financial sentiment analysis, relation extraction, headline classification, named-entity recognition, and the FinGPT-Forecaster, which reads recent market news and predicts the next week's stock-price direction. Because finance data changes constantly, the design favors fast, frequent re-tuning over expensive one-time training.

## What it does

- Open-source financial LLMs published on Hugging Face for sentiment analysis, relation extraction, headline classification, and named-entity recognition
- LoRA fine-tuning that adapts large base models cheaply, reportedly under $300 and runnable on a single RTX 3090 in 8-bit or int4 (QLoRA)
- Multi-task models built on open bases including Llama-2, Falcon, Bloom, MPT, ChatGLM2, and Qwen
- FinGPT-Forecaster: enter a ticker, a date, and a news look-back window to get a company analysis and a next-week price-movement prediction
- Public instruction-tuning datasets for sentiment, financial relations, headlines, NER, and Q&A to reproduce and extend results
- Five-layer full-stack framework spanning data source, data engineering, LLMs, tasks, and applications

## Getting started

FinGPT models are loaded with Hugging Face Transformers and PEFT: you load an open base model and then apply the FinGPT LoRA adapter on top. The example below runs the v3 financial sentiment model.

1. Install the libraries

   Install Transformers and PEFT (plus a GPU-capable PyTorch). The v3 sentiment example was tested with PEFT 0.5.0.

```bash
pip install transformers peft torch
```

2. Load the base model and the FinGPT LoRA adapter

   Load the open base model from Hugging Face, then attach the FinGPT sentiment LoRA weights with PeftModel.

```python
from transformers import LlamaForCausalLM, LlamaTokenizerFast
from peft import PeftModel  # 0.5.0

base_model = "NousResearch/Llama-2-13b-hf"
peft_model = "FinGPT/fingpt-sentiment_llama2-13b_lora"
tokenizer = LlamaTokenizerFast.from_pretrained(base_model, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
model = LlamaForCausalLM.from_pretrained(base_model, trust_remote_code=True, device_map="cuda:0", load_in_8bit=True)
model = PeftModel.from_pretrained(model, peft_model)
model = model.eval()
```

3. Run a financial sentiment prompt

   Format an instruction prompt, generate, and read the answer after the 'Answer:' marker.

```python
prompt = ['''Instruction: What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}
Input: FINANCING OF ASPOCOMP 'S GROWTH Aspocomp is aggressively pursuing its growth strategy.
Answer: ''']

tokens = tokenizer(prompt, return_tensors='pt', padding=True, max_length=512)
res = model.generate(**tokens, max_length=512)
res_sentences = [tokenizer.decode(i) for i in res]
out_text = [o.split("Answer: ")[1] for o in res_sentences]
for sentiment in out_text:
    print(sentiment)
```

## When to use it

- Classify the sentiment of financial news headlines and tweets as positive, neutral, or negative
- Fine-tune your own low-cost financial LLM with LoRA on a single consumer GPU instead of paying for a from-scratch model
- Forecast a stock's next-week price direction from recent market news with FinGPT-Forecaster
- Extract financial relations and named entities from filings and news for downstream analysis
