Skip to content

Commit

Permalink
fix: remove redundant template engine variable and fixed llamaparse f…
Browse files Browse the repository at this point in the history
…lag (#22)
  • Loading branch information
marcusschiesser authored Mar 27, 2024
1 parent b08cad0 commit 459824d
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 164 deletions.
2 changes: 0 additions & 2 deletions create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export type InstallAppArgs = Omit<
export async function createApp({
template,
framework,
engine,
ui,
appPath,
packageManager,
Expand Down Expand Up @@ -76,7 +75,6 @@ export async function createApp({
root,
template,
framework,
engine,
ui,
packageManager,
isOnline,
Expand Down
9 changes: 4 additions & 5 deletions e2e/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ChildProcess } from "child_process";
import fs from "fs";
import path from "path";
import type {
TemplateEngine,
TemplateFramework,
TemplatePostInstallAction,
TemplateType,
Expand All @@ -18,7 +17,7 @@ const templateFrameworks: TemplateFramework[] = [
"express",
"fastapi",
];
const templateEngines: TemplateEngine[] = ["simple", "context"];
const dataSources: string[] = ["--no-files", "--example-file"];
const templateUIs: TemplateUI[] = ["shadcn", "html"];
const templatePostInstallActions: TemplatePostInstallAction[] = [
"none",
Expand All @@ -27,12 +26,12 @@ const templatePostInstallActions: TemplatePostInstallAction[] = [

for (const templateType of templateTypes) {
for (const templateFramework of templateFrameworks) {
for (const templateEngine of templateEngines) {
for (const dataSource of dataSources) {
for (const templateUI of templateUIs) {
for (const templatePostInstallAction of templatePostInstallActions) {
const appType: AppType =
templateFramework === "nextjs" ? "" : "--frontend";
test.describe(`try create-llama ${templateType} ${templateFramework} ${templateEngine} ${templateUI} ${appType} ${templatePostInstallAction}`, async () => {
test.describe(`try create-llama ${templateType} ${templateFramework} ${dataSource} ${templateUI} ${appType} ${templatePostInstallAction}`, async () => {
let port: number;
let externalPort: number;
let cwd: string;
Expand All @@ -49,7 +48,7 @@ for (const templateType of templateTypes) {
cwd,
templateType,
templateFramework,
templateEngine,
dataSource,
templateUI,
vectorDb,
appType,
Expand Down
13 changes: 7 additions & 6 deletions e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { mkdir } from "node:fs/promises";
import * as path from "path";
import waitPort from "wait-port";
import {
TemplateEngine,
TemplateFramework,
TemplatePostInstallAction,
TemplateType,
Expand Down Expand Up @@ -67,18 +66,21 @@ export async function runCreateLlama(
cwd: string,
templateType: TemplateType,
templateFramework: TemplateFramework,
templateEngine: TemplateEngine,
dataSource: string,
templateUI: TemplateUI,
vectorDb: TemplateVectorDB,
appType: AppType,
port: number,
externalPort: number,
postInstallAction: TemplatePostInstallAction,
): Promise<CreateLlamaResult> {
if (!process.env.OPENAI_API_KEY) {
throw new Error("Setting OPENAI_API_KEY is mandatory to run tests");
}
const name = [
templateType,
templateFramework,
templateEngine,
dataSource,
templateUI,
appType,
].join("-");
Expand All @@ -89,8 +91,7 @@ export async function runCreateLlama(
templateType,
"--framework",
templateFramework,
"--engine",
templateEngine,
dataSource,
"--ui",
templateUI,
"--vector-db",
Expand All @@ -100,7 +101,7 @@ export async function runCreateLlama(
"--embedding-model",
EMBEDDING_MODEL,
"--open-ai-key",
process.env.OPENAI_API_KEY || "testKey",
process.env.OPENAI_API_KEY,
appType,
"--eslint",
"--use-pnpm",
Expand Down
27 changes: 27 additions & 0 deletions helpers/datasources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TemplateDataSource } from "./types";

// Example file has an empty config
export const EXAMPLE_FILE: TemplateDataSource = {
type: "file",
config: {},
};

export function getDataSources(
files?: string,
exampleFile?: boolean,
): TemplateDataSource[] | undefined {
let dataSources: TemplateDataSource[] | undefined = undefined;
if (files) {
// If user specified files option, then the program should use context engine
dataSources = files.split(",").map((filePath) => ({
type: "file",
config: {
path: filePath,
},
}));
}
if (exampleFile) {
dataSources = [...(dataSources ? dataSources : []), EXAMPLE_FILE];
}
return dataSources;
}
2 changes: 1 addition & 1 deletion helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export const installTemplate = async (
port: props.externalPort,
});

if (props.engine === "context") {
if (props.dataSources.length > 0) {
console.log("\nGenerating context data...\n");
await copyContextData(props.root, props.dataSources);
if (
Expand Down
78 changes: 37 additions & 41 deletions helpers/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ export const installPythonTemplate = async ({
root,
template,
framework,
engine,
vectorDb,
dataSources,
tools,
Expand All @@ -188,7 +187,6 @@ export const installPythonTemplate = async ({
| "root"
| "framework"
| "template"
| "engine"
| "vectorDb"
| "dataSources"
| "tools"
Expand Down Expand Up @@ -217,7 +215,7 @@ export const installPythonTemplate = async ({
},
});

if (engine === "context") {
if (dataSources.length > 0) {
const enginePath = path.join(root, "app", "engine");
const compPath = path.join(templatesDir, "components");

Expand Down Expand Up @@ -257,46 +255,44 @@ export const installPythonTemplate = async ({
});
}

if (dataSources.length > 0) {
const loaderConfigs: Record<string, any> = {};
const loaderPath = path.join(enginePath, "loaders");
const loaderConfigs: Record<string, any> = {};
const loaderPath = path.join(enginePath, "loaders");

// Copy loaders to enginePath
await copy("**", loaderPath, {
parents: true,
cwd: path.join(compPath, "loaders", "python"),
});
// Copy loaders to enginePath
await copy("**", loaderPath, {
parents: true,
cwd: path.join(compPath, "loaders", "python"),
});

// Generate loaders config
// Web loader config
if (dataSources.some((ds) => ds.type === "web")) {
const webLoaderConfig = dataSources
.filter((ds) => ds.type === "web")
.map((ds) => {
const dsConfig = ds.config as WebSourceConfig;
return {
base_url: dsConfig.baseUrl,
prefix: dsConfig.prefix,
depth: dsConfig.depth,
};
});
loaderConfigs["web"] = webLoaderConfig;
}
// File loader config
if (dataSources.some((ds) => ds.type === "file")) {
loaderConfigs["file"] = {
use_llama_parse: useLlamaParse,
};
}
// Write loaders config
if (Object.keys(loaderConfigs).length > 0) {
const loaderConfigPath = path.join(root, "config/loaders.json");
await fs.mkdir(path.join(root, "config"), { recursive: true });
await fs.writeFile(
loaderConfigPath,
JSON.stringify(loaderConfigs, null, 2),
);
}
// Generate loaders config
// Web loader config
if (dataSources.some((ds) => ds.type === "web")) {
const webLoaderConfig = dataSources
.filter((ds) => ds.type === "web")
.map((ds) => {
const dsConfig = ds.config as WebSourceConfig;
return {
base_url: dsConfig.baseUrl,
prefix: dsConfig.prefix,
depth: dsConfig.depth,
};
});
loaderConfigs["web"] = webLoaderConfig;
}
// File loader config
if (dataSources.some((ds) => ds.type === "file")) {
loaderConfigs["file"] = {
use_llama_parse: useLlamaParse,
};
}
// Write loaders config
if (Object.keys(loaderConfigs).length > 0) {
const loaderConfigPath = path.join(root, "config/loaders.json");
await fs.mkdir(path.join(root, "config"), { recursive: true });
await fs.writeFile(
loaderConfigPath,
JSON.stringify(loaderConfigs, null, 2),
);
}
}

Expand Down
2 changes: 0 additions & 2 deletions helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Tool } from "./tools";

export type TemplateType = "streaming" | "community" | "llamapack";
export type TemplateFramework = "nextjs" | "express" | "fastapi";
export type TemplateEngine = "simple" | "context";
export type TemplateUI = "html" | "shadcn";
export type TemplateVectorDB = "none" | "mongo" | "pg" | "pinecone" | "milvus";
export type TemplatePostInstallAction =
Expand Down Expand Up @@ -43,7 +42,6 @@ export interface InstallTemplateArgs {
isOnline: boolean;
template: TemplateType;
framework: TemplateFramework;
engine: TemplateEngine;
ui: TemplateUI;
dataSources: TemplateDataSource[];
eslint: boolean;
Expand Down
70 changes: 31 additions & 39 deletions helpers/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export const installTSTemplate = async ({
isOnline,
template,
framework,
engine,
ui,
eslint,
customApiPath,
Expand Down Expand Up @@ -142,50 +141,41 @@ export const installTSTemplate = async ({
/**
* Copy the selected chat engine files to the target directory and reference it.
*/
let relativeEngineDestPath;
const compPath = path.join(templatesDir, "components");
if (engine && (framework === "express" || framework === "nextjs")) {
console.log("\nUsing chat engine:", engine, "\n");
const relativeEngineDestPath =
framework === "nextjs"
? path.join("app", "api", "chat")
: path.join("src", "controllers");
const enginePath = path.join(root, relativeEngineDestPath, "engine");

let vectorDBFolder: string = engine;

if (engine !== "simple" && vectorDb) {
if (dataSources.length === 0) {
// use simple hat engine if user neither select tools nor a data source
console.log("\nUsing simple chat engine\n");
} else {
if (vectorDb) {
// copy vector db component
console.log("\nUsing vector DB:", vectorDb, "\n");
vectorDBFolder = vectorDb;
}

relativeEngineDestPath =
framework === "nextjs"
? path.join("app", "api", "chat")
: path.join("src", "controllers");

const enginePath = path.join(root, relativeEngineDestPath, "engine");

// copy vector db component
const vectorDBPath = path.join(
compPath,
"vectordbs",
"typescript",
vectorDBFolder,
);
await copy("**", enginePath, {
parents: true,
cwd: vectorDBPath,
});

// copy loader component
const dataSourceType = dataSources[0]?.type;
if (dataSourceType) {
let loaderFolder: string;
loaderFolder = useLlamaParse ? "llama_parse" : dataSourceType;
const vectorDBPath = path.join(
compPath,
"vectordbs",
"typescript",
vectorDb,
);
await copy("**", enginePath, {
parents: true,
cwd: path.join(compPath, "loaders", "typescript", loaderFolder),
cwd: vectorDBPath,
});
}

// copy tools component
// copy loader component (TS only supports llama_parse and file for now)
let loaderFolder: string;
loaderFolder = useLlamaParse ? "llama_parse" : "file";
await copy("**", enginePath, {
parents: true,
cwd: path.join(compPath, "loaders", "typescript", loaderFolder),
});
if (tools?.length) {
// use agent chat engine if user selects tools
console.log("\nUsing agent chat engine\n");
await copy("**", enginePath, {
parents: true,
cwd: path.join(compPath, "engines", "typescript", "agent"),
Expand All @@ -201,7 +191,9 @@ export const installTSTemplate = async ({
configFilePath,
JSON.stringify(configContent, null, 2),
);
} else if (engine !== "simple") {
} else {
// use context chat engine if user does not select tools
console.log("\nUsing context chat engine\n");
await copy("**", enginePath, {
parents: true,
cwd: path.join(compPath, "engines", "typescript", "chat"),
Expand Down Expand Up @@ -248,7 +240,7 @@ export const installTSTemplate = async ({
// modify the dev script to use the custom api path
}

if (engine === "context" && relativeEngineDestPath) {
if (dataSources.length > 0 && relativeEngineDestPath) {
// add generate script if using context engine
packageJson.scripts = {
...packageJson.scripts,
Expand Down
Loading

0 comments on commit 459824d

Please sign in to comment.