Skip to content

Latest commit

 

History

History
110 lines (79 loc) · 3.47 KB

File metadata and controls

110 lines (79 loc) · 3.47 KB

LLM/Agent Improvements Summary

Changes Implemented

1. Enhanced Prompt with Clear Examples ✅

Before: Vague instructions like "be careful not to make up ticker symbols"

After:

  • Explicit rules with examples
  • Clear VALID vs INVALID examples
  • Specific instructions about what NOT to recommend
  • Better formatting requirements

Key Additions:

  • ✅ Valid examples showing correct format
  • ❌ Invalid examples showing what NOT to do (Canada, sectors, etc.)
  • Explicit list of non-tradeable entities to avoid
  • Clear ticker format specification (1-5 letters, optional class shares like BRK.B)

2. Pre-Storage Validation Function ✅

New Function: validate_recommendation(rec)

Validates:

  • ✅ Required fields present
  • ✅ Ticker format (1-5 uppercase letters, optional .X for class shares)
  • ✅ Recommendation type (BUY/SELL only)
  • ✅ Confidence level (HIGH/MEDIUM/LOW only)
  • ✅ Company name doesn't contain invalid keywords
  • ✅ Company name is not a country name
  • ✅ Company name is not a currency
  • ✅ Company name is not a commodity
  • ✅ Company name is not a sector/index

Returns: (is_valid: bool, error_message: str)

3. Lower Temperature for Structured Output ✅

Before: temperature=0.7 (too creative for structured JSON)

After: temperature=0.2 (more deterministic, better for JSON output)

4. Validation Logging ✅

Before: All recommendations stored, even invalid ones

After:

  • Each recommendation validated before storage
  • Invalid recommendations logged with error messages
  • Only valid recommendations stored in database

How It Works Now

1. LLM generates recommendations
   ↓
2. Each recommendation validated
   ↓
3. Invalid ones logged and rejected
   ↓
4. Only valid recommendations stored

Example Validation Failures

The system will now reject and log:

# Country instead of stock
{"company": "Canada", "ticker": "CAN", ...}
→ Error: "Invalid entity: Canada is a country, not a stock"

# Sector instead of stock
{"company": "Technology Sector", "ticker": "TECH", ...}
→ Error: "Invalid entity type: Technology Sector (contains invalid keyword)"

# Invalid ticker format
{"company": "Apple Inc", "ticker": "AAPL123", ...}
→ Error: "Invalid ticker format: AAPL123 (must be 1-5 uppercase letters)"

# Invalid recommendation type
{"company": "Apple Inc", "ticker": "AAPL", "recommendation": "HOLD", ...}
→ Error: "Invalid recommendation type: HOLD (must be BUY or SELL)"

Benefits

  1. Data Quality: Only valid stock recommendations stored
  2. Error Prevention: Catches common LLM mistakes (countries, sectors, etc.)
  3. Better Prompts: Clear examples help LLM understand requirements
  4. Debugging: Validation errors logged for analysis
  5. Format Consistency: Ensures all recommendations follow database schema

Testing Recommendations

To test the improvements:

  1. Monitor logs for validation failures
  2. Check database - should only contain valid stock recommendations
  3. Review rejected recommendations to identify prompt improvements
  4. Adjust validation rules if needed based on real-world data

Future Enhancements

  1. Ticker Symbol Lookup: Validate against real stock exchange databases
  2. Company Name Normalization: Standardize company names (e.g., "Apple Inc" vs "Apple")
  3. Confidence Calibration: Track which confidence levels correlate with accuracy
  4. Feedback Loop: Allow users to flag incorrect recommendations to improve prompts