Repository navigation
fix(rag): scope chunk IDs to their source document #1552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Elon-Jiang
wants to merge
3
commits into
VoltAgent:main
Choose a base branch
from
Elon-Jiang:fix/document-scoped-chunk-ids
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@voltagent/rag": patch | ||
| --- | ||
|
|
||
| Scope StructuredDocument chunk IDs to their source document so chunks from different documents remain distinct in the returned collection and link graph. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { StructuredDocument } from "../index"; | ||
|
|
||
| const documents = [ | ||
| { docId: "policy", text: "Refunds are available within thirty days. Contact support for help." }, | ||
| { docId: "guide", text: "Keep your receipt. Provide the order number when requesting a refund." }, | ||
| ]; | ||
|
|
||
| describe("StructuredDocument chunk identity", () => { | ||
| it.each(["token", "recursive", "markdown"] as const)( | ||
| "keeps chunks from different documents distinct with %s", | ||
| (strategy) => { | ||
| const doc = new StructuredDocument(documents); | ||
| const { chunks } = doc.chunk({ strategy, maxTokens: 5 }); | ||
| const byId = new Map(chunks.map((chunk) => [chunk.id, chunk])); | ||
|
|
||
| expect(chunks.length).toBeGreaterThan(documents.length); | ||
| expect(byId.size).toBe(chunks.length); | ||
| for (const { docId } of documents) { | ||
| const ownChunks = chunks.filter((chunk) => chunk.metadata?.docId === docId); | ||
| expect(ownChunks.length).toBeGreaterThan(1); | ||
| expect(doc.getLinkGraph()[docId]).toEqual(ownChunks.map((chunk) => chunk.id)); | ||
| for (const chunk of ownChunks) { | ||
| expect(byId.get(chunk.id)?.metadata?.docId).toBe(docId); | ||
| expect(byId.get(chunk.id)?.content).toBe(chunk.content); | ||
| } | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| it("uses generated document identities when no docId is supplied", () => { | ||
| const doc = new StructuredDocument(documents.map(({ text }) => ({ text }))); | ||
| const { chunks } = doc.chunk({ strategy: "token", maxTokens: 100 }); | ||
|
|
||
| expect(new Set(chunks.map((chunk) => chunk.id)).size).toBe(2); | ||
| for (const node of doc.getNodes()) { | ||
| const ownChunk = chunks.find((chunk) => chunk.metadata?.docId === node.id); | ||
| expect(ownChunk).toBeDefined(); | ||
| expect(doc.getLinkGraph()[node.id]).toEqual([ownChunk?.id]); | ||
| } | ||
| }); | ||
|
|
||
| it("keeps IDs stable for an unchanged document and preserves chunk contents", () => { | ||
| const doc = new StructuredDocument([documents[0]]); | ||
| const first = doc.chunk({ strategy: "token", maxTokens: 100 }).chunks; | ||
| const second = doc.chunk({ strategy: "token", maxTokens: 100 }).chunks; | ||
|
|
||
| expect(second.map((chunk) => chunk.id)).toEqual(first.map((chunk) => chunk.id)); | ||
| expect(first.map((chunk) => chunk.content)).toEqual([documents[0].text]); | ||
| expect(first[0]?.metadata?.docId).toBe("policy"); | ||
| }); | ||
|
|
||
| it("keeps chunks distinct even when callers repeat an explicit document ID", () => { | ||
| const doc = new StructuredDocument( | ||
| documents.map((document) => ({ ...document, docId: "shared" })), | ||
| ); | ||
| const { chunks } = doc.chunk({ strategy: "token", maxTokens: 100 }); | ||
|
|
||
| expect(new Set(chunks.map((chunk) => chunk.id)).size).toBe(2); | ||
| expect(chunks.map((chunk) => chunk.content)).toEqual( | ||
| documents.map((document) => document.text), | ||
| ); | ||
| expect(doc.getLinkGraph().shared).toEqual(chunks.map((chunk) => chunk.id)); | ||
| doc.getNodes().forEach((node, index) => { | ||
| expect(node.links).toEqual([{ nodeId: chunks[index]?.id, type: "chunk" }]); | ||
| }); | ||
| }); | ||
|
|
||
| it("keeps empty input empty", () => { | ||
| const doc = new StructuredDocument([]); | ||
| expect(doc.chunk({ strategy: "token" }).chunks).toEqual([]); | ||
| expect(doc.getLinkGraph()).toEqual({}); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.