-
Notifications
You must be signed in to change notification settings - Fork 0
Custom LLM Training
This guide covers training a custom LLM for the Cognitive Engine.
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
The training script is located at train_llm.py.
python train_llm.pyThis will:
- Load training data from
llm_training_data.json - Train the neural network
- Test the trained model
- Save the trained weights to
neural_network_weights.npz
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..."
}
]
}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)- Variety: Include diverse examples
- Accuracy: Ensure responses are correct
- Consistency: Maintain consistent style
- Relevance: Focus on your domain
- Quantity: More examples generally improve performance
The custom LLM uses a neural network with:
- Input layer: Tokenized prompts
- Hidden layers: Multiple processing layers
- Output layer: Generated responses
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)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
The training script includes automatic testing:
python train_llm.pyTest prompts included:
- "hello"
- "what is the cognitive engine"
- "how do you work"
- "what is a thought"
- "tell me about deliberation"
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)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=customfrom 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_providerStart with pre-trained weights and fine-tune:
provider = CustomProvider()
provider.load_weights('pretrained_weights.npz')
provider.train_model(epochs=10) # Fine-tuneAdd new data and retrain periodically:
# Add new examples to training data
# Retrain with more epochs
provider.train_model(epochs=20)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']}")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.jsonIssue: Model doesn't learn well
Solutions:
- Increase training epochs
- Add more diverse training data
- Check data quality
- Adjust learning rate
- Normalize input data
Issue: Training fails due to memory
Solutions:
- Reduce batch size
- Use fewer training examples
- Reduce model complexity
- Use GPU if available
Issue: Training takes too long
Solutions:
- Reduce epochs
- Use smaller dataset
- Use GPU acceleration
- Optimize data loading
-
Data Quality
- Ensure accurate responses
- Maintain consistent formatting
- Remove duplicates
- Balance categories
-
Training Strategy
- Start with small dataset
- Gradually increase complexity
- Monitor loss curves
- Save checkpoints
-
Evaluation
- Use separate test set
- Measure multiple metrics
- Test on real queries
- Compare with baseline
-
Deployment
- Version your models
- Test before deployment
- Monitor performance
- Rollback if needed
- Use GPU acceleration if available
- Batch similar queries
- Cache frequent responses
- Optimize data loading
- Use mixed precision training
- Training script:
train_llm.py - Training data:
llm_training_data.json - Model weights:
neural_network_weights.npz - Custom provider:
llm/client.py(CustomProvider class)
For training issues:
- Email: autobotsolution@gmail.com
- Address: Flushing MI
- Check training logs for errors
- Verify training data format