Skip to content
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

fix: truncate long document #843

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/src/Node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createSHA256, path, randomUUID } from "@llamaindex/env";
import _ from "lodash";
import { Settings } from "./Settings.js";

export enum NodeRelationship {
SOURCE = "SOURCE",
Expand Down Expand Up @@ -208,7 +209,14 @@ export class TextNode<T extends Metadata = Metadata> extends BaseNode<T> {

getContent(metadataMode: MetadataMode = MetadataMode.NONE): string {
const metadataStr = this.getMetadataStr(metadataMode).trim();
return `${metadataStr}\n\n${this.text}`.trim();
const fullText = `${metadataStr}\n\n${this.text}`.trim();
if (Settings.chunkSize) {
if (fullText.length > Settings.chunkSize) {
console.warn(`Content ${this.id_} is too long, truncating`);
return this.text.slice(0, Settings.chunkSize);
}
}
return fullText;
}

getMetadataStr(metadataMode: MetadataMode): string {
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/readers/SimpleDirectoryReader.edge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fs, path } from "@llamaindex/env";
import { Document, type Metadata } from "../Node.js";
import { path } from "@llamaindex/env";
import { Document } from "../Node.js";
import { walk } from "../storage/FileSystem.js";
import { TextFileReader } from "./TextFileReader.js";
import type { BaseReader } from "./type.js";
Expand Down Expand Up @@ -85,7 +85,7 @@ export class SimpleDirectoryReader implements BaseReader {
continue;
}

const fileDocs = await reader.loadData(filePath, fs);
const fileDocs = await reader.loadData(filePath);
fileDocs.forEach(addMetaData(filePath));

// Observer can still cancel addition of the resulting docs from this file
Expand Down Expand Up @@ -123,8 +123,8 @@ export class SimpleDirectoryReader implements BaseReader {
}
}

function addMetaData(filePath: string): (doc: Document<Metadata>) => void {
return (doc: Document<Metadata>) => {
function addMetaData(filePath: string): (doc: Document) => void {
return (doc: Document) => {
doc.metadata["file_path"] = path.resolve(filePath);
doc.metadata["file_name"] = path.basename(filePath);
};
Expand Down