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:
The training script is located at train_llm.py.
python train_llm.py
This will:
llm_training_data.jsonneural_network_weights.npzTraining 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)
The custom LLM uses a neural network with:
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:
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.py
Test prompts included:
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=custom
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
Start with pre-trained weights and fine-tune:
provider = CustomProvider()
provider.load_weights('pretrained_weights.npz')
provider.train_model(epochs=10) # Fine-tune
Add 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.json
Issue: Model doesn't learn well
Solutions:
Issue: Training fails due to memory
Solutions:
Issue: Training takes too long
Solutions:
Balance categories
Training Strategy
Save checkpoints
Evaluation
Compare with baseline
Deployment
train_llm.pyllm_training_data.jsonneural_network_weights.npzllm/client.py (CustomProvider class)For training issues: