Skip to content
Snippets Groups Projects
Commit 9d5248d7 authored by Marcus Schiesser's avatar Marcus Schiesser
Browse files

feat: add OpenAI key to create-llama

parent a31e713f
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,7 @@ export async function createApp({
packageManager,
eslint,
frontend,
openAIKey,
}: Omit<
InstallTemplateArgs,
"appName" | "root" | "isOnline" | "customApiPath"
......@@ -63,6 +64,7 @@ export async function createApp({
packageManager,
isOnline,
eslint,
openAIKey,
};
if (frontend) {
......
......@@ -167,6 +167,7 @@ async function run(): Promise<void> {
ui: "html",
eslint: true,
frontend: false,
openAIKey: "",
};
const getPrefOrDefault = (field: string) =>
preferences[field] ?? defaults[field];
......@@ -300,6 +301,19 @@ async function run(): Promise<void> {
}
}
if (!program.openAIKey) {
const { key } = await prompts(
{
type: "text",
name: "key",
message: "Please provide your OpenAI API key (leave blank to skip):",
},
handlers,
);
program.openAIKey = key;
preferences.openAIKey = key;
}
if (
program.framework !== "fastapi" &&
!process.argv.includes("--eslint") &&
......@@ -332,6 +346,7 @@ async function run(): Promise<void> {
packageManager,
eslint: program.eslint,
frontend: program.frontend,
openAIKey: program.openAIKey,
});
conf.set("preferences", preferences);
}
......
......@@ -7,7 +7,29 @@ import path from "path";
import { bold, cyan } from "picocolors";
import { version } from "../package.json";
import { InstallTemplateArgs } from "./types";
import { InstallTemplateArgs, TemplateFramework } from "./types";
const envFileNameMap: Record<TemplateFramework, string> = {
nextjs: ".env.local",
express: ".env",
fastapi: ".env",
};
const createEnvLocalFile = async (
root: string,
framework: TemplateFramework,
openAIKey?: string,
) => {
if (openAIKey) {
const envFileName = envFileNameMap[framework];
if (!envFileName) return;
await fs.writeFile(
path.join(root, envFileName),
`OPENAI_API_KEY=${openAIKey}\n`,
);
console.log(`Created '${envFileName}' file containing OPENAI_API_KEY`);
}
};
/**
* Install a LlamaIndex internal template to a given `root` directory.
......@@ -23,6 +45,7 @@ const installTSTemplate = async ({
ui,
eslint,
customApiPath,
openAIKey,
}: InstallTemplateArgs) => {
console.log(bold(`Using ${packageManager}.`));
......@@ -86,6 +109,8 @@ const installTSTemplate = async ({
});
}
await createEnvLocalFile(root, framework, openAIKey);
/**
* Update the package.json scripts.
*/
......@@ -181,7 +206,11 @@ const installPythonTemplate = async ({
root,
template,
framework,
}: Pick<InstallTemplateArgs, "root" | "framework" | "template">) => {
openAIKey,
}: Pick<
InstallTemplateArgs,
"root" | "framework" | "template" | "openAIKey"
>) => {
console.log("\nInitializing Python project with template:", template, "\n");
const templatePath = path.join(__dirname, "types", template, framework);
await copy("**", root, {
......@@ -203,6 +232,9 @@ const installPythonTemplate = async ({
}
},
});
await createEnvLocalFile(root, framework, openAIKey);
console.log(
"\nPython project, dependencies won't be installed automatically.\n",
);
......
......@@ -16,4 +16,5 @@ export interface InstallTemplateArgs {
ui: TemplateUI;
eslint: boolean;
customApiPath?: string;
openAIKey?: string;
}
import "dotenv/config";
import express, { Express, Request, Response } from "express";
import chatRouter from "./src/routes/chat.route";
......
......@@ -9,6 +9,7 @@
"dev": "concurrently \"tsup index.ts --format esm --dts --watch\" \"nodemon -q dist/index.js\""
},
"dependencies": {
"dotenv": "^16.3.1",
"express": "^4",
"llamaindex": "0.0.31"
},
......@@ -21,4 +22,4 @@
"tsup": "^7",
"typescript": "^5"
}
}
\ No newline at end of file
}
......@@ -4,6 +4,9 @@ import uvicorn
from app.api.routers.chat import chat_router
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
......
......@@ -11,6 +11,7 @@ fastapi = "^0.104.1"
uvicorn = { extras = ["standard"], version = "^0.23.2" }
llama-index = "^0.8.56"
pypdf = "^3.17.0"
python-dotenv = "^1.0.0"
[build-system]
......
import "dotenv/config";
import express, { Express, Request, Response } from "express";
import chatRouter from "./src/routes/chat.route";
......
......@@ -10,6 +10,7 @@
},
"dependencies": {
"ai": "^2",
"dotenv": "^16.3.1",
"express": "^4",
"llamaindex": "0.0.31"
},
......@@ -22,4 +23,4 @@
"tsup": "^7",
"typescript": "^5"
}
}
\ No newline at end of file
}
......@@ -4,6 +4,9 @@ import uvicorn
from app.api.routers.chat import chat_router
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
......
......@@ -11,6 +11,7 @@ fastapi = "^0.104.1"
uvicorn = { extras = ["standard"], version = "^0.23.2" }
llama-index = "^0.8.56"
pypdf = "^3.17.0"
python-dotenv = "^1.0.0"
[build-system]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment