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 field to messages #5817

Merged
merged 3 commits into from
Jun 19, 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/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export class AIMessageChunk extends BaseMessageChunk {
chunk.response_metadata
),
tool_call_chunks: [],
id: this.id ?? chunk.id,
};
if (
this.tool_call_chunks !== undefined ||
Expand Down
12 changes: 12 additions & 0 deletions langchain-core/src/messages/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ export type BaseMessageFields = {
/** Response metadata. For example: response headers, logprobs, token counts. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response_metadata?: Record<string, any>;
/**
* An optional unique identifier for the message. This should ideally be
* provided by the provider/model which created the message.
*/
id?: string;
};

export function mergeContent(
Expand Down Expand Up @@ -170,6 +175,12 @@ export abstract class BaseMessage
/** Response metadata. For example: response headers, logprobs, token counts. */
response_metadata: NonNullable<BaseMessageFields["response_metadata"]>;

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

/** The type of the message. */
abstract _getType(): MessageType;

Expand Down Expand Up @@ -200,6 +211,7 @@ export abstract class BaseMessage
this.content = fields.content;
this.additional_kwargs = fields.additional_kwargs;
this.response_metadata = fields.response_metadata;
this.id = fields.id;
}

toDict(): StoredMessage {
Expand Down
1 change: 1 addition & 0 deletions langchain-core/src/messages/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class ChatMessageChunk extends BaseMessageChunk {
chunk.response_metadata
),
role: this.role,
id: this.id ?? chunk.id,
});
}
}
1 change: 1 addition & 0 deletions langchain-core/src/messages/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class FunctionMessageChunk extends BaseMessageChunk {
chunk.response_metadata
),
name: this.name ?? "",
id: this.id ?? chunk.id,
});
}
}
1 change: 1 addition & 0 deletions langchain-core/src/messages/human.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class HumanMessageChunk extends BaseMessageChunk {
this.response_metadata,
chunk.response_metadata
),
id: this.id ?? chunk.id,
});
}
}
1 change: 1 addition & 0 deletions langchain-core/src/messages/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class SystemMessageChunk extends BaseMessageChunk {
this.response_metadata,
chunk.response_metadata
),
id: this.id ?? chunk.id,
});
}
}
1 change: 1 addition & 0 deletions langchain-core/src/messages/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export class ToolMessageChunk extends BaseMessageChunk {
chunk.response_metadata
),
tool_call_id: this.tool_call_id,
id: this.id ?? chunk.id,
});
}
}
Expand Down
Loading