From c0e6aee4331dc66585be088c35cdaf552976250b Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Wed, 22 Nov 2023 20:47:40 +0000 Subject: [PATCH 1/6] Support both old and new serialization ids, update prompt base class as example --- .vscode/settings.json | 1 + langchain-core/src/load/index.ts | 2 +- langchain-core/src/prompts/base.ts | 2 +- .../tests/__snapshots__/load.test.ts.snap | 38 +++++++++++++------ langchain/src/load/tests/load.test.ts | 24 +++++++++++- 5 files changed, 52 insertions(+), 15 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1049252fccad..76668924271e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "eslint.workingDirectories": [ "./langchain", + "./langchain-core", "./examples", "./docs", "./test-exports-vercel" diff --git a/langchain-core/src/load/index.ts b/langchain-core/src/load/index.ts index 56eef709bacb..68f713f0ce14 100644 --- a/langchain-core/src/load/index.ts +++ b/langchain-core/src/load/index.ts @@ -123,7 +123,7 @@ async function reviver( } } else { // Currently, we only support langchain imports. - if (namespace[0] === "langchain") { + if (namespace[0] === "langchain" || namespace[0] === "langchain_core") { namespace.shift(); } else { throw new Error(`Invalid namespace: ${pathStr} -> ${str}`); diff --git a/langchain-core/src/prompts/base.ts b/langchain-core/src/prompts/base.ts index 0c9928a24d81..a02a14367039 100644 --- a/langchain-core/src/prompts/base.ts +++ b/langchain-core/src/prompts/base.ts @@ -60,7 +60,7 @@ export abstract class BasePromptTemplate< lc_serializable = true; - lc_namespace = ["langchain", "prompts", this._getPromptType()]; + lc_namespace = ["langchain_core", "prompts", this._getPromptType()]; get lc_attributes(): SerializedFields | undefined { return { diff --git a/langchain/src/load/tests/__snapshots__/load.test.ts.snap b/langchain/src/load/tests/__snapshots__/load.test.ts.snap index e2af9a3df866..2cc724278886 100644 --- a/langchain/src/load/tests/__snapshots__/load.test.ts.snap +++ b/langchain/src/load/tests/__snapshots__/load.test.ts.snap @@ -114,7 +114,7 @@ kwargs: lc: 1 type: constructor id: - - langchain + - langchain_core - prompts - chat - ChatPromptTemplate @@ -134,7 +134,7 @@ kwargs: lc: 1 type: constructor id: - - langchain + - langchain_core - prompts - prompt - PromptTemplate @@ -155,7 +155,7 @@ kwargs: lc: 1 type: constructor id: - - langchain + - langchain_core - prompts - prompt - PromptTemplate @@ -195,7 +195,7 @@ kwargs: lc: 1 type: not_implemented id: - - langchain + - langchain_core - prompts - few_shot - FewShotPromptTemplate @@ -231,7 +231,7 @@ kwargs: lc: 1 type: not_implemented id: - - langchain + - langchain_core - prompts - few_shot - FewShotPromptTemplate @@ -267,7 +267,7 @@ kwargs: lc: 1 type: constructor id: - - langchain + - langchain_core - prompts - prompt - PromptTemplate @@ -326,7 +326,7 @@ kwargs: lc: 1 type: constructor id: - - langchain + - langchain_core - prompts - prompt - PromptTemplate @@ -412,7 +412,7 @@ kwargs: lc: 1 type: constructor id: - - langchain + - langchain_core - prompts - prompt - PromptTemplate @@ -484,7 +484,7 @@ kwargs: lc: 1 type: constructor id: - - langchain + - langchain_core - prompts - prompt - PromptTemplate @@ -535,7 +535,7 @@ kwargs: lc: 1 type: constructor id: - - langchain + - langchain_core - prompts - prompt - PromptTemplate @@ -592,7 +592,7 @@ kwargs: lc: 1 type: constructor id: - - langchain + - langchain_core - prompts - prompt - PromptTemplate @@ -612,3 +612,19 @@ kwargs: - StructuredOutputParser " `; + +exports[`serialize + deserialize with new and old ids 1`] = ` +"lc: 1 +type: constructor +id: + - langchain_core + - prompts + - prompt + - PromptTemplate +kwargs: + input_variables: + - name + template_format: f-string + template: Hello, {name}! +" +`; diff --git a/langchain/src/load/tests/load.test.ts b/langchain/src/load/tests/load.test.ts index 70dc04873455..0a8a610729dc 100644 --- a/langchain/src/load/tests/load.test.ts +++ b/langchain/src/load/tests/load.test.ts @@ -213,6 +213,26 @@ test("serialize + deserialize llm chain string prompt", async () => { expect(JSON.stringify(chain2, null, 2)).toBe(str); }); +test("serialize + deserialize with new and old ids", async () => { + const prompt = PromptTemplate.fromTemplate("Hello, {name}!"); + const strWithNewId = JSON.stringify(prompt, null, 2); + expect(stringify(JSON.parse(strWithNewId))).toMatchSnapshot(); + expect(JSON.parse(strWithNewId).id).toEqual([ + "langchain_core", + "prompts", + "prompt", + "PromptTemplate", + ]); + const strWithOldId = JSON.stringify({ + ...JSON.parse(strWithNewId), + id: ["langchain", "prompts", "prompt", "PromptTemplate"], + }); + const prompt2 = await load(strWithOldId); + expect(prompt2).toBeInstanceOf(PromptTemplate); + const prompt3 = await load(strWithNewId); + expect(prompt3).toBeInstanceOf(PromptTemplate); +}); + test("serialize + deserialize llm chain chat prompt", async () => { // eslint-disable-next-line no-process-env process.env.OPENAI_API_KEY = undefined; @@ -267,7 +287,7 @@ test("serialize + deserialize llm chain few shot prompt w/ examples", async () = OPENAI_API_KEY: "openai-key", }) ).rejects.toThrowError( - 'Trying to load an object that doesn\'t implement serialization: $.kwargs.prompt -> {"lc":1,"type":"not_implemented","id":["langchain","prompts","few_shot","FewShotPromptTemplate"]}' + 'Trying to load an object that doesn\'t implement serialization: $.kwargs.prompt -> {"lc":1,"type":"not_implemented","id":["langchain_core","prompts","few_shot","FewShotPromptTemplate"]}' ); }); @@ -296,7 +316,7 @@ test("serialize + deserialize llm chain few shot prompt w/ selector", async () = OPENAI_API_KEY: "openai-key", }) ).rejects.toThrow( - 'Trying to load an object that doesn\'t implement serialization: $.kwargs.prompt -> {"lc":1,"type":"not_implemented","id":["langchain","prompts","few_shot","FewShotPromptTemplate"]}' + 'Trying to load an object that doesn\'t implement serialization: $.kwargs.prompt -> {"lc":1,"type":"not_implemented","id":["langchain_core","prompts","few_shot","FewShotPromptTemplate"]}' ); }); From 351a8685003f2dd175fd4ed334cfa1ca1a0cab35 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Wed, 22 Nov 2023 12:58:10 -0800 Subject: [PATCH 2/6] More namespace changes --- langchain-core/src/runnables/base.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index 0983151dd2ba..83c8e8966549 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -573,7 +573,7 @@ export class RunnableBinding< return "RunnableBinding"; } - lc_namespace = ["langchain", "schema", "runnable"]; + lc_namespace = ["langchain-core", "runnables"]; lc_serializable = true; @@ -738,7 +738,7 @@ export class RunnableEach< lc_serializable = true; - lc_namespace = ["langchain", "schema", "runnable"]; + lc_namespace = ["langchain-core", "runnables"]; bound: Runnable; @@ -806,7 +806,7 @@ export class RunnableRetry< return "RunnableRetry"; } - lc_namespace = ["langchain", "schema", "runnable"]; + lc_namespace = ["langchain-core", "runnables"]; protected maxAttemptNumber = 3; @@ -994,7 +994,7 @@ export class RunnableSequence< lc_serializable = true; - lc_namespace = ["langchain", "schema", "runnable"]; + lc_namespace = ["langchain-core", "runnables"]; constructor(fields: { first: Runnable; @@ -1290,7 +1290,7 @@ export class RunnableMap extends Runnable< return "RunnableMap"; } - lc_namespace = ["langchain", "schema", "runnable"]; + lc_namespace = ["langchain-core", "runnables"]; lc_serializable = true; @@ -1360,7 +1360,7 @@ export class RunnableLambda extends Runnable< return "RunnableLambda"; } - lc_namespace = ["langchain", "schema", "runnable"]; + lc_namespace = ["langchain-core", "runnables"]; protected func: RunnableFunc; @@ -1411,7 +1411,7 @@ export class RunnableWithFallbacks extends Runnable< return "RunnableWithFallbacks"; } - lc_namespace = ["langchain", "schema", "runnable"]; + lc_namespace = ["langchain-core", "runnables"]; lc_serializable = true; From 985f4b0d7390c5bdf887b8189f10290f87a5293d Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Wed, 22 Nov 2023 13:29:12 -0800 Subject: [PATCH 3/6] Update lc_namespaces --- langchain-core/package.json | 2 +- langchain-core/src/callbacks/base.ts | 4 +- langchain-core/src/documents/transformers.ts | 2 +- langchain-core/src/example_selectors/base.ts | 2 +- .../src/language_models/chat_models.ts | 1 + langchain-core/src/language_models/llms.ts | 1 + langchain-core/src/messages/index.ts | 2 +- langchain-core/src/output_parsers/bytes.ts | 2 +- langchain-core/src/output_parsers/list.ts | 4 +- langchain-core/src/output_parsers/string.ts | 2 +- langchain-core/src/prompt_values.ts | 53 ++++++++++++++++++- langchain-core/src/prompts/chat.ts | 50 +---------------- langchain-core/src/prompts/string.ts | 26 +-------- langchain-core/src/runnables/base.ts | 14 ++--- langchain-core/src/runnables/branch.ts | 2 +- langchain-core/src/runnables/passthrough.ts | 4 +- langchain-core/src/runnables/router.ts | 2 +- langchain-core/src/tools.ts | 2 +- langchain-core/src/vectorstores.ts | 3 +- langchain/src/load/tests/load.test.ts | 21 ++++++++ 20 files changed, 101 insertions(+), 98 deletions(-) diff --git a/langchain-core/package.json b/langchain-core/package.json index 9318742f52c3..193435f8bafa 100644 --- a/langchain-core/package.json +++ b/langchain-core/package.json @@ -1,6 +1,6 @@ { "name": "langchain-core", - "version": "0.0.3", + "version": "0.0.4", "description": "Typescript bindings for langchain", "type": "module", "engines": { diff --git a/langchain-core/src/callbacks/base.ts b/langchain-core/src/callbacks/base.ts index e538c3266b0e..fdc2a5670491 100644 --- a/langchain-core/src/callbacks/base.ts +++ b/langchain-core/src/callbacks/base.ts @@ -281,8 +281,8 @@ export abstract class BaseCallbackHandler { lc_serializable = false; - get lc_namespace(): ["langchain", "callbacks", string] { - return ["langchain", "callbacks", this.name]; + get lc_namespace(): ["langchain_core", "callbacks", string] { + return ["langchain_core", "callbacks", this.name]; } get lc_secrets(): { [key: string]: string } | undefined { diff --git a/langchain-core/src/documents/transformers.ts b/langchain-core/src/documents/transformers.ts index 896630e43fbe..b42ccab2dc16 100644 --- a/langchain-core/src/documents/transformers.ts +++ b/langchain-core/src/documents/transformers.ts @@ -16,7 +16,7 @@ export abstract class BaseDocumentTransformer< RunInput extends Document[] = Document[], RunOutput extends Document[] = Document[] > extends Runnable { - lc_namespace = ["langchain", "document_transformers"]; + lc_namespace = ["langchain_core", "documents", "transformers"]; /** * Transform a list of documents. diff --git a/langchain-core/src/example_selectors/base.ts b/langchain-core/src/example_selectors/base.ts index 4124d9e1b1da..85fc3ae179e6 100644 --- a/langchain-core/src/example_selectors/base.ts +++ b/langchain-core/src/example_selectors/base.ts @@ -5,7 +5,7 @@ import type { Example } from "../prompts/base.js"; * Base class for example selectors. */ export abstract class BaseExampleSelector extends Serializable { - lc_namespace = ["langchain", "prompts", "selectors"]; + lc_namespace = ["langchain_core", "example_selectors", "base"]; /** * Adds an example to the example selector. diff --git a/langchain-core/src/language_models/chat_models.ts b/langchain-core/src/language_models/chat_models.ts index 8d11509801f8..4a426c00b053 100644 --- a/langchain-core/src/language_models/chat_models.ts +++ b/langchain-core/src/language_models/chat_models.ts @@ -88,6 +88,7 @@ export abstract class BaseChatModel< keyof RunnableConfig & "timeout" >; + // Only ever instantiated in main LangChain lc_namespace = ["langchain", "chat_models", this._llmType()]; constructor(fields: BaseChatModelParams) { diff --git a/langchain-core/src/language_models/llms.ts b/langchain-core/src/language_models/llms.ts index e9a3d7ce24fb..c5ccb205cc7d 100644 --- a/langchain-core/src/language_models/llms.ts +++ b/langchain-core/src/language_models/llms.ts @@ -41,6 +41,7 @@ export abstract class BaseLLM< keyof RunnableConfig & "timeout" >; + // Only ever instantiated in main LangChain lc_namespace = ["langchain", "llms", this._llmType()]; constructor({ concurrency, ...rest }: BaseLLMParams) { diff --git a/langchain-core/src/messages/index.ts b/langchain-core/src/messages/index.ts index d3aefafed396..805013f4ba42 100644 --- a/langchain-core/src/messages/index.ts +++ b/langchain-core/src/messages/index.ts @@ -131,7 +131,7 @@ export abstract class BaseMessage extends Serializable implements BaseMessageFields { - lc_namespace = ["langchain", "schema"]; + lc_namespace = ["langchain_core", "messages"]; lc_serializable = true; diff --git a/langchain-core/src/output_parsers/bytes.ts b/langchain-core/src/output_parsers/bytes.ts index b496b57215d9..222d43948135 100644 --- a/langchain-core/src/output_parsers/bytes.ts +++ b/langchain-core/src/output_parsers/bytes.ts @@ -9,7 +9,7 @@ export class BytesOutputParser extends BaseTransformOutputParser { return "BytesOutputParser"; } - lc_namespace = ["langchain", "schema", "output_parser"]; + lc_namespace = ["langchain_core", "output_parsers", "bytes"]; lc_serializable = true; diff --git a/langchain-core/src/output_parsers/list.ts b/langchain-core/src/output_parsers/list.ts index 0b88cea1f84c..bb6af9b80402 100644 --- a/langchain-core/src/output_parsers/list.ts +++ b/langchain-core/src/output_parsers/list.ts @@ -15,7 +15,7 @@ export class CommaSeparatedListOutputParser extends ListOutputParser { return "CommaSeparatedListOutputParser"; } - lc_namespace = ["langchain", "output_parsers", "list"]; + lc_namespace = ["langchain_core", "output_parsers", "list"]; lc_serializable = true; @@ -51,7 +51,7 @@ export class CommaSeparatedListOutputParser extends ListOutputParser { * @augments ListOutputParser */ export class CustomListOutputParser extends ListOutputParser { - lc_namespace = ["langchain", "output_parsers", "list"]; + lc_namespace = ["langchain_core", "output_parsers", "list"]; private length: number | undefined; diff --git a/langchain-core/src/output_parsers/string.ts b/langchain-core/src/output_parsers/string.ts index 99a01266d50f..f6c959b3b98c 100644 --- a/langchain-core/src/output_parsers/string.ts +++ b/langchain-core/src/output_parsers/string.ts @@ -23,7 +23,7 @@ export class StringOutputParser extends BaseTransformOutputParser { return "StrOutputParser"; } - lc_namespace = ["langchain", "schema", "output_parser"]; + lc_namespace = ["langchain_core", "output_parsers", "string"]; lc_serializable = true; diff --git a/langchain-core/src/prompt_values.ts b/langchain-core/src/prompt_values.ts index 53899992b384..2d8e1117da22 100644 --- a/langchain-core/src/prompt_values.ts +++ b/langchain-core/src/prompt_values.ts @@ -1,5 +1,9 @@ import { Serializable } from "./load/serializable.js"; -import { type BaseMessage, HumanMessage } from "./messages/index.js"; +import { + type BaseMessage, + HumanMessage, + getBufferString, +} from "./messages/index.js"; /** * Base PromptValue class. All prompt values should extend this class. @@ -15,7 +19,7 @@ export abstract class BasePromptValue extends Serializable { * class and overrides the toString and toChatMessages methods. */ export class StringPromptValue extends BasePromptValue { - lc_namespace = ["langchain", "prompts", "base"]; + lc_namespace = ["langchain_core", "prompt_values"]; value: string; @@ -32,3 +36,48 @@ export class StringPromptValue extends BasePromptValue { return [new HumanMessage(this.value)]; } } + +/** + * Interface for the fields of a ChatPromptValue. + */ +export interface ChatPromptValueFields { + messages: BaseMessage[]; +} + +/** + * Class that represents a chat prompt value. It extends the + * BasePromptValue and includes an array of BaseMessage instances. + */ +export class ChatPromptValue extends BasePromptValue { + lc_namespace = ["langchain_core", "prompt_values"]; + + lc_serializable = true; + + static lc_name() { + return "ChatPromptValue"; + } + + messages: BaseMessage[]; + + constructor(messages: BaseMessage[]); + + constructor(fields: ChatPromptValueFields); + + constructor(fields: BaseMessage[] | ChatPromptValueFields) { + if (Array.isArray(fields)) { + // eslint-disable-next-line no-param-reassign + fields = { messages: fields }; + } + + super(fields); + this.messages = fields.messages; + } + + toString() { + return getBufferString(this.messages); + } + + toChatMessages() { + return this.messages; + } +} diff --git a/langchain-core/src/prompts/chat.ts b/langchain-core/src/prompts/chat.ts index 8a573b159ebf..95fc18f4976e 100644 --- a/langchain-core/src/prompts/chat.ts +++ b/langchain-core/src/prompts/chat.ts @@ -2,7 +2,6 @@ // Replace with "string" when we are comfortable with a breaking change. import type { BaseCallbackConfig } from "../callbacks/manager.js"; -import { getBufferString } from "../messages/index.js"; import { AIMessage, HumanMessage, @@ -13,7 +12,7 @@ import { coerceMessageLikeToMessage, isBaseMessage, } from "../messages/index.js"; -import { BasePromptValue } from "../prompt_values.js"; +import { ChatPromptValue } from "../prompt_values.js"; import type { InputValues, PartialValues } from "../utils/types.js"; import { Runnable } from "../runnables/base.js"; import { BaseStringPromptTemplate } from "./string.js"; @@ -34,7 +33,7 @@ export abstract class BaseMessagePromptTemplate< RunInput extends InputValues = any, RunOutput extends BaseMessage[] = BaseMessage[] > extends Runnable { - lc_namespace = ["langchain", "prompts", "chat"]; + lc_namespace = ["langchain_core", "prompts", "chat"]; lc_serializable = true; @@ -68,51 +67,6 @@ export abstract class BaseMessagePromptTemplate< } } -/** - * Interface for the fields of a ChatPromptValue. - */ -export interface ChatPromptValueFields { - messages: BaseMessage[]; -} - -/** - * Class that represents a chat prompt value. It extends the - * BasePromptValue and includes an array of BaseMessage instances. - */ -export class ChatPromptValue extends BasePromptValue { - lc_namespace = ["langchain", "prompts", "chat"]; - - lc_serializable = true; - - static lc_name() { - return "ChatPromptValue"; - } - - messages: BaseMessage[]; - - constructor(messages: BaseMessage[]); - - constructor(fields: ChatPromptValueFields); - - constructor(fields: BaseMessage[] | ChatPromptValueFields) { - if (Array.isArray(fields)) { - // eslint-disable-next-line no-param-reassign - fields = { messages: fields }; - } - - super(fields); - this.messages = fields.messages; - } - - toString() { - return getBufferString(this.messages); - } - - toChatMessages() { - return this.messages; - } -} - /** * Interface for the fields of a MessagePlaceholder. */ diff --git a/langchain-core/src/prompts/string.ts b/langchain-core/src/prompts/string.ts index 72b198121698..900dbd198f79 100644 --- a/langchain-core/src/prompts/string.ts +++ b/langchain-core/src/prompts/string.ts @@ -1,34 +1,10 @@ // Default generic "any" values are for backwards compatibility. // Replace with "string" when we are comfortable with a breaking change. -import { HumanMessage } from "../messages/index.js"; import type { InputValues } from "../utils/types.js"; -import { BasePromptValue } from "../prompt_values.js"; +import { StringPromptValue } from "../prompt_values.js"; import { BasePromptTemplate, type TypedPromptInputValues } from "./base.js"; -/** - * Represents a prompt value as a string. It extends the BasePromptValue - * class and overrides the toString and toChatMessages methods. - */ -export class StringPromptValue extends BasePromptValue { - lc_namespace = ["langchain", "prompts", "base"]; - - value: string; - - constructor(value: string) { - super({ value }); - this.value = value; - } - - toString() { - return this.value; - } - - toChatMessages() { - return [new HumanMessage(this.value)]; - } -} - /** * Base class for string prompt templates. It extends the * BasePromptTemplate class and overrides the formatPromptValue method to diff --git a/langchain-core/src/runnables/base.ts b/langchain-core/src/runnables/base.ts index 83c8e8966549..0403b91486bb 100644 --- a/langchain-core/src/runnables/base.ts +++ b/langchain-core/src/runnables/base.ts @@ -573,7 +573,7 @@ export class RunnableBinding< return "RunnableBinding"; } - lc_namespace = ["langchain-core", "runnables"]; + lc_namespace = ["langchain_core", "runnables"]; lc_serializable = true; @@ -738,7 +738,7 @@ export class RunnableEach< lc_serializable = true; - lc_namespace = ["langchain-core", "runnables"]; + lc_namespace = ["langchain_core", "runnables"]; bound: Runnable; @@ -806,7 +806,7 @@ export class RunnableRetry< return "RunnableRetry"; } - lc_namespace = ["langchain-core", "runnables"]; + lc_namespace = ["langchain_core", "runnables"]; protected maxAttemptNumber = 3; @@ -994,7 +994,7 @@ export class RunnableSequence< lc_serializable = true; - lc_namespace = ["langchain-core", "runnables"]; + lc_namespace = ["langchain_core", "runnables"]; constructor(fields: { first: Runnable; @@ -1290,7 +1290,7 @@ export class RunnableMap extends Runnable< return "RunnableMap"; } - lc_namespace = ["langchain-core", "runnables"]; + lc_namespace = ["langchain_core", "runnables"]; lc_serializable = true; @@ -1360,7 +1360,7 @@ export class RunnableLambda extends Runnable< return "RunnableLambda"; } - lc_namespace = ["langchain-core", "runnables"]; + lc_namespace = ["langchain_core", "runnables"]; protected func: RunnableFunc; @@ -1411,7 +1411,7 @@ export class RunnableWithFallbacks extends Runnable< return "RunnableWithFallbacks"; } - lc_namespace = ["langchain-core", "runnables"]; + lc_namespace = ["langchain_core", "runnables"]; lc_serializable = true; diff --git a/langchain-core/src/runnables/branch.ts b/langchain-core/src/runnables/branch.ts index 1928de34d190..094ea50bee1d 100644 --- a/langchain-core/src/runnables/branch.ts +++ b/langchain-core/src/runnables/branch.ts @@ -62,7 +62,7 @@ export class RunnableBranch extends Runnable< return "RunnableBranch"; } - lc_namespace = ["langchain", "runnable", "branch"]; + lc_namespace = ["langchain_core", "runnables"]; lc_serializable = true; diff --git a/langchain-core/src/runnables/passthrough.ts b/langchain-core/src/runnables/passthrough.ts index 77cf1e74e6fb..a3a4039d1bd5 100644 --- a/langchain-core/src/runnables/passthrough.ts +++ b/langchain-core/src/runnables/passthrough.ts @@ -11,7 +11,7 @@ export class RunnableAssign< RunOutput extends Record = any, CallOptions extends RunnableConfig = RunnableConfig > extends Runnable { - lc_namespace = ["langchain", "schema", "runnable"]; + lc_namespace = ["langchain_core", "runnables"]; mapper: RunnableMap; @@ -67,7 +67,7 @@ export class RunnablePassthrough extends Runnable< return "RunnablePassthrough"; } - lc_namespace = ["langchain", "schema", "runnable"]; + lc_namespace = ["langchain_core", "runnables"]; lc_serializable = true; diff --git a/langchain-core/src/runnables/router.ts b/langchain-core/src/runnables/router.ts index 28a720071dc7..3c08ee39b2b1 100644 --- a/langchain-core/src/runnables/router.ts +++ b/langchain-core/src/runnables/router.ts @@ -21,7 +21,7 @@ export class RouterRunnable< return "RouterRunnable"; } - lc_namespace = ["langchain", "schema", "runnable"]; + lc_namespace = ["langchain_core", "runnables"]; lc_serializable = true; diff --git a/langchain-core/src/tools.ts b/langchain-core/src/tools.ts index eeedab18ecd8..c843fcfb7981 100644 --- a/langchain-core/src/tools.ts +++ b/langchain-core/src/tools.ts @@ -43,7 +43,7 @@ export abstract class StructuredTool< abstract schema: T | z.ZodEffects; get lc_namespace() { - return ["langchain", "tools"]; + return ["langchain_core", "tools"]; } constructor(fields?: ToolParams) { diff --git a/langchain-core/src/vectorstores.ts b/langchain-core/src/vectorstores.ts index d564c3a537b9..81f120bfbb66 100644 --- a/langchain-core/src/vectorstores.ts +++ b/langchain-core/src/vectorstores.ts @@ -65,7 +65,7 @@ export class VectorStoreRetriever< } get lc_namespace() { - return ["langchain", "retrievers", "base"]; + return ["langchain_core", "vectorstores"]; } vectorStore: V; @@ -137,6 +137,7 @@ export class VectorStoreRetriever< export abstract class VectorStore extends Serializable { declare FilterType: object | string; + // Only ever instantiated in main LangChain lc_namespace = ["langchain", "vectorstores", this._vectorstoreType()]; embeddings: Embeddings; diff --git a/langchain/src/load/tests/load.test.ts b/langchain/src/load/tests/load.test.ts index 0a8a610729dc..ade213085070 100644 --- a/langchain/src/load/tests/load.test.ts +++ b/langchain/src/load/tests/load.test.ts @@ -17,6 +17,7 @@ import { LangChainTracer } from "../../callbacks/index.js"; import { FewShotPromptTemplate, LengthBasedExampleSelector, + StringPromptValue, } from "../../prompts/index.js"; import { initializeAgentExecutorWithOptions } from "../../agents/initialize.js"; import { Calculator } from "../../tools/calculator.js"; @@ -233,6 +234,26 @@ test("serialize + deserialize with new and old ids", async () => { expect(prompt3).toBeInstanceOf(PromptTemplate); }); +test("serialize + deserialize prompt value with new and old ids", async () => { + const prompt = StringPromptValue.fromTemplate("Hello, {name}!"); + const strWithNewId = JSON.stringify(prompt, null, 2); + expect(stringify(JSON.parse(strWithNewId))).toMatchSnapshot(); + expect(JSON.parse(strWithNewId).id).toEqual([ + "langchain_core", + "prompts", + "prompt", + "PromptTemplate", + ]); + const strWithOldId = JSON.stringify({ + ...JSON.parse(strWithNewId), + id: ["langchain", "prompts", "prompt", "PromptTemplate"], + }); + const prompt2 = await load(strWithOldId); + expect(prompt2).toBeInstanceOf(PromptTemplate); + const prompt3 = await load(strWithNewId); + expect(prompt3).toBeInstanceOf(PromptTemplate); +}); + test("serialize + deserialize llm chain chat prompt", async () => { // eslint-disable-next-line no-process-env process.env.OPENAI_API_KEY = undefined; From 8f5328705322146883cdf7933c5273cc1e01b43e Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Wed, 22 Nov 2023 13:52:56 -0800 Subject: [PATCH 4/6] Fix test --- langchain-core/src/language_models/base.ts | 7 ++- langchain-core/src/tools.ts | 2 +- .../tests/__snapshots__/load.test.ts.snap | 63 ++++++++++++++++++- langchain/src/load/tests/load.test.ts | 26 ++++---- langchain/src/prompts/base.ts | 3 +- langchain/src/prompts/chat.ts | 7 ++- 6 files changed, 87 insertions(+), 21 deletions(-) diff --git a/langchain-core/src/language_models/base.ts b/langchain-core/src/language_models/base.ts index 3fcd390033e6..a09e3d40444c 100644 --- a/langchain-core/src/language_models/base.ts +++ b/langchain-core/src/language_models/base.ts @@ -1,7 +1,11 @@ import type { Tiktoken, TiktokenModel } from "js-tiktoken/lite"; import { type BaseCache, InMemoryCache } from "../caches.js"; -import { type BasePromptValue, StringPromptValue } from "../prompt_values.js"; +import { + type BasePromptValue, + StringPromptValue, + ChatPromptValue, +} from "../prompt_values.js"; import { type BaseMessage, type BaseMessageLike, @@ -18,7 +22,6 @@ import { AsyncCaller, AsyncCallerParams } from "../utils/async_caller.js"; import { encodingForModel } from "../utils/tiktoken.js"; import { Runnable } from "../runnables/base.js"; import { RunnableConfig } from "../runnables/config.js"; -import { ChatPromptValue } from "../prompts/chat.js"; // https://www.npmjs.com/package/js-tiktoken diff --git a/langchain-core/src/tools.ts b/langchain-core/src/tools.ts index c843fcfb7981..eeedab18ecd8 100644 --- a/langchain-core/src/tools.ts +++ b/langchain-core/src/tools.ts @@ -43,7 +43,7 @@ export abstract class StructuredTool< abstract schema: T | z.ZodEffects; get lc_namespace() { - return ["langchain_core", "tools"]; + return ["langchain", "tools"]; } constructor(fields?: ToolParams) { diff --git a/langchain/src/load/tests/__snapshots__/load.test.ts.snap b/langchain/src/load/tests/__snapshots__/load.test.ts.snap index 2cc724278886..cf24e88de6c3 100644 --- a/langchain/src/load/tests/__snapshots__/load.test.ts.snap +++ b/langchain/src/load/tests/__snapshots__/load.test.ts.snap @@ -125,7 +125,7 @@ kwargs: - lc: 1 type: constructor id: - - langchain + - langchain_core - prompts - chat - SystemMessagePromptTemplate @@ -146,7 +146,7 @@ kwargs: - lc: 1 type: constructor id: - - langchain + - langchain_core - prompts - chat - HumanMessagePromptTemplate @@ -498,7 +498,7 @@ kwargs: lc: 1 type: constructor id: - - langchain + - langchain_core - output_parsers - list - CommaSeparatedListOutputParser @@ -613,6 +613,63 @@ kwargs: " `; +exports[`serialize + deserialize runnable sequence with new and old ids 1`] = ` +"lc: 1 +type: constructor +id: + - langchain_core + - runnables + - RunnableSequence +kwargs: + first: + lc: 1 + type: constructor + id: + - langchain_core + - prompts + - chat + - ChatPromptTemplate + kwargs: + input_variables: [] + messages: + - 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: [] + template_format: f-string + template: hi there + middle: [] + last: + lc: 1 + type: constructor + id: + - langchain + - chat_models + - openai + - ChatOpenAI + kwargs: + openai_api_key: + lc: 1 + type: secret + id: + - OPENAI_API_KEY +" +`; + exports[`serialize + deserialize with new and old ids 1`] = ` "lc: 1 type: constructor diff --git a/langchain/src/load/tests/load.test.ts b/langchain/src/load/tests/load.test.ts index ade213085070..8f4348bc1ba7 100644 --- a/langchain/src/load/tests/load.test.ts +++ b/langchain/src/load/tests/load.test.ts @@ -1,6 +1,7 @@ import { test, expect } from "@jest/globals"; import { stringify } from "yaml"; import { z } from "zod"; +import { RunnableSequence } from "langchain-core/runnables"; import { load } from "../index.js"; import { OpenAI } from "../../llms/openai.js"; @@ -17,7 +18,6 @@ import { LangChainTracer } from "../../callbacks/index.js"; import { FewShotPromptTemplate, LengthBasedExampleSelector, - StringPromptValue, } from "../../prompts/index.js"; import { initializeAgentExecutorWithOptions } from "../../agents/initialize.js"; import { Calculator } from "../../tools/calculator.js"; @@ -234,24 +234,26 @@ test("serialize + deserialize with new and old ids", async () => { expect(prompt3).toBeInstanceOf(PromptTemplate); }); -test("serialize + deserialize prompt value with new and old ids", async () => { - const prompt = StringPromptValue.fromTemplate("Hello, {name}!"); - const strWithNewId = JSON.stringify(prompt, null, 2); +test("serialize + deserialize runnable sequence with new and old ids", async () => { + const runnable = RunnableSequence.from([ + ChatPromptTemplate.fromTemplate("hi there"), + new ChatOpenAI(), + ]); + const strWithNewId = JSON.stringify(runnable, null, 2); expect(stringify(JSON.parse(strWithNewId))).toMatchSnapshot(); expect(JSON.parse(strWithNewId).id).toEqual([ "langchain_core", - "prompts", - "prompt", - "PromptTemplate", + "runnables", + "RunnableSequence", ]); const strWithOldId = JSON.stringify({ ...JSON.parse(strWithNewId), - id: ["langchain", "prompts", "prompt", "PromptTemplate"], + id: ["langchain", "schema", "runnable", "RunnableSequence"], }); - const prompt2 = await load(strWithOldId); - expect(prompt2).toBeInstanceOf(PromptTemplate); - const prompt3 = await load(strWithNewId); - expect(prompt3).toBeInstanceOf(PromptTemplate); + const runnable2 = await load(strWithOldId); + expect(runnable2).toBeInstanceOf(RunnableSequence); + const runnable3 = await load(strWithNewId); + expect(runnable3).toBeInstanceOf(RunnableSequence); }); test("serialize + deserialize llm chain chat prompt", async () => { diff --git a/langchain/src/prompts/base.ts b/langchain/src/prompts/base.ts index 25ba80f6fe9e..879479b53b21 100644 --- a/langchain/src/prompts/base.ts +++ b/langchain/src/prompts/base.ts @@ -1,9 +1,10 @@ export { type TypedPromptInputValues, - StringPromptValue, BaseStringPromptTemplate, type BasePromptTemplateInput, BasePromptTemplate, } from "langchain-core/prompts"; +export { StringPromptValue } from "langchain-core/prompt_values"; + export { BaseExampleSelector } from "langchain-core/example_selectors"; diff --git a/langchain/src/prompts/chat.ts b/langchain/src/prompts/chat.ts index 8e9a2dc59c77..08192f617976 100644 --- a/langchain/src/prompts/chat.ts +++ b/langchain/src/prompts/chat.ts @@ -1,7 +1,5 @@ export { BaseMessagePromptTemplate, - type ChatPromptValueFields, - ChatPromptValue, type MessagesPlaceholderFields as MessagePlaceholderFields, MessagesPlaceholder, type MessageStringPromptTemplateFields, @@ -16,3 +14,8 @@ export { type BaseMessagePromptTemplateLike, ChatPromptTemplate, } from "langchain-core/prompts"; + +export { + type ChatPromptValueFields, + ChatPromptValue, +} from "langchain-core/prompt_values"; From d5d98c2c48156ad2db6530c7071791dd19b0179a Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Wed, 22 Nov 2023 13:57:27 -0800 Subject: [PATCH 5/6] Fix tests --- langchain/src/callbacks/tests/tracer.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/langchain/src/callbacks/tests/tracer.test.ts b/langchain/src/callbacks/tests/tracer.test.ts index 5468f166790c..7b6e69830b69 100644 --- a/langchain/src/callbacks/tests/tracer.test.ts +++ b/langchain/src/callbacks/tests/tracer.test.ts @@ -101,8 +101,8 @@ test("Test Chat Model Run", async () => { [ { "id": [ - "langchain", - "schema", + "langchain_core", + "messages", "HumanMessage", ], "kwargs": { From 99f8e0a814ec43c646362dd68354cf9a85c49aa7 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Wed, 22 Nov 2023 14:08:32 -0800 Subject: [PATCH 6/6] Bump versions --- langchain/package.json | 2 +- libs/langchain-anthropic/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/langchain/package.json b/langchain/package.json index 1c641903a9bd..a23196029822 100644 --- a/langchain/package.json +++ b/langchain/package.json @@ -1386,7 +1386,7 @@ "js-tiktoken": "^1.0.7", "js-yaml": "^4.1.0", "jsonpointer": "^5.0.1", - "langchain-core": "^0.0.3", + "langchain-core": "^0.0.4", "langchainhub": "~0.0.6", "langsmith": "~0.0.48", "ml-distance": "^4.0.0", diff --git a/libs/langchain-anthropic/package.json b/libs/langchain-anthropic/package.json index 409c3dc04bfa..6e3513623f3f 100644 --- a/libs/langchain-anthropic/package.json +++ b/libs/langchain-anthropic/package.json @@ -33,7 +33,7 @@ "license": "MIT", "dependencies": { "@anthropic-ai/sdk": "^0.10.0", - "langchain-core": "^0.0.3" + "langchain-core": "^0.0.4" }, "devDependencies": { "@jest/globals": "^29.5.0",