文字分割、嵌入與向量儲存

使用 LangChain 的 Retrieval Augmented Generation(RAG)

Meri Nova

Machine Learning Engineer

檢索前的資料準備

正在載入文件。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

檢索前的資料準備

正在分割文件。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

檢索前的資料準備

已為文件區塊建立嵌入。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

檢索前的資料準備

正在儲存文件區塊。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

檢索前的資料準備

在 RAG 開發流程中凸顯分割步驟。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

chunk_size

箭頭指出理想區塊大小介於中間;過大會使檢索變慢且難以理解,過小則脈絡不足。

chunk_overlap

  • 納入超出邊界的資訊

兩個區塊有一段重疊區域被標示。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

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 )
使用 LangChain 的 Retrieval Augmented Generation(RAG)

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]
  • 區塊可能缺乏脈絡
  • 區塊可能大於 chunk_size
使用 LangChain 的 Retrieval Augmented Generation(RAG)

RecursiveCharacterTextSplitter

from langchain_text_splitters import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter(

separators=["\n\n", "\n", " ", ""],
chunk_size=100, chunk_overlap=10
)
使用 LangChain 的 Retrieval Augmented Generation(RAG)

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]
使用 LangChain 的 Retrieval Augmented Generation(RAG)

分割文件

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)
使用 LangChain 的 Retrieval Augmented Generation(RAG)

分割文件

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,...]
使用 LangChain 的 Retrieval Augmented Generation(RAG)

嵌入與儲存

已凸顯嵌入與儲存步驟。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

什麼是嵌入?

一句話被傳入嵌入模型。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

什麼是嵌入?

嵌入模型將文字嵌入為數值向量。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

什麼是嵌入?

what_are_embeddings3.jpg

使用 LangChain 的 Retrieval Augmented Generation(RAG)

什麼是嵌入?

what_are_embeddings4.jpg

使用 LangChain 的 Retrieval Augmented Generation(RAG)

將區塊嵌入並儲存

  • 使用 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 )
使用 LangChain 的 Retrieval Augmented Generation(RAG)

一起來練習吧!

使用 LangChain 的 Retrieval Augmented Generation(RAG)

Preparing Video For Download...