From 08c49b0d5ff2c349232295170360308d5e4a1fbd Mon Sep 17 00:00:00 2001
From: Yi Ding <yi.s.ding@gmail.com>
Date: Sat, 2 Sep 2023 12:11:52 -0700
Subject: [PATCH] more demo work

---
 apps/simple/notion.ts | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/apps/simple/notion.ts b/apps/simple/notion.ts
index e73411894..685c237bc 100644
--- a/apps/simple/notion.ts
+++ b/apps/simple/notion.ts
@@ -1,6 +1,10 @@
 import { Client } from "@notionhq/client";
 import { program } from "commander";
-import { NotionReader } from "llamaindex";
+import { NotionReader, VectorStoreIndex } from "llamaindex";
+// readline/promises is still experimental so not in @types/node yet
+// @ts-ignore
+import { stdin as input, stdout as output } from "node:process";
+import readline from "node:readline/promises";
 
 program
   .argument("[page]", "Notion page id (must be provided)")
@@ -60,6 +64,26 @@ program
     const reader = new NotionReader({ client: notion });
     const documents = await reader.loadData(page);
     console.log(documents);
+
+    // Split text and create embeddings. Store them in a VectorStoreIndex
+    const index = await VectorStoreIndex.fromDocuments(documents);
+
+    // Create query engine
+    const queryEngine = index.asQueryEngine();
+
+    const rl = readline.createInterface({ input, output });
+    while (true) {
+      const query = await rl.question("Query: ");
+
+      if (!query) {
+        break;
+      }
+
+      const response = await queryEngine.query(query);
+
+      // Output response
+      console.log(response.toString());
+    }
   });
 
 program.parse();
-- 
GitLab