Tách văn bản, embeddings và lưu trữ vector

Retrieval Augmented Generation (RAG) với LangChain

Meri Nova

Machine Learning Engineer

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

Đang tải tài liệu.

Retrieval Augmented Generation (RAG) với LangChain

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

Đang tách tài liệu.

Retrieval Augmented Generation (RAG) với LangChain

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

Nhúng các mảnh tài liệu.

Retrieval Augmented Generation (RAG) với LangChain

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

Lưu trữ các mảnh tài liệu.

Retrieval Augmented Generation (RAG) với LangChain

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

Bước tách được làm nổi bật trong quy trình phát triển RAG.

Retrieval Augmented Generation (RAG) với LangChain

chunk_size

Mũi tên cho thấy kích thước mảnh lý tưởng nằm ở giữa; mảnh lớn có thể truy xuất chậm và khó diễn giải, mảnh nhỏ có thể thiếu ngữ cảnh.

chunk_overlap

  • Bao gồm thông tin vượt qua ranh giới

Hai mảnh có vùng được tô sáng chồng lấp cả hai.

Retrieval Augmented Generation (RAG) với LangChain

CharacterTextSplitter

from langchain_text_splitters import CharacterTextSplitter

text = """Machine learning is a fascinating field.\n\nIt involves algorithms and models that can learn from data. These models can then make predictions or decisions without being explicitly programmed to perform the task.\nThis capability is increasingly valuable in various industries, from finance to healthcare.\n\nThere are many types of machine learning, including supervised, unsupervised, and reinforcement learning.\nEach type has its own strengths and applications."""
text_splitter = CharacterTextSplitter( separator="\n\n", chunk_size=100, chunk_overlap=10 )
Retrieval Augmented Generation (RAG) với LangChain

CharacterTextSplitter

chunks = text_splitter.split_text(text)

print(chunks) print([len(chunk) for chunk in chunks])
['Machine learning is a fascinating field.',
 'It involves algorithms and models that can learn from data. These models can...',
 'There are many types of machine learning, including supervised, unsupervised...']

[40, 260, 155]
  • Các mảnh có thể thiếu ngữ cảnh
  • Mảnh có thể lớn hơn chunk_size
Retrieval Augmented Generation (RAG) với LangChain

RecursiveCharacterTextSplitter

from langchain_text_splitters import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter(

separators=["\n\n", "\n", " ", ""],
chunk_size=100, chunk_overlap=10
)
Retrieval Augmented Generation (RAG) với LangChain

RecursiveCharacterTextSplitter

chunks = splitter.split_text(text)

print(chunks)
print([len(chunk) for chunk in chunks])
['Machine learning is a fascinating field.',
 'It involves algorithms and models that can learn from data. These models ...',
 'or decisions without being explicitly programmed to perform the task.',
 'This capability is increasingly valuable in various industries, from ...',
 'There are many types of machine learning, including supervised, ...',
 'learning.',
 'Each type has its own strengths and applications.']
[40, 98, 69, 91, 95, 9, 49]
Retrieval Augmented Generation (RAG) với LangChain

Tách tài liệu

from langchain_community.document_loaders import PyPDFLoader

loader = PyPDFLoader("research_paper.pdf")
documents = loader.load()

splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)

chunks = splitter.split_documents(documents)
Retrieval Augmented Generation (RAG) với LangChain

Tách tài liệu

print(chunks)

print([len(chunk.page_content) for chunk in chunks])
[Document(metadata={'source': 'Rag Paper.pdf', 'page': 0}, page_content='...'),
 Document(metadata={'source': 'Rag Paper.pdf', 'page': 0}, page_content='...'),
 Document(metadata={'source': 'Rag Paper.pdf', 'page': 0}, page_content='...')]

[928, 946, 921,...]
Retrieval Augmented Generation (RAG) với LangChain

Nhúng và lưu trữ

Các bước nhúng và lưu trữ được làm nổi bật.

Retrieval Augmented Generation (RAG) với LangChain

Embeddings là gì?

Một câu được đưa vào mô hình embedding.

Retrieval Augmented Generation (RAG) với LangChain

Embeddings là gì?

Mô hình embedding mã hóa văn bản thành vector các giá trị số.

Retrieval Augmented Generation (RAG) với LangChain

Embeddings là gì?

what_are_embeddings3.jpg

Retrieval Augmented Generation (RAG) với LangChain

Embeddings là gì?

what_are_embeddings4.jpg

Retrieval Augmented Generation (RAG) với LangChain

Nhúng và lưu trữ các mảnh

  • Nhúng và lưu với: OpenAIChromaDB
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma

embedding_model = OpenAIEmbeddings(
    api_key=openai_api_key,
    model="text-embedding-3-small"
)


vector_store = Chroma.from_documents( documents=chunks, embedding=embedding_model )
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...