From c7cb79367930e9e6b358a84e328140c8bc62db6d Mon Sep 17 00:00:00 2001 From: David Illing Date: Sun, 3 Dec 2023 20:12:53 -0500 Subject: [PATCH 01/16] add RunnableMapLike to infer RunnableMap output --- langchain-core/src/runnables/base.ts | 25 +++++++++++-------- .../src/runnables/tests/runnable.test.ts | 12 +++++---- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index bd97724a15a0..73ac8df02aa7 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -31,11 +31,15 @@ export type RunnableFunc = ( | (Record & { config: RunnableConfig }) ) => RunOutput | Promise; +export type RunnableMapLike> = { + [K in keyof RunOutput]: RunnableLike +} + // eslint-disable-next-line @typescript-eslint/no-explicit-any export type RunnableLike = | Runnable | RunnableFunc - | { [key: string]: RunnableLike }; + | RunnableMapLike & RunOutput>; export type RunnableBatchOptions = { maxConcurrency?: number; @@ -1404,10 +1408,9 @@ export class RunnableSequence< * const result = await mapChain.invoke({ topic: "bear" }); * ``` */ -export class RunnableMap extends Runnable< +export class RunnableMap = Record> extends Runnable< RunInput, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Record + RunOutput > { static lc_name() { return "RunnableMap"; @@ -1423,7 +1426,7 @@ export class RunnableMap extends Runnable< return Object.keys(this.steps); } - constructor(fields: { steps: Record> }) { + constructor(fields: { steps: RunnableMapLike }) { super(fields); this.steps = {}; for (const [key, value] of Object.entries(fields.steps)) { @@ -1431,15 +1434,17 @@ export class RunnableMap extends Runnable< } } - static from(steps: Record>) { - return new RunnableMap({ steps }); + static from = Record>( + steps: RunnableMapLike + ): RunnableMap { + return new RunnableMap({ steps }); } async invoke( input: RunInput, options?: Partial // eslint-disable-next-line @typescript-eslint/no-explicit-any - ): Promise> { + ): Promise { const callbackManager_ = await getCallbackMangerForConfig(options); const runManager = await callbackManager_?.handleChainStart( this.toJSON(), @@ -1468,7 +1473,7 @@ export class RunnableMap extends Runnable< throw e; } await runManager?.handleChainEnd(output); - return output; + return output as RunOutput; } } @@ -1701,7 +1706,7 @@ export function _coerceToRunnable( for (const [key, value] of Object.entries(coerceable)) { runnables[key] = _coerceToRunnable(value); } - return new RunnableMap({ + return new RunnableMap({ steps: runnables, }) as unknown as Runnable>; } else { diff --git a/langchain-core/src/runnables/tests/runnable.test.ts b/langchain-core/src/runnables/tests/runnable.test.ts index 149a39112c4f..e25cece5eac8 100644 --- a/langchain-core/src/runnables/tests/runnable.test.ts +++ b/langchain-core/src/runnables/tests/runnable.test.ts @@ -22,7 +22,7 @@ import { FakeRunnable, FakeListChatModel, } from "../../utils/testing/index.js"; -import { RunnableSequence, RunnableMap, RunnableLambda } from "../base.js"; +import { RunnableSequence, RunnableMap, RunnableLambda, _coerceToRunnable } from "../base.js"; import { RouterRunnable } from "../router.js"; import { Document } from "../../documents/document.js"; @@ -80,16 +80,18 @@ test("Create a runnable sequence with a runnable map", async () => { `Context:\n{documents}\n\nQuestion:\n{question}` ), ]); + const llm = new FakeChatModel({}); - const inputs = { - question: (input: string) => input, + const inputs = RunnableMap.from({ + question: ((input: string) => input), documents: RunnableSequence.from([ new FakeRetriever(), (docs: Document[]) => JSON.stringify(docs), ]), extraField: new FakeLLM({}), - }; - const runnable = new RunnableMap({ steps: inputs }) + }); + + const runnable = inputs .pipe(promptTemplate) .pipe(llm); const result = await runnable.invoke("Do you know the Muffin Man?"); From d6cfaae931df79a15237e91890c3f7b1c39a2cb6 Mon Sep 17 00:00:00 2001 From: David Illing Date: Sun, 3 Dec 2023 20:19:03 -0500 Subject: [PATCH 02/16] remove unneeded changes --- langchain-core/src/runnables/tests/runnable.test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/langchain-core/src/runnables/tests/runnable.test.ts b/langchain-core/src/runnables/tests/runnable.test.ts index e25cece5eac8..71d986683d83 100644 --- a/langchain-core/src/runnables/tests/runnable.test.ts +++ b/langchain-core/src/runnables/tests/runnable.test.ts @@ -22,7 +22,7 @@ import { FakeRunnable, FakeListChatModel, } from "../../utils/testing/index.js"; -import { RunnableSequence, RunnableMap, RunnableLambda, _coerceToRunnable } from "../base.js"; +import { RunnableSequence, RunnableMap, RunnableLambda } from "../base.js"; import { RouterRunnable } from "../router.js"; import { Document } from "../../documents/document.js"; @@ -80,17 +80,15 @@ test("Create a runnable sequence with a runnable map", async () => { `Context:\n{documents}\n\nQuestion:\n{question}` ), ]); - const llm = new FakeChatModel({}); const inputs = RunnableMap.from({ - question: ((input: string) => input), + question: (input: string) => input, documents: RunnableSequence.from([ new FakeRetriever(), (docs: Document[]) => JSON.stringify(docs), ]), extraField: new FakeLLM({}), }); - const runnable = inputs .pipe(promptTemplate) .pipe(llm); From eab845741ced8c40ef5ac32b87030cb0fbc4ac99 Mon Sep 17 00:00:00 2001 From: David Illing Date: Sun, 3 Dec 2023 20:22:08 -0500 Subject: [PATCH 03/16] fix linting --- langchain-core/src/runnables/base.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index 73ac8df02aa7..d9a596da4507 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -31,6 +31,7 @@ export type RunnableFunc = ( | (Record & { config: RunnableConfig }) ) => RunOutput | Promise; +// eslint-disable-next-line @typescript-eslint/no-explicit-any export type RunnableMapLike> = { [K in keyof RunOutput]: RunnableLike } @@ -39,6 +40,7 @@ export type RunnableMapLike> = { export type RunnableLike = | Runnable | RunnableFunc + // eslint-disable-next-line @typescript-eslint/no-explicit-any | RunnableMapLike & RunOutput>; export type RunnableBatchOptions = { @@ -1408,6 +1410,7 @@ export class RunnableSequence< * const result = await mapChain.invoke({ topic: "bear" }); * ``` */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any export class RunnableMap = Record> extends Runnable< RunInput, RunOutput @@ -1434,6 +1437,7 @@ export class RunnableMap = Recor } } + // eslint-disable-next-line @typescript-eslint/no-explicit-any static from = Record>( steps: RunnableMapLike ): RunnableMap { From acdab12cd84ad939a17ea6167922e597cefa48f0 Mon Sep 17 00:00:00 2001 From: David Illing Date: Sun, 3 Dec 2023 20:40:15 -0500 Subject: [PATCH 04/16] format --- langchain-core/src/runnables/base.ts | 15 +++++++++------ .../src/runnables/tests/runnable.test.ts | 4 +--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index d9a596da4507..42345b7ca380 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -33,8 +33,8 @@ export type RunnableFunc = ( // eslint-disable-next-line @typescript-eslint/no-explicit-any export type RunnableMapLike> = { - [K in keyof RunOutput]: RunnableLike -} + [K in keyof RunOutput]: RunnableLike; +}; // eslint-disable-next-line @typescript-eslint/no-explicit-any export type RunnableLike = @@ -1411,10 +1411,10 @@ export class RunnableSequence< * ``` */ // eslint-disable-next-line @typescript-eslint/no-explicit-any -export class RunnableMap = Record> extends Runnable< +export class RunnableMap< RunInput, - RunOutput -> { + RunOutput extends Record = Record +> extends Runnable { static lc_name() { return "RunnableMap"; } @@ -1438,7 +1438,10 @@ export class RunnableMap = Recor } // eslint-disable-next-line @typescript-eslint/no-explicit-any - static from = Record>( + static from< + RunInput, + RunOutput extends Record = Record + >( steps: RunnableMapLike ): RunnableMap { return new RunnableMap({ steps }); diff --git a/langchain-core/src/runnables/tests/runnable.test.ts b/langchain-core/src/runnables/tests/runnable.test.ts index 71d986683d83..9d4b639175d8 100644 --- a/langchain-core/src/runnables/tests/runnable.test.ts +++ b/langchain-core/src/runnables/tests/runnable.test.ts @@ -89,9 +89,7 @@ test("Create a runnable sequence with a runnable map", async () => { ]), extraField: new FakeLLM({}), }); - const runnable = inputs - .pipe(promptTemplate) - .pipe(llm); + const runnable = inputs.pipe(promptTemplate).pipe(llm); const result = await runnable.invoke("Do you know the Muffin Man?"); console.log(result); expect(result.content).toEqual( From 095a4e8ea125a9c5dc9f10d6a0cc6ac3953312c1 Mon Sep 17 00:00:00 2001 From: David Illing Date: Sun, 3 Dec 2023 20:47:18 -0500 Subject: [PATCH 05/16] fix runnable_stream_log.test --- .../tests/runnable_stream_log.test.ts | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/langchain-core/src/runnables/tests/runnable_stream_log.test.ts b/langchain-core/src/runnables/tests/runnable_stream_log.test.ts index 8f7c1d1e589a..29980dd89226 100644 --- a/langchain-core/src/runnables/tests/runnable_stream_log.test.ts +++ b/langchain-core/src/runnables/tests/runnable_stream_log.test.ts @@ -50,21 +50,21 @@ test("Runnable streamLog method with a more complicated sequence", async () => { new Document({ pageContent: "foo" }), new Document({ pageContent: "bar" }), ]; - const inputs = { - question: (input: string) => input, - documents: RunnableSequence.from([ - new FakeRetriever({ - output: retrieverOutputDocs, - }), - (docs: Document[]) => JSON.stringify(docs), - ]).withConfig({ runName: "CUSTOM_NAME" }), - extraField: new FakeLLM({ - response: "testing", - }).withConfig({ tags: ["only_one"] }), - }; - const runnable = new RunnableMap({ steps: inputs }) - .pipe(promptTemplate) - .pipe(llm); + const inputs = new RunnableMap({ + steps: { + question: (input: string) => input, + documents: RunnableSequence.from([ + new FakeRetriever({ + output: retrieverOutputDocs, + }), + (docs: Document[]) => JSON.stringify(docs), + ]).withConfig({ runName: "CUSTOM_NAME" }), + extraField: new FakeLLM({ + response: "testing", + }).withConfig({ tags: ["only_one"] }), + }, + }); + const runnable = inputs.pipe(promptTemplate).pipe(llm); const stream = await runnable.streamLog( "Do you know the Muffin Man?", {}, From 723e2edc6f277a98e6fe3e63693bfe75f22c9f29 Mon Sep 17 00:00:00 2001 From: David Illing Date: Tue, 5 Dec 2023 23:17:04 -0500 Subject: [PATCH 06/16] upgrade typescript version --- docs/api_refs/package.json | 2 +- examples/package.json | 2 +- langchain-core/package.json | 2 +- langchain/package.json | 2 +- libs/langchain-anthropic/package.json | 2 +- libs/langchain-openai/package.json | 2 +- package.json | 2 +- yarn.lock | 54 ++++----------------------- 8 files changed, 14 insertions(+), 54 deletions(-) diff --git a/docs/api_refs/package.json b/docs/api_refs/package.json index 262b3b25ec30..13a1d21c6ae9 100644 --- a/docs/api_refs/package.json +++ b/docs/api_refs/package.json @@ -25,6 +25,6 @@ "postcss": "^8", "tailwindcss": "^3.3.0", "ts-morph": "^20.0.0", - "typescript": "^5" + "typescript": "^5.1.6" } } diff --git a/examples/package.json b/examples/package.json index 10bbc61b2be3..03a979cfe5e0 100644 --- a/examples/package.json +++ b/examples/package.json @@ -78,6 +78,6 @@ "eslint-plugin-unused-imports": "^3.0.0", "prettier": "^2.8.3", "tsx": "^3.12.3", - "typescript": "^5.0.0" + "typescript": "^5.1.6" } } diff --git a/langchain-core/package.json b/langchain-core/package.json index 1927a865c10c..ab6f879eb6cf 100644 --- a/langchain-core/package.json +++ b/langchain-core/package.json @@ -61,7 +61,7 @@ "prettier": "^2.8.3", "release-it": "^15.10.1", "rimraf": "^5.0.1", - "typescript": "^5.0.0" + "typescript": "^5.1.6" }, "publishConfig": { "access": "public" diff --git a/langchain/package.json b/langchain/package.json index 3d5e56386799..dcfd1184041e 100644 --- a/langchain/package.json +++ b/langchain/package.json @@ -1000,7 +1000,7 @@ "srt-parser-2": "^1.2.2", "ts-jest": "^29.1.0", "typeorm": "^0.3.12", - "typescript": "^5.0.0", + "typescript": "^5.1.6", "typesense": "^1.5.3", "usearch": "^1.1.1", "vectordb": "^0.1.4", diff --git a/libs/langchain-anthropic/package.json b/libs/langchain-anthropic/package.json index 1dc1841c789f..c064a31c01d8 100644 --- a/libs/langchain-anthropic/package.json +++ b/libs/langchain-anthropic/package.json @@ -55,7 +55,7 @@ "prettier": "^2.8.3", "release-it": "^15.10.1", "rimraf": "^5.0.1", - "typescript": "^5.0.0" + "typescript": "^5.1.6" }, "publishConfig": { "access": "public" diff --git a/libs/langchain-openai/package.json b/libs/langchain-openai/package.json index 9c55ce2f4e66..e6654ae6153b 100644 --- a/libs/langchain-openai/package.json +++ b/libs/langchain-openai/package.json @@ -57,7 +57,7 @@ "prettier": "^2.8.3", "release-it": "^15.10.1", "rimraf": "^5.0.1", - "typescript": "^5.0.0" + "typescript": "^5.1.6" }, "publishConfig": { "access": "public" diff --git a/package.json b/package.json index af0779770ea1..4f801b19dc9e 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "dotenv": "^16.0.3", "lint-staged": "^13.1.1", "prettier": "^2.8.3", - "typescript": "^5.0.0" + "typescript": "^5.1.6" }, "dependencies": { "turbo": "latest" diff --git a/yarn.lock b/yarn.lock index 4a4b0fe8b086..5c14be0912d4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7993,7 +7993,7 @@ __metadata: prettier: ^2.8.3 release-it: ^15.10.1 rimraf: ^5.0.1 - typescript: ^5.0.0 + typescript: ^5.1.6 languageName: unknown linkType: soft @@ -8024,7 +8024,7 @@ __metadata: prettier: ^2.8.3 release-it: ^15.10.1 rimraf: ^5.0.1 - typescript: ^5.0.0 + typescript: ^5.1.6 uuid: ^9.0.0 zod: ^3.22.3 languageName: unknown @@ -8053,7 +8053,7 @@ __metadata: prettier: ^2.8.3 release-it: ^15.10.1 rimraf: ^5.0.1 - typescript: ^5.0.0 + typescript: ^5.1.6 zod-to-json-schema: 3.20.3 languageName: unknown linkType: soft @@ -13087,7 +13087,7 @@ __metadata: react-dom: ^18 tailwindcss: ^3.3.0 ts-morph: ^20.0.0 - typescript: ^5 + typescript: ^5.1.6 languageName: unknown linkType: soft @@ -18058,7 +18058,7 @@ __metadata: sqlite3: ^5.1.4 tsx: ^3.12.3 typeorm: ^0.3.12 - typescript: ^5.0.0 + typescript: ^5.1.6 typesense: ^1.5.3 uuid: ^9.0.0 vectordb: ^0.1.4 @@ -22728,7 +22728,7 @@ __metadata: srt-parser-2: ^1.2.2 ts-jest: ^29.1.0 typeorm: ^0.3.12 - typescript: ^5.0.0 + typescript: ^5.1.6 typesense: ^1.5.3 usearch: ^1.1.1 uuid: ^9.0.0 @@ -23066,7 +23066,7 @@ __metadata: lint-staged: ^13.1.1 prettier: ^2.8.3 turbo: latest - typescript: ^5.0.0 + typescript: ^5.1.6 languageName: unknown linkType: soft @@ -30925,26 +30925,6 @@ __metadata: languageName: node linkType: hard -"typescript@npm:^5": - version: 5.2.2 - resolution: "typescript@npm:5.2.2" - bin: - tsc: bin/tsc - tsserver: bin/tsserver - checksum: 7912821dac4d962d315c36800fe387cdc0a6298dba7ec171b350b4a6e988b51d7b8f051317786db1094bd7431d526b648aba7da8236607febb26cf5b871d2d3c - languageName: node - linkType: hard - -"typescript@npm:^5.0.0": - version: 5.0.4 - resolution: "typescript@npm:5.0.4" - bin: - tsc: bin/tsc - tsserver: bin/tsserver - checksum: 82b94da3f4604a8946da585f7d6c3025fff8410779e5bde2855ab130d05e4fd08938b9e593b6ebed165bda6ad9292b230984f10952cf82f0a0ca07bbeaa08172 - languageName: node - linkType: hard - "typescript@npm:^5.1.6": version: 5.1.6 resolution: "typescript@npm:5.1.6" @@ -30965,26 +30945,6 @@ __metadata: languageName: node linkType: hard -"typescript@patch:typescript@^5#~builtin": - version: 5.2.2 - resolution: "typescript@patch:typescript@npm%3A5.2.2#~builtin::version=5.2.2&hash=1f5320" - bin: - tsc: bin/tsc - tsserver: bin/tsserver - checksum: 07106822b4305de3f22835cbba949a2b35451cad50888759b6818421290ff95d522b38ef7919e70fb381c5fe9c1c643d7dea22c8b31652a717ddbd57b7f4d554 - languageName: node - linkType: hard - -"typescript@patch:typescript@^5.0.0#~builtin": - version: 5.0.4 - resolution: "typescript@patch:typescript@npm%3A5.0.4#~builtin::version=5.0.4&hash=1f5320" - bin: - tsc: bin/tsc - tsserver: bin/tsserver - checksum: 6a1fe9a77bb9c5176ead919cc4a1499ee63e46b4e05bf667079f11bf3a8f7887f135aa72460a4c3b016e6e6bb65a822cb8689a6d86cbfe92d22cc9f501f09213 - languageName: node - linkType: hard - "typescript@patch:typescript@^5.1.6#~builtin": version: 5.1.6 resolution: "typescript@patch:typescript@npm%3A5.1.6#~builtin::version=5.1.6&hash=1f5320" From 102042bc4aecb5446e3d42ec5cc70f599ade3202 Mon Sep 17 00:00:00 2001 From: David Illing Date: Tue, 5 Dec 2023 23:20:25 -0500 Subject: [PATCH 07/16] clean types --- langchain-core/src/runnables/base.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index 42345b7ca380..dfabe6ce930a 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -32,7 +32,7 @@ export type RunnableFunc = ( ) => RunOutput | Promise; // eslint-disable-next-line @typescript-eslint/no-explicit-any -export type RunnableMapLike> = { +export type RunnableMapLike = { [K in keyof RunOutput]: RunnableLike; }; @@ -41,7 +41,7 @@ export type RunnableLike = | Runnable | RunnableFunc // eslint-disable-next-line @typescript-eslint/no-explicit-any - | RunnableMapLike & RunOutput>; + | RunnableMapLike export type RunnableBatchOptions = { maxConcurrency?: number; @@ -1711,7 +1711,7 @@ export function _coerceToRunnable( } else if (!Array.isArray(coerceable) && typeof coerceable === "object") { const runnables: Record> = {}; for (const [key, value] of Object.entries(coerceable)) { - runnables[key] = _coerceToRunnable(value); + runnables[key] = _coerceToRunnable(value as RunnableLike); } return new RunnableMap({ steps: runnables, From 954a14f2092f704342c955d83818faf069258317 Mon Sep 17 00:00:00 2001 From: David Illing Date: Tue, 5 Dec 2023 23:31:55 -0500 Subject: [PATCH 08/16] fix structured_output_runnables.int.test --- .../src/agents/tests/structured_output_runnables.int.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/langchain/src/agents/tests/structured_output_runnables.int.test.ts b/langchain/src/agents/tests/structured_output_runnables.int.test.ts index 9624cfe517ec..b55882b711b1 100644 --- a/langchain/src/agents/tests/structured_output_runnables.int.test.ts +++ b/langchain/src/agents/tests/structured_output_runnables.int.test.ts @@ -97,7 +97,7 @@ test("Pass custom structured output parsers", async () => { /** Create the runnable */ const runnableAgent = RunnableSequence.from([ { - input: (i: { input: string }) => i.input, + input: i => i.input, agent_scratchpad: (i: { input: string; steps: Array }) => formatForOpenAIFunctions(i.steps), }, From a8acd75f4de5c383fc6ec6bb4d4a6f0ac0995926 Mon Sep 17 00:00:00 2001 From: David Illing Date: Tue, 5 Dec 2023 23:41:09 -0500 Subject: [PATCH 09/16] ts version ~5.1.6 --- docs/api_refs/package.json | 2 +- examples/package.json | 2 +- langchain-core/package.json | 2 +- langchain/package.json | 2 +- .../create-langchain-integration/package.json | 2 +- libs/langchain-anthropic/package.json | 2 +- libs/langchain-openai/package.json | 2 +- package.json | 2 +- yarn.lock | 42 +++++++++---------- 9 files changed, 29 insertions(+), 29 deletions(-) diff --git a/docs/api_refs/package.json b/docs/api_refs/package.json index 13a1d21c6ae9..3008943d4996 100644 --- a/docs/api_refs/package.json +++ b/docs/api_refs/package.json @@ -25,6 +25,6 @@ "postcss": "^8", "tailwindcss": "^3.3.0", "ts-morph": "^20.0.0", - "typescript": "^5.1.6" + "typescript": "~5.1.6" } } diff --git a/examples/package.json b/examples/package.json index 03a979cfe5e0..f6b5afdaf9fe 100644 --- a/examples/package.json +++ b/examples/package.json @@ -78,6 +78,6 @@ "eslint-plugin-unused-imports": "^3.0.0", "prettier": "^2.8.3", "tsx": "^3.12.3", - "typescript": "^5.1.6" + "typescript": "~5.1.6" } } diff --git a/langchain-core/package.json b/langchain-core/package.json index 017a46f1d961..5b1261b66322 100644 --- a/langchain-core/package.json +++ b/langchain-core/package.json @@ -63,7 +63,7 @@ "release-it": "^15.10.1", "rimraf": "^5.0.1", "turbo": "latest", - "typescript": "^5.1.6" + "typescript": "~5.1.6" }, "publishConfig": { "access": "public" diff --git a/langchain/package.json b/langchain/package.json index 05a12a47b6ef..94fd29bb9156 100644 --- a/langchain/package.json +++ b/langchain/package.json @@ -1012,7 +1012,7 @@ "srt-parser-2": "^1.2.2", "ts-jest": "^29.1.0", "typeorm": "^0.3.12", - "typescript": "^5.1.6", + "typescript": "~5.1.6", "typesense": "^1.5.3", "usearch": "^1.1.1", "vectordb": "^0.1.4", diff --git a/libs/create-langchain-integration/package.json b/libs/create-langchain-integration/package.json index 91622e85427e..d8c9daafbc8b 100644 --- a/libs/create-langchain-integration/package.json +++ b/libs/create-langchain-integration/package.json @@ -24,7 +24,7 @@ "picocolors": "^1.0.0", "prettier": "^2.8.3", "prompts": "^2.4.2", - "typescript": "<5.2.0", + "typescript": "~5.1.6", "update-check": "^1.5.4", "validate-npm-package-name": "^5.0.0" } diff --git a/libs/langchain-anthropic/package.json b/libs/langchain-anthropic/package.json index bb84619d874d..bc302635f1a0 100644 --- a/libs/langchain-anthropic/package.json +++ b/libs/langchain-anthropic/package.json @@ -55,7 +55,7 @@ "prettier": "^2.8.3", "release-it": "^15.10.1", "rimraf": "^5.0.1", - "typescript": "^5.1.6" + "typescript": "~5.1.6" }, "publishConfig": { "access": "public" diff --git a/libs/langchain-openai/package.json b/libs/langchain-openai/package.json index 7a8a2fbc9eea..6e5aea4a189c 100644 --- a/libs/langchain-openai/package.json +++ b/libs/langchain-openai/package.json @@ -57,7 +57,7 @@ "prettier": "^2.8.3", "release-it": "^15.10.1", "rimraf": "^5.0.1", - "typescript": "^5.1.6" + "typescript": "~5.1.6" }, "publishConfig": { "access": "public" diff --git a/package.json b/package.json index 4f801b19dc9e..44b3f60cc84a 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "dotenv": "^16.0.3", "lint-staged": "^13.1.1", "prettier": "^2.8.3", - "typescript": "^5.1.6" + "typescript": "~5.1.6" }, "dependencies": { "turbo": "latest" diff --git a/yarn.lock b/yarn.lock index 111afc1421b4..c65636e4ee51 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7993,7 +7993,7 @@ __metadata: prettier: ^2.8.3 release-it: ^15.10.1 rimraf: ^5.0.1 - typescript: ^5.1.6 + typescript: ~5.1.6 languageName: unknown linkType: soft @@ -8025,7 +8025,7 @@ __metadata: release-it: ^15.10.1 rimraf: ^5.0.1 turbo: latest - typescript: ^5.1.6 + typescript: ~5.1.6 uuid: ^9.0.0 zod: ^3.22.3 languageName: unknown @@ -8054,7 +8054,7 @@ __metadata: prettier: ^2.8.3 release-it: ^15.10.1 rimraf: ^5.0.1 - typescript: ^5.1.6 + typescript: ~5.1.6 zod-to-json-schema: 3.20.3 languageName: unknown linkType: soft @@ -13112,7 +13112,7 @@ __metadata: react-dom: ^18 tailwindcss: ^3.3.0 ts-morph: ^20.0.0 - typescript: ^5.1.6 + typescript: ~5.1.6 languageName: unknown linkType: soft @@ -15586,7 +15586,7 @@ __metadata: picocolors: ^1.0.0 prettier: ^2.8.3 prompts: ^2.4.2 - typescript: <5.2.0 + typescript: ~5.1.6 update-check: ^1.5.4 validate-npm-package-name: ^5.0.0 bin: @@ -18136,7 +18136,7 @@ __metadata: sqlite3: ^5.1.4 tsx: ^3.12.3 typeorm: ^0.3.12 - typescript: ^5.1.6 + typescript: ~5.1.6 typesense: ^1.5.3 uuid: ^9.0.0 vectordb: ^0.1.4 @@ -22779,7 +22779,7 @@ __metadata: srt-parser-2: ^1.2.2 ts-jest: ^29.1.0 typeorm: ^0.3.12 - typescript: ^5.1.6 + typescript: ~5.1.6 typesense: ^1.5.3 usearch: ^1.1.1 uuid: ^9.0.0 @@ -23117,7 +23117,7 @@ __metadata: lint-staged: ^13.1.1 prettier: ^2.8.3 turbo: latest - typescript: ^5.1.6 + typescript: ~5.1.6 languageName: unknown linkType: soft @@ -30958,16 +30958,6 @@ __metadata: languageName: node linkType: hard -"typescript@npm:<5.2.0": - version: 5.1.6 - resolution: "typescript@npm:5.1.6" - bin: - tsc: bin/tsc - tsserver: bin/tsserver - checksum: b2f2c35096035fe1f5facd1e38922ccb8558996331405eb00a5111cc948b2e733163cc22fab5db46992aba7dd520fff637f2c1df4996ff0e134e77d3249a7350 - languageName: node - linkType: hard - "typescript@npm:^4.9.4": version: 4.9.5 resolution: "typescript@npm:4.9.5" @@ -30988,13 +30978,13 @@ __metadata: languageName: node linkType: hard -"typescript@patch:typescript@<5.2.0#~builtin": +"typescript@npm:~5.1.6": version: 5.1.6 - resolution: "typescript@patch:typescript@npm%3A5.1.6#~builtin::version=5.1.6&hash=1f5320" + resolution: "typescript@npm:5.1.6" bin: tsc: bin/tsc tsserver: bin/tsserver - checksum: 21e88b0a0c0226f9cb9fd25b9626fb05b4c0f3fddac521844a13e1f30beb8f14e90bd409a9ac43c812c5946d714d6e0dee12d5d02dfc1c562c5aacfa1f49b606 + checksum: b2f2c35096035fe1f5facd1e38922ccb8558996331405eb00a5111cc948b2e733163cc22fab5db46992aba7dd520fff637f2c1df4996ff0e134e77d3249a7350 languageName: node linkType: hard @@ -31018,6 +31008,16 @@ __metadata: languageName: node linkType: hard +"typescript@patch:typescript@~5.1.6#~builtin": + version: 5.1.6 + resolution: "typescript@patch:typescript@npm%3A5.1.6#~builtin::version=5.1.6&hash=1f5320" + bin: + tsc: bin/tsc + tsserver: bin/tsserver + checksum: 21e88b0a0c0226f9cb9fd25b9626fb05b4c0f3fddac521844a13e1f30beb8f14e90bd409a9ac43c812c5946d714d6e0dee12d5d02dfc1c562c5aacfa1f49b606 + languageName: node + linkType: hard + "typesense@npm:^1.5.3": version: 1.5.3 resolution: "typesense@npm:1.5.3" From 7652bf2f1cd6e8a7cf80cf6b21d5f0446a4f9893 Mon Sep 17 00:00:00 2001 From: David Illing Date: Wed, 6 Dec 2023 08:08:12 -0500 Subject: [PATCH 10/16] remove unused eslint-disable-next-line --- langchain-core/src/runnables/base.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index 0f36d9d9afe7..84b517a4e95c 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -40,7 +40,6 @@ export type RunnableMapLike = { export type RunnableLike = | Runnable | RunnableFunc - // eslint-disable-next-line @typescript-eslint/no-explicit-any | RunnableMapLike export type RunnableBatchOptions = { From 5cb1edde98d9f1c5924800f85efcbe7ddb257fa9 Mon Sep 17 00:00:00 2001 From: David Illing Date: Wed, 6 Dec 2023 09:00:17 -0500 Subject: [PATCH 11/16] remove another disable no-explicit-any --- langchain-core/src/runnables/base.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index 84b517a4e95c..b4f4e2ca2c06 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -31,7 +31,6 @@ export type RunnableFunc = ( | (Record & { config: RunnableConfig }) ) => RunOutput | Promise; -// eslint-disable-next-line @typescript-eslint/no-explicit-any export type RunnableMapLike = { [K in keyof RunOutput]: RunnableLike; }; From d07ce12df6a00b582f3acda7cfe39ae4635c965a Mon Sep 17 00:00:00 2001 From: David Illing Date: Wed, 6 Dec 2023 09:15:59 -0500 Subject: [PATCH 12/16] remove another no-explicit-any --- langchain-core/src/runnables/base.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index b4f4e2ca2c06..0bb797d1eaf2 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -1412,7 +1412,6 @@ export class RunnableMap< async invoke( input: RunInput, options?: Partial - // eslint-disable-next-line @typescript-eslint/no-explicit-any ): Promise { const callbackManager_ = await getCallbackMangerForConfig(options); const runManager = await callbackManager_?.handleChainStart( From 78265624e3dc452880363f04904aa395f66983b1 Mon Sep 17 00:00:00 2001 From: David Illing Date: Sat, 9 Dec 2023 12:39:45 -0500 Subject: [PATCH 13/16] move eslint --- langchain-core/src/runnables/base.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index 0bb797d1eaf2..4e6f167808b7 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -1372,9 +1372,9 @@ export class RunnableSequence< * const result = await mapChain.invoke({ topic: "bear" }); * ``` */ -// eslint-disable-next-line @typescript-eslint/no-explicit-any export class RunnableMap< RunInput, + // eslint-disable-next-line @typescript-eslint/no-explicit-any RunOutput extends Record = Record > extends Runnable { static lc_name() { @@ -1399,9 +1399,9 @@ export class RunnableMap< } } - // eslint-disable-next-line @typescript-eslint/no-explicit-any static from< RunInput, + // eslint-disable-next-line @typescript-eslint/no-explicit-any RunOutput extends Record = Record >( steps: RunnableMapLike @@ -1670,9 +1670,9 @@ export function _coerceToRunnable( } else if (Runnable.isRunnable(coerceable)) { return coerceable as Runnable>; } else if (!Array.isArray(coerceable) && typeof coerceable === "object") { - const runnables: Record> = {}; +const runnables: Record> = {}; for (const [key, value] of Object.entries(coerceable)) { - runnables[key] = _coerceToRunnable(value as RunnableLike); + runnables[key] = _coerceToRunnable(value as RunnableLike); } return new RunnableMap({ steps: runnables, From 602be5049b537ba216efa454da6fcf25233aa687 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Tue, 12 Dec 2023 14:24:41 -0800 Subject: [PATCH 14/16] Format --- langchain-core/src/runnables/base.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index f07a73879fb5..683187212b12 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -39,7 +39,7 @@ export type RunnableMapLike = { export type RunnableLike = | Runnable | RunnableFunc - | RunnableMapLike + | RunnableMapLike; export type RunnableBatchOptions = { maxConcurrency?: number; @@ -1672,9 +1672,9 @@ export function _coerceToRunnable( } else if (Runnable.isRunnable(coerceable)) { return coerceable as Runnable>; } else if (!Array.isArray(coerceable) && typeof coerceable === "object") { -const runnables: Record> = {}; + const runnables: Record> = {}; for (const [key, value] of Object.entries(coerceable)) { - runnables[key] = _coerceToRunnable(value as RunnableLike); + runnables[key] = _coerceToRunnable(value as RunnableLike); } return new RunnableMap({ steps: runnables, From a81428b685a5a8c088280147dc7bf9f898e9dc5a Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Tue, 12 Dec 2023 17:45:55 -0800 Subject: [PATCH 15/16] Default runnable maps to any type in case inference is not possible --- langchain-core/src/runnables/base.ts | 3 ++- .../tests/runnable_stream_log.test.ts | 26 ++++++++++--------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index 683187212b12..8504b24fdedd 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -1373,7 +1373,8 @@ export class RunnableSequence< * ``` */ export class RunnableMap< - RunInput, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + RunInput = any, // eslint-disable-next-line @typescript-eslint/no-explicit-any RunOutput extends Record = Record > extends Runnable { diff --git a/langchain-core/src/runnables/tests/runnable_stream_log.test.ts b/langchain-core/src/runnables/tests/runnable_stream_log.test.ts index 29980dd89226..6675edf4c27d 100644 --- a/langchain-core/src/runnables/tests/runnable_stream_log.test.ts +++ b/langchain-core/src/runnables/tests/runnable_stream_log.test.ts @@ -50,19 +50,21 @@ test("Runnable streamLog method with a more complicated sequence", async () => { new Document({ pageContent: "foo" }), new Document({ pageContent: "bar" }), ]; + const steps = { + question: (input: string) => input, + documents: RunnableSequence.from([ + new FakeRetriever({ + output: retrieverOutputDocs, + }), + (docs: Document[]) => JSON.stringify(docs), + ]).withConfig({ runName: "CUSTOM_NAME" }), + extraField: new FakeLLM({ + response: "testing", + }).withConfig({ tags: ["only_one"] }), + }; + const inputs = new RunnableMap({ - steps: { - question: (input: string) => input, - documents: RunnableSequence.from([ - new FakeRetriever({ - output: retrieverOutputDocs, - }), - (docs: Document[]) => JSON.stringify(docs), - ]).withConfig({ runName: "CUSTOM_NAME" }), - extraField: new FakeLLM({ - response: "testing", - }).withConfig({ tags: ["only_one"] }), - }, + steps, }); const runnable = inputs.pipe(promptTemplate).pipe(llm); const stream = await runnable.streamLog( From 361c250ee568bf80216405a970038a69e595a710 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Tue, 12 Dec 2023 18:04:25 -0800 Subject: [PATCH 16/16] Add tests --- .../src/runnables/tests/runnable.test.ts | 32 +----- .../src/runnables/tests/runnable_map.test.ts | 105 ++++++++++++++++++ .../tests/runnable_stream_log.test.ts | 11 +- 3 files changed, 112 insertions(+), 36 deletions(-) create mode 100644 langchain-core/src/runnables/tests/runnable_map.test.ts diff --git a/langchain-core/src/runnables/tests/runnable.test.ts b/langchain-core/src/runnables/tests/runnable.test.ts index 9d4b639175d8..647df66282e2 100644 --- a/langchain-core/src/runnables/tests/runnable.test.ts +++ b/langchain-core/src/runnables/tests/runnable.test.ts @@ -10,21 +10,18 @@ import { StringOutputParser } from "../../output_parsers/string.js"; import { ChatPromptTemplate, SystemMessagePromptTemplate, - HumanMessagePromptTemplate, } from "../../prompts/chat.js"; import { PromptTemplate } from "../../prompts/prompt.js"; import { FakeLLM, FakeChatModel, - FakeRetriever, FakeStreamingLLM, FakeSplitIntoListParser, FakeRunnable, FakeListChatModel, } from "../../utils/testing/index.js"; -import { RunnableSequence, RunnableMap, RunnableLambda } from "../base.js"; +import { RunnableSequence, RunnableLambda } from "../base.js"; import { RouterRunnable } from "../router.js"; -import { Document } from "../../documents/document.js"; test("Test batch", async () => { const llm = new FakeLLM({}); @@ -70,33 +67,6 @@ test("Pipe from one runnable to the next", async () => { expect(result).toBe("Hello world!"); }); -test("Create a runnable sequence with a runnable map", async () => { - const promptTemplate = ChatPromptTemplate.fromMessages<{ - documents: string; - question: string; - }>([ - SystemMessagePromptTemplate.fromTemplate(`You are a nice assistant.`), - HumanMessagePromptTemplate.fromTemplate( - `Context:\n{documents}\n\nQuestion:\n{question}` - ), - ]); - const llm = new FakeChatModel({}); - const inputs = RunnableMap.from({ - question: (input: string) => input, - documents: RunnableSequence.from([ - new FakeRetriever(), - (docs: Document[]) => JSON.stringify(docs), - ]), - extraField: new FakeLLM({}), - }); - const runnable = inputs.pipe(promptTemplate).pipe(llm); - const result = await runnable.invoke("Do you know the Muffin Man?"); - console.log(result); - expect(result.content).toEqual( - `You are a nice assistant.\nContext:\n[{"pageContent":"foo","metadata":{}},{"pageContent":"bar","metadata":{}}]\n\nQuestion:\nDo you know the Muffin Man?` - ); -}); - test("Stream the entire way through", async () => { const llm = new FakeStreamingLLM({}); const stream = await llm.pipe(new StringOutputParser()).stream("Hi there!"); diff --git a/langchain-core/src/runnables/tests/runnable_map.test.ts b/langchain-core/src/runnables/tests/runnable_map.test.ts new file mode 100644 index 000000000000..d820e53ee0ed --- /dev/null +++ b/langchain-core/src/runnables/tests/runnable_map.test.ts @@ -0,0 +1,105 @@ +/* eslint-disable no-promise-executor-return */ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +import { StringOutputParser } from "../../output_parsers/string.js"; +import { + ChatPromptTemplate, + SystemMessagePromptTemplate, + HumanMessagePromptTemplate, +} from "../../prompts/chat.js"; +import { + FakeLLM, + FakeChatModel, + FakeRetriever, +} from "../../utils/testing/index.js"; +import { RunnableSequence, RunnableMap } from "../base.js"; +import { RunnablePassthrough } from "../passthrough.js"; + +test("Create a runnable sequence with a runnable map", async () => { + const promptTemplate = ChatPromptTemplate.fromMessages<{ + documents: string; + question: string; + }>([ + SystemMessagePromptTemplate.fromTemplate(`You are a nice assistant.`), + HumanMessagePromptTemplate.fromTemplate( + `Context:\n{documents}\n\nQuestion:\n{question}` + ), + ]); + const llm = new FakeChatModel({}); + const inputs = { + question: (input: string) => input, + documents: RunnableSequence.from([ + new FakeRetriever(), + (docs: Document[]) => JSON.stringify(docs), + ]), + extraField: new FakeLLM({}), + }; + const runnable = new RunnableMap({ steps: inputs }) + .pipe(promptTemplate) + .pipe(llm); + const result = await runnable.invoke("Do you know the Muffin Man?"); + console.log(result); + expect(result.content).toEqual( + `You are a nice assistant.\nContext:\n[{"pageContent":"foo","metadata":{}},{"pageContent":"bar","metadata":{}}]\n\nQuestion:\nDo you know the Muffin Man?` + ); +}); + +test("Test map inference in a sequence", async () => { + const prompt = ChatPromptTemplate.fromTemplate( + "context: {context}, question: {question}" + ); + const chain = RunnableSequence.from([ + { + question: new RunnablePassthrough(), + context: async () => "SOME STUFF", + }, + prompt, + new FakeLLM({}), + new StringOutputParser(), + ]); + const response = await chain.invoke("Just passing through."); + console.log(response); + expect(response).toBe( + `Human: context: SOME STUFF, question: Just passing through.` + ); +}); + +test("Should not allow mismatched inputs", async () => { + const prompt = ChatPromptTemplate.fromTemplate( + "context: {context}, question: {question}" + ); + const badChain = RunnableSequence.from([ + { + // @ts-expect-error TS compiler should flag mismatched input types + question: new FakeLLM({}), + context: async (input: number) => input, + }, + prompt, + new FakeLLM({}), + new StringOutputParser(), + ]); + console.log(badChain); +}); + +test("Should not allow improper inputs into a map in a sequence", async () => { + const prompt = ChatPromptTemplate.fromTemplate( + "context: {context}, question: {question}" + ); + const map = RunnableMap.from({ + question: new FakeLLM({}), + context: async (_input: string) => 9, + }); + // @ts-expect-error TS compiler should flag mismatched output types + const runnable = prompt.pipe(map); + console.log(runnable); +}); + +test("Should not allow improper outputs from a map into the next item in a sequence", async () => { + const map = RunnableMap.from({ + question: new FakeLLM({}), + context: async (_input: string) => 9, + }); + // @ts-expect-error TS compiler should flag mismatched output types + const runnable = map.pipe(new FakeLLM({})); + console.log(runnable); +}); diff --git a/langchain-core/src/runnables/tests/runnable_stream_log.test.ts b/langchain-core/src/runnables/tests/runnable_stream_log.test.ts index 6675edf4c27d..f9ab046be648 100644 --- a/langchain-core/src/runnables/tests/runnable_stream_log.test.ts +++ b/langchain-core/src/runnables/tests/runnable_stream_log.test.ts @@ -50,7 +50,7 @@ test("Runnable streamLog method with a more complicated sequence", async () => { new Document({ pageContent: "foo" }), new Document({ pageContent: "bar" }), ]; - const steps = { + const inputs = { question: (input: string) => input, documents: RunnableSequence.from([ new FakeRetriever({ @@ -63,10 +63,11 @@ test("Runnable streamLog method with a more complicated sequence", async () => { }).withConfig({ tags: ["only_one"] }), }; - const inputs = new RunnableMap({ - steps, - }); - const runnable = inputs.pipe(promptTemplate).pipe(llm); + const runnable = new RunnableMap({ + steps: inputs, + }) + .pipe(promptTemplate) + .pipe(llm); const stream = await runnable.streamLog( "Do you know the Muffin Man?", {},