forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix example runner (langchain-ai#164)
* Fix example runner * Improve example runner, add tsx for running without building first, add catch to promise returned by example
- Loading branch information
Showing
7 changed files
with
349 additions
and
7 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,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` |
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 |
---|---|---|
@@ -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); | ||
}); | ||
} |
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
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
Oops, something went wrong.