diff --git a/templates/components/engines/context/generate.mjs b/templates/components/engines/context/generate.mjs index 8420dd5f81eab52574ec51770cf0e09ccb139563..9334f98e47558b5d451e21143b9eacf5b0287955 100644 --- a/templates/components/engines/context/generate.mjs +++ b/templates/components/engines/context/generate.mjs @@ -5,6 +5,8 @@ import { VectorStoreIndex, } from "llamaindex"; +import * as dotenv from "dotenv"; + import { CHUNK_OVERLAP, CHUNK_SIZE, @@ -12,6 +14,9 @@ import { STORAGE_DIR, } from "./constants.mjs"; +// Load environment variables from local .env file +dotenv.config(); + async function getRuntime(func) { const start = Date.now(); await func(); diff --git a/templates/index.ts b/templates/index.ts index a9f0998eb2315ee1e2166904e548d14db7fb5b00..2cc8307d2351f6bc83a20fb8c605436223b1be30 100644 --- a/templates/index.ts +++ b/templates/index.ts @@ -14,26 +14,14 @@ import { TemplateFramework, } from "./types"; -const envFileNameMap: Record<TemplateFramework, string> = { - nextjs: ".env.local", - express: ".env", - fastapi: ".env", -}; - -const createEnvLocalFile = async ( - root: string, - framework: TemplateFramework, - openAIKey?: string, -) => { +const createEnvLocalFile = async (root: string, openAIKey?: string) => { if (openAIKey) { - const envFileName = envFileNameMap[framework]; - if (!envFileName) return; + const envFileName = ".env"; await fs.writeFile( path.join(root, envFileName), `OPENAI_API_KEY=${openAIKey}\n`, ); console.log(`Created '${envFileName}' file containing OPENAI_API_KEY`); - process.env["OPENAI_API_KEY"] = openAIKey; } }; @@ -42,6 +30,7 @@ const copyTestData = async ( framework: TemplateFramework, packageManager?: PackageManager, engine?: TemplateEngine, + openAIKey?: string, ) => { if (engine === "context" || framework === "fastapi") { const srcPath = path.join(__dirname, "components", "data"); @@ -54,7 +43,7 @@ const copyTestData = async ( } if (packageManager && engine === "context") { - if (process.env["OPENAI_API_KEY"]) { + if (openAIKey || process.env["OPENAI_API_KEY"]) { console.log( `\nRunning ${cyan( `${packageManager} run generate`, @@ -313,7 +302,7 @@ export const installTemplate = async ( // This is a backend, so we need to copy the test data and create the env file. // Copy the environment file to the target directory. - await createEnvLocalFile(props.root, props.framework, props.openAIKey); + await createEnvLocalFile(props.root, props.openAIKey); // Copy test pdf file await copyTestData( @@ -321,6 +310,7 @@ export const installTemplate = async ( props.framework, props.packageManager, props.engine, + props.openAIKey, ); } }; diff --git a/templates/types/simple/nextjs/package.json b/templates/types/simple/nextjs/package.json index 990b41c832c69b81883c88b2d078c55bef15b42c..5faf066d60bb3256f245b9f1536c9cf58e2fe2f4 100644 --- a/templates/types/simple/nextjs/package.json +++ b/templates/types/simple/nextjs/package.json @@ -9,6 +9,7 @@ }, "dependencies": { "llamaindex": "0.0.31", + "dotenv": "^16.3.1", "nanoid": "^5", "next": "^13", "react": "^18", @@ -18,11 +19,11 @@ "@types/node": "^20", "@types/react": "^18", "@types/react-dom": "^18", - "autoprefixer": "^10", + "autoprefixer": "^10.1", "eslint": "^8", "eslint-config-next": "^13", "postcss": "^8", - "tailwindcss": "^3", + "tailwindcss": "^3.3", "typescript": "^5" } } \ No newline at end of file diff --git a/templates/types/streaming/express/package.json b/templates/types/streaming/express/package.json index 72f127b407a8c7ce675bf4d634a42f57af78bb4c..6af064a8b6915055d556271119335ee341cec65f 100644 --- a/templates/types/streaming/express/package.json +++ b/templates/types/streaming/express/package.json @@ -9,7 +9,7 @@ "dev": "concurrently \"tsup index.ts --format esm --dts --watch\" \"nodemon -q dist/index.js\"" }, "dependencies": { - "ai": "^2", + "ai": "^2.2.5", "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4", @@ -25,4 +25,4 @@ "tsup": "^7", "typescript": "^5" } -} +} \ No newline at end of file diff --git a/templates/types/streaming/nextjs/package.json b/templates/types/streaming/nextjs/package.json index e9f23201d893320cfdfcbd3fbac9ad48ebbb8d7c..fc5e483e074b76b101f6089ee7850a57530ea3f4 100644 --- a/templates/types/streaming/nextjs/package.json +++ b/templates/types/streaming/nextjs/package.json @@ -8,8 +8,9 @@ "lint": "next lint" }, "dependencies": { - "ai": "^2", + "ai": "^2.2.5", "llamaindex": "0.0.31", + "dotenv": "^16.3.1", "next": "^13", "react": "^18", "react-dom": "^18" @@ -18,11 +19,11 @@ "@types/node": "^20", "@types/react": "^18", "@types/react-dom": "^18", - "autoprefixer": "^10", + "autoprefixer": "^10.1", "eslint": "^8", "eslint-config-next": "^13", "postcss": "^8", - "tailwindcss": "^3", + "tailwindcss": "^3.3", "typescript": "^5" } } \ No newline at end of file