File size: 8,462 Bytes
0fa4cc9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
#!/usr/bin/env python3
"""
🧪 Test script for converted GGUF model
Tests both llama.cpp and Ollama integration
"""
import os
import subprocess
import time
import requests
import json
from pathlib import Path
def test_llamacpp_direct():
"""Test model directly with llama.cpp"""
print("🧪 Testing with llama.cpp directly...")
model_file = "my_custom_model.gguf"
if not os.path.exists(model_file):
print(f"❌ Model file not found: {model_file}")
return False
llamacpp_main = "./llama.cpp/main"
if not os.path.exists(llamacpp_main):
print(f"❌ llama.cpp main not found: {llamacpp_main}")
print("Run: ./convert_to_gguf.sh first")
return False
test_prompts = [
"Hello, how are you?",
"Wyjaśnij co to jest Docker",
"Napisz prostą funkcję w Pythonie"
]
for i, prompt in enumerate(test_prompts, 1):
print(f"\n--- Test {i}/3: {prompt[:30]}... ---")
cmd = [
llamacpp_main,
"-m", model_file,
"-p", prompt,
"-n", "100",
"--temp", "0.7",
"--top-p", "0.9"
]
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
if result.returncode == 0:
print("✅ Response generated successfully")
print("Response preview:", result.stdout[:200] + "..." if len(result.stdout) > 200 else result.stdout)
else:
print(f"❌ Error: {result.stderr}")
return False
except subprocess.TimeoutExpired:
print("⏰ Timeout - model may be too slow")
return False
except Exception as e:
print(f"❌ Exception: {e}")
return False
return True
def test_ollama_integration():
"""Test model through Ollama"""
print("\n🤖 Testing Ollama integration...")
# Check if Ollama is installed
try:
result = subprocess.run(["ollama", "list"], capture_output=True, text=True)
if result.returncode != 0:
print("❌ Ollama not installed or not running")
return False
except FileNotFoundError:
print("❌ Ollama command not found")
return False
model_name = "my-custom-model"
# Check if our custom model exists in Ollama
if model_name not in result.stdout:
print(f"⚠️ Model '{model_name}' not found in Ollama")
print("Create it first:")
print("1. ollama create my-custom-model -f Modelfile")
return False
print(f"✅ Found model: {model_name}")
# Test through Ollama API
test_prompts = [
"Cześć! Kim jesteś?",
"Jak zoptymalizować kod Python?",
"Co to jest machine learning?"
]
for i, prompt in enumerate(test_prompts, 1):
print(f"\n--- Ollama Test {i}/3: {prompt[:30]}... ---")
try:
# Test via CLI
cmd = ["ollama", "run", model_name, prompt]
result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
if result.returncode == 0:
print("✅ Ollama CLI response successful")
print("Response preview:", result.stdout[:200] + "..." if len(result.stdout) > 200 else result.stdout)
else:
print(f"❌ Ollama CLI error: {result.stderr}")
continue
except subprocess.TimeoutExpired:
print("⏰ Ollama timeout")
continue
except Exception as e:
print(f"❌ Ollama exception: {e}")
continue
# Test via API
print("\n🌐 Testing Ollama API...")
try:
api_url = "http://localhost:11434/api/generate"
test_data = {
"model": model_name,
"prompt": "Hello! Test API call.",
"stream": False
}
response = requests.post(api_url, json=test_data, timeout=60)
if response.status_code == 200:
data = response.json()
print("✅ Ollama API response successful")
print("API Response:", data.get('response', 'No response field')[:100])
else:
print(f"❌ API Error: {response.status_code}")
return False
except requests.exceptions.RequestException as e:
print(f"❌ API Request failed: {e}")
return False
return True
def benchmark_model():
"""Simple benchmark of the model"""
print("\n📊 Running simple benchmark...")
model_file = "my_custom_model.gguf"
if not os.path.exists(model_file):
print("❌ Model file not found for benchmark")
return
# Get file size
file_size = os.path.getsize(model_file) / (1024 ** 3) # GB
print(f"📁 Model size: {file_size:.2f} GB")
# Benchmark prompt
benchmark_prompt = "Explain artificial intelligence in simple terms."
llamacpp_main = "./llama.cpp/main"
if os.path.exists(llamacpp_main):
print("⏱️ Timing generation speed...")
cmd = [
llamacpp_main,
"-m", model_file,
"-p", benchmark_prompt,
"-n", "100",
"--temp", "0.7"
]
start_time = time.time()
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
end_time = time.time()
if result.returncode == 0:
duration = end_time - start_time
# Rough tokens estimation
tokens = len(result.stdout.split())
tokens_per_second = tokens / duration if duration > 0 else 0
print(f"⚡ Generation time: {duration:.2f} seconds")
print(f"🚀 Speed: ~{tokens_per_second:.1f} tokens/second")
print(f"📝 Generated tokens: ~{tokens}")
else:
print("❌ Benchmark failed")
except subprocess.TimeoutExpired:
print("⏰ Benchmark timeout")
def main():
"""Main test runner"""
print("🧪 Custom Model Test Suite")
print("=" * 40)
# Check prerequisites
print("🔍 Checking prerequisites...")
required_files = [
"my_custom_model.gguf",
"./llama.cpp/main",
"Modelfile"
]
missing_files = [f for f in required_files if not os.path.exists(f)]
if missing_files:
print("❌ Missing required files:")
for f in missing_files:
print(f" • {f}")
print("\nRun these commands first:")
print("1. python create_custom_model.py # fine-tune model")
print("2. ./convert_to_gguf.sh # convert to GGUF")
print("3. ollama create my-custom-model -f Modelfile # import to Ollama")
return
print("✅ All required files found")
# Run tests
tests_passed = 0
total_tests = 3
# Test 1: Direct llama.cpp
if test_llamacpp_direct():
tests_passed += 1
print("✅ llama.cpp test PASSED")
else:
print("❌ llama.cpp test FAILED")
# Test 2: Ollama integration
if test_ollama_integration():
tests_passed += 1
print("✅ Ollama test PASSED")
else:
print("❌ Ollama test FAILED")
# Test 3: Benchmark
benchmark_model()
tests_passed += 1 # Benchmark always "passes"
# Results
print("\n" + "=" * 40)
print(f"🎯 Test Results: {tests_passed}/{total_tests} passed")
if tests_passed == total_tests:
print("🎉 All tests passed! Your custom model is ready!")
print("\n🚀 Next steps:")
print("• ollama push my-custom-model # Share with the world")
print("• Integrate into your applications")
print("• Fine-tune further with more data")
else:
print("⚠️ Some tests failed. Check the output above.")
# Usage examples
print("\n📚 Usage Examples:")
print("# Ollama CLI:")
print("ollama run my-custom-model 'Your question here'")
print("\n# Ollama API:")
print("curl -X POST http://localhost:11434/api/generate \\")
print(" -H 'Content-Type: application/json' \\")
print(" -d '{\"model\": \"my-custom-model\", \"prompt\": \"Hello!\"}'")
print("\n# Python integration:")
print("import ollama")
print("response = ollama.chat(model='my-custom-model', messages=[")
print(" {'role': 'user', 'content': 'Hello!'}])")
if __name__ == "__main__":
main() |