From c89e3157783a462b00a75062b189c9a3cb53bb15 Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Wed, 18 Dec 2024 15:44:54 -0800 Subject: [PATCH] feat(js): Adds developer role as a system role alias (#7397) --- langchain-core/src/messages/base.ts | 1 + .../src/messages/tests/base_message.test.ts | 8 +++++++ langchain-core/src/messages/transformers.ts | 23 +++++++++++++++++++ langchain-core/src/messages/utils.ts | 10 +++++++- langchain-core/src/prompts/tests/chat.test.ts | 2 +- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/langchain-core/src/messages/base.ts b/langchain-core/src/messages/base.ts index 790555f11ead..521f5b77935b 100644 --- a/langchain-core/src/messages/base.ts +++ b/langchain-core/src/messages/base.ts @@ -34,6 +34,7 @@ export type MessageType = | "human" | "ai" | "generic" + | "developer" | "system" | "function" | "tool" diff --git a/langchain-core/src/messages/tests/base_message.test.ts b/langchain-core/src/messages/tests/base_message.test.ts index b1c040f828f5..5a1d7aaef455 100644 --- a/langchain-core/src/messages/tests/base_message.test.ts +++ b/langchain-core/src/messages/tests/base_message.test.ts @@ -346,6 +346,7 @@ describe("Message like coercion", () => { role: "system", content: "6", }, + { role: "developer", content: "6.1" }, { role: "user", content: [{ type: "image_url", image_url: { url: "7.1" } }], @@ -374,6 +375,13 @@ describe("Message like coercion", () => { new SystemMessage({ id: "foobar", content: "6", + additional_kwargs: {}, + }), + new SystemMessage({ + content: "6.1", + additional_kwargs: { + __openai_role__: "developer", + }, }), new HumanMessage({ content: [{ type: "image_url", image_url: { url: "7.1" } }], diff --git a/langchain-core/src/messages/transformers.ts b/langchain-core/src/messages/transformers.ts index 811defbbd41a..81d0d7df6738 100644 --- a/langchain-core/src/messages/transformers.ts +++ b/langchain-core/src/messages/transformers.ts @@ -906,6 +906,10 @@ const _MSG_CHUNK_MAP: Record< message: SystemMessage, messageChunk: SystemMessageChunk, }, + developer: { + message: SystemMessage, + messageChunk: SystemMessageChunk, + }, tool: { message: ToolMessage, messageChunk: ToolMessageChunk, @@ -977,6 +981,25 @@ function _switchTypeToMessage( msg = new SystemMessage(fields); } break; + case "developer": + if (returnChunk) { + chunk = new SystemMessageChunk({ + ...fields, + additional_kwargs: { + ...fields.additional_kwargs, + __openai_role__: "developer", + }, + }); + } else { + msg = new SystemMessage({ + ...fields, + additional_kwargs: { + ...fields.additional_kwargs, + __openai_role__: "developer", + }, + }); + } + break; case "tool": if ("tool_call_id" in fields) { if (returnChunk) { diff --git a/langchain-core/src/messages/utils.ts b/langchain-core/src/messages/utils.ts index ae32060afb58..01f5beb3f56c 100644 --- a/langchain-core/src/messages/utils.ts +++ b/langchain-core/src/messages/utils.ts @@ -107,6 +107,14 @@ function _constructMessageFromParams( return new AIMessage({ ...other, tool_calls }); } else if (type === "system") { return new SystemMessage(rest); + } else if (type === "developer") { + return new SystemMessage({ + ...rest, + additional_kwargs: { + ...rest.additional_kwargs, + __openai_role__: "developer", + }, + }); } else if (type === "tool" && "tool_call_id" in rest) { return new ToolMessage({ ...rest, @@ -117,7 +125,7 @@ function _constructMessageFromParams( } else { const error = addLangChainErrorFields( new Error( - `Unable to coerce message from array: only human, AI, system, or tool message coercion is currently supported.\n\nReceived: ${JSON.stringify( + `Unable to coerce message from array: only human, AI, system, developer, or tool message coercion is currently supported.\n\nReceived: ${JSON.stringify( params, null, 2 diff --git a/langchain-core/src/prompts/tests/chat.test.ts b/langchain-core/src/prompts/tests/chat.test.ts index 62049ca55707..e9c6f9c09966 100644 --- a/langchain-core/src/prompts/tests/chat.test.ts +++ b/langchain-core/src/prompts/tests/chat.test.ts @@ -373,7 +373,7 @@ test("Test MessagesPlaceholder not optional with invalid input should throw", as badInput, null, 2 - )}\n\nAdditional message: Unable to coerce message from array: only human, AI, system, or tool message coercion is currently supported.` + )}\n\nAdditional message: Unable to coerce message from array: only human, AI, system, developer, or tool message coercion is currently supported.` ); });