From d1db2189c99302ad5376762568bb15e3df311183 Mon Sep 17 00:00:00 2001
From: Marcus Schiesser <mail@marcusschiesser.de>
Date: Fri, 14 Feb 2025 12:14:51 +0700
Subject: [PATCH] docs: update starter to ask for api key (#1661)

---
 examples/starter.ts | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/examples/starter.ts b/examples/starter.ts
index e73e8dee8..a47e7b04a 100644
--- a/examples/starter.ts
+++ b/examples/starter.ts
@@ -3,6 +3,18 @@ import fs from "node:fs/promises";
 import { createInterface } from "node:readline/promises";
 
 async function main() {
+  const rl = createInterface({ input: process.stdin, output: process.stdout });
+
+  if (!process.env.OPENAI_API_KEY) {
+    console.log("OpenAI API key not found in environment variables.");
+    console.log(
+      "You can get an API key at https://platform.openai.com/account/api-keys",
+    );
+    process.env.OPENAI_API_KEY = await rl.question(
+      "Please enter your OpenAI API key: ",
+    );
+  }
+
   const path = "node_modules/llamaindex/examples/abramov.txt";
   const essay = await fs.readFile(path, "utf-8");
   const document = new Document({ text: essay, id_: path });
@@ -10,8 +22,6 @@ async function main() {
   const index = await VectorStoreIndex.fromDocuments([document]);
   const queryEngine = index.asQueryEngine();
 
-  const rl = createInterface({ input: process.stdin, output: process.stdout });
-
   console.log(
     "Try asking a question about the essay: https://github.com/run-llama/LlamaIndexTS/blob/main/packages/llamaindex/examples/abramov.txt",
     "\nExample: When did the author graduate from high school?",
-- 
GitLab