Model Card: Llama 3.1 8B Fine-tuned with Socratic Chain-of-Thought

Model Overview

This model is a specialized fine-tuned variant of Llama 3.1 8B Instruct designed for stock price prediction using Socratic Chain-of-Thought (CoT) methodology. The model has been trained through knowledge distillation using GPT-4o Mini as the teacher model, emphasizing step-by-step reasoning through question-and-answer interactions to improve prediction quality.

Model ID: ajiayi/llama-3.1-8b-merged-unsloth-cot

Model Size: 8 Billion Parameters

Base Model: Meta-Llama-3.1-8B-Instruct

Training Details

Fine-tuning Approach

  • Method: Knowledge Distillation via Socratic Chain-of-Thought (CoT)
  • Teacher Model: GPT-4o Mini (via OpenAI API)
  • Framework: Unsloth + Hugging Face Transformers
  • Quantization: 4-bit QLoRA (Quantized Low-Rank Adaptation)
  • Training Environment: Google Colab with GPU acceleration

LoRA Configuration

- r (Rank): Optimized via rank finder (tested ranks: 8, 16, 32, 64)
- lora_alpha: 16
- lora_dropout: 0
- Target Modules: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj
- Bias: None
- Use Gradient Checkpointing: Unsloth (optimized)

Training Hyperparameters

  • Batch Size: 4 (per device)
  • Learning Rate: 2e-4
  • Epochs: 3
  • Max Sequence Length: 2048 tokens
  • Optimizer: AdamW with bf16 precision (where supported)

Training Data

  • Train Set: 8,698 samples
  • Validation Set: 1,243 samples
  • Total Training Data: 9,941 samples
  • Data Format: JSONL with instruction-following format
  • Dataset: train_with_qa_instruction_format.jsonl

Socratic Chain-of-Thought Methodology

Concept

The Socratic method teaches through guided questioning and structured reasoning. In this model, a single student model is trained end-to-end to both generate questions and provide answers, creating an integrated reasoning chain.

Note: While the original Socratic CoT approach in literature often uses two separate models (a question generator and an answer generator), this implementation uses a unified single-student architecture trained to produce both questions and answers in a single forward pass.

Single Student Architecture

Unlike the two-student approach in the original paper:

  • Single Model: One Llama 3.1 8B model handles both question generation and answer provision
  • End-to-End Training: Learns to generate relevant financial analysis questions and provide corresponding answers
  • Unified Reasoning: Questions and answers are generated as an integrated chain-of-thought in one model
  • Efficiency: Eliminates latency and complexity of coordinating two separate models

Reasoning Process

The single student learns to:

  1. Generate Contextual Questions: Creates relevant analytical questions about:

    • Recent price trends and trading momentum
    • Technical indicator signals (RSI, MACD, Bollinger Bands)
    • News sentiment analysis and market context
    • Confidence justification based on signal alignment
  2. Provide Detailed Answers: Generates answers that:

    • Reference specific indicator values from the input
    • Explain financial implications of each signal
    • Build reasoning toward the final prediction
  3. Synthesize Predictions: Combines question-answer reasoning to:

    • Determine predicted closing price
    • Assess confidence level based on signal agreement
    • Output a complete structured JSON response

Training Approach

During training on 8,698 examples, the single student model learns to:

  • Generate contextually relevant questions about financial data
  • Provide detailed answers that reference specific indicator values
  • Build a chain-of-thought that justifies the final price prediction
  • Assign confidence levels based on signal agreement/disagreement

Each training sample contains:

  • Same market data input as justification variant
  • Four structured question-answer pairs in the output (generated by teacher model, learned by student)
  • A final predicted_close and likelihood value

Input/Output Specification

Training Data Format

This model was trained using the Alpaca instruction-following format with question-answer pairs integrated into the output:

{
  "instruction": "[System prompt describing task]",
  "input": "[Market data input]",
  "output": "[JSON with predicted_close, likelihood, and justification Q&A chain]"
}

