Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/steady-neural-chunks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@voltagent/rag": patch
---

Assign distinct NeuralChunker IDs to fitting detector segments after earlier token expansion, so returned chunks do not overwrite each other in ID-keyed stores. Fixes #1518.
26 changes: 26 additions & 0 deletions packages/rag/src/chunkers/neural-chunker.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, it } from "vitest";
import { whitespaceTokenizer } from "../utils/tokenizer";
import { NeuralChunker } from "./neural-chunker";

describe("NeuralChunker output IDs", () => {
it.each([
{ name: "default tokenizer", tokenizer: undefined },
{ name: "whitespace tokenizer", tokenizer: whitespaceTokenizer },
])("keeps IDs distinct after token expansion with $name", async ({ tokenizer }) => {
for (const label of ["neural", "custom"]) {
const chunks = await new NeuralChunker(tokenizer).chunk("a b c d e f", {
detector: () => [1, 9],
maxTokens: 2,
label,
});
expect(chunks).toHaveLength(4);
expect(chunks.map((chunk) => chunk.id)).toEqual([
`${label}-0`,
`${label}-1`,
`${label}-2`,
`${label}-3`,
]);
expect(chunks.every((chunk) => (chunk.tokens ?? 0) <= 2)).toBe(true);
}
});
});
1 change: 1 addition & 0 deletions packages/rag/src/chunkers/neural-chunker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class NeuralChunker implements Chunker<NeuralChunkerOptions> {
if ((chunk.tokens ?? 0) <= maxTokens) {
finalChunks.push({
...chunk,
id: `${label}-${finalChunks.length}`,
metadata: buildMetadata({
format: "neural",
sourceType: "neural",
Expand Down