From bc594a0674dd0eccee0cabf3f5d272ef3915ce45 Mon Sep 17 00:00:00 2001
From: Marcus Schiesser <mail@marcusschiesser.de>
Date: Tue, 16 Apr 2024 10:27:34 +0800
Subject: [PATCH] doc: update vector index example to show source nodes

---
 examples/vectorIndex.ts | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/examples/vectorIndex.ts b/examples/vectorIndex.ts
index afa95e540..421bfe871 100644
--- a/examples/vectorIndex.ts
+++ b/examples/vectorIndex.ts
@@ -1,6 +1,6 @@
 import fs from "node:fs/promises";
 
-import { Document, VectorStoreIndex } from "llamaindex";
+import { Document, NodeWithScore, VectorStoreIndex } from "llamaindex";
 
 async function main() {
   // Load essay from abramov.txt in Node
@@ -16,12 +16,18 @@ async function main() {
 
   // Query the index
   const queryEngine = index.asQueryEngine();
-  const response = await queryEngine.query({
+  const { response, sourceNodes } = await queryEngine.query({
     query: "What did the author do in college?",
   });
 
-  // Output response
-  console.log(response.toString());
+  // Output response with sources
+  console.log(response);
+
+  sourceNodes.forEach((source: NodeWithScore, index: number) => {
+    console.log(
+      `\n${index}: Score: ${source.score} - ${source.node.text.substring(0, 50)}...\n`,
+    );
+  });
 }
 
 main().catch(console.error);
-- 
GitLab