-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples[minor]: Port langchain examples code to run ts files in exam…
…ples (#383)
- Loading branch information
1 parent
8ecfa0a
commit 8fa3478
Showing
4 changed files
with
369 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import path from "path"; | ||
import url from "url"; | ||
import { awaitAllCallbacks } from "@langchain/core/callbacks/promises"; | ||
|
||
const [exampleName, ...args] = process.argv.slice(2); | ||
|
||
if (!exampleName) { | ||
console.error("Please provide path to example to run"); | ||
process.exit(1); | ||
} | ||
|
||
// 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 = exampleName; | ||
|
||
if (exampleRelativePath.startsWith("./examples/")) { | ||
exampleRelativePath = exampleName.slice(11); | ||
} else if (exampleRelativePath.startsWith("examples/")) { | ||
exampleRelativePath = exampleName.slice(9); | ||
} | ||
|
||
if (exampleRelativePath.startsWith("./src/")) { | ||
exampleRelativePath = exampleRelativePath.slice(6); | ||
} else if (exampleRelativePath.startsWith("./dist/")) { | ||
exampleRelativePath = exampleRelativePath.slice(7); | ||
} else if (exampleRelativePath.startsWith("src/")) { | ||
exampleRelativePath = exampleRelativePath.slice(4); | ||
} else if (exampleRelativePath.startsWith("dist/")) { | ||
exampleRelativePath = exampleRelativePath.slice(5); | ||
} | ||
|
||
let runExample; | ||
try { | ||
({ run: runExample } = await import( | ||
path.join( | ||
path.dirname(url.fileURLToPath(import.meta.url)), | ||
exampleRelativePath | ||
) | ||
)); | ||
} catch (e) { | ||
console.log(e); | ||
throw new Error(`Could not load example ${exampleName}: ${e}`); | ||
} | ||
|
||
if (runExample) { | ||
const maybePromise = runExample(args); | ||
|
||
if (maybePromise instanceof Promise) { | ||
maybePromise | ||
.catch((e) => { | ||
console.error(`Example failed with:`); | ||
console.error(e); | ||
}) | ||
.finally(() => awaitAllCallbacks()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"extends": "@tsconfig/recommended", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"lib": [ | ||
"ES2021", | ||
"ES2022.Object", | ||
"DOM" | ||
], | ||
"target": "ES2021", | ||
"module": "nodenext", | ||
"sourceMap": true, | ||
"allowSyntheticDefaultImports": true, | ||
"baseUrl": "./src", | ||
"declaration": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noUnusedParameters": true, | ||
"useDefineForClassFields": true, | ||
"strictPropertyInitialization": false | ||
}, | ||
"exclude": [ | ||
"node_modules/", | ||
"dist/", | ||
"tests/" | ||
], | ||
"include": [ | ||
"*.ts", | ||
"./src" | ||
] | ||
} |
Oops, something went wrong.