การสร้าง LCEL retrieval chain

Retrieval Augmented Generation (RAG) ด้วย LangChain

Meri Nova

Machine Learning Engineer

การเตรียมข้อมูลสำหรับการค้นคืน

ชิ้นส่วนเอกสารถูกจัดเก็บไว้

Retrieval Augmented Generation (RAG) ด้วย LangChain

แนะนำ LCEL สำหรับ RAG

อินพุตของผู้ใช้

Retrieval Augmented Generation (RAG) ด้วย LangChain

แนะนำ LCEL สำหรับ RAG

อินพุตของผู้ใช้เข้าสู่ chain ผ่าน RunnablePassthrough และถูกกำหนดให้กับ "question"

Retrieval Augmented Generation (RAG) ด้วย LangChain

แนะนำ LCEL สำหรับ RAG

อินพุตถูกใช้ค้นหาผ่าน retriever และถูกกำหนดให้กับ "context"

Retrieval Augmented Generation (RAG) ด้วย LangChain

แนะนำ LCEL สำหรับ RAG

"context" และ "question" ถูกนำเข้าสู่ prompt template

Retrieval Augmented Generation (RAG) ด้วย LangChain

แนะนำ LCEL สำหรับ RAG

prompt ถูกส่งเข้า LLM หรือโมเดล เพื่อสร้างผลลัพธ์

Retrieval Augmented Generation (RAG) ด้วย LangChain

แนะนำ LCEL สำหรับ RAG

ผลลัพธ์จากโมเดลถูกส่งต่อไปยัง parser

Retrieval Augmented Generation (RAG) ด้วย LangChain

การสร้าง retriever

vector_store = Chroma.from_documents(
    documents=chunks, 
    embedding=embedding_model
)


retriever = vector_store.as_retriever( search_type="similarity", search_kwargs={"k": 2} )
Retrieval Augmented Generation (RAG) ด้วย LangChain

การสร้าง prompt template

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template(""" Use the following pieces of context to answer the question at the end. If you don't know the answer, say that you don't know. Context: {context} Question: {question} """)
llm = ChatOpenAI(model="gpt-4o-mini", api_key="...", temperature=0)
Retrieval Augmented Generation (RAG) ด้วย LangChain

การสร้าง LCEL retrieval chain

from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
Retrieval Augmented Generation (RAG) ด้วย LangChain

การเรียกใช้ retrieval chain

result = chain.invoke({"question": "What are the key findings or results presented in the paper?"})
print(result)
- Top Performance: RAG models set new records on open-domain question answering tasks...
- Better Generation: RAG models produce more specific, diverse, and factual language...
- Dynamic Knowledge Use: The non-parametric memory allows RAG models to access and ...
Retrieval Augmented Generation (RAG) ด้วย LangChain

มาฝึกกันเถอะ!

Retrieval Augmented Generation (RAG) ด้วย LangChain

Preparing Video For Download...