Skip to content

Commit 9fc3623

Browse files
authored
Merge pull request #33 from AKKI0511/implement-sentiment-analysis-with-litellm
Add LiteLLM sentiment analysis
2 parents 2460110 + 01cfd15 commit 9fc3623

File tree

13 files changed

+853
-10
lines changed

13 files changed

+853
-10
lines changed

LLM.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,34 @@ sharpe = sharpe_ratio(returns, risk_free_rate=0.02)
241241
3. Test individual components
242242
4. Verify configuration parameters
243243

244+
## LLM Sentiment Analysis
245+
246+
LiteLLM provides a unified interface for scoring text sentiment. Enable it through `config/features_config.yaml`:
247+
248+
```yaml
249+
sentiment:
250+
enabled: true
251+
provider: openai
252+
model: gpt-3.5-turbo
253+
api_key_env_var: OPENAI_API_KEY
254+
```
255+
256+
Set the corresponding API key:
257+
258+
```bash
259+
export OPENAI_API_KEY="sk-..."
260+
```
261+
262+
Switch providers by editing `provider`, `model`, and `api_key_env_var`. The data pipeline automatically adds a `sentiment_score` column during the `generate_sentiment` step.
263+
264+
For CLI and Python examples see [docs/llm-sentiment.md](docs/llm-sentiment.md).
265+
244266
## Future Development Areas
245267

246268
### High Priority
247269
- Real-time data streaming
248270
- Advanced risk management
249271
- Multi-timeframe support
250-
- LLM integration for sentiment analysis
251272

252273
### Medium Priority
253274
- GPU acceleration

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ poetry run quanttradeai evaluate -m models/trained/AAPL
2727
- **📊 Multi-source Data Fetching** - YFinance, AlphaVantage with intelligent caching
2828
- **🔧 Advanced Feature Engineering** - 20+ technical indicators and custom features
2929
- **🤖 Ensemble ML Models** - Voting Classifier with LR, RF, XGBoost
30+
- **🗞️ LLM Sentiment Analysis** - Swapable provider/model via LiteLLM
3031
- **⚡ Hyperparameter Optimization** - Optuna-based automatic tuning
3132
- **📈 Comprehensive Backtesting** - Risk management and performance metrics
3233
- **🎯 Production Ready** - Model persistence, CLI interface, configuration management
@@ -129,6 +130,26 @@ X, y = classifier.prepare_data(df_labeled)
129130
classifier.train(X, y)
130131
```
131132

133+
### Sentiment Analysis
134+
135+
LiteLLM powers provider-agnostic sentiment scoring. Configure it in
136+
`config/features_config.yaml` and set the API key in your environment:
137+
138+
```yaml
139+
sentiment:
140+
enabled: true
141+
provider: openai
142+
model: gpt-3.5-turbo
143+
api_key_env_var: OPENAI_API_KEY
144+
```
145+
146+
```bash
147+
export OPENAI_API_KEY="sk-..."
148+
```
149+
150+
Switching providers is as simple as updating the YAML config to point to a
151+
different `provider`/`model` pair supported by LiteLLM.
152+
132153
## 📊 Performance Metrics
133154

134155
| Metric | Description |

config/features_config.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ feature_combinations:
5252
- ['close', 'sma_20']
5353
- ['high', 'low']
5454

55+
# Sentiment Analysis (LLM-based)
56+
# Configure LiteLLM provider/model and API key env var. Set `enabled: true` to
57+
# generate a numeric sentiment score (-1 negative, 1 positive) for any text
58+
# column named "text".
59+
sentiment:
60+
enabled: false
61+
provider: openai # e.g., openai, anthropic, huggingface, ollama
62+
model: gpt-3.5-turbo # model name for the chosen provider
63+
api_key_env_var: OPENAI_API_KEY # environment variable holding the API key
64+
extra: {} # Additional LiteLLM params (e.g., base_url, temperature)
65+
5566
# Feature Selection
5667
feature_selection:
5768
method: 'recursive' # Options: recursive, lasso, random_forest
@@ -74,6 +85,7 @@ pipeline:
7485
- generate_technical_indicators
7586
- generate_volume_features
7687
- generate_custom_features
88+
- generate_sentiment
7789
- handle_missing_values
7890
- remove_outliers
7991
- scale_features

docs/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Welcome to the QuantTradeAI documentation! This comprehensive machine learning f
77
### Core Documentation
88
- **[API Documentation](api/)** - Comprehensive reference for all public APIs, functions, and components
99
- **[Quick Reference Guide](quick-reference.md)** - Common usage patterns and examples
10+
- **[LLM Sentiment Analysis](llm-sentiment.md)** - Configure LLM-based sentiment scoring
1011
- **[Main README](../README.md)** - Project overview and getting started guide
1112

1213
## 🚀 Quick Start
@@ -240,4 +241,4 @@ For questions and support:
240241

241242
---
242243

243-
This documentation provides comprehensive coverage of the QuantTradeAI framework. Start with the [Quick Reference Guide](quick-reference.md) for common usage patterns, then refer to the [API Documentation](api/) for detailed function references.
244+
This documentation provides comprehensive coverage of the QuantTradeAI framework. Start with the [Quick Reference Guide](quick-reference.md) for common usage patterns, then refer to the [API Documentation](api/) for detailed function references.

docs/configuration.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,23 @@ feature_combinations:
140140
- ['high', 'low']
141141
```
142142

