الاسترجاع المعزّز (RAG) من الصفر / RAG from scratch

الترتيب / Order
10 من 14 / 10 of 14
آخر تحديث / Updated
8 أكتوبر 2026 / 8 October 2026

المستوى: متوسط · الدرس 10 من 14

أهداف الدرس

  1. أن تبني مساعداً يجيب من مستنداتك.
  2. أن تفهم التقطيع والتضمين والاسترجاع.

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

مساران: فهرسة مرة واحدة، وإجابة مع كل سؤال
مساران: فهرسة مرة واحدة، وإجابة مع كل سؤال
Python
from sentence_transformers import SentenceTransformer
import numpy as np

docs = [  # example documents
    "يبدأ التسجيل للفصل الثاني يوم الأحد الأول من فبراير.",
    "الحد الأقصى للغياب 15٪ من ساعات المساق.",
    "تُسلَّم مشاريع التخرج قبل نهاية الأسبوع الرابع عشر.",
]
model = SentenceTransformer("intfloat/multilingual-e5-small")
doc_vecs = model.encode(["passage: " + d for d in docs], normalize_embeddings=True)

question = "متى يبدأ التسجيل؟"
q_vec = model.encode("query: " + question, normalize_embeddings=True)

best = int(np.argmax(doc_vecs @ q_vec))
prompt = f"أجب من المصدر فقط واذكره.\nالمصدر: {docs[best]}\nالسؤال: {question}"
print(prompt)
قرارنصيحة
حجم القطعةفقرة أو فقرتان (نحو 200–500 كلمة)
التداخلجملة أو جملتان بين القطع
البيانات الوصفيةالعنوان والتاريخ والمصدر مع كل قطعة
عدد النتائج3–5 قطع في السياق

علّم المساعد أن يقول «لم أجد هذا في المصادر» بدل أن يخترع إجابة.

تمرين

لماذا نضع «passage:» و«query:» قبل النص في هذا النموذج؟

الإجابة

لأنه تدرّب هكذا: يميّز بين النص المخزّن والسؤال، فيتحسّن الاسترجاع.

Level: Intermediate · Lesson 10 of 14

Lesson goals

  1. Build an assistant that answers from your documents.
  2. Understand chunking, embedding and retrieval.

RAG gives the model an «open book»: we chunk the documents, turn each chunk into an embedding and store it. For each question we retrieve the closest chunks and put them in the context, so the model answers from them and cites them.

Two lanes: index once, answer every question
Two lanes: index once, answer every question
Python
from sentence_transformers import SentenceTransformer
import numpy as np

docs = [  # example documents
    "يبدأ التسجيل للفصل الثاني يوم الأحد الأول من فبراير.",
    "الحد الأقصى للغياب 15٪ من ساعات المساق.",
    "تُسلَّم مشاريع التخرج قبل نهاية الأسبوع الرابع عشر.",
]
model = SentenceTransformer("intfloat/multilingual-e5-small")
doc_vecs = model.encode(["passage: " + d for d in docs], normalize_embeddings=True)

question = "متى يبدأ التسجيل؟"
q_vec = model.encode("query: " + question, normalize_embeddings=True)

best = int(np.argmax(doc_vecs @ q_vec))
prompt = f"أجب من المصدر فقط واذكره.\nالمصدر: {docs[best]}\nالسؤال: {question}"
print(prompt)
DecisionAdvice
Chunk sizeOne or two paragraphs (about 200–500 words)
OverlapA sentence or two between chunks
MetadataTitle, date and source with each chunk
How many results3–5 chunks in the context

Teach the assistant to say «I didn't find this in the sources» instead of inventing an answer.

Exercise

Why prefix «passage:» and «query:» for this model?

Answer

It was trained that way: it tells stored text from questions, which improves retrieval.

التعليقات / Comments

لا تعليقات بعد. كن أول من يسأل. / No comments yet. Be the first to ask.

تعليق الطالب يظهر فوراً، وتعليق الزائر بعد موافقة المدرّس. / A student's comment shows at once; a visitor's once the teacher approves it.

في «الذكاء الاصطناعي: من الصفر إلى الاحتراف» / In “AI: from zero to hero”

  1. 1ما الذكاء الاصطناعي التوليدي؟ / What is generative AI?فيها صور / has picturesالتعليقات مفتوحة / comments open
  2. 2الرموز والتضمينات / Tokens and embeddingsفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  3. 3المحوّل والانتباه / Transformers and attentionفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  4. 4التدريب المسبق / Pre-trainingفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  5. 5الضبط بالتعليمات / Instruction tuning (SFT)فيها كود / has codeالتعليقات مفتوحة / comments open
  6. 6التعلّم من التفضيلات / Learning from preferencesفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  7. 7نماذج الاستدلال / Reasoning modelsفيها كود / has codeالتعليقات مفتوحة / comments open
  8. 8الضبط الدقيق الفعّال / Efficient fine-tuningفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  9. 9هندسة الأوامر والسياق / Prompt and context engineeringفيها كود / has codeالتعليقات مفتوحة / comments open
  10. 10الاسترجاع المعزّز (RAG) من الصفر / RAG from scratchفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  11. 11RAG المتقدّم / Advanced RAGفيها كود / has codeالتعليقات مفتوحة / comments open
  12. 12الوكلاء والأدوات / Agents and toolsفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  13. 13التقييم والأمان / Evaluation and safetyفيها كود / has codeفيها صور / has picturesالتعليقات مفتوحة / comments open
  14. 14مشروع ختامي: مساعد عربي / Capstone: an Arabic assistantفيها كود / has codeفيها صور / has picturesفيها نموذج / has a formالتعليقات مفتوحة / comments open

↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق↑ ↓ to move · Enter to open · Esc to close