Skip to content

Commit

Permalink
Fix example runner (langchain-ai#164)
Browse files Browse the repository at this point in the history
* Fix example runner

* Improve example runner, add tsx for running without building first, add catch to promise returned by example
  • Loading branch information
nfcampos authored Feb 28, 2023
1 parent 4396c15 commit 4a640bd
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 7 deletions.
6 changes: 4 additions & 2 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
],
"scripts": {
"build": "tsc --declaration --outDir dist/",
"start": "yarn build && node -r dotenv/config dist/index.js",
"start": "tsx -r dotenv/config src/index.ts",
"start:dist": "yarn build && node -r dotenv/config dist/index.js",
"lint": "eslint src",
"lint:fix": "yarn lint --fix",
"format": "prettier --write \"**/*.ts\"",
Expand Down Expand Up @@ -41,6 +42,7 @@
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.8.3",
"tsx": "^3.12.3",
"typescript": "^4.9.5"
}
}
}
23 changes: 23 additions & 0 deletions examples/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# langchain-examples

This folder contains examples of how to use LangChain.

## Run an example

What you'll usually want to do.

`yarn run start <path to example>`

eg.

`yarn run start ./src/prompts/few_shot.ts`

## Run an example with the transpiled JS

You shouldn't need to do this, but if you want to run an example with the transpiled JS, you can do so with:

`yarn run start:dist <path to example>`

eg.

`yarn run start:dist ./dist/prompts/few_shot.js`
34 changes: 31 additions & 3 deletions examples/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
import path from "path";
import url from "url";

const [exampleName, ...args] = process.argv.slice(2);

// Allow people to pass all possible variations of a path to an example
// ./src/foo.ts, ./dist/foo.js, src/foo.ts, dist/foo.js, foo.ts
let exampleRelativePath: string;
if (exampleName.startsWith("./src/")) {
exampleRelativePath = exampleName.slice(6);
} else if (exampleName.startsWith("./dist/")) {
exampleRelativePath = exampleName.slice(7);
} else if (exampleName.startsWith("src/")) {
exampleRelativePath = exampleName.slice(4);
} else if (exampleName.startsWith("dist/")) {
exampleRelativePath = exampleName.slice(5);
} else {
exampleRelativePath = exampleName;
}

let runExample;
try {
// eslint-disable-next-line import/no-dynamic-require,global-require
({ run: runExample } = require(path.join(__dirname, exampleName)));
({ run: runExample } = await import(
path.join(
path.dirname(url.fileURLToPath(import.meta.url)),
exampleRelativePath
)
));
} catch (e) {
throw new Error(`Could not load example ${exampleName}: ${e}`);
}

runExample(args);
const maybePromise = runExample(args);

if (maybePromise instanceof Promise) {
maybePromise.catch((e) => {
console.error(`Example failed with ${e}`);
process.exit(1);
});
}
2 changes: 2 additions & 0 deletions examples/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"ESNext",
"DOM"
],
"target": "ES2020",
"module": "nodenext",
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"baseUrl": "./src",
Expand Down
2 changes: 1 addition & 1 deletion langchain/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
sourceType: "module",
},
plugins: ["@typescript-eslint"],
ignorePatterns: [".eslintrc.cjs", "node_modules"],
ignorePatterns: [".eslintrc.cjs", "create-entrypoints.js", "node_modules"],
rules: {
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/no-empty-function": 0,
Expand Down
1 change: 1 addition & 0 deletions test-exports/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"ESNext",
"DOM"
],
"module": "nodenext",
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"baseUrl": "./",
Expand Down
Loading

0 comments on commit 4a640bd

Please sign in to comment.