ما هو RAG وكيف يربط الذكاء الاصطناعي بمصادر المعرفة؟ | What Is RAG and How Does It Connect AI to Knowledge?
تعلّم ببساطة، خطوة بخطوة
Learn simply, step by step
معلومة واضحة، ثم خطوة جديدة.
Clear knowledge, one step at a time.
ما هو RAG وكيف يربط الذكاء الاصطناعي بمصادر المعرفة؟ What Is RAG and How Does It Connect AI to Knowledge?
ما هو RAG في الذكاء الاصطناعي؟
يُعد RAG في الذكاء الاصطناعي من التقنيات المهمة عند بناء تطبيقات AI التي تحتاج إلى استخدام معلومات من مستندات أو قواعد معرفة أو مصادر خارجية.
RAG هو اختصار لـ Retrieval-Augmented Generation، ويمكن تبسيط فكرته بأنها تسمح للنظام بالبحث عن معلومات مرتبطة بسؤال المستخدم أولًا، ثم إرسال المعلومات المسترجعة إلى نموذج اللغة لمساعدته على إنشاء إجابة أكثر ارتباطًا بالمصدر.

لماذا نحتاج إلى RAG؟
نموذج اللغة لا يحتوي بالضرورة على كل المعلومات التي يحتاجها تطبيقك، كما أن بعض المعلومات قد تكون خاصة بمؤسسة أو مشروع، أو موجودة في مستندات لم تكن جزءًا من بيانات تدريب النموذج.
على سبيل المثال، إذا أنشأت مساعد AI لموقع شركة وتريد منه الإجابة اعتمادًا على سياسات الشركة ومستنداتها، فإن الاعتماد على معرفة النموذج وحدها لن يكون كافيًا.
هنا يمكن استخدام RAG في الذكاء الاصطناعي لاسترجاع المعلومات المناسبة من مصادر المعرفة وإضافتها إلى السياق الذي يستخدمه النموذج عند إنشاء الإجابة.
كيف يعمل RAG؟
يمكن تبسيط العملية بالشكل التالي:
User Question → Knowledge Base → Retrieval → Relevant Context → LLM → Answer
بدل إرسال سؤال المستخدم مباشرة إلى نموذج AI فقط، يبحث النظام أولًا في مصدر المعرفة عن المعلومات الأكثر ارتباطًا بالسؤال.
بعد ذلك يتم توفير المعلومات المسترجعة للنموذج مع السؤال، ويستخدمها النموذج كسياق إضافي عند توليد الإجابة.
ما هي Knowledge Base؟
Knowledge Base هي مجموعة المعلومات التي يستطيع نظام RAG البحث فيها. ويمكن أن تتكون من أنواع مختلفة من المحتوى حسب التطبيق.
- مستندات PDF.
- صفحات ومقالات.
- أدلة الاستخدام.
- معلومات المنتجات.
- وثائق داخلية.
- مصادر بيانات أخرى مناسبة للنظام.
الهدف هو توفير مصدر معرفة يمكن للنظام الرجوع إليه بدل الاعتماد فقط على المعرفة العامة للنموذج.
ما المقصود بـChunks؟
عند وجود مستندات كبيرة، لا يكون من العملي دائمًا التعامل مع المستند بالكامل كوحدة واحدة. لذلك يتم غالبًا تقسيم المحتوى إلى أجزاء أصغر تسمى Chunks.
على سبيل المثال، يمكن تقسيم دليل طويل إلى أجزاء تحتوي كل منها على فقرة أو مجموعة فقرات مترابطة.
عندما يسأل المستخدم سؤالًا، يحاول نظام الاسترجاع العثور على الأجزاء الأكثر ارتباطًا بالسؤال بدل إرسال كل المستندات إلى النموذج.
ما هي Embeddings؟
Embeddings هي تمثيلات رقمية تساعد الأنظمة على مقارنة المعنى أو التشابه بين أجزاء من البيانات، مثل النصوص.
بشكل مبسط، يتم تحويل النصوص إلى تمثيلات رقمية، ثم يمكن للنظام البحث عن المحتوى الأقرب في المعنى إلى سؤال المستخدم.
لا يحتاج المبتدئ إلى فهم الرياضيات الموجودة خلف Embeddings في هذه المرحلة؛ المهم هو فهم دورها في مساعدة النظام على العثور على معلومات مرتبطة بالسؤال.
ما هي Vector Database؟
يمكن استخدام Vector Database لتخزين والبحث في التمثيلات الرقمية المرتبطة بالمحتوى، مما يساعد نظام RAG على استرجاع أجزاء المعرفة ذات الصلة.
بشكل مبسط:
Documents → Chunks → Embeddings → Vector Search → Relevant Chunks
ثم يتم إرسال المحتوى المسترجع إلى نموذج اللغة مع سؤال المستخدم.
مثال عملي على RAG
تخيل أن لدينا مساعد AI لموقع يحتوي على عشرات الأدلة التعليمية.
يسأل المستخدم:
كيف أحمي API Key الخاص بي؟
بدل أن يعتمد النظام فقط على المعرفة العامة للنموذج، يمكنه البحث في قاعدة المعرفة والعثور على الجزء الذي يشرح حماية API Keys.
بعد ذلك يرسل السؤال والمعلومات المسترجعة إلى نموذج AI، ثم يقوم النموذج بإنشاء إجابة تعتمد على هذا السياق.
هل RAG يعني تدريب نموذج AI جديد؟
لا. من المهم التمييز بين RAG وتدريب أو إعادة تدريب نموذج.
في RAG لا نحتاج عادةً إلى إعادة تدريب النموذج على كل مستند جديد. بدلًا من ذلك، يتم استرجاع المعلومات المناسبة وقت تنفيذ الطلب وإضافتها إلى السياق الذي يحصل عليه النموذج.
وهذه إحدى المزايا المهمة لـRAG في الذكاء الاصطناعي، خصوصًا عندما تتغير قاعدة المعرفة أو يتم إضافة مستندات جديدة باستمرار.
أين يمكن استخدام RAG؟
يمكن استخدام RAG في تطبيقات عديدة، مثل:
- Chatbots التي تجيب من مستندات محددة.
- أنظمة دعم العملاء.
- البحث داخل قواعد المعرفة.
- المساعدات الداخلية للشركات.
- تحليل واسترجاع المعلومات من المستندات.
- تطبيقات AI التي تحتاج إلى بيانات خاصة أو متخصصة.
هل يمنع RAG الأخطاء تمامًا؟
لا. يمكن لـRAG أن يساعد على توفير معلومات أكثر صلة بالسؤال، لكنه لا يضمن أن كل إجابة ستكون صحيحة.
جودة النتيجة تعتمد أيضًا على جودة مصادر المعرفة، وطريقة تقسيم المحتوى، ودقة عملية الاسترجاع، والتعليمات المرسلة إلى النموذج.
ولهذا يجب التحقق من المعلومات المهمة وتصميم النظام بحيث يتعامل بشكل مناسب مع الحالات التي لا يجد فيها مصدرًا كافيًا للإجابة.
RAG وStructured Outputs
يمكن أيضًا دمج RAG مع التقنيات التي تعلمناها في الدروس السابقة. على سبيل المثال، يمكن للنظام استرجاع المعلومات باستخدام RAG ثم مطالبة النموذج بإرجاع النتيجة في JSON منظم باستخدام Structured Outputs.
وهكذا تبدأ مكونات تطبيق AI المتقدم في العمل معًا:
Retrieval → LLM → Structured Output → Application
مصادر إضافية
يمكنك زيارة
AI Prompt Generator من TheCrypTechAI
للتدرب على بناء تعليمات واضحة لنماذج الذكاء الاصطناعي.
وللتعمق أكثر في مفهوم RAG، يمكنك الاطلاع على
دليل Retrieval الرسمي من OpenAI.
الخلاصة
يسمح RAG في الذكاء الاصطناعي للتطبيقات باسترجاع معلومات مرتبطة بسؤال المستخدم من مصادر معرفة خارجية، ثم توفير هذه المعلومات لنموذج اللغة كسياق يساعده على إنشاء الإجابة.
وفهم Knowledge Bases وChunks وEmbeddings وVector Search يمثل أساسًا مهمًا قبل الانتقال إلى المرحلة التالية من المسار، وهي جعل نموذج AI قادرًا على استخدام الأدوات وتنفيذ مهام أكثر تعقيدًا.
What Is RAG in AI?
RAG in AI is an important technique for building AI applications that need to use information from documents, knowledge bases, or other external sources.
RAG stands for Retrieval-Augmented Generation. In simple terms, it allows a system to retrieve information related to a user's question first and then provide that information to a language model as additional context for generating an answer.

