LangChain을 활용한 RAG(검색 증강 생성)
Meri Nova
Machine Learning Engineer

청크를 0이 아닌 성분을 가진 단일 벡터로 인코딩

청크를 0이 아닌 성분을 가진 단일 벡터로 인코딩

대부분 0인 성분으로 단어 매칭을 통해 인코딩

TF-IDF: 문서의 고유한 단어를 사용하여 문서를 인코딩

BM25: 고빈도 단어가 인코딩을 포화시키는 문제를 완화
from langchain_community.retrievers import BM25Retrieverchunks = [ "Python was created by Guido van Rossum and released in 1991.", "Python is a popular language for machine learning (ML).", "The PyTorch library is a popular Python library for AI and ML." ]bm25_retriever = BM25Retriever.from_texts(chunks, k=3)
results = bm25_retriever.invoke("When was Python created?")
print("Most Relevant Document:")
print(results[0].page_content)
Most Relevant Document:
Python was created by Guido van Rossum and released in 1991.
retriever = BM25Retriever.from_documents( documents=chunks, k=5 )chain = ({"context": retriever, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() )
print(chain.invoke("How can LLM hallucination impact a RAG application?"))
The RAG application may generate responses that are off-topic or inaccurate.
LangChain을 활용한 RAG(검색 증강 생성)