Skip to content

Commit

Permalink
core[minor]: Adds response_metadata field to messages (langchain-ai#4741
Browse files Browse the repository at this point in the history
)

* Adds response_metadata field to messages

* Format
  • Loading branch information
jacoblee93 authored Mar 13, 2024
1 parent dd24fef commit f0e033c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
25 changes: 23 additions & 2 deletions langchain-core/src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export interface StoredMessageData {
tool_call_id: string | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
additional_kwargs?: Record<string, any>;
/** Response metadata. For example: response headers, logprobs, token counts. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response_metadata?: Record<string, any>;
}

export interface StoredMessage {
Expand Down Expand Up @@ -90,6 +93,9 @@ export interface BaseMessageFields {
tool_calls?: ToolCall[];
[key: string]: unknown;
};
/** Response metadata. For example: response headers, logprobs, token counts. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response_metadata?: Record<string, any>;
}

export interface ChatMessageFieldsWithRole extends BaseMessageFields {
Expand Down Expand Up @@ -140,7 +146,10 @@ export abstract class BaseMessage

get lc_aliases(): Record<string, string> {
// exclude snake case conversion to pascal case
return { additional_kwargs: "additional_kwargs" };
return {
additional_kwargs: "additional_kwargs",
response_metadata: "response_metadata",
};
}

/**
Expand All @@ -160,6 +169,9 @@ export abstract class BaseMessage
/** Additional keyword arguments */
additional_kwargs: NonNullable<BaseMessageFields["additional_kwargs"]>;

/** Response metadata. For example: response headers, logprobs, token counts. */
response_metadata: NonNullable<BaseMessageFields["response_metadata"]>;

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

Expand All @@ -170,17 +182,26 @@ export abstract class BaseMessage
) {
if (typeof fields === "string") {
// eslint-disable-next-line no-param-reassign
fields = { content: fields, additional_kwargs: kwargs };
fields = {
content: fields,
additional_kwargs: kwargs,
response_metadata: {},
};
}
// Make sure the default value for additional_kwargs is passed into super() for serialization
if (!fields.additional_kwargs) {
// eslint-disable-next-line no-param-reassign
fields.additional_kwargs = {};
}
if (!fields.response_metadata) {
// eslint-disable-next-line no-param-reassign
fields.response_metadata = {};
}
super(fields);
this.name = fields.name;
this.content = fields.content;
this.additional_kwargs = fields.additional_kwargs;
this.response_metadata = fields.response_metadata;
}

toDict(): StoredMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports[`Multi-modal, multi part chat prompt works with instances of BaseMessage
"kwargs": {
"additional_kwargs": {},
"content": "You are an AI assistant named Bob",
"response_metadata": {},
},
"lc": 1,
"type": "constructor",
Expand All @@ -29,6 +30,7 @@ exports[`Multi-modal, multi part chat prompt works with instances of BaseMessage
"type": "image_url",
},
],
"response_metadata": {},
},
"lc": 1,
"type": "constructor",
Expand All @@ -47,6 +49,7 @@ exports[`Multi-modal, multi part chat prompt works with instances of BaseMessage
"type": "text",
},
],
"response_metadata": {},
},
"lc": 1,
"type": "constructor",
Expand All @@ -68,6 +71,7 @@ exports[`Multi-modal, multi part chat prompt works with instances of BaseMessage
"type": "image_url",
},
],
"response_metadata": {},
},
"lc": 1,
"type": "constructor",
Expand All @@ -93,6 +97,7 @@ exports[`Multi-modal, multi part chat prompt works with instances of BaseMessage
"type": "image_url",
},
],
"response_metadata": {},
},
"lc": 1,
"type": "constructor",
Expand Down
1 change: 1 addition & 0 deletions langchain-core/src/tracers/tests/tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ test("Test Chat Model Run", async () => {
"kwargs": {
"additional_kwargs": {},
"content": "Avast",
"response_metadata": {},
},
"lc": 1,
"type": "constructor",
Expand Down

0 comments on commit f0e033c

Please sign in to comment.