RAG
Retrieval-Augmented Generation
Retrieval-augmented generation. A technique that retrieves external knowledge to improve the accuracy of LLM responses. Reduces hallucination.
Retrieval-Augmented Generation
Retrieval-augmented generation. A technique that retrieves external knowledge to improve the accuracy of LLM responses. Reduces hallucination.
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.
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"])
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free