ENع
→RAG
الدرس 21 · النماذج اللغوية الكبيرة عمليًا

Retrieval-augmented generation

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة9 خطوات5 أسئلةAdvanced
عن هذا الدرس

A language model only knows the text it was trained on, so it cannot answer from your own documents, and when it does not know it may still answer fluently. Retrieval-augmented generation (RAG) adds a lookup: split your documents into chunks, turn each chunk into a vector with an embedding model, find the chunks closest to the question by cosine similarity, and paste them into the prompt. Follow one question through a tiny, runnable Python pipeline.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 21 · تفاعلي
01 / 09

Ask the model directly

A large language model (LLM) learned from public text up to a cutoff date. It never read this shop's rules, so for "Can I return an item after 20 days?" it can only guess. A fluent answer with no source is a hallucination.

question  Can I return an item after 20 days?

the LLM knows only what was in its training text:
  public pages, up to a cutoff date
  not this shop's policy, not last week's changes
answer    "Most shops allow 14 days. Maybe?"   (a guess)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Ask the model directly

    A large language model (LLM) learned from public text up to a cutoff date. It never read this shop's rules, so for "Can I return an item after 20 days?" it can only guess. A fluent answer with no source is a hallucination.

    question  Can I return an item after 20 days?
    
    the LLM knows only what was in its training text:
      public pages, up to a cutoff date
      not this shop's policy, not last week's changes
    answer    "Most shops allow 14 days. Maybe?"   (a guess)
  2. RAG: look it up first

    Retrieval-augmented generation (RAG) adds a lookup step. Retrieve: find the few passages of your own documents that match the question. Augment: paste them into the prompt. Generate: the model answers from them. No retraining needed.

    retrieve   search your documents for the question
    augment    paste the best passages into the prompt
    generate   the LLM answers, reading those passages
    
    two memories (Lewis et al., 2020):
      parametric      what the weights learned in training
      non-parametric  a document index you can edit
  3. Split documents into chunks

    First the documents are split into chunks: short passages that can be found and pasted on their own. Ours are four one-line chunks. The original RAG paper split Wikipedia into 21 million chunks of 100 words.

    chunks:
      0  Refunds: return any item within 30 days for your money ba
      1  Shipping: orders arrive in 3 to 5 days.
      2  Support hours: open 9am to 5pm, Sunday to Thursday.
      3  Gift cards never expire.
    
    too long: wastes prompt space · too short: loses meaning
  4. Embed every chunk

    An embedding model turns each chunk into a vector, a list of numbers, so that texts about the same thing get similar vectors. Ours counts topic words, giving four numbers. Real ones are trained networks giving hundreds, such as 384.

    vecs = [embed(d) for d in docs]
    
    topics      ret  ship hrs  gift 
      refunds   [3, 1, 0, 0]
      shipping  [0, 3, 0, 0]
      hours     [0, 0, 3, 0]
      gifts     [0, 0, 0, 3]
    
    stored once, ahead of time: the vector index
  5. Embed the question

    When a question comes in, the same embedding model turns it into a vector too. "return" and "days" give [1, 1, 0, 0]. It must be the same model, or the two kinds of vector could not be compared.

    q = embed(question)
      "Can I return an item after 20 days?"
      return -> ret · days -> ship
      q = [1, 1, 0, 0]
  6. Find the closest chunks

    Search scores every chunk by cosine similarity with the question: 1 means the vectors point the same way, 0 means no topic in common. Refunds scores 0.89, shipping 0.71, the other two 0. We keep the top k = 2.

    cosine(q, v) = q·v / (|q| |v|)
      refunds   4 / (1.41 × 3.16) = 0.89
      shipping  3 / (1.41 × 3.00) = 0.71
      hours     0 / (1.41 × 3.00) = 0.00
      gifts     0 / (1.41 × 3.00) = 0.00
    
    hits = top 2: refunds, shipping
  7. Paste them into the prompt

    Augment: the two retrieved chunks and the question are pasted into one prompt, with an instruction to answer only from that context. The model itself does not change at all; only what it reads does.

    Answer using only this context.
    Context:
    - Refunds: return any item within 30 days for your money back.
    - Shipping: orders arrive in 3 to 5 days.
    Question: Can I return an item after 20 days?
  8. Generate a grounded answer

    The LLM reads the prompt and answers from it: yes, returns are accepted within 30 days, so 20 days is fine. It can name its source, the refunds chunk. The shipping chunk was retrieved too, but it is not needed.

    llm(prompt)  ->
      "Yes: returns are OK within 30 days."
      source: refunds
    
    grounded: every claim can be checked in the context
  9. Change documents, not weights

    If the refund rule changes, edit that chunk and embed it again; the next answer follows. No retraining. The weak spot is search: if it fetches the wrong chunks, the model answers from the wrong facts.

    policy changes -> edit docs[0] -> vecs[0] = embed(docs[0])
    the LLM weights stay exactly the same
    
    limits:
      wrong chunks retrieved  -> wrong or missing answer
      the model can still ignore or misread the context

اختبار يتاح بعد الخطوة الأخيرة.