الدرس 7 من 22

أساسيات Ollama

تخصيص النماذج

3 دقيقة للقراءة

ملف Modelfile في Ollama يتيح لك إنشاء تكوينات نماذج مخصصة مع طلبات نظام محددة، ومعاملات، وسلوكيات. فكر به كـ Dockerfile لنماذج اللغة الكبيرة.

ما هو Modelfile؟

┌─────────────────────────────────────────────────────────────────┐
│                        Modelfile                                 │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  ملف نصي يحدد:                                                  │
│                                                                 │
│  • النموذج الأساسي للبدء منه                                   │
│  • طلب النظام (الشخصية/التعليمات)                              │
│  • معاملات التشغيل (الحرارة، السياق، إلخ)                       │
│  • تنسيق القالب المخصص                                          │
│                                                                 │
│  النتيجة: متغير نموذج جديد مصمم لحالة استخدامك                  │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

إنشاء أول نموذج مخصص لك

الخطوة 1: إنشاء Modelfile

# أنشئ ملف اسمه "Modelfile" (بدون امتداد)
cat > Modelfile << 'EOF'
FROM llama3.2

SYSTEM """
You are a helpful coding assistant specialized in Python.
You always:
- Write clean, well-documented code
- Include type hints
- Add docstrings to functions
- Suggest improvements when reviewing code
Keep responses concise and focused on code.
"""

PARAMETER temperature 0.3
PARAMETER top_p 0.9
EOF

الخطوة 2: بناء النموذج

# أنشئ النموذج المخصص
ollama create python-coder -f Modelfile

# المخرجات:
# transferring model data
# using existing layer sha256:...
# creating new layer sha256:...
# writing manifest
# success

# تحقق من إنشائه
ollama list | grep python-coder

الخطوة 3: استخدم نموذجك المخصص

ollama run python-coder "Write a function to validate email addresses"

مرجع صيغة Modelfile

FROM (مطلوب)

# استخدم نموذج أساسي من مكتبة Ollama
FROM llama3.2

# استخدم إصدار/حجم محدد
FROM llama3.2:70b

# استخدم نموذج مخصص آخر كأساس
FROM my-other-model

SYSTEM (طلب النظام)

# سطر واحد
SYSTEM "You are a helpful assistant."

# متعدد الأسطر مع علامات اقتباس ثلاثية
SYSTEM """
You are a senior software engineer.
You write production-quality code.
You explain your reasoning step by step.
"""

PARAMETER (إعدادات التشغيل)

# معاملات التوليد
PARAMETER temperature 0.7
PARAMETER top_p 0.9
PARAMETER top_k 40
PARAMETER repeat_penalty 1.1

# السياق والذاكرة
PARAMETER num_ctx 4096
PARAMETER num_keep 24

# رموز الإيقاف
PARAMETER stop "<|endoftext|>"
PARAMETER stop "User:"

TEMPLATE (تنسيق الطلب)

# قالب محادثة مخصص
TEMPLATE """
{{ if .System }}<|system|>
{{ .System }}<|end|>
{{ end }}{{ if .Prompt }}<|user|>
{{ .Prompt }}<|end|>
{{ end }}<|assistant|>
{{ .Response }}<|end|>
"""

أمثلة عملية

المثال 1: مولد استعلامات SQL

FROM llama3.2

SYSTEM """
You are a SQL expert. When given a description of data needs:
1. Write the SQL query
2. Explain what it does
3. Suggest indexes if relevant

Database type: PostgreSQL
Output format: SQL code block followed by explanation.
"""

PARAMETER temperature 0.2
PARAMETER num_ctx 4096
ollama create sql-expert -f Modelfile
ollama run sql-expert "Get all users who signed up last month and made at least 3 purchases"

المثال 2: مراجع الكود

FROM llama3.2:70b

SYSTEM """
You are a code review assistant. For each code snippet:

1. Security: Check for vulnerabilities (injection, XSS, etc.)
2. Performance: Identify bottlenecks and inefficiencies
3. Maintainability: Suggest cleaner patterns
4. Bugs: Find potential runtime errors

Format: Use markdown with sections for each category.
Be specific and actionable in your feedback.
"""

PARAMETER temperature 0.3
PARAMETER num_ctx 8192

المثال 3: الكاتب التقني

FROM llama3.2

SYSTEM """
You are a technical documentation writer.
Write clear, concise documentation following these rules:
- Use active voice
- Include code examples for all concepts
- Structure with clear headings
- Add notes for gotchas and edge cases
Target audience: Intermediate developers
"""

PARAMETER temperature 0.5

التخصيص المتقدم

رموز إيقاف متعددة

FROM llama3.2

SYSTEM "You are a chatbot."

# تسلسلات إيقاف متعددة
PARAMETER stop "Human:"
PARAMETER stop "User:"
PARAMETER stop "###"

الدمج مع المحولات

FROM llama3.2

# طبق محول LoRA (إذا دربت واحداً)
ADAPTER /path/to/adapter.gguf

SYSTEM "You are specialized in medical terminology."

إدارة النماذج المخصصة

# اعرض جميع النماذج (بما في ذلك المخصصة)
ollama list

# اعرض تفاصيل النموذج المخصص
ollama show python-coder --modelfile

# انسخ نموذج مخصص
ollama cp python-coder python-coder-v2

# احذف نموذج مخصص
ollama rm python-coder

# تصدير (غير مدعوم مباشرة، لكن يمكنك حفظ Modelfile)
ollama show python-coder --modelfile > python-coder.Modelfile

أفضل الممارسات

1. ابدأ بسيطاً

# جيد: طلب نظام مركز
SYSTEM "You are a Python expert. Write clean, typed code."

# سيء: طلب معقد جداً
SYSTEM "You are an AI that does everything perfectly..."

2. نسخ ملفات Modelfile الخاصة بك

models/
├── sql-expert/
│   ├── Modelfile
│   └── README.md
├── code-reviewer/
│   ├── Modelfile
│   └── README.md
└── python-coder/
    ├── Modelfile
    └── README.md

3. اختبر بشكل متكرر

# أنشئ واختبر
ollama create test-model -f Modelfile
ollama run test-model "test prompt"

# عدّل Modelfile، أعد الإنشاء
ollama rm test-model
ollama create test-model -f Modelfile

النماذج المخصصة تتيح لك إنشاء مساعدين ذكاء اصطناعي متخصصين بدون أي تدريب. في الوحدة التالية، سنبني تطبيقات باستخدام واجهة Ollama API. :::

اختبار

الوحدة 2: أساسيات Ollama

خذ الاختبار