تعلّم

كيف يعمل الذكاء الاصطناعي حقًا

دليل بلغة بسيطة للمصطلحات التي تراها في كل صفحة نموذج — الرموز، ونوافذ السياق، وإصابات التخزين المؤقت، والوكلاء، وMCP، وخليط الخبراء — مع عرض تفاعلي في كل فصل وأمثلة حقيقية من 274 نموذجًا نتابعها.

20 فصلًا · نحو 15 دقيقة · شرح صوتي

العروض التفاعلية والملاحظات التقنية باللغة الإنجليزية.

01 · ما الذكاء الاصطناعي؟

ما الذكاء الاصطناعي؟

الذكاء الاصطناعي برنامج يتعلّم الأنماط من الأمثلة بدلًا من اتباع قواعد كتبها إنسان يدويًا. فمرشّح الرسائل المزعجة الذي رأى ملايين الرسائل يتعلّم بنفسه شكل الرسالة المزعجة؛ ولا أحد يكتب كل قاعدة.

الذكاء الاصطناعي في أدوات البرمجة هو نموذج لغوي كبير (LLM). دُرِّب بقراءة كمّ هائل من النصوص والشيفرات — كتب ومواقع ووثائق ومستودعات مفتوحة المصدر — وبضبط مليارات الأرقام الداخلية التي تُسمّى المعاملات، حتى صار بارعًا جدًا في مهمة واحدة: تخمين النص الذي يأتي بعد ذلك.

تبدو مهمة صغيرة، لكن لتخمين السطر التالي من برنامج بدقة، على النموذج أن يتعلّم القواعد اللغوية والحقائق وقواعد لغات البرمجة وفهمًا عمليًا لطريقة حلّ المشكلات. كل ما يفعله — من الإجابة عن الأسئلة إلى كتابة الشيفرة وإصلاح الأخطاء — مبني على هذه المهارة الواحدة.

تعمّق أكثرTransformers, layers and parameters · ملاحظات تقنية بالإنجليزية

Today's language models are transformers. Text becomes a sequence of token IDs; each ID is looked up in an embedding table to get a vector of a few thousand numbers (the model's width, d_model). That sequence of vectors then flows through a stack of identical blocks — 30 to 120 of them — and each block does two things:

  • Self-attention: every position looks back at earlier positions and pulls in the information it needs (which noun does “it” refer to, which variable was declared above).
  • A feed-forward network (MLP): a wide two-layer network applied to each position separately, where much of the model's stored knowledge lives.

Residual connections add each block's output back onto its input, and normalisation keeps the numbers in range. At the end, an unembedding matrix turns the final vector into one score (a logit) per vocabulary entry. Parameters are just the entries of all these matrices. For a dense model, a good estimate is:

params ≈ 12 × layers × d_model²   (+ vocab × d_model for embeddings)
# e.g. 32 layers, d_model 4096  →  12 × 32 × 4096² ≈ 6.4B  (+0.5B embeddings) ≈ "7B"

Nothing in that list is a rule about language. All of the behaviour comes from the values the parameters take during training.

02 · كيف يجيب النموذج

كيف يجيب النموذج

يمرّ بناء النموذج بمرحلتين. التدريب هو الجزء المكلف: آلاف الشرائح تعمل لأسابيع بينما يقرأ النموذج بياناته وتُضبط معاملاته. بعد ذلك يُجمَّد النموذج. أما الاستدلال فهو ما يحدث عند استخدامه: يدخل نصك، وينتج النموذج الإجابة قطعة صغيرة تلو الأخرى.

في كل خطوة ينظر النموذج إلى كل ما كُتب حتى الآن، ويعطي كل قطعة تالية محتملة احتمالًا. تُختار قطعة وتُضاف إلى النص، ثم يتكرر ذلك حتى تكتمل الإجابة. ولهذا تظهر الإجابات على الشاشة كلمةً كلمة.

يتحكّم إعداد يُسمّى درجة الحرارة في مدى جرأة هذا الاختيار. الحرارة المنخفضة تختار الخيار الأرجح دائمًا تقريبًا، وهذا يناسب الشيفرة. والحرارة المرتفعة تسمح بكلمات أقل احتمالًا، وهذا يناسب العصف الذهني.

Try itNext-token sampler

