Skip to content

Commit

Permalink
Relax types
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Apr 9, 2024
1 parent afa1bfe commit fa712c3
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions langchain-core/src/messages/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export type AIMessageFields = BaseMessageFields & {
* Represents an AI message in a conversation.
*/
export class AIMessage extends BaseMessage {
tool_calls: ToolCall[] = [];
// These are typed as optional to avoid breaking changes and allow for casting
// from BaseMessage.
tool_calls?: ToolCall[] = [];

invalid_tool_calls: InvalidToolCall[] = [];
invalid_tool_calls?: InvalidToolCall[] = [];

get lc_aliases(): Record<string, string> {
// exclude snake case conversion to pascal case
Expand All @@ -37,10 +39,19 @@ export class AIMessage extends BaseMessage {
};
}

constructor(fields: string | AIMessageFields) {
let initParams;
constructor(
fields: string | AIMessageFields,
/** @deprecated */
kwargs?: Record<string, unknown>
) {
let initParams: AIMessageFields;
if (typeof fields === "string") {
initParams = { content: fields, tool_calls: [], invalid_tool_calls: [] };
initParams = {
content: fields,
tool_calls: [],
invalid_tool_calls: [],
additional_kwargs: kwargs ?? {},
};
} else {
initParams = fields;
const rawToolCalls = initParams.additional_kwargs?.tool_calls;
Expand Down Expand Up @@ -103,12 +114,14 @@ export type AIMessageChunkFields = AIMessageFields & {
* other AI message chunks.
*/
export class AIMessageChunk extends BaseMessageChunk {
// Must redeclare tool call fields since there is no multiple inhertiance in JS.
tool_calls: ToolCall[] = [];
// Must redeclare tool call fields since there is no multiple inheritance in JS.
// These are typed as optional to avoid breaking changes and allow for casting
// from BaseMessage.
tool_calls?: ToolCall[] = [];

invalid_tool_calls: InvalidToolCall[] = [];
invalid_tool_calls?: InvalidToolCall[] = [];

tool_call_chunks: ToolCallChunk[] = [];
tool_call_chunks?: ToolCallChunk[] = [];

constructor(fields: string | AIMessageChunkFields) {
let initParams: AIMessageChunkFields;
Expand Down

0 comments on commit fa712c3

Please sign in to comment.