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

core[minor]: Add ID field to document #5893

Merged
merged 7 commits into from
Jun 28, 2024
Merged
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
28 changes: 28 additions & 0 deletions langchain-core/src/documents/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ export interface DocumentInput<
pageContent: string;

metadata?: Metadata;

/**
* An optional identifier for the document.
*
* Ideally this should be unique across the document collection and formatted
* as a UUID, but this will not be enforced.
*/
id?: string;
}

export interface DocumentInterface<
Expand All @@ -14,6 +22,14 @@ export interface DocumentInterface<
pageContent: string;

metadata: Metadata;

/**
* An optional identifier for the document.
*
* Ideally this should be unique across the document collection and formatted
* as a UUID, but this will not be enforced.
*/
id?: string;
}

/**
Expand All @@ -28,9 +44,21 @@ export class Document<

metadata: Metadata;

// The ID field is optional at the moment.
// It will likely become required in a future major release after
// it has been adopted by enough vectorstore implementations.
/**
* An optional identifier for the document.
*
* Ideally this should be unique across the document collection and formatted
* as a UUID, but this will not be enforced.
*/
id?: string;

constructor(fields: DocumentInput<Metadata>) {
this.pageContent =
fields.pageContent !== undefined ? fields.pageContent.toString() : "";
this.metadata = fields.metadata ?? ({} as Metadata);
this.id = fields.id;
}
}
2 changes: 2 additions & 0 deletions langchain/src/document_loaders/tests/csv-blob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ test("Test CSV loader from blob", async () => {
expect(docs.length).toBe(2);
expect(docs[0]).toMatchInlineSnapshot(`
Document {
"id": undefined,
"metadata": {
"blobType": "text/csv",
"line": 1,
Expand All @@ -57,6 +58,7 @@ test("Test CSV loader from blob", async () => {
`);
expect(docs[1]).toMatchInlineSnapshot(`
Document {
"id": undefined,
"metadata": {
"blobType": "text/csv",
"line": 2,
Expand Down
4 changes: 4 additions & 0 deletions langchain/src/document_loaders/tests/json-blob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ test("Test JSON loader from blob", async () => {
expect(docs.length).toBe(2);
expect(docs[0]).toMatchInlineSnapshot(`
Document {
"id": undefined,
"metadata": {
"blobType": "application/json",
"line": 1,
Expand All @@ -49,6 +50,7 @@ test("Test JSON loader from blob", async () => {
`);
expect(docs[1]).toMatchInlineSnapshot(`
Document {
"id": undefined,
"metadata": {
"blobType": "application/json",
"line": 2,
Expand Down Expand Up @@ -87,6 +89,7 @@ test("Test JSON loader from blob", async () => {
expect(docs.length).toBe(10);
expect(docs[0]).toMatchInlineSnapshot(`
Document {
"id": undefined,
"metadata": {
"blobType": "application/json",
"line": 1,
Expand All @@ -97,6 +100,7 @@ test("Test JSON loader from blob", async () => {
`);
expect(docs[1]).toMatchInlineSnapshot(`
Document {
"id": undefined,
"metadata": {
"blobType": "application/json",
"line": 2,
Expand Down
2 changes: 2 additions & 0 deletions langchain/src/document_loaders/tests/jsonl-blob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ test("Test JSONL loader from blob", async () => {
expect(docs.length).toBe(2);
expect(docs[0]).toMatchInlineSnapshot(`
Document {
"id": undefined,
"metadata": {
"blobType": "application/jsonl+json",
"line": 1,
Expand All @@ -50,6 +51,7 @@ test("Test JSONL loader from blob", async () => {
`);
expect(docs[1]).toMatchInlineSnapshot(`
Document {
"id": undefined,
"metadata": {
"blobType": "application/jsonl+json",
"line": 2,
Expand Down
2 changes: 0 additions & 2 deletions libs/langchain-community/src/vectorstores/typeorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export interface TypeORMVectorStoreArgs {
*/
export class TypeORMVectorStoreDocument extends Document {
embedding: string;

id?: string;
}

const defaultDocumentTableName = "documents";
Expand Down
Loading