Why Do We Need RAG?
A language model does not necessarily contain every piece of information an application needs. Some information may also be private to a company or project, or stored in documents that were not part of the model's training data.
For example, imagine building an AI assistant for a company that should answer questions using the company's policies and internal documents. Relying only on the model's general knowledge may not be enough.
RAG in AI can retrieve relevant information from those knowledge sources and add it to the context used by the model when generating an answer.
How Does RAG Work?
The process can be simplified as:
User Question → Knowledge Base → Retrieval → Relevant Context → LLM → Answer
Instead of sending the user's question directly to an AI model alone, the system first searches a knowledge source for information that is relevant to the question.
The retrieved information is then provided to the model together with the question, allowing the model to use it as additional context when generating the response.
What Is a Knowledge Base?
A Knowledge Base is a collection of information that a RAG system can search. It may contain different types of content depending on the application.
- PDF documents.
- Web pages and articles.
- User guides.
- Product information.
- Internal documentation.
- Other relevant data sources.
The goal is to provide a source of knowledge that the system can consult instead of relying only on the model's general knowledge.
What Are Chunks?
When documents are large, processing an entire document as one unit may not always be practical. Content is therefore often divided into smaller sections called Chunks.
For example, a long guide could be divided into chunks containing individual paragraphs or groups of related paragraphs.
When a user asks a question, the retrieval system can search for the chunks that are most relevant instead of sending every document to the model.
What Are Embeddings?
Embeddings are numerical representations that help systems compare meaning or similarity between pieces of data such as text.
In simple terms, text can be converted into numerical representations, allowing the system to search for content that is semantically related to the user's question.
At this stage, you do not need to understand the mathematics behind Embeddings. The important point is understanding their role in helping a system find information related to a query.
What Is a Vector Database?
A Vector Database can be used to store and search the numerical representations associated with content, helping a RAG system retrieve relevant pieces of knowledge.
In simplified form:
Documents → Chunks → Embeddings → Vector Search → Relevant Chunks
The retrieved content can then be provided to the language model together with the user's question.
A Practical RAG Example
Imagine an AI assistant for a website containing dozens of educational guides.
A user asks:
How should I protect my API Key?
Instead of relying only on the model's general knowledge, the system can search the knowledge base and retrieve the section explaining API Key security.
The question and retrieved information are then provided to the AI model, which generates an answer using that additional context.
Does RAG Mean Training a New AI Model?
No. It is important to distinguish RAG from training or retraining an AI model.
With RAG, you normally do not need to retrain the model whenever a new document is added. Instead, relevant information is retrieved when a request is made and added to the context provided to the model.
This is one of the important advantages of RAG in AI, especially when a knowledge base changes or new documents are added regularly.
Where Can RAG Be Used?
RAG can be useful in many types of AI applications, including:
- Chatbots that answer questions from specific documents.
- Customer support systems.
- Knowledge base search.
- Internal company assistants.
- Document information retrieval.
- AI applications that need private or specialized data.
Does RAG Completely Prevent AI Errors?
No. RAG can help provide information that is more relevant to a user's question, but it does not guarantee that every generated answer will be correct.
The quality of the result also depends on the quality of the knowledge sources, how the content is divided, the accuracy of the retrieval process, and the instructions provided to the model.
Important information should still be verified, and applications should be designed to handle situations where there is not enough reliable information to answer a question.
RAG and Structured Outputs
RAG can also be combined with techniques from the previous lessons. For example, a system could retrieve information using RAG and then ask the model to return the result as structured JSON using Structured Outputs.
This allows several components of an advanced AI application to work together:
Retrieval → LLM → Structured Output → Application
Additional Resources
You can practice writing clear AI instructions with the
TheCrypTechAI AI Prompt Generator.
To explore retrieval concepts in more detail, see the
official OpenAI Retrieval guide.
Summary
RAG in AI allows applications to retrieve information related to a user's question from external knowledge sources and provide that information to a language model as additional context for generating an answer.
Understanding Knowledge Bases, Chunks, Embeddings, and Vector Search provides an important foundation for the next stage of this learning path: allowing AI models to use tools and perform more complex tasks.
اختبر فهمك
Check Your Understanding
سؤالان سريعان لتثبيت أهم ما تعلمته.
Two quick questions to reinforce the key ideas.