πŸ€– AI/ML

RAG

Retrieval-Augmented Generation

Retrieval-augmented generation. A technique that retrieves external knowledge to improve the accuracy of LLM responses. Reduces hallucination.

πŸ“– Detailed Explanation

RAG (Retrieval-Augmented Generation) is a technology in which a large language model (LLM) retrieves and uses relevant information from an external knowledge base before generating a response. This makes it possible to overcome the knowledge limitations of LLMs and to provide accurate answers that reflect up-to-date information or domain-specific knowledge.

RAG was first proposed in 2020 by Facebook AI Research, and rapidly emerged as an alternative for addressing the hallucination problem of LLMs. Existing LLMs relied solely on their training data, which made it difficult to reflect up-to-date information and led to the problem of generating content that differed from the facts.

The core principle of RAG is a two-stage "retrieval + generation" pipeline. First, the user's question is embedded and similar documents are retrieved (Retrieval) from a vector database, and the retrieved documents are passed to the LLM as context to generate a response (Generation). In this process, Chunking, Embedding, and Vector Search are used as core technologies.

In practice, RAG is applied as an essential component in internal enterprise document search, customer support chatbots, and legal/medical specialist assistants. It is more cost-effective than fine-tuning, and because up-to-date information can be reflected immediately just by updating the knowledge base, it has become the standard for enterprise AI solutions.

πŸ’» Code Example

This is a basic RAG pipeline example using LangChain.

# pip install langchain langchain-openai chromadb
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains import RetrievalQA

# 1. λ¬Έμ„œ λ‘œλ“œ 및 μ²­ν‚Ή
documents = ["AIλŠ” 인곡지λŠ₯의 μ•½μžμž…λ‹ˆλ‹€...", "λ¨Έμ‹ λŸ¬λ‹μ€ AI의 ν•˜μœ„ λΆ„μ•Όμž…λ‹ˆλ‹€..."]
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
splits = text_splitter.create_documents(documents)

# 2. μž„λ² λ”© 및 벑터 μ €μž₯μ†Œ 생성
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vectorstore = Chroma.from_documents(splits, embeddings)

# 3. RAG 체인 ꡬ성
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    retriever=vectorstore.as_retriever(search_kwargs={"k": 3})
)

# 4. 질의 μ‹€ν–‰
response = qa_chain.invoke({"query": "AI와 λ¨Έμ‹ λŸ¬λ‹μ˜ κ΄€κ³„λŠ”?"})
print(response["result"])

πŸ—£οΈ Say It Like This in Practice

πŸ’¬ In a Meeting
"A chatbot based on internal documents can be implemented with RAG. Updates are easier than with fine-tuning, and it also reduces costs."
πŸ’¬ In an Interview
"With RAG, the quality of the Retrieval stage determines overall performance. Chunk size, Embedding model selection, and Reranking strategy are the key tuning points."
πŸ’¬ In a Technical Discussion
"To overcome the limitations of Naive RAG, you need to apply Advanced RAG techniques such as Query Expansion, HyDE, and Self-RAG."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

⚠️ Common Mistakes & Cautions

❌
Not considering chunk size: If it is too small, context is cut off; if it is too large, irrelevant information is included. Generally, 500-1000 tokens is appropriate.
❌
Using search results as they are: Top-K search results are not always optimal. Apply a reranker (Cohere, BGE) to re-evaluate relevance.
βœ…
Using Hybrid Search: Combining keyword search (BM25) with semantic search improves retrieval accuracy by 20-30%.

πŸ”— Related Terms

πŸ“š Learn More