Skip to content

Commit

Permalink
examples[minor]: Port langchain examples code to run ts files in exam…
Browse files Browse the repository at this point in the history
…ples (#383)
  • Loading branch information
bracesproul authored Aug 26, 2024
1 parent 8ecfa0a commit 8fa3478
Show file tree
Hide file tree
Showing 4 changed files with 369 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"name": "examples",
"type": "module",
"description": "Dependencies for LangGraph examples.",
"scripts": {
"start": "tsx --experimental-wasm-modules -r dotenv/config src/index.ts"
},
"devDependencies": {
"@langchain/anthropic": "^0.2.15",
"@langchain/cloudflare": "^0.0.7",
Expand All @@ -20,6 +24,7 @@
"langchain": "^0.2.16",
"pg": "^8.11.0",
"tslab": "^1.0.22",
"tsx": "^4.18.0",
"uuid": "^10.0.0",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.23.2"
Expand Down
56 changes: 56 additions & 0 deletions examples/src/index.ts
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());
}
}
31 changes: 31 additions & 0 deletions examples/tsconfig.json
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"
]
}
Loading

0 comments on commit 8fa3478

Please sign in to comment.