跳到正文
DEV Community· Rahul Vijayvergiya·· 56 分鐘前AI 評分35

將文件切分成區塊時,LLM 會否失去整體脈絡?

When we chop a document into chunks, doesn't the LLM lose the big picture?

AI 導讀

RAG 把大型文件切成較小區塊,有助節省模型 token、降低成本並檢索相關資訊,但只取回單一區塊可能令 LLM 缺少上下文,導致答案不完整或錯誤。可透過重疊區塊、取回最相關的 3 或 5 個區塊,以及儲存文件名稱、頁碼、章節標題和區塊編號等中繼資料,保留相關脈絡。

正文

If we split a large document into smaller chunks, how does the LLM understand the complete context? Doesn't it lose the connection between different parts of the document?

Let's understand this with a simple example.

1. Why do we split documents?

Imagine we have a company policy document containing 100 pages.

A user asks:

"How many days of leave can I carry forward?"

Instead of sending all 100 pages to the LLM, RAG splits the document into smaller pieces called chunks. This saves tokens, reduces costs and helps retrieve only relevant information.

2. But here's the problem!

Suppose our original document contains:

Chunk 1:

"Employees are entitled to 20 days of paid leave every year. Unused leave can be carried forward to the next year."

Chunk 2:

"However, the maximum accumulated leave balance cannot exceed 30 days."

Now, imagine vector search retrieves only Chunk 2 for the user's question:

"What is the maximum leave balance I can accumulate?"

The LLM receives the 30-day limit but might not understand which type of leave this refers to.

We have found the right information, but we may have lost some context!

3. How can we solve this?

There are a few ways to handle this problem:

1. Overlapping chunks

Keep some text from the previous chunk when creating the next one.

For example, repeat the last sentence of Chunk 1 at the beginning of Chunk 2. This helps preserve the connection between them.

2. Retrieve multiple chunks

Instead of retrieving only one matching chunk, retrieve the top 3 or 5 relevant chunks. The LLM gets more context and can connect information from different parts of the document.

3. Store metadata

Store details like document name, page number, section title and chunk number along with each chunk. This helps us identify related chunks and retrieve additional context when needed.

Final thoughts

Chunking makes it easier and cheaper to search large documents, but splitting documents without considering context can lead to incomplete or incorrect answers.

The goal isn't just to find the right chunk; it's to provide enough related information so the LLM understands the complete picture.

來源:DEV Community · dev.to