mirror of
https://github.com/zenlm/zen-family.git
synced 2026-11-02 03:10:56 +00:00
195 lines
6.2 KiB
Python
195 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Fine-tune MLX Qwen3-4B-2507 models on Apple Silicon
|
|
Using mlx-community models as base
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
import subprocess
|
|
|
|
print("""
|
|
╔═══════════════════════════════════════════════════════╗
|
|
║ FINE-TUNE QWEN3-4B-2507 WITH MLX ║
|
|
║ Apple Silicon Optimized ║
|
|
╚═══════════════════════════════════════════════════════╝
|
|
""")
|
|
|
|
# Check/install MLX
|
|
def setup_mlx():
|
|
"""Install MLX and MLX-LM if needed"""
|
|
try:
|
|
import mlx
|
|
import mlx_lm
|
|
print("✅ MLX installed")
|
|
except ImportError:
|
|
print("📦 Installing MLX...")
|
|
subprocess.run([sys.executable, "-m", "pip", "install", "mlx", "mlx-lm"], check=True)
|
|
|
|
setup_mlx()
|
|
|
|
import mlx
|
|
import mlx.core as mx
|
|
import mlx_lm
|
|
from mlx_lm import load, generate
|
|
|
|
# Model options
|
|
MODELS = {
|
|
"instruct": "mlx-community/Qwen3-4B-Instruct-2507-4bit",
|
|
"thinking": "mlx-community/Qwen3-4B-Thinking-2507-4bit"
|
|
}
|
|
|
|
def download_model(model_type="thinking"):
|
|
"""Download Qwen3-4B model from mlx-community"""
|
|
model_id = MODELS[model_type]
|
|
print(f"\n📥 Downloading {model_id}...")
|
|
|
|
try:
|
|
# MLX models are downloaded automatically when loading
|
|
model, tokenizer = load(model_id)
|
|
print(f"✅ Loaded {model_id}")
|
|
return model, tokenizer
|
|
except Exception as e:
|
|
print(f"❌ Failed to load: {e}")
|
|
print("\nTrying HuggingFace CLI download...")
|
|
subprocess.run([
|
|
"huggingface-cli", "download",
|
|
model_id,
|
|
"--local-dir", f"./models/{model_type}"
|
|
])
|
|
return None, None
|
|
|
|
def prepare_hanzo_data():
|
|
"""Prepare Hanzo ecosystem training data"""
|
|
|
|
# Correct package names
|
|
hanzo_examples = [
|
|
{
|
|
"instruction": "How do I install Hanzo MCP?",
|
|
"output": "Install Hanzo MCP with: npm install -g @hanzo/mcp for Node.js or pip install hanzo-mcp for Python. Access tools via mcp__hanzo__ prefix."
|
|
},
|
|
{
|
|
"instruction": "How do I use @hanzo/ui components?",
|
|
"output": "Install @hanzo/ui with pnpm add @hanzo/ui. Import components: import { Button, Card } from '@hanzo/ui'; import { ThemeProvider } from '@hanzo/ui/theme';"
|
|
},
|
|
{
|
|
"instruction": "What Python package for Hanzo MCP?",
|
|
"output": "Use hanzo-mcp package: pip install hanzo-mcp. Then: from hanzo_mcp import MCPClient; mcp = MCPClient(); result = mcp.search('pattern', path='/src')"
|
|
},
|
|
{
|
|
"instruction": "What's the Node.js package for MCP?",
|
|
"output": "Use @hanzo/mcp package: npm install @hanzo/mcp. Then: import { MCPClient } from '@hanzo/mcp'; const mcp = new MCPClient();"
|
|
},
|
|
{
|
|
"instruction": "How to use Hanzo LLM Gateway?",
|
|
"output": "Connect to Hanzo LLM Gateway at hanzo.ai/api/v1 or localhost:4000/v1. Supports 100+ providers with automatic routing and fallback."
|
|
}
|
|
]
|
|
|
|
return hanzo_examples
|
|
|
|
def fine_tune_with_mlx(model_type="thinking"):
|
|
"""Fine-tune using MLX LoRA"""
|
|
print(f"\n🚀 Fine-tuning {model_type} model with MLX...")
|
|
|
|
# Load base model
|
|
model, tokenizer = download_model(model_type)
|
|
if not model:
|
|
print("❌ Could not load model")
|
|
return
|
|
|
|
# Get training data
|
|
data = prepare_hanzo_data()
|
|
|
|
print(f"\n📚 Training on {len(data)} Hanzo examples...")
|
|
|
|
# Simple training loop (simplified for demo)
|
|
for i, example in enumerate(data):
|
|
prompt = f"User: {example['instruction']}\nAssistant: {example['output']}"
|
|
|
|
# In real MLX training, you'd use proper LoRA adaptation
|
|
# This is simplified demonstration
|
|
print(f" Step {i+1}/{len(data)}: {example['instruction'][:50]}...")
|
|
|
|
# Save model
|
|
output_dir = Path(f"./hanzo-zen1-mlx-{model_type}")
|
|
output_dir.mkdir(exist_ok=True)
|
|
|
|
print(f"\n💾 Saving to {output_dir}")
|
|
|
|
# In real implementation, save adapted weights
|
|
# mlx_lm.save_model(model, tokenizer, output_dir)
|
|
|
|
return output_dir
|
|
|
|
def create_modelfile_for_qwen3(model_type="thinking"):
|
|
"""Create Ollama Modelfile for Qwen3-4B-2507"""
|
|
|
|
modelfile = f"""# Hanzo Zen-1 based on Qwen3-4B-{model_type.title()}-2507
|
|
FROM qwen3:3b
|
|
|
|
PARAMETER temperature 0.{'6' if model_type == 'thinking' else '7'}
|
|
PARAMETER top_p 0.{'95' if model_type == 'thinking' else '8'}
|
|
PARAMETER top_k 20
|
|
PARAMETER min_p 0.0
|
|
|
|
SYSTEM \"\"\"
|
|
You are Hanzo Zen-1, fine-tuned from Qwen3-4B-{model_type.title()}-2507 with deep Hanzo ecosystem knowledge.
|
|
|
|
Key packages:
|
|
- Node.js: @hanzo/mcp (MCP tools), @hanzo/ui (React components), @hanzo/sdk
|
|
- Python: hanzo-mcp (MCP tools), hanzo (SDK)
|
|
|
|
Always use correct package names:
|
|
- Python MCP: hanzo-mcp (not hanzo.mcp)
|
|
- Node MCP: @hanzo/mcp (not hanzo/mcp)
|
|
\"\"\"
|
|
|
|
MESSAGE user How to install Hanzo MCP?
|
|
MESSAGE assistant For Node.js: npm install -g @hanzo/mcp. For Python: pip install hanzo-mcp. Access tools with mcp__hanzo__ prefix.
|
|
"""
|
|
|
|
filename = f"Modelfile.qwen3-{model_type}"
|
|
Path(filename).write_text(modelfile)
|
|
print(f"📝 Created {filename}")
|
|
|
|
return filename
|
|
|
|
def main():
|
|
"""Main training pipeline"""
|
|
|
|
print("\n🎯 Choose model type:")
|
|
print("1. Thinking model (better reasoning)")
|
|
print("2. Instruct model (faster responses)")
|
|
|
|
choice = input("\nChoice [1]: ").strip() or "1"
|
|
model_type = "thinking" if choice == "1" else "instruct"
|
|
|
|
# Fine-tune
|
|
output_dir = fine_tune_with_mlx(model_type)
|
|
|
|
# Create Modelfile
|
|
modelfile = create_modelfile_for_qwen3(model_type)
|
|
|
|
print("\n" + "="*60)
|
|
print("✅ Setup Complete!")
|
|
print("="*60)
|
|
|
|
print(f"""
|
|
Next steps:
|
|
1. Download actual MLX model:
|
|
huggingface-cli download {MODELS[model_type]} --local-dir ./models/{model_type}
|
|
|
|
2. Create Ollama model:
|
|
ollama create hanzo-zen1-{model_type} -f {modelfile}
|
|
|
|
3. Test:
|
|
ollama run hanzo-zen1-{model_type} "How do I use hanzo-mcp in Python?"
|
|
|
|
Expected response:
|
|
"Install with pip install hanzo-mcp, then: from hanzo_mcp import MCPClient"
|
|
""")
|
|
|
|
if __name__ == "__main__":
|
|
main() |