LlamaIndexTS icon indicating copy to clipboard operation
LlamaIndexTS copied to clipboard

doc.toJSON is not a function

Open lukemovement opened this issue 1 year ago • 0 comments

I think the export for the Document class might be missing. It's not present in llamaindex nor is @llamaindex/core/schema a valid import path.

import { HuggingFaceEmbedding, Settings, VectorStoreIndex } from "llamaindex";

Settings.embedModel = new HuggingFaceEmbedding({});

// Load essay from abramov.txt in Node
const essay =
  'The Normans (Norman: Nourmands; French: Normands; Latin: Normanni) were the people who in the 10th and 11th centuries gave their name to Normandy, a region in France. They were descended from Norse ("Norman" comes from "Norseman") raiders and pirates from Denmark, Iceland and Norway who, under their leader Rollo, agreed to swear fealty to King Charles III of West Francia. Through generations of assimilation and mixing with the native Frankish and Roman-Gaulish populations, their descendants would gradually merge with the Carolingian-based cultures of West Francia. The distinct cultural and ethnic identity of the Normans emerged initially in the first half of the 10th century, and it continued to evolve over the succeeding centuries.';

// Create Document object with essay
const document = { text: essay };

// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments([document]);

// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
  query: "In what country is Normandy located?",
});

// Output response
console.log(response.toString());
file:///home/luke/Personal/ts-ai/node_modules/llamaindex/dist/storage/docStore/utils.js:9
        [DATA_KEY]: JSON.stringify(doc.toJSON()),
                                       ^

TypeError: doc.toJSON is not a function
    at docToJson (file:///home/luke/Personal/ts-ai/node_modules/llamaindex/dist/storage/docStore/utils.js:9:40)
    at SimpleDocumentStore.addDocuments (file:///home/luke/Personal/ts-ai/node_modules/llamaindex/dist/storage/docStore/KVDocumentStore.js:41:26)
    at file:///home/luke/Personal/ts-ai/node_modules/llamaindex/dist/ingestion/strategies/UpsertsStrategy.js:21:33
    at async runTransformations (file:///home/luke/Personal/ts-ai/node_modules/llamaindex/dist/ingestion/IngestionPipeline.js:12:17)
    at async VectorStoreIndex.fromDocuments (file:///home/luke/Personal/ts-ai/node_modules/llamaindex/dist/indices/vectorStore/index.js:492:22)
    at async file:///home/luke/Personal/ts-ai/src/llama-index/index.mts:8:15

Node.js v20.14.0

import { Document } from "@llamaindex/core/dist/schema";
import { HuggingFaceEmbedding, Settings, VectorStoreIndex } from "llamaindex";

Settings.embedModel = new HuggingFaceEmbedding({});

// Load essay from abramov.txt in Node
const essay =
  'The Normans (Norman: Nourmands; French: Normands; Latin: Normanni) were the people who in the 10th and 11th centuries gave their name to Normandy, a region in France. They were descended from Norse ("Norman" comes from "Norseman") raiders and pirates from Denmark, Iceland and Norway who, under their leader Rollo, agreed to swear fealty to King Charles III of West Francia. Through generations of assimilation and mixing with the native Frankish and Roman-Gaulish populations, their descendants would gradually merge with the Carolingian-based cultures of West Francia. The distinct cultural and ethnic identity of the Normans emerged initially in the first half of the 10th century, and it continued to evolve over the succeeding centuries.';

// Create Document object with essay
const document = new Document({ text: essay });

// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments([document]);

// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query({
  query: "In what country is Normandy located?",
});

// Output response
console.log(response.toString());

node:internal/modules/run_main:129
    triggerUncaughtException(
    ^
Error: ERR_PACKAGE_PATH_NOT_EXPORTED /home/luke/Personal/ts-ai/node_modules/@llamaindex/core/ ./dist/schema /home/luke/Personal/ts-ai/src/llama-index/index.mts
    at throwExportsNotFound (/home/luke/Personal/ts-ai/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:380:9)
    at packageExportsResolve (/home/luke/Personal/ts-ai/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:605:3)
    at packageResolve (/home/luke/Personal/ts-ai/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:746:14)
    at moduleResolve (/home/luke/Personal/ts-ai/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:798:18)
    at Object.defaultResolve (/home/luke/Personal/ts-ai/node_modules/ts-node/dist-raw/node-internal-modules-esm-resolve.js:912:11)
    at /home/luke/Personal/ts-ai/node_modules/ts-node/src/esm.ts:218:35
    at entrypointFallback (/home/luke/Personal/ts-ai/node_modules/ts-node/src/esm.ts:168:34)
    at /home/luke/Personal/ts-ai/node_modules/ts-node/src/esm.ts:217:14
    at addShortCircuitFlag (/home/luke/Personal/ts-ai/node_modules/ts-node/src/esm.ts:409:21)
    at resolve (/home/luke/Personal/ts-ai/node_modules/ts-node/src/esm.ts:197:12)

Node.js v20.14.0

lukemovement avatar Aug 10 '24 13:08 lukemovement