Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use different import maps for core vs main langchain #3617

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions langchain-core/src/load/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
get_lc_unique_name,
} from "./serializable.js";
import { optionalImportEntrypoints as defaultOptionalImportEntrypoints } from "./import_constants.js";
import * as defaultImportMap from "./import_map.js";
import * as coreImportMap from "./import_map.js";
import type { OptionalImportMap, SecretMap } from "./import_type.js";
import { type SerializedFields, keyFromJson, mapKeys } from "./map_keys.js";
import { getEnvironmentVariable } from "../utils/env.js";
Expand Down Expand Up @@ -98,10 +98,11 @@ async function reviver(
const str = JSON.stringify(serialized);
const [name, ...namespaceReverse] = serialized.id.slice().reverse();
const namespace = namespaceReverse.reverse();
const finalImportMap = { ...defaultImportMap, ...importMap };
const importMaps = { langchain_core: coreImportMap, langchain: importMap };

let module:
| (typeof finalImportMap)[keyof typeof finalImportMap]
| (typeof importMaps)["langchain_core"][keyof (typeof importMaps)["langchain_core"]]
| (typeof importMaps)["langchain"][keyof (typeof importMaps)["langchain"]]
| OptionalImportMap[keyof OptionalImportMap]
| null = null;

Expand Down Expand Up @@ -132,14 +133,12 @@ async function reviver(
);
}
} else {
// Currently, we only support langchain imports.
if (
namespace[0] === "langchain" ||
namespace[0] === "langchain_core" ||
namespace[0] === "langchain_community" ||
namespace[0] === "langchain_anthropic" ||
namespace[0] === "langchain_openai"
) {
let finalImportMap:
| (typeof importMaps)["langchain"]
| (typeof importMaps)["langchain_core"];
// Currently, we only support langchain and langchain_core imports.
if (namespace[0] === "langchain" || namespace[0] === "langchain_core") {
finalImportMap = importMaps[namespace[0]];
namespace.shift();
} else {
throw new Error(`Invalid namespace: ${pathStr} -> ${str}`);
Expand Down
2 changes: 2 additions & 0 deletions langchain-core/src/runnables/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,8 @@ export class RunnableLambda<RunInput, RunOutput> extends Runnable<
}
}

export class RunnableParallel<RunInput> extends RunnableMap<RunInput> {}

/**
* A Runnable that can fallback to other Runnables if it fails.
*/
Expand Down
1 change: 1 addition & 0 deletions langchain-core/src/runnables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
RunnableRetry,
RunnableSequence,
RunnableMap,
RunnableParallel,
RunnableLambda,
RunnableWithFallbacks,
_coerceToRunnable,
Expand Down
86 changes: 70 additions & 16 deletions langchain/src/load/tests/cross_language.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fileURLToPath } from "node:url";
import { readFileSync } from "node:fs";
import * as path from "node:path";

// import { load } from "../index.js";
import { load } from "../index.js";

