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

core[minor]: Add ID #5441

Closed
wants to merge 12 commits into from
18 changes: 18 additions & 0 deletions langchain-core/src/messages/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface StoredMessageData {
content: string;
role: string | undefined;
name: string | undefined;
/** An optional unique identifier for the message. */
id: string | undefined;
tool_call_id: string | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
additional_kwargs?: Record<string, any>;
Expand Down Expand Up @@ -98,6 +100,11 @@ export interface ToolCall {
export type BaseMessageFields = {
content: MessageContent;
name?: string;
/**
* An optional unique identifier for the message. This should ideally be
* provided by the provider/model which created the message.
*/
id?: string;
additional_kwargs?: {
function_call?: FunctionCall;
tool_calls?: ToolCall[];
Expand Down Expand Up @@ -164,6 +171,12 @@ export abstract class BaseMessage
/** The name of the message sender in a multi-user chat. */
name?: string;

/**
* An optional unique identifier for the message. This should ideally be
* provided by the provider/model which created the message.
*/
id?: string;

/** Additional keyword arguments */
additional_kwargs: NonNullable<BaseMessageFields["additional_kwargs"]>;

Expand Down Expand Up @@ -195,7 +208,12 @@ export abstract class BaseMessage
// eslint-disable-next-line no-param-reassign
fields.response_metadata = {};
}
if (!fields.id) {
// eslint-disable-next-line no-param-reassign
fields.id = undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unnecessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary. Otherwise ids added via mutation won' tbe serialized

}
super(fields);
this.id = fields.id;
this.name = fields.name;
this.content = fields.content;
this.additional_kwargs = fields.additional_kwargs;
Expand Down
28 changes: 28 additions & 0 deletions langchain-core/src/messages/tests/base_message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,31 @@ test("Deserialisation and serialisation of tool_call_id", async () => {
const deserialized: ToolMessage = await load(JSON.stringify(message), config);
expect(deserialized).toEqual(message);
});

test("Deserialisation and serialisation of id", async () => {
const config = {
importMap: { messages: { AIMessage } },
optionalImportEntrypoints: [],
optionalImportsMap: {},
secretsMap: {},
};

const message = new AIMessage({
content: "I am a message 1",
id: "my-cool-id",
});

const deserialized: AIMessage = await load(JSON.stringify(message), config);
expect(deserialized).toEqual(message);

// Ensure it works if you mutate the id after creation
const newMessage = new AIMessage({
content: "I am a message 2",
});
newMessage.id = "my-other-cool-id";
const deserializedNew: AIMessage = await load(
JSON.stringify(newMessage),
config
);
expect(deserializedNew.id).toEqual(newMessage.id);
});
1 change: 1 addition & 0 deletions langchain-core/src/messages/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function mapV1MessageToStoredMessage(
role: v1Message.role,
name: undefined,
tool_call_id: undefined,
id: undefined,
},
};
}
Expand Down
9 changes: 0 additions & 9 deletions langchain-core/src/prompts/tests/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@ function createChatPromptTemplate() {
template: "I'm a generic message. I'm {foo}. I'm {bar}.",
inputVariables: ["foo", "bar"],
});
// return new ChatPromptTemplate({
// promptMessages: [
// new SystemMessagePromptTemplate(systemPrompt),
// new HumanMessagePromptTemplate(userPrompt),
// new AIMessagePromptTemplate({ prompt: aiPrompt }),
// new ChatMessagePromptTemplate(genericPrompt, "test"),
// ],
// inputVariables: ["context", "foo", "bar"],
// });
return ChatPromptTemplate.fromMessages<{
foo: string;
bar: string;
Expand Down
Loading