Skip to content

Commit

Permalink
core[minor]: Add ID field to messages (#5817)
Browse files Browse the repository at this point in the history
* core[minor]: Add ID field to messages

* added id to openai

* revert oai change
  • Loading branch information
bracesproul authored Jun 19, 2024
1 parent 2876d4e commit 0a178dc
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 0 deletions.
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

0 comments on commit 0a178dc

Please sign in to comment.