Menu ▾ ▴

Custom LLM Training

Robert Trenaman

This guide covers training a custom LLM for the Cognitive Engine.

Overview

The Cognitive Engine includes a custom LLM provider that can be trained on domain-specific data. This allows the system to:

  • Learn from your specific use cases
  • Adapt to your terminology
  • Improve performance on your tasks
  • Reduce dependency on external APIs

Training Script

The training script is located at train_llm.py.

Basic Usage

python train_llm.py

This will:

  1. Load training data from llm_training_data.json
  2. Train the neural network
  3. Test the trained model
  4. Save the trained weights to neural_network_weights.npz

Training Data

Data Format

Training data should be in JSON format:

{
  "examples": [
    {
      "prompt": "What is artificial intelligence?",
      "response": "AI is the simulation of human intelligence in machines..."
    },
    {
      "prompt": "How do neural networks learn?",
      "response": "Neural networks learn through backpropagation..."
    }
  ]
}

Creating Training Data

Create or edit llm_training_data.json:

import json

training_data = {
    "examples": [
        {
            "prompt": "Your question here",
            "response": "Your answer here"
        }
    ]
}

with open('llm_training_data.json', 'w') as f:
    json.dump(training_data, f, indent=2)

Data Quality Guidelines

  • Variety: Include diverse examples
  • Accuracy: Ensure responses are correct
  • Consistency: Maintain consistent style
  • Relevance: Focus on your domain
  • Quantity: More examples generally improve performance

Training Process

Neural Network Architecture

The custom LLM uses a neural network with:

  • Input layer: Tokenized prompts
  • Hidden layers: Multiple processing layers
  • Output layer: Generated responses

Training Parameters

Default training parameters in train_llm.py:

provider.train_model(epochs=50)

You can adjust parameters by modifying the training script:

# More epochs for better training
provider.train_model(epochs=100)

# Learning rate
provider.train_model(epochs=50, learning_rate=0.001)

# Batch size
provider.train_model(epochs=50, batch_size=32)

Monitoring Training

The training script outputs:

  • Initial loss
  • Final loss
  • Improvement metrics

Example output:

Training neural network...
Initial loss: 2.3456
Final loss: 0.1234
Improvement: 2.2222

Testing the Trained Model

Built-in Testing

The training script includes automatic testing:

python train_llm.py

Test prompts included:

  • "hello"
  • "what is the cognitive engine"
  • "how do you work"
  • "what is a thought"
  • "tell me about deliberation"

Manual Testing

Test the trained model programmatically:

from llm.client import CustomProvider

provider = CustomProvider()
provider.load_weights('neural_network_weights.npz')

response = await provider.generate("Your question", mode='response')
print(response)

Using the Custom LLM

Configuration

Enable the custom LLM in your configuration:

from llm.client import CustomProvider
from core.config import Config

# Use custom provider
config = Config(default_llm_provider='custom')

# Or set environment variable
DEFAULT_LLM_PROVIDER=custom

Integration with Cognitive Engine

from core.engine import CognitiveEngine
from llm.client import CustomProvider

# Initialize custom provider
custom_provider = CustomProvider()
custom_provider.load_weights('neural_network_weights.npz')

# Use in engine
engine = CognitiveEngine()
engine.llm_client.provider = custom_provider

Advanced Training

Transfer Learning

Start with pre-trained weights and fine-tune:

provider = CustomProvider()
provider.load_weights('pretrained_weights.npz')
provider.train_model(epochs=10)  # Fine-tune

Continuous Learning

Add new data and retrain periodically:

# Add new examples to training data
# Retrain with more epochs
provider.train_model(epochs=20)

Evaluation Metrics

Evaluate model performance:

provider = CustomProvider()
provider.load_weights('neural_network_weights.npz')

# Test on validation set
test_results = provider.evaluate(validation_data)
print(f"Accuracy: {test_results['accuracy']}")
print(f"Loss: {test_results['loss']}")

Troubleshooting

No Training Data

Issue: "No training data found"

Solution:

# Ensure llm_training_data.json exists
ls llm_training_data.json

# Create if missing
echo '{"examples":[]}' > llm_training_data.json

Poor Training Results

Issue: Model doesn't learn well

Solutions:

  • Increase training epochs
  • Add more diverse training data
  • Check data quality
  • Adjust learning rate
  • Normalize input data

Out of Memory

Issue: Training fails due to memory

Solutions:

  • Reduce batch size
  • Use fewer training examples
  • Reduce model complexity
  • Use GPU if available

Slow Training

Issue: Training takes too long

Solutions:

  • Reduce epochs
  • Use smaller dataset
  • Use GPU acceleration
  • Optimize data loading

Best Practices

  1. Data Quality
  2. Ensure accurate responses
  3. Maintain consistent formatting
  4. Remove duplicates
  5. Balance categories

  6. Training Strategy

  7. Start with small dataset
  8. Gradually increase complexity
  9. Monitor loss curves
  10. Save checkpoints

  11. Evaluation

  12. Use separate test set
  13. Measure multiple metrics
  14. Test on real queries
  15. Compare with baseline

  16. Deployment

  17. Version your models
  18. Test before deployment
  19. Monitor performance
  20. Rollback if needed

Performance Tips

  • Use GPU acceleration if available
  • Batch similar queries
  • Cache frequent responses
  • Optimize data loading
  • Use mixed precision training

File Locations

  • Training script: train_llm.py
  • Training data: llm_training_data.json
  • Model weights: neural_network_weights.npz
  • Custom provider: llm/client.py (CustomProvider class)

Support

For training issues:

  • Email: autobotsolution@gmail.com
  • Address: Flushing MI
  • Check training logs for errors
  • Verify training data format