Expected Input Format

The model expects inputs formatted as financial analysis prompts containing:

TICKER: [Stock Symbol]
DATE: [YYYY-MM-DD]

RECENT CLOSING PRICES (most recent last): [comma-separated prices]

TECHNICAL INDICATORS:
SMA_20=[value], SMA_50=[value],
EMA_12=[value], EMA_26=[value],
RSI_14=[value], MACD=[value], MACD_signal=[value], MACD_hist=[value],
BB_width_20_2=[value]

SENTIMENT AGGREGATES:
headline_count=[count], sent_compound_mean=[score]

HEADLINES (concise):
[News headlines if available]

System Instruction

You are a financial analyst with expertise in stock market forecasting.
Your task is to analyze market data and predict the next trading day stock price.
Use historical price trends, technical indicators, and sentiment analysis to provide an informed forecast.
Ensure that your predictions are well-justified, considering multiple financial factors.

• Predicted Stock Price: The forecasted close price for the next trading day.
• Price Movement Likelihood: The likelihood of the predicted stock price.
• Justification: Provide an explanation for the predicted stock price and the corresponding likelihood, considering the following:
  - Historical market data (e.g., recent closing prices).
  - Technical indicators (e.g., SMA, EMA, RSI, MACD, Bollinger Bands).
  - Sentiment analysis (e.g., news sentiment, market sentiment).

Please weigh these signals and justify the predicted stock price.

Return STRICT JSON with keys:
- predicted_close (float, next-day close price),
- likelihood (float in [0,1]),
- justification (array of objects with question-answer pairs).

Expected Output Format

The model generates output with embedded question-answer reasoning chains:

{
  "predicted_close": 27.18,
  "likelihood": 0.5,
  "justification": [
    {
      "question": "What is the significance of the recent closing prices in predicting AAPL's next trading day?",
      "answer": "The closing prices show a slight downward trend, indicating selling pressure in the recent days, which could affect the predicted price."
    },
    {
      "question": "How do technical indicators like RSI and MACD influence the prediction?",
      "answer": "The RSI of 13.54 suggests that AAPL is oversold, which could lead to a potential price recovery, while the negative MACD indicates weakening momentum."
    },
    {
      "question": "What does the sentiment analysis indicate about AAPL's stock performance?",
      "answer": "The negative compound sentiment of -0.07955 from news headlines suggests bearish sentiment in the market, which could hinder any positive price movement in the short term."
    },
    {
      "question": "Why is the predicted likelihood of the price movement set at 0.5?",
      "answer": "The likelihood is set at 0.5 due to mixed signals from technical indicators and negative sentiment, indicating uncertainty about the stock's short-term performance."
    }
  ]
}

Data Characteristics

Stocks Covered

The training data includes diverse stocks across sectors:

  • Technology: AAPL (Apple)
  • Consumer Goods: PEP (PepsiCo)
  • Banking: HSBC
  • International Markets: 0700.HK (Hong Kong), 7203.T (Japan)

Data Splits

  • Training Period: 2015-01-01 to 2021-12-31
  • Validation Period: 2022-01-01 to 2022-12-31
  • Temporal Coverage: ~7 years of historical market data

Technical Features Included

  • Price Indicators: SMA (20, 50), EMA (12, 26), Bollinger Bands
  • Momentum: RSI (14), MACD with signal line and histogram
  • Sentiment: VADER compound sentiment scores from news headlines
  • Temporal Information: Historical closing prices (5-day window)

Model Performance

Key Strengths

  1. Interpretable Reasoning: Generates step-by-step explanations making predictions more transparent
  2. Multi-step Analysis: Systematically addresses different aspects of market analysis
  3. Question Decomposition: Breaks complex analysis into manageable analytical steps
  4. Educational Value: Suitable for learning financial analysis through structured reasoning
  5. Debugging Capability: Step-by-step outputs allow identification of reasoning errors

