Retrieval Augmented Generation (RAG) ด้วย LangChain
Meri Nova
Machine Learning Engineer

เข้ารหัส chunk เป็นเวกเตอร์เดียวที่มีองค์ประกอบไม่เป็นศูนย์

เข้ารหัส chunk เป็นเวกเตอร์เดียวที่มีองค์ประกอบไม่เป็นศูนย์

เข้ารหัสโดยใช้การจับคู่คำ โดยส่วนใหญ่องค์ประกอบเป็นศูนย์

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.
Retrieval Augmented Generation (RAG) ด้วย LangChain