Skip to content

Commit

Permalink
Added option to automatically install dependencies (for Python and TS) (
Browse files Browse the repository at this point in the history
  • Loading branch information
leehuwuj authored Jan 16, 2024
1 parent 5773f97 commit 9492cc6
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-bugs-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-llama": patch
---

Added option to automatically install dependencies (for Python and TS)
12 changes: 12 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
- ".github/workflows/e2e.yml"
branches: [main]

env:
POETRY_VERSION: "1.6.1"

jobs:
e2e:
name: create-llama
Expand All @@ -16,10 +19,19 @@ jobs:
fail-fast: true
matrix:
node-version: [18, 20]
python-version: ["3.11"]
os: [macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: ${{ env.POETRY_VERSION }}
- uses: pnpm/action-setup@v2
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
Expand Down
2 changes: 2 additions & 0 deletions packages/create-llama/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export async function createApp({
communityProjectPath,
vectorDb,
externalPort,
installDependencies,
}: InstallAppArgs): Promise<void> {
const root = path.resolve(appPath);

Expand Down Expand Up @@ -75,6 +76,7 @@ export async function createApp({
communityProjectPath,
vectorDb,
externalPort,
installDependencies,
};

if (frontend) {
Expand Down
1 change: 1 addition & 0 deletions packages/create-llama/e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export function runCreateLlama(
"--use-npm",
"--external-port",
externalPort,
"--install-dependencies",
].join(" ");
console.log(`running command '${command}' in ${cwd}`);
execSync(command, {
Expand Down
22 changes: 14 additions & 8 deletions packages/create-llama/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { cyan } from "picocolors";

import { COMMUNITY_OWNER, COMMUNITY_REPO } from "./constant";
import { PackageManager } from "./get-pkg-manager";
import { isHavingPoetryLockFile, tryPoetryRun } from "./poetry";
import { installPythonTemplate } from "./python";
import { downloadAndExtractRepo } from "./repo";
import {
Expand Down Expand Up @@ -89,18 +90,23 @@ const copyTestData = async (
if (packageManager && engine === "context") {
const runGenerate = `${cyan(
framework === "fastapi"
? "python app/engine/generate.py"
? "poetry run python app/engine/generate.py"
: `${packageManager} run generate`,
)}`;
const hasOpenAiKey = openAiKey || process.env["OPENAI_API_KEY"];
const hasVectorDb = vectorDb && vectorDb !== "none";
const shouldRunGenerateAfterInstall =
hasOpenAiKey && framework !== "fastapi" && vectorDb === "none";
if (shouldRunGenerateAfterInstall) {
console.log(`\nRunning ${runGenerate} to generate the context data.\n`);
await callPackageManager(packageManager, true, ["run", "generate"]);
console.log();
return;
if (framework === "fastapi") {
if (hasOpenAiKey && vectorDb === "none" && isHavingPoetryLockFile()) {
console.log(`Running ${runGenerate} to generate the context data.`);
tryPoetryRun("python app/engine/generate.py");
return;
}
} else {
if (hasOpenAiKey && vectorDb === "none") {
console.log(`Running ${runGenerate} to generate the context data.`);
await callPackageManager(packageManager, true, ["run", "generate"]);
return;
}
}

const settings = [];
Expand Down
34 changes: 34 additions & 0 deletions packages/create-llama/helpers/poetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable import/no-extraneous-dependencies */
import { execSync } from "child_process";
import fs from "fs";

export function isPoetryAvailable(): boolean {
try {
execSync("poetry --version", { stdio: "ignore" });
return true;
} catch (_) {}
return false;
}

export function tryPoetryInstall(): boolean {
try {
execSync("poetry install", { stdio: "inherit" });
return true;
} catch (_) {}
return false;
}

export function tryPoetryRun(command: string): boolean {
try {
execSync(`poetry run ${command}`, { stdio: "inherit" });
return true;
} catch (_) {}
return false;
}

export function isHavingPoetryLockFile(): boolean {
try {
return fs.existsSync("poetry.lock");
} catch (_) {}
return false;
}
39 changes: 34 additions & 5 deletions packages/create-llama/helpers/python.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from "fs/promises";
import path from "path";
import { cyan } from "picocolors";
import { cyan, yellow } from "picocolors";
import { parse, stringify } from "smol-toml";
import terminalLink from "terminal-link";
import { copy } from "./copy";
import { isPoetryAvailable, tryPoetryInstall } from "./poetry";
import { InstallTemplateArgs, TemplateVectorDB } from "./types";

interface Dependency {
Expand Down Expand Up @@ -96,9 +98,15 @@ export const installPythonTemplate = async ({
framework,
engine,
vectorDb,
installDependencies,
}: Pick<
InstallTemplateArgs,
"root" | "framework" | "template" | "engine" | "vectorDb"
| "root"
| "framework"
| "template"
| "engine"
| "vectorDb"
| "installDependencies"
>) => {
console.log("\nInitializing Python project with template:", template, "\n");
const templatePath = path.join(
Expand Down Expand Up @@ -146,7 +154,28 @@ export const installPythonTemplate = async ({
const addOnDependencies = getAdditionalDependencies(vectorDb);
await addDependencies(root, addOnDependencies);

console.log(
"\nPython project, dependencies won't be installed automatically.\n",
);
// install python dependencies
if (installDependencies) {
if (isPoetryAvailable()) {
console.log(
`Installing python dependencies using poetry. This may take a while...`,
);
const installSuccessful = tryPoetryInstall();
if (!installSuccessful) {
console.warn(
yellow("Install failed. Please install dependencies manually."),
);
}
} else {
console.warn(
yellow(
`Poetry is not available in the current environment. The Python dependencies will not be installed automatically.
Please check ${terminalLink(
"Poetry Installation",
`https://python-poetry.org/docs/#installation`,
)} to install poetry first, then install the dependencies manually.`,
),
);
}
}
};
1 change: 1 addition & 0 deletions packages/create-llama/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export interface InstallTemplateArgs {
communityProjectPath?: string;
vectorDb?: TemplateVectorDB;
externalPort?: number;
installDependencies?: boolean;
}
19 changes: 11 additions & 8 deletions packages/create-llama/helpers/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const installTSTemplate = async ({
customApiPath,
forBackend,
vectorDb,
installDependencies,
}: InstallTemplateArgs) => {
console.log(bold(`Using ${packageManager}.`));

Expand Down Expand Up @@ -210,15 +211,17 @@ export const installTSTemplate = async ({
JSON.stringify(packageJson, null, 2) + os.EOL,
);

console.log("\nInstalling dependencies:");
for (const dependency in packageJson.dependencies)
console.log(`- ${cyan(dependency)}`);
if (installDependencies) {
console.log("\nInstalling dependencies:");
for (const dependency in packageJson.dependencies)
console.log(`- ${cyan(dependency)}`);

console.log("\nInstalling devDependencies:");
for (const dependency in packageJson.devDependencies)
console.log(`- ${cyan(dependency)}`);
console.log("\nInstalling devDependencies:");
for (const dependency in packageJson.devDependencies)
console.log(`- ${cyan(dependency)}`);

console.log();
console.log();

await callPackageManager(packageManager, isOnline);
await callPackageManager(packageManager, isOnline);
}
};
8 changes: 8 additions & 0 deletions packages/create-llama/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ const program = new Commander.Command(packageJson.name)
`
Select external port.
`,
)
.option(
"--install-dependencies",
`
Whether install dependencies (backend/frontend) automatically or not.
`,
)
.allowUnknownOption()
Expand Down Expand Up @@ -224,6 +231,7 @@ async function run(): Promise<void> {
communityProjectPath: program.communityProjectPath,
vectorDb: program.vectorDb,
externalPort: program.externalPort,
installDependencies: program.installDependencies,
});
conf.set("preferences", preferences);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/create-llama/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ export const askQuestions = async (
}
}

if (program.installDependencies === undefined) {
const { installDependencies } = await prompts({
onState: onPromptState,
type: "toggle",
name: "installDependencies",
message: `Would you like to install dependencies automatically? This may take a while`,
initial: getPrefOrDefault("installDependencies"),
active: "Yes",
inactive: "No",
});
program.installDependencies = Boolean(installDependencies);
}

if (!program.model) {
if (ciInfo.isCI) {
program.model = getPrefOrDefault("model");
Expand Down

0 comments on commit 9492cc6

Please sign in to comment.