The best language for a quick script is

    Illustrative odds. At temperature 0 the top choice always wins; turn it up and the long tail starts getting picked.
    تعمّق أكثرLogits, softmax, temperature and sampling · ملاحظات تقنية بالإنجليزية

    The model outputs a logit zᵢ for every token in its vocabulary. Softmax turns logits into probabilities, and temperature T divides them first:

    p(i) = exp(zᵢ / T) / Σⱼ exp(zⱼ / T)
    T → 0   : all probability on the top token (greedy decoding)
    T = 1   : the model's own distribution
    T > 1   : flatter — rare tokens get picked more often

    Samplers then trim the tail before picking: top-k keeps the k most likely tokens, top-p (nucleus) keeps the smallest set whose probabilities sum to p, and min-p drops tokens below a fraction of the top token's probability. Repetition and frequency penalties lower the logits of tokens already used. A fixed seed makes sampling repeatable on the same hardware and software.

    The generation loop

    tokens = tokenize(prompt)
    kv = prefill(tokens)                 # one parallel pass over the whole prompt
    while True:
        logits = forward(last_token, kv)  # one step, reusing the cached keys/values
        next = sample(logits, T, top_p)
        if next == END: break
        tokens.append(next); stream(next)

    Prefill is a big parallel matrix multiply and is limited by compute; each decode step is tiny and is limited by how fast weights can be read from memory. That split explains most of what you'll see about speed later in this guide.

    03 · الرموز

    ما الرموز (Tokens)؟

    لا تقرأ النماذج حروفًا ولا كلمات كاملة، بل تقرأ رموزًا (tokens): أجزاء من النص تكون غالبًا كلمة أو جزءًا من كلمة أو رمزًا. الكلمات الإنجليزية الشائعة رمز واحد، أما الكلمة النادرة فقد تنقسم إلى جزأين أو ثلاثة. في الإنجليزية يساوي الرمز نحو أربعة أحرف، أما العربية فتحتاج غالبًا إلى رموز أكثر للمعنى نفسه. وتستهلك الشيفرة رموزًا أكثر في كل سطر بسبب الرموز والمسافات البادئة.

    تهمّ الرموز لأنها وحدة قياس النماذج ومحاسبتها. تُذكر الأسعار لكل مليون رمز، بسعر لما ترسله (المدخلات) وسعر آخر لما يكتبه النموذج (المخرجات). والمخرجات عادةً أغلى بعدة أضعاف.

    Try itToken counter

    An approximation — every model family has its own tokenizer, so exact counts differ a little.
    تعمّق أكثرByte-pair encoding and vocabularies · ملاحظات تقنية بالإنجليزية

    Most tokenizers use byte-pair encoding (BPE) or a close relative. Training starts from raw bytes and repeatedly merges the most frequent adjacent pair into a new token, until the vocabulary reaches its target size. Frequent strings (“ the”, “function”, four spaces of indentation) end up as single tokens; rare strings fall back to smaller pieces, and ultimately to individual bytes, so any text can be encoded.

    • Vocabulary sizes range from about 32,000 (older Llama, Mistral) to 150,000–260,000 (Qwen, Gemma, GPT-4o-era tokenizers). Bigger vocabularies mean fewer tokens per text but a bigger embedding table.
    • Languages are not treated equally. English averages about 4 characters per token; many other languages and scripts need noticeably more tokens for the same meaning, so they cost more and fill the context faster. Newer multilingual tokenizers have narrowed the gap.
    • Special tokens mark structure the user never types: start and end of a message, role markers such as <|im_start|>assistant, tool-call delimiters, image placeholders.
    • Numbers are tokenized oddly (“1234” might be “12” + “34”), which is one reason models used to struggle with arithmetic; many tokenizers now split digits individually.

    04 · نافذة السياق

    نافذة السياق

    نافذة السياق هي مقدار النص الذي يستطيع النموذج رؤيته دفعة واحدة: تعليماتك، والمحادثة حتى الآن، والملفات التي فتحها وكيل البرمجة، والإجابة التي يكتبها. إنها الذاكرة العاملة للنموذج، وكل ما يقع خارجها لا وجود له بالنسبة إليه.

    يُقاس السياق بالرموز. يستطيع نموذج بنافذة مليون رمز أن يحمل نحو 750 ألف كلمة إنجليزية في آن واحد — ما يعادل عشر روايات أو قاعدة شيفرة متوسطة الحجم. لا يتذكر النموذج محادثاتك السابقة بنفسه؛ بل تمنحه الأدوات ذاكرة بإعادة النص القديم إلى النافذة. وعندما تمتلئ النافذة يُحذف أقدم المحتوى أو يُلخَّص.

    Try itFill the window

    • System prompt
    • Files
    • Chat
    • Logs
    • Repo
    When something new does not fit, the oldest items fall out of the window (the system prompt always stays).
    تعمّق أكثرAttention cost, positions and the KV cache · ملاحظات تقنية بالإنجليزية

    In attention, every token computes a query, a key and a value. Each query is compared with every earlier key, and the resulting weights mix the values:

    Attention(Q, K, V) = softmax(Q·Kᵀ / √d) · V
    cost of prefill  ∝ n²   (every token vs. every earlier token)
    memory per token = 2 × layers × kv_heads × head_dim × bytes   ← the KV cache

    During generation the keys and values of past tokens are kept in the KV cache, so each new token only computes its own. That cache grows linearly with context and is often what limits long contexts — far more than the weights. Designs that shrink it: grouped-query attention (many query heads share a few KV heads), multi-head latent attention (DeepSeek; stores a compressed latent instead of full keys and values), sliding-window layers that only look back a few thousand tokens, and hybrids that mix in linear-attention or state-space layers with constant-size memory.

    Positions are encoded with RoPE (rotary embeddings), which rotate queries and keys by an angle that depends on position. Context windows are extended after training by stretching those angles (position interpolation, NTK scaling, YaRN) and fine-tuning briefly on long documents. Advertised and effective context differ: models retrieve facts from the start and end of a long prompt more reliably than from the middle, so put the most important material at the edges.

    05 · موجّهات النظام

    الموجّهات و موجّهات النظام

    كل ما تكتبه هو موجّه (prompt). لكن قبل أن تصل رسالتك، يكون التطبيق قد أرسل إلى النموذج تعليماته المخفية: موجّه النظام. يحدّد من هو النموذج، وما الأدوات التي يمكنه استخدامها، وبأي أسلوب يكتب، وما الذي يجب ألا يفعله أبدًا. ترسل وكلاء البرمجة موجّهات نظام طويلة تشرح كيفية تعديل الملفات ومتى تُشغَّل الاختبارات ومتى يُطلب الإذن.

    لهذا قد يبدو النموذج نفسه مختلفًا تمامًا في أداتين: العقل نفسه، والإحاطة مختلفة. الموجّهات الجيدة محدّدة: قل ما تريد، وقدّم السياق الذي لا يستطيع النموذج تخمينه، واعرض مثالًا على النتيجة، ووضّح ما معنى «انتهى».

    Try itSame question, different system prompt
    System

    You

    How do I reverse a list in Python?

    Model

    تعمّق أكثرChat templates and structured output · ملاحظات تقنية بالإنجليزية

    A conversation is serialised into one token sequence by the model's chat template. The system prompt is simply the first message, wrapped in role markers:

    <|im_start|>system
    You are a careful coding assistant. Tools: read_file, run_tests …<|im_end|>
    <|im_start|>user
    How do I reverse a list in Python?<|im_end|>
    <|im_start|>assistant
    

    Models are trained to give the system role more authority than user or tool messages (an instruction hierarchy), but it's still just text in the same window. That's the root of prompt injection: instructions hidden in a web page, file or tool result can try to override the real ones. Agents defend with permissions, sandboxes and by treating tool output as data.

    Structured output goes further than asking nicely for JSON. With constrained decoding, the runtime compiles a JSON Schema or grammar into a state machine and masks the logits of any token that would break it, so the output is guaranteed to parse. llama.cpp calls these GBNF grammars; hosted APIs expose them as “structured outputs” or “JSON mode”.

    06 · إصابات التخزين المؤقت

    ما إصابة التخزين المؤقت؟

    يرسل وكيل البرمجة إلى النموذج الكتلة الكبيرة نفسها من النص مرة بعد مرة: تعليمات النظام، وملفات مشروعك، والمحادثة حتى الآن. ولا يتغير في كل دور إلا آخر بضعة أسطر. ومعالجة كل ذلك من الصفر كل مرة ستكون بطيئة ومكلفة.

    التخزين المؤقت للموجّهات يحل هذه المشكلة. يحفظ المزوّد ما أنجزه على بداية طلبك، وفي المرة التالية التي يبدأ فيها طلب بالنص نفسه تمامًا يعيد استخدام ذلك العمل بدلًا من تكراره. هذه هي إصابة التخزين المؤقت، وتُحاسَب الرموز المخزّنة بخصم كبير. وإذا تغيّر حرف واحد قرب البداية، فكل ما بعده إخفاق ويُحاسَب بالسعر الكامل.

    التخزين المؤقت قصير العمر — غالبًا دقائق — لذا يفيد أكثر في الجلسات المزدحمة. العمل نفسه بتكلفة أقل بكثير وسرعة أعلى؛ ولهذا تحرص الأدوات على إبقاء بداية كل طلب متطابقة.

    Try itSession cost · Claude Sonnet 5.5 on Anthropic
    Without cache$5.36
    With cache hits$0.91

    83% cheaper with caching

    Each turn repeats the prefix, adds 2,000 new tokens and gets 1,000 back. Fresh input $2.00, cache hit $0.10 per million tokens (95% off).
    تعمّق أكثرHow prefix caching works · ملاحظات تقنية بالإنجليزية

    What's cached is the KV cache of a prompt prefix — the keys and values computed during prefill. Serving engines split the cache into fixed blocks (vLLM's PagedAttention uses 16-token pages), hash each block together with everything before it, and reuse any block whose hash matches a new request. A change at position k changes the hash of every block after k, which is why one edited character near the start invalidates the rest.

    cost = cached_tokens × cache_read_price
         + new_tokens    × input_price
         + written_tokens × cache_write_price   (some providers)
         + output_tokens × output_price

    Some APIs cache automatically; others ask you to mark a breakpoint after the stable part (system prompt, tool definitions, documents). Practical rules: keep the stable material first and byte-identical (no timestamps in the system prompt), append rather than edit conversation history, and keep tool definitions in a fixed order. Entries are evicted after minutes of inactivity, or held longer for a fee. Running locally, llama.cpp and Ollama keep the last prompt's KV cache in memory, which is why a follow-up question starts answering almost instantly.

    07 · رموز الاستدلال

    رموز الاستدلال

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

    هذا يجعلها أفضل بكثير في المسائل الصعبة، مثل الأخطاء المعقدة والرياضيات والتخطيط لتغيير كبير. لكن التفكير نفسه مصنوع من الرموز أيضًا. تُحاسَب رموز الاستدلال على أنها مخرجات، حتى لو لم ترها أبدًا. وتتيح لك أدوات كثيرة اختيار مستوى الجهد، من منخفض إلى مرتفع.

    Try itReasoning effort · priced on Claude Opus 5.5

    Question: “Why does this test fail only on Mondays?” Token counts are typical, not exact.
    تعمّق أكثرTest-time compute and reinforcement learning · ملاحظات تقنية بالإنجليزية

    Reasoning models are trained with reinforcement learning on verifiable tasks: maths with checkable answers, code with unit tests, puzzles with known solutions. The model generates long chains of thought; chains that end in a correct answer are rewarded. Over training, behaviours that help — breaking problems down, checking work, backtracking — become more frequent, and the chains get longer.

    This adds a second scaling axis: besides a bigger model, you can spend more test-time compute. Accuracy on hard problems rises roughly with the logarithm of the thinking budget, up to a point. Effort settings (low/medium/high) or explicit budget tokens cap that spend.

    • Thinking tokens are output tokens: billed as output and generated at decode speed, so they add latency as well as cost.
    • Many APIs return a summary or an encrypted copy of the reasoning instead of the raw chain; agents pass it back on the next turn so the model keeps its train of thought between tool calls (interleaved thinking).
    • More thinking isn't always better: simple lookups get slower and occasionally worse when a model over-thinks.

    08 · الأدوات والوكلاء

    استدعاء الأدوات والوكلاء

    وحده، لا يستطيع النموذج إلا كتابة النص. استدعاء الأدوات يغيّر ذلك. يخبر التطبيقُ النموذجَ بالأدوات المتاحة — قراءة ملف، تشغيل أمر، البحث في الويب — وعندما يحتاج النموذج إلى أداة يكتب طلبًا منظّمًا بدلًا من رد عادي. ينفّذ التطبيق الأداة فعليًا ويعيد النتيجة، فيقرؤها النموذج ويقرر ما يفعله بعد ذلك.

    ضع ذلك في حلقة متكررة تحصل على وكيل: اقرأ الاختبار الفاشل، افتح الملف، أجرِ تعديلًا، شغّل الاختبارات مجددًا، وكرّر حتى تنجح. كل وكيل برمجة في هذا الموقع هو في جوهره هذه الحلقة.

    Try itStep through an agent loop
    تعمّق أكثرTool definitions and the agent loop · ملاحظات تقنية بالإنجليزية

    A tool is described to the model as a name, a description and a JSON Schema for its arguments. The model replies with a structured call instead of text; the client executes it and sends the result back as a tool message.

    {"name": "run_tests",
     "description": "Run the project's test suite and return failures",
     "input_schema": {"type": "object",
       "properties": {"path": {"type": "string"}}, "required": []}}
    
    messages = [system, user]
    while True:
        reply = model(messages, tools)
        if not reply.tool_calls: return reply.text
        for call in reply.tool_calls:            # may run several in parallel
            result = execute(call)               # sandbox + permission checks here
            messages += [reply, tool_result(call.id, result)]

    What separates good agents from bad ones is mostly around this loop: precise tool descriptions, small and composable tools, truncating huge outputs, asking permission for risky actions, and context management — summarising or dropping old tool results so long sessions don't overflow the window. tool_choice can force a specific tool or forbid tools; constrained decoding keeps the arguments valid JSON.

    09 · MCP

    MCP، بروتوكول سياق النموذج

    كل أداة يستخدمها النموذج يجب أن يربطها أحدهم. قبل MCP كان كل تطبيق يربط كل خدمة بطريقته الخاصة. بروتوكول سياق النموذج (Model Context Protocol) معيار مشترك لهذا الربط — فكّر فيه كمنفذ USB-C لأدوات الذكاء الاصطناعي.

    يغلّف خادم MCP خدمة ما — GitHub، أو قاعدة بيانات، أو متصفحًا، أو ملفات التصميم — ويصف الأدوات التي يقدّمها. وأي تطبيق يتحدث MCP يمكنه توصيله، فيستخدم النموذج تلك الأدوات فورًا. اكتب الخادم مرة واحدة وسيعمل في كل مكان؛ ولهذا ظهر كثير منها بهذه السرعة.

    Try itPlug in a server
    MCP clientYour coding agent0 tools available
    Click servers to plug them in. Each one adds its tools to what the model can do — no changes to the model or the app.
    تعمّق أكثرInside the protocol · ملاحظات تقنية بالإنجليزية

    MCP is JSON-RPC 2.0 between a client (inside the agent or IDE) and a server. Local servers run as a subprocess and talk over stdio; remote ones use Streamable HTTP, with OAuth 2.1 for login. The session starts with an initialize handshake in which both sides declare their capabilities.

    → {"jsonrpc":"2.0","id":1,"method":"tools/list"}
    ← {"jsonrpc":"2.0","id":1,"result":{"tools":[{"name":"create_issue",
         "description":"Open a GitHub issue","inputSchema":{…}}]}}
    → {"jsonrpc":"2.0","id":2,"method":"tools/call",
       "params":{"name":"create_issue","arguments":{"title":"Fix login"}}}

    Servers offer three primitives: tools (actions the model can call), resources (data the app can read into context, addressed by URI) and prompts (reusable templates the user can invoke). Clients can offer sampling (the server asks the host's model to generate text) and elicitation (the server asks the user a question). Every tool's schema is sent to the model, so dozens of servers can eat thousands of tokens of context — one reason agents now load tool definitions lazily.

    10 · UTCP

    UTCP، بروتوكول استدعاء الأدوات الشامل

    يضع MCP خادمًا بين النموذج والخدمة. أما بروتوكول استدعاء الأدوات الشامل (UTCP) فينطلق من سؤال آخر: إذا كانت للخدمة واجهة برمجية أصلًا، فلماذا نغلّفها بخادم آخر؟ UTCP معيار مفتوح يتيح للوكيل استدعاء الأداة مباشرة.

    تنشر كل أداة دليلًا: ملف JSON يذكر ما تفعله كل أداة، والمدخلات التي تأخذها، وكيفية استدعائها. يقرأ الوكيل الدليل مرة واحدة، ثم يتحدث مع الخدمة عبر بروتوكولها الخاص، مثل HTTP أو سطر الأوامر، وبتسجيل الدخول الخاص بها.

    المشكلة أن العمل ينتقل ولا يختفي. على عميل الوكيل أن يتحدث كل بروتوكول ويتولى تسجيل الدخول وإعادة المحاولة والأخطاء بنفسه، وكل أداة مسؤولة عن أمانها. كما أن لدى MCP خوادم ودعمًا من الشركات أكثر بكثير.

    لست مضطرًا إلى الاختيار للأبد: لدى UTCP إضافة لاستدعاء خوادم MCP الموجودة.

    Try itFollow one tool call
    Your agent MCP server Weather API
    The agent sends
    Someone maintains
    Login handled by
    {
      "manual_version": "1.0.0",
      "utcp_version": "1.0.1",
      "tools": [{
        "name": "get_weather",
        "description": "Get current weather for a location",
        "inputs": {
          "type": "object",
          "properties": { "location": { "type": "string" } },
          "required": ["location"]
        },
        "tool_call_template": {
          "call_template_type": "http",
          "url": "https://api.example.com/weather",
          "http_method": "GET"
        }
      }]
    }
    Switch protocols to compare the same weather lookup. The API is a made-up example.
    تعمّق أكثرManuals and call templates · ملاحظات تقنية بالإنجليزية

    A UTCP manual lists tools, each with JSON Schema inputs and outputs and a tool_call_template saying how to reach it. Templates exist for HTTP, server-sent events, streamable HTTP, the command line, text files and MCP itself. The client substitutes the arguments into the template and calls the service directly, with the service's own authentication (API key, OAuth or basic auth, declared in the template and filled from local variables).

    agent ──reads manual once──▶  /utcp  (JSON)
    agent ──GET /weather?location=Oslo──▶  api.example.com   # no proxy in between

    The trade-off versus MCP: no extra server process and no extra hop, and existing APIs work as they are; but the client must implement every transport, and features MCP standardises — resources, prompts, sampling, server-initiated notifications — are out of scope.

    11 · التضمينات وRAG

    التضمينات وRAG

    لا تستطيع الحواسيب مقارنة المعاني مباشرة، لذا تحوّل النماذج النص إلى قوائم أرقام تُسمّى التضمينات (embeddings). كل تضمين نقطة في فضاء من مئات الأبعاد، وتقع النصوص المتقاربة في المعنى قرب بعضها — فعبارة «أصلح خطأ تسجيل الدخول» تقع قرب «خطأ في المصادقة» رغم أنهما لا تشتركان في أي كلمة.

    وهذا يجعل البحث بالمعنى ممكنًا: حوّل كل مستند مرة واحدة، ثم حوّل كل سؤال واسترجع أقرب المستندات. أما التوليد المعزّز بالاسترجاع (RAG) فيضع تلك المستندات في نافذة السياق قبل أن يجيب النموذج. بهذه الطريقة يستطيع النموذج الإجابة عن أسئلة حول قاعدة شيفرتك دون أن يُدرَّب عليها.

    Try itSearch by meaning
      A 2-D sketch of a space that really has hundreds of dimensions. The three closest documents are what RAG would hand to the model.
      تعمّق أكثرVectors, similarity and retrieval pipelines · ملاحظات تقنية بالإنجليزية

      An embedding model maps text to a vector, typically 384 to 4,096 dimensions. Similarity is usually cosine similarity:

      cos(a, b) = (a · b) / (‖a‖ ‖b‖)      # 1 = same direction, 0 = unrelated

      Search over millions of vectors uses approximate nearest-neighbour indexes such as HNSW graphs or IVF clusters, in a vector database or a library like FAISS. A production RAG pipeline usually adds:

      • Chunking: split documents into passages of a few hundred tokens, ideally along headings or function boundaries, with some overlap.
      • Hybrid search: combine vectors with keyword search (BM25), which catches exact names, error codes and identifiers that embeddings blur.
      • Reranking: a cross-encoder scores the top 50–100 candidates against the question and keeps the best handful.
      • Grounded prompting: put the passages in the context with their sources and ask the model to cite them.

      Coding agents often skip vectors entirely and search with grep-style tools in a loop — exact, always up to date, and good enough when the model can iterate.

      12 · الهلوسة

      لماذا تهلوس النماذج

      مهمة النموذج إنتاج نص مرجّح، لا نص صحيح. غالبًا ما يتطابقان؛ وحين لا يتطابقان قد يقول شيئًا خاطئًا بثقة تامة. هذه هي الهلوسة.

      في الشيفرة تظهر كدالة غير موجودة، أو خيار إعداد لم يُضف قط، أو اسم حزمة يبدو صحيحًا تمامًا. وهي أكثر شيوعًا مع المكتبات غير المعروفة والتغييرات الحديثة والتفاصيل الدقيقة. والحلول: أعطِ النموذج المصدر الحقيقي (الوثائق والملفات والبحث)، ودعه يشغّل الشيفرة والاختبارات، وتحقّق من كل ما هو مهم.

      Try itSpot the made-up answer

      Each question has two real answers and one a model might plausibly invent.
      تعمّق أكثرWhy it happens and how it is measured · ملاحظات تقنية بالإنجليزية

      Pre-training rewards predicting plausible text, not saying “I don't know”; benchmarks that grade only right or wrong reward guessing over abstaining. So a model that hasn't stored a fact still produces the most fact-shaped continuation. Rare facts — seen a handful of times in training — are the most at risk, which is why niche libraries and recent APIs suffer most.

      • Calibration measures whether stated confidence matches accuracy. Some evaluations now score abstentions separately and penalise confident errors.
      • Grounding (RAG, search, reading the actual source file) moves the answer from memory to the context, where models are much more reliable.
      • Verification loops — run the code, run the tests, type-check, call the real API — catch invented functions immediately.
      • Package hallucinations are a security problem: attackers register package names that models tend to invent (“slopsquatting”). Check that a dependency exists and is the one you meant before installing it.

      13 · كيف تُدرَّب النماذج

      كيف تُدرَّب النماذج

      يُبنى النموذج على مراحل. أولًا التدريب المسبق: يقرأ تريليونات الرموز ويتعلّم التنبؤ بالرمز التالي. من هنا تأتي معرفته، وهي أغلى خطوة بفارق كبير — لكن النتيجة ليست بعد مساعدًا مفيدًا.

      بعد ذلك يعلّمه الضبط الدقيق على أمثلة لمحادثات جيدة أن يتبع التعليمات. وأخيرًا التعلّم المعزّز: يحاول أداء مهام، وتُقيَّم إجاباته — من قِبل أشخاص أو نماذج أخرى أو فحوص آلية مثل نجاح الشيفرة في اختباراتها — وتُكافأ الإجابات الجيدة. هذه الخطوة الأخيرة سبب كبير في سرعة تحسّن نماذج البرمجة.

      Try itSame prompt, three stages
      Prompt

      Write a function that checks whether a word is a palindrome.

      Model after pre-training

      تعمّق أكثرLoss, compute and post-training · ملاحظات تقنية بالإنجليزية

      Pre-training minimises cross-entropy loss: the negative log-probability the model gave to each actual next token, averaged over trillions of tokens. Gradients from that loss nudge every parameter, batch after batch.

      loss = − (1/N) Σ log p(tokenₜ | tokens<ₜ)
      training compute ≈ 6 × parameters × training tokens   (FLOPs)
      # 70B params × 15T tokens ≈ 6.3 × 10²⁴ FLOPs

      Scaling laws describe how loss falls predictably with more parameters, data and compute; modern models are trained on far more tokens per parameter than the old “compute-optimal” ratio of about 20, because a smaller model that's been trained longer is cheaper to serve.

      Post-training turns the predictor into an assistant: supervised fine-tuning on curated conversations; preference optimisation (RLHF with a reward model, or DPO directly on pairs of better/worse answers); and RL with verifiable rewards for maths, code and agent tasks, run in sandboxes with real tools. Distillation trains a smaller model on a larger one's outputs, which is how many fast “mini” and “flash” models are made.

      14 · خليط الخبراء

      خليط الخبراء

      يستخدم النموذج العادي، أو الكثيف، كل معاملاته لكل رمز. أما نموذج خليط الخبراء (MoE) فمقسّم إلى شبكات فرعية أصغر كثيرة تُسمّى الخبراء. ولكل رمز يختار موجّه صغير بضعة خبراء فقط للعمل، ويبقى الباقون خاملين.

      النتيجة: يحمل النموذج معرفة شبكة هائلة، بينما تكلّف كل خطوة ما يكلّفه نموذج صغير. لهذا تُذكر المعاملات الكلية والنشطة بشكل منفصل. الحجم الكلي يحدد الذاكرة اللازمة لاستضافة النموذج؛ والحجم النشط يحدد سرعة كل رمز وكلفته.

      Try itRoute tokens through experts

      token router expert 1expert 2expert 3expert 4expert 5expert 6expert 7expert 8
      Different tokens wake different experts. Over a sentence, most experts get used — but only 2 of 8 at a time.
      تعمّق أكثرRouters, top-k gating and load balancing · ملاحظات تقنية بالإنجليزية

      In an MoE layer the feed-forward network is replaced by E experts and a router. For each token the router computes a score per expert, keeps the top k and mixes their outputs by those scores:

      g = softmax(W_router · x)          # one weight per expert
      y = Σ_{i ∈ topk(g)} gᵢ · Expertᵢ(x)
      active params ≈ shared (attention, embeddings, shared experts) + k/E × expert params

      Modern MoEs use many small experts (128–384) with 4–10 active, often plus one always-on shared expert. Training adds a load-balancing term (or a bias adjustment) so tokens spread evenly; otherwise a few experts take all the work. Serving at scale uses expert parallelism — experts spread over many GPUs, tokens routed between them. At home the useful fact is the asymmetry: memory scales with total parameters, speed with active ones, and the rarely-used experts can sit in system RAM while attention runs on the GPU.

      15 · التكميم

      التكميم (fp8 وfp4)

      كل معامل هو رقم، ويمكن تخزين الأرقام بدقة أعلى أو أقل. يخزّنها التكميم بعدد أقل من البتات — مثل 8 بتات (fp8) أو 4 بتات (fp4) بدلًا من 16. فيصبح النموذج أصغر وأسرع في التشغيل وأرخص عادةً، مقابل تراجع طفيف في الجودة يكون ملحوظًا أحيانًا.

      ولهذا قد يختلف سعر النموذج المفتوح نفسه بين مزوّد وآخر: فربما يشغّلون دقّات مختلفة على عتاد مختلف.

      Try itShrink a model

      The orange line is a smooth signal stored with the chosen precision: fewer bits, fewer levels, more rounding.
      تعمّق أكثرScales, blocks and quantization formats · ملاحظات تقنية بالإنجليزية

      Quantization maps each weight to a small integer and stores a scale (and sometimes an offset) per block of weights:

      q = round(w / scale)           # e.g. a 4-bit integer in [-8, 7]
      w ≈ q × scale                  # what the GPU reconstructs on the fly
      bits per weight = bits + (scale bits / block size)
      # Q4_K: blocks of 32 inside super-blocks of 256, 6-bit scales → ~4.5 bpw;
      # Q4_K_M keeps some tensors at Q6_K → ~4.85 bpw on average
      • Post-training quantization methods (GPTQ, AWQ, llama.cpp's importance matrix) use a little calibration text to decide where precision matters most and round to minimise output error, not weight error.
      • Hardware formats: FP8 (E4M3) is native on recent NVIDIA, AMD and Intel accelerators; MXFP4 and NVFP4 are 4-bit floats with shared block scales supported by the newest GPUs, so 4-bit can run at full speed instead of being unpacked.
      • Quantization-aware training (QAT) simulates low precision during training, so the released 4-bit model loses almost nothing.
      • Quality loss is measured with perplexity or, better, the KL divergence between the quantized and full-precision output distributions, plus task benchmarks.

      16 · الأوزان المفتوحة والمغلقة

      الأوزان المفتوحة والمغلقة

      أوزان النموذج هي معاملاته المتعلَّمة — الملف الذي هو النموذج نفسه. النماذج المغلقة تبقيها خاصة: لا يمكنك استخدامها إلا عبر تطبيق المختبر أو واجهته البرمجية. أما النماذج مفتوحة الأوزان فتنشرها، فيستطيع أي أحد تنزيلها وتشغيلها وضبطها أو بيع الوصول إليها.

      كثيرًا ما تقدّمت النماذج المغلقة في القدرة الخام. أما المفتوحة فتمنحك الخيار: مزوّدون كثيرون يتنافسون في السعر والسرعة، وبيانات تبقى على أجهزتك، ونموذج يواصل العمل حتى بعد أن ينتقل صانعه إلى غيره.

      Try itThe models we track
      Price is per million input tokens.
      تعمّق أكثرLicences, formats and fine-tuning · ملاحظات تقنية بالإنجليزية

      “Open weights” isn't automatically “open source”. Licences range from permissive (Apache 2.0, MIT) to custom ones with usage policies, attribution rules or limits for very large companies. The Open Source Initiative's definition of open-source AI also asks for enough information about the training data to reproduce the model, which few releases provide.

      Weights are distributed as safetensors files (a simple, memory-mappable format that can't execute code, unlike old pickle checkpoints) with a config.json describing the architecture and a tokenizer. Community conversions add GGUF (llama.cpp) and MLX (Apple).

      Open weights can be fine-tuned. LoRA freezes the model and trains a low-rank update for selected matrices — W' = W + B·A, with A and B thin — so only a fraction of a percent of the parameters train; QLoRA does this on a 4-bit base model, which puts fine-tuning a 30B model within reach of a single 24 GB GPU.

      17 · الذكاء الاصطناعي في المنزل

      تشغيل الذكاء الاصطناعي في المنزل

      يمكن تشغيل النماذج مفتوحة الأوزان على حاسوبك الخاص. يحمّل برنامج يُسمّى بيئة التشغيل — مثل Ollama أو LM Studio أو llama.cpp — أوزان النموذج إلى الذاكرة، ويفضّل أن تكون ذاكرة بطاقة الرسوميات (VRAM)، ثم يولّد النص تمامًا كما يفعل المزوّد السحابي. لا يغادر شيء جهازك، ولا فاتورة لكل رمز، ويعمل دون اتصال.

      رقمان يحددان ما يمكنك تشغيله. الذاكرة تحدد أي النماذج تتسع: تقريبًا عدد المعاملات × البتات لكل وزن ÷ 8، مع مساحة للسياق. يحتاج نموذج من 8 مليارات معامل بدقة 4 بتات إلى نحو 5 غيغابايت، ونموذج من 70 مليارًا إلى أكثر من 40. وعرض نطاق الذاكرة يحدد السرعة، لأن كل رمز جديد يتطلب قراءة كل الأوزان النشطة من جديد — ولهذا تحظى نماذج خليط الخبراء، بمعاملاتها النشطة القليلة، بشعبية كبيرة في المنزل.

      افتح الحاسبة: تكتشف عتادك، وتخبرك بالنماذج التي ستعمل وبأي سرعة، وما التعديلات أو الترقيات المفيدة، والأوامر الدقيقة لتثبيتها.

      Try itWill it run?

      Check your own computer →

      Simplified: weights plus a 16K-token context. The full calculator uses each model’s real size and attention shape.

      اعرف ما الذي يستطيع حاسوبك تشغيله ←

      تعمّق أكثرThe memory and speed formulas · ملاحظات تقنية بالإنجليزية
      memory  ≈ params × bpw / 8 + KV cache + ~0.8 GB
      KV      = 2 × layers × kv_heads × head_dim × 2 bytes × context
      tok/s   ≈ 0.65 × bandwidth / (active_params × bpw / 8)
      
      RTX 4090 (1008 GB/s), 32B dense @ Q4_K_M (19.4 GB):  ≈ 34 tok/s
      Apple M4 Max (546 GB/s), 30B-A3B MoE @ Q4 (1.8 GB/token): ≈ 100+ tok/s
      Dual-channel DDR5 (~80 GB/s), 8B @ Q4 (4.9 GB): ≈ 10 tok/s

      Offloading splits the bytes read per token between fast VRAM and slow system RAM; the slow part dominates. Mixture-of-experts models offload best: keep attention, embeddings and shared experts on the GPU (they're read every token), and put routed experts in RAM (each is read only when chosen). The Run AI at home page applies these formulas to every open model with its real attention shape.

      18 · النماذج متعددة الوسائط

      النماذج متعددة الوسائط

      كانت النماذج اللغوية الأولى تتعامل مع النص فقط. أما النماذج متعددة الوسائط فتقبل أيضًا الصور وملفات PDF والصوت وحتى الفيديو. وهذا مفيد للبرمجة على نحو مفاجئ: الصق لقطة شاشة لتصميم معطّل أو نموذجًا أوليًا واطلب من النموذج بناءه، أو دع الوكيل يلتقط صورًا للتطبيق الذي يعمل عليه ليتحقق من عمله.

      في الداخل تتحول الصورة إلى رموز أيضًا. تُقطَّع إلى رقع صغيرة تصبح كل منها رمزًا تقريبًا — لذا قد تستهلك صورة كبيرة من السياق ما تستهلكه صفحة من النص.

      Try itHow many tokens is an image?

      Uses one common rule of thumb — about one token per 750 pixels. Each provider resizes and counts a little differently.
      تعمّق أكثرVision encoders and image tokens · ملاحظات تقنية بالإنجليزية

      Most multimodal LLMs bolt a vision encoder (a Vision Transformer) onto the language model. The image is resized, cut into patches (often 14×14 or 16×16 pixels), each patch becomes a vector, and a small projector maps those vectors into the language model's embedding space, where they sit in the sequence like word tokens. Many models merge neighbouring patches 2×2 to save tokens:

      image tokens ≈ (width / 28) × (height / 28)    # 14-px patches, 2×2 merge
      1024 × 768  →  ~37 × 27 ≈ 1,000 tokens

      Providers cap the resolution or tile large images, so counts differ by API. Audio is handled the same way — a speech encoder turns audio frames into embeddings — and video as a sequence of sampled frames. “Natively multimodal” models are pre-trained on mixed text and images from the start rather than having vision added later. Generating images is usually a separate system (diffusion, or autoregressive image tokens).

      19 · السرعة

      السرعة: زمن الاستجابة والإنتاجية

      رقمان يحددان مدى سرعة النموذج في الإحساس. زمن الاستجابة، أي الوقت حتى أول رمز، هو كم تنتظر قبل أن يظهر أي شيء. والإنتاجية، بالرموز في الثانية، هي سرعة تدفق النص بعد أن يبدأ. للسؤال السريع يهمّ زمن الاستجابة أكثر؛ ولوكيل يكتب ملفًا كبيرًا تهمّ الإنتاجية.

      تعتمد السرعة على حجم النموذج، ومدى انشغاله، وعتاد وبرمجيات من يقدّمه. كما تقضي نماذج الاستدلال وقتًا في التفكير قبل أن تبدأ الإجابة المرئية.

      Try itFeel the difference
      
          
      0.0 s
      تعمّق أكثرThe roofline: compute vs memory bandwidth · ملاحظات تقنية بالإنجليزية

      A chip is limited either by arithmetic (FLOPs per second) or by how fast it can move data (bytes per second). The ratio of work to data — arithmetic intensity — decides which:

      decode, batch 1 : ~2 FLOPs per weight byte read  → memory-bound
      prefill, 2k tok : ~4,000 FLOPs per weight byte    → compute-bound
      time to first token ≈ queueing + prefill(prompt) + network

      That's why serving providers batch many users together: reading the weights once and applying them to 64 requests makes decode far more efficient, at a small latency cost per user. Speculative decoding exploits the same slack for one user: a cheap draft (a small model, or extra prediction heads) proposes several tokens, the big model verifies them all in one parallel pass and keeps the prefix it agrees with — identical output, often 2–3× faster when drafts are accepted. Specialised chips (Cerebras, Groq, SambaNova) keep weights in on-chip SRAM with enormous bandwidth, which is how they reach thousands of tokens per second.

      20 · المقاييس المعيارية

      قراءة المقاييس المعيارية

      المقياس المعياري (benchmark) اختبار موحّد: المهام نفسها لكل النماذج لتمكن مقارنة الدرجات. بعضها يختبر المعرفة والاستدلال بأسئلة صعبة على طريقة الامتحانات؛ ومقاييس البرمجة تعطي النموذج أخطاء حقيقية من مشاريع حقيقية وتتحقق مما إذا كان إصلاحه يجعل الاختبارات تنجح.

      اقرأها بحذر. ربما رأى النموذج الأسئلة أثناء التدريب فتضخّمت درجته؛ والمختبرات تختار النتائج التي تعرضها؛ والدرجة العالية في نوع من المهام لا تقول الكثير عن نوع آخر. أفضل مقياس هو عملك أنت — جرّب بضعة نماذج على ما تفعله فعلًا.

      Try itTop tracked models

      Real GitHub issues fixed in Python repos.

      Scores from Epoch AI. Compare many more on the leaderboard.
      تعمّق أكثرContamination, pass@k and arena ratings · ملاحظات تقنية بالإنجليزية
      • Contamination: if test questions leaked into training data, scores measure memory. Defences include held-out private test sets, fresh problems dated after a model's cutoff (LiveCodeBench-style) and checking whether a model can complete a question it shouldn't know.
      • pass@k for code is the probability that at least one of k samples passes the tests, estimated without bias from n ≥ k samples with c correct: pass@k = 1 − C(n−c, k) / C(n, k). Agentic benchmarks such as SWE-bench Verified give the model a real repository and issue, then run the project's hidden tests on its patch.
      • Arena ratings come from people comparing two anonymous answers; a Bradley–Terry model turns millions of votes into Elo-like scores. They measure preference — including style and length — not correctness.
      • Watch the error bars: on a 500-question test, ±2 points is often noise. And benchmarks saturate — once top models score above 90%, differences stop meaning much and a harder test replaces it.

      الأمثلة محسوبة من الفهرس العام للنماذج ومزوّديها. التعليق الصوتي بواسطة Magnific.