Limitations

  1. Complexity Trade-off: Longer outputs may be harder to parse than direct predictions
  2. Data Scarcity: Limited to sectors/stocks in training data
  3. Historical Bias: Reflects market patterns from 2015-2021 training period
  4. Sentiment Dependency: Relies on headline sentiment which may be incomplete
  5. Reasoning Quality: Intermediate steps may contain speculative analysis

Usage

Installation

pip install transformers torch huggingface_hub

Basic Inference

from transformers import AutoModelForCausalLM, AutoTokenizer
import json

model_id = "ajiayi/llama-3.1-8b-merged-unsloth-cot"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto")

# Prepare your input with Socratic questions
prompt = """[Your financial analysis input with Q1-Q4]"""

inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.7)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)

# Parse JSON output
try:
    prediction = json.loads(response)
    print(f"Question-Answer Justification: {prediction['justification']}")
    print(f"Predicted Close: {prediction['predicted_close']}")
    print(f"Confidence: {prediction['likelihood']:.2%}")
except json.JSONDecodeError:
    print(response)

Hugging Face Inference Endpoints

This model supports Hugging Face Inference Endpoints for production deployment:

  1. Go to ajiayi/llama-3.1-8b-merged-unsloth-cot
  2. Click "Deploy → Inference Endpoints"
  3. Configure hardware and enable endpoint
  4. Use the provided API URL for inference requests

With Pipeline

from transformers import pipeline

pipe = pipeline("text-generation", model="ajiayi/llama-3.1-8b-merged-unsloth-cot")
results = pipe("[Your prompt with questions]", max_new_tokens=512, temperature=0.7)
print(results[0]['generated_text'])

Ethical Considerations

Intended Use

  • Financial analysis and prediction research
  • Educational purposes to demonstrate Socratic reasoning and knowledge distillation
  • Explainable AI research for financial domains
  • Baseline model for stock price forecasting with interpretability

Not Recommended For

  • Making real financial investment decisions without human oversight
  • Financial advice to retail investors
  • Mission-critical financial applications without additional validation
  • Systems requiring high throughput (due to longer outputs)

Limitations & Disclaimers

  • This model generates predictions based on limited historical data
  • Market conditions change; historical patterns may not persist
  • Sentiment analysis may be incomplete or subject to manipulation
  • Intermediate reasoning steps may contain speculative analysis
  • Model should not be used as sole basis for financial decisions
  • Past performance does not guarantee future results
  • Socratic reasoning, while transparent, should be validated by domain experts

Dataset Attribution

Training data derived from:

  • Stock Prices: Historical market data from public sources
  • News Sentiment: Google News scraped with VADER sentiment analysis
  • Technical Indicators: Computed using standard financial libraries
  • Socratic Annotations: Question-answer chains generated using GPT-4o Mini

The distillation targets (teacher model outputs) were generated using OpenAI's GPT-4o Mini API with explicit question-answer formatting.

Citation

If you use this model in research, please cite:

@model{llama_socratic_cot_distillation_2025,
  title={Llama 3.1 8B Fine-tuned with Socratic Chain-of-Thought for Stock Price Prediction},
  author={Jiayi Ang},
  year={2025},
  howpublished={Hugging Face Model Hub},
  note={Knowledge distillation from GPT-4o Mini with Socratic methodology}
}

Model Card Contact

For questions or issues related to this model, please open an issue on the model repository or contact the model creator.

Version History

Version Date Changes
1.0 2025 Initial release with merged LoRA weights and Socratic CoT training

Related Models

License

This model follows the license of the base Llama 3.1 8B Instruct model. Please refer to the base model's license for details.

Downloads last month
46
Safetensors
Model size
8B params
Tensor type
BF16
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for ajiayi/llama-3.1-8b-merged-unsloth-cot

Finetuned
(2919)
this model