Xây dựng một chuỗi truy xuất LCEL

Retrieval Augmented Generation (RAG) với LangChain

Meri Nova

Machine Learning Engineer

Chuẩn bị dữ liệu cho truy xuất

Các đoạn tài liệu được lưu trữ.

Retrieval Augmented Generation (RAG) với LangChain

Giới thiệu LCEL cho RAG

Một đầu vào từ người dùng.

Retrieval Augmented Generation (RAG) với LangChain

Giới thiệu LCEL cho RAG

Đầu vào đi vào chuỗi qua RunnablePassthrough và được gán cho "question".

Retrieval Augmented Generation (RAG) với LangChain

Giới thiệu LCEL cho RAG

Đầu vào được dùng để truy vấn retriever và được gán cho "context".

Retrieval Augmented Generation (RAG) với LangChain

Giới thiệu LCEL cho RAG

"context" và "question" được đưa vào mẫu prompt.

Retrieval Augmented Generation (RAG) với LangChain

Giới thiệu LCEL cho RAG

Prompt được đưa vào LLM, hay mô hình, để sinh đầu ra.

Retrieval Augmented Generation (RAG) với LangChain

Giới thiệu LCEL cho RAG

Đầu ra của mô hình được chuyển cho một parser.

Retrieval Augmented Generation (RAG) với LangChain

Khởi tạo 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) với LangChain

Tạo mẫu prompt

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) với LangChain

Xây dựng chuỗi truy xuất LCEL

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) với LangChain

Gọi chuỗi truy xuất

result = chain.invoke({"question": "What are the key findings or results presented in the paper?"})
print(result)
- Hiệu năng hàng đầu: các mô hình RAG lập kỷ lục mới trên tác vụ trả lời câu hỏi miền mở...
- Sinh ngôn ngữ tốt hơn: mô hình RAG tạo ra ngôn ngữ cụ thể, đa dạng và chính xác hơn...
- Khai thác tri thức động: bộ nhớ phi tham số cho phép mô hình RAG truy cập và ...
Retrieval Augmented Generation (RAG) với LangChain

Cùng luyện tập nào!

Retrieval Augmented Generation (RAG) với LangChain

Preparing Video For Download...