143+
### Sentiment Features
144+
145+
```yaml
146+
sentiment:
147+
enabled: true
148+
provider: openai # e.g. openai, anthropic, huggingface, ollama
149+
model: gpt-3.5-turbo
150+
api_key_env_var: OPENAI_API_KEY
151+
extra: {}
152+
```
153+
154+
Set the API key before running:
155+
156+
```bash
157+
export OPENAI_API_KEY="sk-..."
158+
```
159+
143160
### Preprocessing
144161

145162
```yaml
@@ -170,6 +187,7 @@ pipeline:
170187
- generate_technical_indicators
171188
- generate_volume_features
172189
- generate_custom_features
190+
- generate_sentiment
173191
- handle_missing_values
174192
- remove_outliers
175193
- scale_features

docs/llm-sentiment.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# LLM-Powered Sentiment Analysis
2+
3+
QuantTradeAI can attach numeric sentiment scores to any dataset that includes a text column. The feature uses [LiteLLM](https://github.com/BerriAI/litellm) so you can swap providers and models without changing code.
4+
5+
## Setup
6+
7+
1. **Install QuantTradeAI (if not already installed)**
8+
```bash
9+
poetry install
10+
```
11+
2. **Configure `features_config.yaml`**
12+
```yaml
13+
sentiment:
14+
enabled: true
15+
provider: openai # e.g. openai, anthropic, huggingface, ollama
16+
model: gpt-3.5-turbo # model name for the chosen provider
17+
api_key_env_var: OPENAI_API_KEY
18+
extra: {} # optional LiteLLM params
19+
```
20+
3. **Set the API key**
21+
```bash
22+
export OPENAI_API_KEY="sk-..."
23+
```
24+
25+
### Switching Providers
26+
Change `provider`, `model`, and `api_key_env_var` in the YAML config. Example for Anthropic:
27+
```yaml
28+
sentiment:
29+
enabled: true
30+
provider: anthropic
31+
model: claude-instant-1
32+
api_key_env_var: ANTHROPIC_API_KEY
33+
```
34+
No code changes are required—update the YAML and corresponding environment variable.
35+
36+
## Usage
37+
38+
### Command Line
39+
```bash
40+
# with sentiment enabled in features_config.yaml
41+
export OPENAI_API_KEY="sk-..."
42+
poetry run quanttradeai train
43+
```
44+
The training pipeline loads sentiment settings automatically and adds a `sentiment_score` column.
45+
46+
### Python
47+
```python
48+
import pandas as pd
49+
from quanttradeai.data import DataProcessor
50+
51+
# DataFrame must contain a 'text' column
52+
raw = pd.DataFrame({"text": ["Stock surges on earnings", "Lawsuit worries investors"]})
53+
processor = DataProcessor("config/features_config.yaml")
54+
features = processor.process_data(raw)
55+
print(features[["text", "sentiment_score"]])
56+
```
57+
58+
## Input & Output
59+
- **Input**: DataFrame with a `text` column
60+
- **Output**: New `sentiment_score` column with values in `[-1, 1]`
61+
- Added via the `generate_sentiment` step in the feature pipeline
62+
63+
## Troubleshooting
64+
| Problem | Fix |
65+
|--------|-----|
66+
| `API key environment variable ... is not set` | Ensure the variable from `api_key_env_var` is exported |
67+
| Unsupported provider/model | Verify the provider/model pair is supported by LiteLLM |
68+
| `sentiment enabled but 'text' column not found` | Provide a `text` column in your input DataFrame |
69+
| Network/API errors | Check connectivity, credentials, or provider status |

0 commit comments

Comments
 (0)