Implementation:AI4Finance Foundation FinGPT model generate
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Inference |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
HuggingFace model generation API used in the FinGPT Forecaster for producing stock price movement predictions via sampling-based decoding.
Description
The model.generate call in the Forecaster's `predict` function generates a financial analysis prediction from a constructed prompt. It uses sampling-based generation (`do_sample=True`) with `max_length=4096`, streaming output via `TextStreamer`, and stops at the EOS token. The response is decoded and the answer extracted by stripping everything before `[/INST]`.
The GenerationEvalCallback also uses `model.generate` during training evaluation, but with `use_cache=True` and without sampling or streaming.
Usage
Use this when you need to generate predictions from a fine-tuned model:
- Forecaster inference: Generating stock price movement analysis from live prompts.
- Training evaluation: Generating predictions on eval samples during training (via callback).
Code Reference
Source Location
- Repository: FinGPT
- File: fingpt/FinGPT_Forecaster/app.py
- Lines: L272-295 (predict), L59-62 (callback)
Signature
# Inference (app.py predict)
res = model.generate(
**inputs,
max_length: int = 4096,
do_sample: bool = True,
eos_token_id: int = tokenizer.eos_token_id,
use_cache: bool = True,
streamer: TextStreamer = streamer,
) -> torch.LongTensor
# Evaluation (callback)
res = model.generate(
**inputs,
use_cache: bool = True,
) -> torch.LongTensor
Import
from transformers import TextStreamer
# model.generate is a method on the loaded PeftModel
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| inputs | dict | Yes | Tokenized inputs (input_ids, attention_mask) on model.device |
| max_length | int | No | Maximum generation length (default 4096) |
| do_sample | bool | No | Enable sampling (True for inference, False for eval) |
| eos_token_id | int | No | Token ID for end-of-sequence |
| use_cache | bool | No | Enable KV cache (True) |
| streamer | TextStreamer | No | Streaming output callback |
Outputs
| Name | Type | Description |
|---|---|---|
| return value | torch.LongTensor | Generated token IDs |
| answer | str | Decoded text after [/INST] extraction (via regex) |
Usage Examples
Generate Prediction
import re
import torch
from transformers import TextStreamer
streamer = TextStreamer(tokenizer)
inputs = tokenizer(prompt, return_tensors='pt', padding=False)
inputs = {k: v.to(model.device) for k, v in inputs.items()}
res = model.generate(
**inputs, max_length=4096, do_sample=True,
eos_token_id=tokenizer.eos_token_id,
use_cache=True, streamer=streamer,
)
output = tokenizer.decode(res[0], skip_special_tokens=True)
answer = re.sub(r'.*\[/INST\]\s*', '', output, flags=re.DOTALL)
Related Pages
Implements Principle
- Principle:AI4Finance_Foundation_FinGPT_Model_Inference
- Principle:AI4Finance_Foundation_FinGPT_Prompt_Engineering