AI engineering glossary · RAG vector search
What is RAG (retrieval-augmented generation)?
Retrieval-augmented generation (also called RAG): A pattern in which a system first looks up relevant passages from an external knowledge source and then passes them to a language model along with the question, so the answer is grounded in that material. It lets a model use private or fresh information without retraining and makes answers easier to check against sources.
Retrieval-augmented generation: an example
A Bengaluru bank chatbot searches its 3,000 policy PDFs for the top 5 passages about home-loan prepayment and asks the model to answer using only those.
Retrieval-augmented generation in Python
A short example you can run (Python 3.11). The output below is what it really prints.
docs = {
"refund": "Refunds are allowed within 7 days if under 20% of the course is complete.",
"cert": "Every module earns a certificate with a public verification ID.",
}
def retrieve(question):
words = set(question.lower().split())
return max(docs.values(), key=lambda d: len(words & set(d.lower().split())))
def rag_prompt(question):
context = retrieve(question)
return f"Answer only from the context.\nContext: {context}\nQuestion: {question}"
print(rag_prompt("Within how many days are refunds allowed?"))Output
Answer only from the context. Context: Refunds are allowed within 7 days if under 20% of the course is complete. Question: Within how many days are refunds allowed?
Related terms
More RAG vector search terms
- Retriever
- Scalar quantisation
- Score fusion
- Self-RAG
- Semantic cache
- Semantic chunking
- Sentence window retrieval
- Similarity threshold
Where you learn it
Retrieval-augmented generation is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.