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

feat(js): Adds developer role as a system role alias #7397

Merged
merged 8 commits into from
Dec 18, 2024
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
1 change: 1 addition & 0 deletions langchain-core/src/messages/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type MessageType =
| "human"
| "ai"
| "generic"
| "developer"
| "system"
| "function"
| "tool"
Expand Down
8 changes: 8 additions & 0 deletions langchain-core/src/messages/tests/base_message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" } }],
Expand Down Expand Up @@ -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" } }],
Expand Down
23 changes: 23 additions & 0 deletions langchain-core/src/messages/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,10 @@ const _MSG_CHUNK_MAP: Record<
message: SystemMessage,
messageChunk: SystemMessageChunk,
},
developer: {
message: SystemMessage,
messageChunk: SystemMessageChunk,
},
tool: {
message: ToolMessage,
messageChunk: ToolMessageChunk,
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion langchain-core/src/messages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion langchain-core/src/prompts/tests/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
);
});

Expand Down
Loading