Skip to content
Snippets Groups Projects
Commit 149e58bc authored by yisding's avatar yisding Committed by GitHub
Browse files

Merge pull request #228 from run-llama/ms/create-llama-fixes

Several fixes for improving compatibility with Next.JS
parents 6d2a60cb 7bd25b38
No related branches found
No related tags found
No related merge requests found
Showing
with 110 additions and 36 deletions
...@@ -5,6 +5,8 @@ import { ...@@ -5,6 +5,8 @@ import {
VectorStoreIndex, VectorStoreIndex,
} from "llamaindex"; } from "llamaindex";
import * as dotenv from "dotenv";
import { import {
CHUNK_OVERLAP, CHUNK_OVERLAP,
CHUNK_SIZE, CHUNK_SIZE,
...@@ -12,6 +14,9 @@ import { ...@@ -12,6 +14,9 @@ import {
STORAGE_DIR, STORAGE_DIR,
} from "./constants.mjs"; } from "./constants.mjs";
// Load environment variables from local .env file
dotenv.config();
async function getRuntime(func) { async function getRuntime(func) {
const start = Date.now(); const start = Date.now();
await func(); await func();
......
...@@ -14,26 +14,14 @@ import { ...@@ -14,26 +14,14 @@ import {
TemplateFramework, TemplateFramework,
} from "./types"; } from "./types";
const envFileNameMap: Record<TemplateFramework, string> = { const createEnvLocalFile = async (root: string, openAIKey?: string) => {
nextjs: ".env.local",
express: ".env",
fastapi: ".env",
};
const createEnvLocalFile = async (
root: string,
framework: TemplateFramework,
openAIKey?: string,
) => {
if (openAIKey) { if (openAIKey) {
const envFileName = envFileNameMap[framework]; const envFileName = ".env";
if (!envFileName) return;
await fs.writeFile( await fs.writeFile(
path.join(root, envFileName), path.join(root, envFileName),
`OPENAI_API_KEY=${openAIKey}\n`, `OPENAI_API_KEY=${openAIKey}\n`,
); );
console.log(`Created '${envFileName}' file containing OPENAI_API_KEY`); console.log(`Created '${envFileName}' file containing OPENAI_API_KEY`);
process.env["OPENAI_API_KEY"] = openAIKey;
} }
}; };
...@@ -42,7 +30,16 @@ const copyTestData = async ( ...@@ -42,7 +30,16 @@ const copyTestData = async (
framework: TemplateFramework, framework: TemplateFramework,
packageManager?: PackageManager, packageManager?: PackageManager,
engine?: TemplateEngine, engine?: TemplateEngine,
openAIKey?: string,
) => { ) => {
if (framework === "nextjs") {
// XXX: This is a hack to make the build for nextjs work with pdf-parse
// pdf-parse needs './test/data/05-versions-space.pdf' to exist - can be removed when pdf-parse is removed
const srcFile = path.join(__dirname, "components", "data", "101.pdf");
const destPath = path.join(root, "test", "data");
await fs.mkdir(destPath, { recursive: true });
await fs.copyFile(srcFile, path.join(destPath, "05-versions-space.pdf"));
}
if (engine === "context" || framework === "fastapi") { if (engine === "context" || framework === "fastapi") {
const srcPath = path.join(__dirname, "components", "data"); const srcPath = path.join(__dirname, "components", "data");
const destPath = path.join(root, "data"); const destPath = path.join(root, "data");
...@@ -54,7 +51,7 @@ const copyTestData = async ( ...@@ -54,7 +51,7 @@ const copyTestData = async (
} }
if (packageManager && engine === "context") { if (packageManager && engine === "context") {
if (process.env["OPENAI_API_KEY"]) { if (openAIKey || process.env["OPENAI_API_KEY"]) {
console.log( console.log(
`\nRunning ${cyan( `\nRunning ${cyan(
`${packageManager} run generate`, `${packageManager} run generate`,
...@@ -226,6 +223,7 @@ const installTSTemplate = async ({ ...@@ -226,6 +223,7 @@ const installTSTemplate = async ({
"tailwind-merge": "^2", "tailwind-merge": "^2",
"@radix-ui/react-slot": "^1", "@radix-ui/react-slot": "^1",
"class-variance-authority": "^0.7", "class-variance-authority": "^0.7",
clsx: "^1.2.1",
"lucide-react": "^0.291", "lucide-react": "^0.291",
remark: "^14.0.3", remark: "^14.0.3",
"remark-code-import": "^1.2.0", "remark-code-import": "^1.2.0",
...@@ -313,7 +311,7 @@ export const installTemplate = async ( ...@@ -313,7 +311,7 @@ export const installTemplate = async (
// This is a backend, so we need to copy the test data and create the env file. // 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. // 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 // Copy test pdf file
await copyTestData( await copyTestData(
...@@ -321,6 +319,7 @@ export const installTemplate = async ( ...@@ -321,6 +319,7 @@ export const installTemplate = async (
props.framework, props.framework,
props.packageManager, props.packageManager,
props.engine, props.engine,
props.openAIKey,
); );
} }
}; };
......
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
webpack: (config) => {
// See https://webpack.js.org/configuration/resolve/#resolvealias
config.resolve.alias = {
...config.resolve.alias,
sharp$: false,
"onnxruntime-node$": false,
mongodb$: false,
};
return config;
},
experimental: { experimental: {
serverComponentsExternalPackages: ["llamaindex"], serverComponentsExternalPackages: ["llamaindex"],
outputFileTracingIncludes: { outputFileTracingIncludes: {
......
...@@ -2,6 +2,16 @@ ...@@ -2,6 +2,16 @@
const nextConfig = { const nextConfig = {
output: "export", output: "export",
images: { unoptimized: true }, images: { unoptimized: true },
webpack: (config) => {
// See https://webpack.js.org/configuration/resolve/#resolvealias
config.resolve.alias = {
...config.resolve.alias,
sharp$: false,
"onnxruntime-node$": false,
mongodb$: false,
};
return config;
},
experimental: { experimental: {
serverComponentsExternalPackages: ["llamaindex"], serverComponentsExternalPackages: ["llamaindex"],
outputFileTracingIncludes: { outputFileTracingIncludes: {
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
}, },
"dependencies": { "dependencies": {
"llamaindex": "0.0.31", "llamaindex": "0.0.31",
"dotenv": "^16.3.1",
"nanoid": "^5", "nanoid": "^5",
"next": "^13", "next": "^13",
"react": "^18", "react": "^18",
...@@ -18,11 +19,11 @@ ...@@ -18,11 +19,11 @@
"@types/node": "^20", "@types/node": "^20",
"@types/react": "^18", "@types/react": "^18",
"@types/react-dom": "^18", "@types/react-dom": "^18",
"autoprefixer": "^10", "autoprefixer": "^10.1",
"eslint": "^8", "eslint": "^8",
"eslint-config-next": "^13", "eslint-config-next": "^13",
"postcss": "^8", "postcss": "^8",
"tailwindcss": "^3", "tailwindcss": "^3.3",
"typescript": "^5" "typescript": "^5"
} }
} }
\ No newline at end of file
{ {
"compilerOptions": { "compilerOptions": {
"target": "es5", "target": "es5",
"lib": ["dom", "dom.iterable", "esnext"], "lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true, "allowJs": true,
"skipLibCheck": true, "skipLibCheck": true,
"strict": true, "strict": true,
...@@ -19,9 +23,19 @@ ...@@ -19,9 +23,19 @@
} }
], ],
"paths": { "paths": {
"@/*": ["./*"] "@/*": [
} "./*"
]
},
"forceConsistentCasingInFileNames": true,
}, },
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], "include": [
"exclude": ["node_modules"] "next-env.d.ts",
} "**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}
\ No newline at end of file
...@@ -18,7 +18,7 @@ Then call the express API endpoint `/api/chat` to see the result: ...@@ -18,7 +18,7 @@ Then call the express API endpoint `/api/chat` to see the result:
``` ```
curl --location 'localhost:8000/api/chat' \ curl --location 'localhost:8000/api/chat' \
--header 'Content-Type: application/json' \ --header 'Content-Type: text/plain' \
--data '{ "messages": [{ "role": "user", "content": "Hello" }] }' --data '{ "messages": [{ "role": "user", "content": "Hello" }] }'
``` ```
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
"dev": "concurrently \"tsup index.ts --format esm --dts --watch\" \"nodemon -q dist/index.js\"" "dev": "concurrently \"tsup index.ts --format esm --dts --watch\" \"nodemon -q dist/index.js\""
}, },
"dependencies": { "dependencies": {
"ai": "^2", "ai": "^2.2.5",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"express": "^4", "express": "^4",
...@@ -25,4 +25,4 @@ ...@@ -25,4 +25,4 @@
"tsup": "^7", "tsup": "^7",
"typescript": "^5" "typescript": "^5"
} }
} }
\ No newline at end of file
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const nextConfig = { const nextConfig = {
webpack: (config) => {
// See https://webpack.js.org/configuration/resolve/#resolvealias
config.resolve.alias = {
...config.resolve.alias,
sharp$: false,
"onnxruntime-node$": false,
mongodb$: false,
};
return config;
},
experimental: { experimental: {
serverComponentsExternalPackages: ["llamaindex"], serverComponentsExternalPackages: ["llamaindex"],
outputFileTracingIncludes: { outputFileTracingIncludes: {
......
...@@ -2,6 +2,16 @@ ...@@ -2,6 +2,16 @@
const nextConfig = { const nextConfig = {
output: "export", output: "export",
images: { unoptimized: true }, images: { unoptimized: true },
webpack: (config) => {
// See https://webpack.js.org/configuration/resolve/#resolvealias
config.resolve.alias = {
...config.resolve.alias,
sharp$: false,
"onnxruntime-node$": false,
mongodb$: false,
};
return config;
},
experimental: { experimental: {
serverComponentsExternalPackages: ["llamaindex"], serverComponentsExternalPackages: ["llamaindex"],
outputFileTracingIncludes: { outputFileTracingIncludes: {
......
...@@ -8,8 +8,9 @@ ...@@ -8,8 +8,9 @@
"lint": "next lint" "lint": "next lint"
}, },
"dependencies": { "dependencies": {
"ai": "^2", "ai": "^2.2.5",
"llamaindex": "0.0.31", "llamaindex": "0.0.31",
"dotenv": "^16.3.1",
"next": "^13", "next": "^13",
"react": "^18", "react": "^18",
"react-dom": "^18" "react-dom": "^18"
...@@ -18,11 +19,11 @@ ...@@ -18,11 +19,11 @@
"@types/node": "^20", "@types/node": "^20",
"@types/react": "^18", "@types/react": "^18",
"@types/react-dom": "^18", "@types/react-dom": "^18",
"autoprefixer": "^10", "autoprefixer": "^10.1",
"eslint": "^8", "eslint": "^8",
"eslint-config-next": "^13", "eslint-config-next": "^13",
"postcss": "^8", "postcss": "^8",
"tailwindcss": "^3", "tailwindcss": "^3.3",
"typescript": "^5" "typescript": "^5"
} }
} }
\ No newline at end of file
{ {
"compilerOptions": { "compilerOptions": {
"target": "es5", "target": "es5",
"lib": ["dom", "dom.iterable", "esnext"], "lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true, "allowJs": true,
"skipLibCheck": true, "skipLibCheck": true,
"strict": true, "strict": true,
...@@ -19,9 +23,19 @@ ...@@ -19,9 +23,19 @@
} }
], ],
"paths": { "paths": {
"@/*": ["./*"] "@/*": [
} "./*"
]
},
"forceConsistentCasingInFileNames": true,
}, },
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], "include": [
"exclude": ["node_modules"] "next-env.d.ts",
} "**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}
\ No newline at end of file
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