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[patch]: Improve console.log format for messages #6121

Merged
merged 3 commits into from
Jul 18, 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
19 changes: 19 additions & 0 deletions langchain-core/src/messages/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ export class AIMessage extends BaseMessage {
_getType(): MessageType {
return "ai";
}

override get _printableFields(): Record<string, unknown> {
return {
...super._printableFields,
tool_calls: this.tool_calls,
invalid_tool_calls: this.invalid_tool_calls,
usage_metadata: this.usage_metadata,
};
}
}

export function isAIMessage(x: BaseMessage): x is AIMessage {
Expand Down Expand Up @@ -234,6 +243,16 @@ export class AIMessageChunk extends BaseMessageChunk {
return "ai";
}

override get _printableFields(): Record<string, unknown> {
return {
...super._printableFields,
tool_calls: this.tool_calls,
tool_call_chunks: this.tool_call_chunks,
invalid_tool_calls: this.invalid_tool_calls,
usage_metadata: this.usage_metadata,
};
}

concat(chunk: AIMessageChunk) {
const combinedFields: AIMessageChunkFields = {
content: mergeContent(this.content, chunk.content),
Expand Down
61 changes: 61 additions & 0 deletions langchain-core/src/messages/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,34 @@ export function mergeContent(
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function stringifyWithDepthLimit(obj: any, depthLimit: number): string {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function helper(obj: any, currentDepth: number): any {
if (typeof obj !== "object" || obj === null || obj === undefined) {
return obj;
}
if (currentDepth >= depthLimit) {
if (Array.isArray(obj)) {
return "[Array]";
}
return "[Object]";
}

if (Array.isArray(obj)) {
return obj.map((item) => helper(item, currentDepth + 1));
}

const result: Record<string, unknown> = {};
for (const key of Object.keys(obj)) {
result[key] = helper(obj[key], currentDepth + 1);
}
return result;
}

return JSON.stringify(helper(obj, 0), null, 2);
}

/**
* Base class for all types of messages in a conversation. It includes
* properties like `content`, `name`, and `additional_kwargs`. It also
Expand Down Expand Up @@ -227,6 +255,39 @@ export abstract class BaseMessage
.kwargs as StoredMessageData,
};
}

static lc_name() {
return "BaseMessage";
}

// Can't be protected for silly reasons
get _printableFields(): Record<string, unknown> {
return {
id: this.id,
content: this.content,
name: this.name,
additional_kwargs: this.additional_kwargs,
response_metadata: this.response_metadata,
};
}

get [Symbol.toStringTag]() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (this.constructor as any).lc_name();
}

// Override the default behavior of console.log
[Symbol.for("nodejs.util.inspect.custom")](depth: number | null) {
if (depth === null) {
return this;
}
const printable = stringifyWithDepthLimit(
this._printableFields,
Math.max(4, depth)
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return `${(this.constructor as any).lc_name()} ${printable}`;
}
}

// TODO: Deprecate when SDK typing is updated
Expand Down
14 changes: 14 additions & 0 deletions langchain-core/src/messages/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ export class ChatMessage
static isInstance(message: BaseMessage): message is ChatMessage {
return message._getType() === "generic";
}

override get _printableFields(): Record<string, unknown> {
return {
...super._printableFields,
role: this.role,
};
}
}

/**
Expand Down Expand Up @@ -93,4 +100,11 @@ export class ChatMessageChunk extends BaseMessageChunk {
id: this.id ?? chunk.id,
});
}

override get _printableFields(): Record<string, unknown> {
return {
...super._printableFields,
role: this.role,
};
}
}
7 changes: 7 additions & 0 deletions langchain-core/src/messages/modifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ export class RemoveMessage extends BaseMessage {
_getType(): MessageType {
return "remove";
}

override get _printableFields(): Record<string, unknown> {
return {
...super._printableFields,
id: this.id,
};
}
}
16 changes: 16 additions & 0 deletions langchain-core/src/messages/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ export class ToolMessage extends BaseMessage {
static isInstance(message: BaseMessage): message is ToolMessage {
return message._getType() === "tool";
}

override get _printableFields(): Record<string, unknown> {
return {
...super._printableFields,
tool_call_id: this.tool_call_id,
artifact: this.artifact,
};
}
}

/**
Expand Down Expand Up @@ -124,6 +132,14 @@ export class ToolMessageChunk extends BaseMessageChunk {
id: this.id ?? chunk.id,
});
}

override get _printableFields(): Record<string, unknown> {
return {
...super._printableFields,
tool_call_id: this.tool_call_id,
artifact: this.artifact,
};
}
}

/**
Expand Down
Loading