LangChain による検索拡張生成(RAG)
Meri Nova
Machine Learning Engineer

チャンクを非ゼロ成分のみの単一ベクトルにエンコード

チャンクを非ゼロ成分のみの単一ベクトルにエンコード

主にゼロ成分で、語一致によりエンコード

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?"))
RAG アプリは、的外れや不正確な応答を生成する可能性があります。
LangChain による検索拡張生成(RAG)