const IMPORTANT_IMPORTS = JSON.parse(
readFileSync(
Expand All @@ -15,24 +15,78 @@ const IMPORTANT_IMPORTS = JSON.parse(
).toString()
);

const CURRENT_KNOWN_FAILURES = [
"langchain/schema/agent/AgentAction",
"langchain/schema/agent/AgentFinish",
"langchain/schema/prompt_template/BasePromptTemplate",
"langchain/schema/agent/AgentActionMessageLog",
"langchain/schema/agent/OpenAIToolAgentAction",
"langchain/prompts/chat/BaseMessagePromptTemplate",
"langchain/schema/output/ChatGeneration",
"langchain/schema/output/Generation",
"langchain/schema/document/Document",
"langchain/schema/runnable/DynamicRunnable",
"langchain/schema/prompt/PromptValue",
"langchain/llms/openai/BaseOpenAI",
"langchain/llms/openai/AzureOpenAI",
"langchain/schema/prompt_template/BaseChatPromptTemplate",
"langchain/prompts/few_shot_with_templates/FewShotPromptWithTemplates",
"langchain/prompts/base/StringPromptTemplate",
"langchain/prompts/chat/BaseStringMessagePromptTemplate",
"langchain/prompts/chat/ChatPromptValue",
"langchain/prompts/chat/ChatPromptValueConcrete",
"langchain/schema/runnable/HubRunnable",
"langchain/schema/runnable/RunnableBindingBase",
"langchain/schema/runnable/OpenAIFunctionsRouter",
"langchain/schema/runnable/RunnableEachBase",
"langchain/schema/runnable/RunnableConfigurableAlternatives",
"langchain/schema/runnable/RunnableConfigurableFields",
"langchain_core/agents/AgentAction",
"langchain_core/agents/AgentFinish",
"langchain_core/agents/AgentActionMessageLog",
"langchain/agents/output_parsers/openai_tools/OpenAIToolAgentAction",
"langchain_core/outputs/chat_generation/ChatGeneration",
"langchain_core/outputs/generation/Generation",
"langchain_core/runnables/configurable/DynamicRunnable",
"langchain_core/prompt_values/PromptValue",
"langchain/llms/openai/BaseOpenAI",
"langchain/llms/openai/AzureOpenAI",
"langchain_core/prompts/few_shot_with_templates/FewShotPromptWithTemplates",
"langchain_core/prompts/string/StringPromptTemplate",
"langchain_core/prompts/chat/BaseStringMessagePromptTemplate",
"langchain_core/prompt_values/ChatPromptValueConcrete",
"langchain/runnables/hub/HubRunnable",
"langchain_core/runnables/base/RunnableBindingBase",
"langchain/runnables/openai_functions/OpenAIFunctionsRouter",
"langchain_core/runnables/base/RunnableEachBase",
"langchain_core/runnables/configurable/RunnableConfigurableAlternatives",
"langchain_core/runnables/configurable/RunnableConfigurableFields",
];

const CROSS_LANGUAGE_ENTRYPOINTS = Object.keys(IMPORTANT_IMPORTS)
.concat(Object.values(IMPORTANT_IMPORTS))
.filter((v) => !CURRENT_KNOWN_FAILURES.includes(v));

describe("Test cross language serialization of important modules", () => {
// https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/load/mapping.py
test.each(Object.keys(IMPORTANT_IMPORTS))(
test.each(CROSS_LANGUAGE_ENTRYPOINTS)(
"Test matching serialization names for: %s",
async (_item) => {
// const idComponents = item.split("/");
// const mockItem = {
// lc: 1,
// type: "constructor",
// id: idComponents,
// kwargs: {}
// };
// try {
// const result = await load(JSON.stringify(mockItem)) as any;
// expect(result.constructor.name).toEqual(idComponents[idComponents.length - 1]);
// } catch (e: any) {
// expect(e.message).not.toContain("Invalid identifer: $");
// }
async (item) => {
const idComponents = item.split("/");
const mockItem = {
lc: 1,
type: "constructor",
id: idComponents,
kwargs: {},
};
try {
const result = (await load(JSON.stringify(mockItem))) as any;
expect(result.constructor.name).toEqual(
idComponents[idComponents.length - 1]
);
} catch (e: any) {
expect(e.message).not.toContain("Invalid identifer: $");
}
}
);
});
14 changes: 14 additions & 0 deletions langchain/src/load/tests/load.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { test, expect } from "@jest/globals";
import { RunnableSequence } from "@langchain/core/runnables";

import { load } from "../index.js";

test("Should load and invoke real-world serialized chain", async () => {
const serializedValue = `{"lc": 1, "type": "constructor", "id": ["langchain_core", "runnables", "RunnableSequence"], "kwargs": {"first": {"lc": 1, "type": "constructor", "id": ["langchain_core", "runnables", "RunnableParallel"], "kwargs": {"steps": {"equation_statement": {"lc": 1, "type": "constructor", "id": ["langchain_core", "runnables", "RunnablePassthrough"], "kwargs": {"func": null, "afunc": null, "input_type": null}}}}}, "middle": [{"lc": 1, "type": "constructor", "id": ["langchain_core", "prompts", "chat", "ChatPromptTemplate"], "kwargs": {"input_variables": ["equation_statement"], "messages": [{"lc": 1, "type": "constructor", "id": ["langchain_core", "prompts", "chat", "SystemMessagePromptTemplate"], "kwargs": {"prompt": {"lc": 1, "type": "constructor", "id": ["langchain_core", "prompts", "prompt", "PromptTemplate"], "kwargs": {"input_variables": [], "template": "Write out the following equation using algebraic symbols then solve it. Use the format\\n\\nEQUATION:...\\nSOLUTION:...\\n\\n", "template_format": "f-string", "partial_variables": {}}}}}, {"lc": 1, "type": "constructor", "id": ["langchain_core", "prompts", "chat", "HumanMessagePromptTemplate"], "kwargs": {"prompt": {"lc": 1, "type": "constructor", "id": ["langchain_core", "prompts", "prompt", "PromptTemplate"], "kwargs": {"input_variables": ["equation_statement"], "template": "{equation_statement}", "template_format": "f-string", "partial_variables": {}}}}}]}}, {"lc": 1, "type": "constructor", "id": ["langchain", "chat_models", "openai", "ChatOpenAI"], "kwargs": {"temperature": 0.0, "openai_api_key": {"lc": 1, "type": "secret", "id": ["OPENAI_API_KEY"]}}}], "last": {"lc": 1, "type": "constructor", "id": ["langchain_core", "output_parsers", "string", "StrOutputParser"], "kwargs": {}}}}`;
const chain = await load<RunnableSequence>(serializedValue);
const result = await chain.invoke(
"x raised to the third plus seven equals 12"
);
console.log(result);
expect(typeof result).toBe("string");
});
17 changes: 17 additions & 0 deletions langchain/src/load/tests/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,20 @@ test("Should load traces even if the constructor name changes (minified environm
console.log(JSON.stringify(llm2, null, 2));
expect(JSON.stringify(llm2, null, 2)).toBe(str);
});

test("Should load a real-world serialized chain", async () => {
const serializedValue = `{"lc": 1, "type": "constructor", "id": ["langchain_core", "runnables", "RunnableSequence"], "kwargs": {"first": {"lc": 1, "type": "constructor", "id": ["langchain_core", "runnables", "RunnableParallel"], "kwargs": {"steps": {"equation_statement": {"lc": 1, "type": "constructor", "id": ["langchain_core", "runnables", "RunnablePassthrough"], "kwargs": {"func": null, "afunc": null, "input_type": null}}}}}, "middle": [{"lc": 1, "type": "constructor", "id": ["langchain_core", "prompts", "chat", "ChatPromptTemplate"], "kwargs": {"input_variables": ["equation_statement"], "messages": [{"lc": 1, "type": "constructor", "id": ["langchain_core", "prompts", "chat", "SystemMessagePromptTemplate"], "kwargs": {"prompt": {"lc": 1, "type": "constructor", "id": ["langchain_core", "prompts", "prompt", "PromptTemplate"], "kwargs": {"input_variables": [], "template": "Write out the following equation using algebraic symbols then solve it. Use the format\\n\\nEQUATION:...\\nSOLUTION:...\\n\\n", "template_format": "f-string", "partial_variables": {}}}}}, {"lc": 1, "type": "constructor", "id": ["langchain_core", "prompts", "chat", "HumanMessagePromptTemplate"], "kwargs": {"prompt": {"lc": 1, "type": "constructor", "id": ["langchain_core", "prompts", "prompt", "PromptTemplate"], "kwargs": {"input_variables": ["equation_statement"], "template": "{equation_statement}", "template_format": "f-string", "partial_variables": {}}}}}]}}, {"lc": 1, "type": "constructor", "id": ["langchain", "chat_models", "openai", "ChatOpenAI"], "kwargs": {"temperature": 0.0, "openai_api_key": {"lc": 1, "type": "secret", "id": ["OPENAI_API_KEY"]}}}], "last": {"lc": 1, "type": "constructor", "id": ["langchain_core", "output_parsers", "string", "StrOutputParser"], "kwargs": {}}}}`;
const chain = await load<RunnableSequence>(serializedValue, {
OPENAI_API_KEY: "openai-key",
});
// @ts-expect-error testing
expect(chain.first.constructor.lc_name()).toBe("RunnableMap");
// @ts-expect-error testing
expect(chain.middle.length).toBe(2);
// @ts-expect-error testing
expect(chain.middle[0].constructor.lc_name()).toBe(`ChatPromptTemplate`);
// @ts-expect-error testing
expect(chain.middle[1].constructor.lc_name()).toBe(`ChatOpenAI`);
// @ts-expect-error testing
expect(chain.last.constructor.lc_name()).toBe(`StrOutputParser`);
});
1 change: 1 addition & 0 deletions langchain/src/schema/runnable/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
RunnableRetry,
RunnableSequence,
RunnableMap,
RunnableParallel,
RunnableLambda,
RunnableWithFallbacks,
_coerceToRunnable,
Expand Down