@langchain/core 0.1.55-rc.0 → 0.1.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents.d.ts +1 -1
- package/dist/caches.d.ts +1 -1
- package/dist/language_models/chat_models.d.ts +14 -3
- package/dist/messages/ai.cjs +213 -0
- package/dist/messages/ai.d.ts +37 -0
- package/dist/messages/ai.js +207 -0
- package/dist/messages/base.cjs +212 -0
- package/dist/messages/base.d.ts +137 -0
- package/dist/messages/base.js +201 -0
- package/dist/messages/chat.cjs +71 -0
- package/dist/messages/chat.d.ts +28 -0
- package/dist/messages/chat.js +66 -0
- package/dist/messages/function.cjs +46 -0
- package/dist/messages/function.d.ts +24 -0
- package/dist/messages/function.js +41 -0
- package/dist/messages/human.cjs +36 -0
- package/dist/messages/human.d.ts +17 -0
- package/dist/messages/human.js +31 -0
- package/dist/messages/index.cjs +27 -633
- package/dist/messages/index.d.ts +8 -273
- package/dist/messages/index.js +10 -611
- package/dist/messages/system.cjs +36 -0
- package/dist/messages/system.d.ts +17 -0
- package/dist/messages/system.js +31 -0
- package/dist/messages/tool.cjs +101 -0
- package/dist/messages/tool.d.ts +101 -0
- package/dist/messages/tool.js +95 -0
- package/dist/messages/utils.cjs +172 -0
- package/dist/messages/utils.d.ts +31 -0
- package/dist/messages/utils.js +163 -0
- package/dist/output_parsers/json.cjs +6 -93
- package/dist/output_parsers/json.d.ts +2 -2
- package/dist/output_parsers/json.js +2 -88
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +79 -13
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +18 -0
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +75 -12
- package/dist/output_parsers/transform.cjs +7 -6
- package/dist/output_parsers/transform.d.ts +1 -1
- package/dist/output_parsers/transform.js +3 -2
- package/dist/utils/function_calling.cjs +18 -6
- package/dist/utils/function_calling.d.ts +2 -1
- package/dist/utils/function_calling.js +16 -5
- package/dist/utils/json.cjs +93 -0
- package/dist/utils/json.d.ts +2 -0
- package/dist/utils/json.js +88 -0
- package/dist/utils/testing/index.cjs +3 -0
- package/dist/utils/testing/index.js +3 -0
- package/messages/tool.cjs +1 -0
- package/messages/tool.d.cts +1 -0
- package/messages/tool.d.ts +1 -0
- package/messages/tool.js +1 -0
- package/package.json +14 -1
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBaseMessageChunk = exports.isBaseMessage = exports.BaseMessageChunk = exports._mergeLists = exports._mergeDicts = exports.isOpenAIToolCallArray = exports.BaseMessage = exports.mergeContent = void 0;
|
|
4
|
+
const serializable_js_1 = require("../load/serializable.cjs");
|
|
5
|
+
function mergeContent(firstContent, secondContent) {
|
|
6
|
+
// If first content is a string
|
|
7
|
+
if (typeof firstContent === "string") {
|
|
8
|
+
if (typeof secondContent === "string") {
|
|
9
|
+
return firstContent + secondContent;
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
return [{ type: "text", text: firstContent }, ...secondContent];
|
|
13
|
+
}
|
|
14
|
+
// If both are arrays
|
|
15
|
+
}
|
|
16
|
+
else if (Array.isArray(secondContent)) {
|
|
17
|
+
return [...firstContent, ...secondContent];
|
|
18
|
+
// If the first content is a list and second is a string
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
// Otherwise, add the second content as a new element of the list
|
|
22
|
+
return [...firstContent, { type: "text", text: secondContent }];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.mergeContent = mergeContent;
|
|
26
|
+
/**
|
|
27
|
+
* Base class for all types of messages in a conversation. It includes
|
|
28
|
+
* properties like `content`, `name`, and `additional_kwargs`. It also
|
|
29
|
+
* includes methods like `toDict()` and `_getType()`.
|
|
30
|
+
*/
|
|
31
|
+
class BaseMessage extends serializable_js_1.Serializable {
|
|
32
|
+
get lc_aliases() {
|
|
33
|
+
// exclude snake case conversion to pascal case
|
|
34
|
+
return {
|
|
35
|
+
additional_kwargs: "additional_kwargs",
|
|
36
|
+
response_metadata: "response_metadata",
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @deprecated
|
|
41
|
+
* Use {@link BaseMessage.content} instead.
|
|
42
|
+
*/
|
|
43
|
+
get text() {
|
|
44
|
+
return typeof this.content === "string" ? this.content : "";
|
|
45
|
+
}
|
|
46
|
+
constructor(fields,
|
|
47
|
+
/** @deprecated */
|
|
48
|
+
kwargs) {
|
|
49
|
+
if (typeof fields === "string") {
|
|
50
|
+
// eslint-disable-next-line no-param-reassign
|
|
51
|
+
fields = {
|
|
52
|
+
content: fields,
|
|
53
|
+
additional_kwargs: kwargs,
|
|
54
|
+
response_metadata: {},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
// Make sure the default value for additional_kwargs is passed into super() for serialization
|
|
58
|
+
if (!fields.additional_kwargs) {
|
|
59
|
+
// eslint-disable-next-line no-param-reassign
|
|
60
|
+
fields.additional_kwargs = {};
|
|
61
|
+
}
|
|
62
|
+
if (!fields.response_metadata) {
|
|
63
|
+
// eslint-disable-next-line no-param-reassign
|
|
64
|
+
fields.response_metadata = {};
|
|
65
|
+
}
|
|
66
|
+
super(fields);
|
|
67
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: ["langchain_core", "messages"]
|
|
72
|
+
});
|
|
73
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: true
|
|
78
|
+
});
|
|
79
|
+
/** The content of the message. */
|
|
80
|
+
Object.defineProperty(this, "content", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true,
|
|
84
|
+
value: void 0
|
|
85
|
+
});
|
|
86
|
+
/** The name of the message sender in a multi-user chat. */
|
|
87
|
+
Object.defineProperty(this, "name", {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
configurable: true,
|
|
90
|
+
writable: true,
|
|
91
|
+
value: void 0
|
|
92
|
+
});
|
|
93
|
+
/** Additional keyword arguments */
|
|
94
|
+
Object.defineProperty(this, "additional_kwargs", {
|
|
95
|
+
enumerable: true,
|
|
96
|
+
configurable: true,
|
|
97
|
+
writable: true,
|
|
98
|
+
value: void 0
|
|
99
|
+
});
|
|
100
|
+
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
101
|
+
Object.defineProperty(this, "response_metadata", {
|
|
102
|
+
enumerable: true,
|
|
103
|
+
configurable: true,
|
|
104
|
+
writable: true,
|
|
105
|
+
value: void 0
|
|
106
|
+
});
|
|
107
|
+
this.name = fields.name;
|
|
108
|
+
this.content = fields.content;
|
|
109
|
+
this.additional_kwargs = fields.additional_kwargs;
|
|
110
|
+
this.response_metadata = fields.response_metadata;
|
|
111
|
+
}
|
|
112
|
+
toDict() {
|
|
113
|
+
return {
|
|
114
|
+
type: this._getType(),
|
|
115
|
+
data: this.toJSON()
|
|
116
|
+
.kwargs,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.BaseMessage = BaseMessage;
|
|
121
|
+
function isOpenAIToolCallArray(value) {
|
|
122
|
+
return (Array.isArray(value) &&
|
|
123
|
+
value.every((v) => typeof v.index === "number"));
|
|
124
|
+
}
|
|
125
|
+
exports.isOpenAIToolCallArray = isOpenAIToolCallArray;
|
|
126
|
+
function _mergeDicts(
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
128
|
+
left,
|
|
129
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
130
|
+
right
|
|
131
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
132
|
+
) {
|
|
133
|
+
const merged = { ...left };
|
|
134
|
+
for (const [key, value] of Object.entries(right)) {
|
|
135
|
+
if (merged[key] == null) {
|
|
136
|
+
merged[key] = value;
|
|
137
|
+
}
|
|
138
|
+
else if (value == null) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
else if (typeof merged[key] !== typeof value ||
|
|
142
|
+
Array.isArray(merged[key]) !== Array.isArray(value)) {
|
|
143
|
+
throw new Error(`field[${key}] already exists in the message chunk, but with a different type.`);
|
|
144
|
+
}
|
|
145
|
+
else if (typeof merged[key] === "string") {
|
|
146
|
+
merged[key] = merged[key] + value;
|
|
147
|
+
}
|
|
148
|
+
else if (!Array.isArray(merged[key]) && typeof merged[key] === "object") {
|
|
149
|
+
merged[key] = _mergeDicts(merged[key], value);
|
|
150
|
+
}
|
|
151
|
+
else if (Array.isArray(merged[key])) {
|
|
152
|
+
merged[key] = _mergeLists(merged[key], value);
|
|
153
|
+
}
|
|
154
|
+
else if (merged[key] === value) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
console.warn(`field[${key}] already exists in this message chunk and value has unsupported type.`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return merged;
|
|
162
|
+
}
|
|
163
|
+
exports._mergeDicts = _mergeDicts;
|
|
164
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
165
|
+
function _mergeLists(left, right) {
|
|
166
|
+
if (left === undefined && right === undefined) {
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
else if (left === undefined || right === undefined) {
|
|
170
|
+
return left || right;
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
const merged = [...left];
|
|
174
|
+
for (const item of right) {
|
|
175
|
+
if (typeof item === "object" &&
|
|
176
|
+
"index" in item &&
|
|
177
|
+
typeof item.index === "number") {
|
|
178
|
+
const toMerge = merged.findIndex((leftItem) => leftItem.index === item.index);
|
|
179
|
+
if (toMerge !== -1) {
|
|
180
|
+
merged[toMerge] = _mergeDicts(merged[toMerge], item);
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
merged.push(item);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
merged.push(item);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return merged;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports._mergeLists = _mergeLists;
|
|
194
|
+
/**
|
|
195
|
+
* Represents a chunk of a message, which can be concatenated with other
|
|
196
|
+
* message chunks. It includes a method `_merge_kwargs_dict()` for merging
|
|
197
|
+
* additional keyword arguments from another `BaseMessageChunk` into this
|
|
198
|
+
* one. It also overrides the `__add__()` method to support concatenation
|
|
199
|
+
* of `BaseMessageChunk` instances.
|
|
200
|
+
*/
|
|
201
|
+
class BaseMessageChunk extends BaseMessage {
|
|
202
|
+
}
|
|
203
|
+
exports.BaseMessageChunk = BaseMessageChunk;
|
|
204
|
+
function isBaseMessage(messageLike) {
|
|
205
|
+
return typeof messageLike?._getType === "function";
|
|
206
|
+
}
|
|
207
|
+
exports.isBaseMessage = isBaseMessage;
|
|
208
|
+
function isBaseMessageChunk(messageLike) {
|
|
209
|
+
return (isBaseMessage(messageLike) &&
|
|
210
|
+
typeof messageLike.concat === "function");
|
|
211
|
+
}
|
|
212
|
+
exports.isBaseMessageChunk = isBaseMessageChunk;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Serializable } from "../load/serializable.js";
|
|
2
|
+
import { StringWithAutocomplete } from "../utils/types/index.js";
|
|
3
|
+
export interface StoredMessageData {
|
|
4
|
+
content: string;
|
|
5
|
+
role: string | undefined;
|
|
6
|
+
name: string | undefined;
|
|
7
|
+
tool_call_id: string | undefined;
|
|
8
|
+
additional_kwargs?: Record<string, any>;
|
|
9
|
+
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
10
|
+
response_metadata?: Record<string, any>;
|
|
11
|
+
}
|
|
12
|
+
export interface StoredMessage {
|
|
13
|
+
type: string;
|
|
14
|
+
data: StoredMessageData;
|
|
15
|
+
}
|
|
16
|
+
export interface StoredGeneration {
|
|
17
|
+
text: string;
|
|
18
|
+
message?: StoredMessage;
|
|
19
|
+
}
|
|
20
|
+
export interface StoredMessageV1 {
|
|
21
|
+
type: string;
|
|
22
|
+
role: string | undefined;
|
|
23
|
+
text: string;
|
|
24
|
+
}
|
|
25
|
+
export type MessageType = "human" | "ai" | "generic" | "system" | "function" | "tool";
|
|
26
|
+
export type ImageDetail = "auto" | "low" | "high";
|
|
27
|
+
export type MessageContentText = {
|
|
28
|
+
type: "text";
|
|
29
|
+
text: string;
|
|
30
|
+
};
|
|
31
|
+
export type MessageContentImageUrl = {
|
|
32
|
+
type: "image_url";
|
|
33
|
+
image_url: string | {
|
|
34
|
+
url: string;
|
|
35
|
+
detail?: ImageDetail;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export type MessageContentComplex = MessageContentText | MessageContentImageUrl | (Record<string, any> & {
|
|
39
|
+
type?: "text" | "image_url" | string;
|
|
40
|
+
}) | (Record<string, any> & {
|
|
41
|
+
type?: never;
|
|
42
|
+
});
|
|
43
|
+
export type MessageContent = string | MessageContentComplex[];
|
|
44
|
+
export interface FunctionCall {
|
|
45
|
+
/**
|
|
46
|
+
* The arguments to call the function with, as generated by the model in JSON
|
|
47
|
+
* format. Note that the model does not always generate valid JSON, and may
|
|
48
|
+
* hallucinate parameters not defined by your function schema. Validate the
|
|
49
|
+
* arguments in your code before calling your function.
|
|
50
|
+
*/
|
|
51
|
+
arguments: string;
|
|
52
|
+
/**
|
|
53
|
+
* The name of the function to call.
|
|
54
|
+
*/
|
|
55
|
+
name: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @deprecated
|
|
59
|
+
* Import as "OpenAIToolCall" instead
|
|
60
|
+
*/
|
|
61
|
+
export interface ToolCall {
|
|
62
|
+
/**
|
|
63
|
+
* The ID of the tool call.
|
|
64
|
+
*/
|
|
65
|
+
id: string;
|
|
66
|
+
/**
|
|
67
|
+
* The function that the model called.
|
|
68
|
+
*/
|
|
69
|
+
function: FunctionCall;
|
|
70
|
+
/**
|
|
71
|
+
* The type of the tool. Currently, only `function` is supported.
|
|
72
|
+
*/
|
|
73
|
+
type: "function";
|
|
74
|
+
}
|
|
75
|
+
export type BaseMessageFields = {
|
|
76
|
+
content: MessageContent;
|
|
77
|
+
name?: string;
|
|
78
|
+
additional_kwargs?: {
|
|
79
|
+
function_call?: FunctionCall;
|
|
80
|
+
tool_calls?: ToolCall[];
|
|
81
|
+
[key: string]: unknown;
|
|
82
|
+
};
|
|
83
|
+
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
84
|
+
response_metadata?: Record<string, any>;
|
|
85
|
+
};
|
|
86
|
+
export declare function mergeContent(firstContent: MessageContent, secondContent: MessageContent): MessageContent;
|
|
87
|
+
/**
|
|
88
|
+
* Base class for all types of messages in a conversation. It includes
|
|
89
|
+
* properties like `content`, `name`, and `additional_kwargs`. It also
|
|
90
|
+
* includes methods like `toDict()` and `_getType()`.
|
|
91
|
+
*/
|
|
92
|
+
export declare abstract class BaseMessage extends Serializable implements BaseMessageFields {
|
|
93
|
+
lc_namespace: string[];
|
|
94
|
+
lc_serializable: boolean;
|
|
95
|
+
get lc_aliases(): Record<string, string>;
|
|
96
|
+
/**
|
|
97
|
+
* @deprecated
|
|
98
|
+
* Use {@link BaseMessage.content} instead.
|
|
99
|
+
*/
|
|
100
|
+
get text(): string;
|
|
101
|
+
/** The content of the message. */
|
|
102
|
+
content: MessageContent;
|
|
103
|
+
/** The name of the message sender in a multi-user chat. */
|
|
104
|
+
name?: string;
|
|
105
|
+
/** Additional keyword arguments */
|
|
106
|
+
additional_kwargs: NonNullable<BaseMessageFields["additional_kwargs"]>;
|
|
107
|
+
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
108
|
+
response_metadata: NonNullable<BaseMessageFields["response_metadata"]>;
|
|
109
|
+
/** The type of the message. */
|
|
110
|
+
abstract _getType(): MessageType;
|
|
111
|
+
constructor(fields: string | BaseMessageFields,
|
|
112
|
+
/** @deprecated */
|
|
113
|
+
kwargs?: Record<string, unknown>);
|
|
114
|
+
toDict(): StoredMessage;
|
|
115
|
+
}
|
|
116
|
+
export type OpenAIToolCall = ToolCall & {
|
|
117
|
+
index: number;
|
|
118
|
+
};
|
|
119
|
+
export declare function isOpenAIToolCallArray(value?: unknown): value is OpenAIToolCall[];
|
|
120
|
+
export declare function _mergeDicts(left: Record<string, any>, right: Record<string, any>): Record<string, any>;
|
|
121
|
+
export declare function _mergeLists(left?: any[], right?: any[]): any[] | undefined;
|
|
122
|
+
/**
|
|
123
|
+
* Represents a chunk of a message, which can be concatenated with other
|
|
124
|
+
* message chunks. It includes a method `_merge_kwargs_dict()` for merging
|
|
125
|
+
* additional keyword arguments from another `BaseMessageChunk` into this
|
|
126
|
+
* one. It also overrides the `__add__()` method to support concatenation
|
|
127
|
+
* of `BaseMessageChunk` instances.
|
|
128
|
+
*/
|
|
129
|
+
export declare abstract class BaseMessageChunk extends BaseMessage {
|
|
130
|
+
abstract concat(chunk: BaseMessageChunk): BaseMessageChunk;
|
|
131
|
+
}
|
|
132
|
+
export type BaseMessageLike = BaseMessage | [
|
|
133
|
+
StringWithAutocomplete<MessageType | "user" | "assistant" | "placeholder">,
|
|
134
|
+
MessageContent
|
|
135
|
+
] | string;
|
|
136
|
+
export declare function isBaseMessage(messageLike?: unknown): messageLike is BaseMessage;
|
|
137
|
+
export declare function isBaseMessageChunk(messageLike?: unknown): messageLike is BaseMessageChunk;
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { Serializable } from "../load/serializable.js";
|
|
2
|
+
export function mergeContent(firstContent, secondContent) {
|
|
3
|
+
// If first content is a string
|
|
4
|
+
if (typeof firstContent === "string") {
|
|
5
|
+
if (typeof secondContent === "string") {
|
|
6
|
+
return firstContent + secondContent;
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
return [{ type: "text", text: firstContent }, ...secondContent];
|
|
10
|
+
}
|
|
11
|
+
// If both are arrays
|
|
12
|
+
}
|
|
13
|
+
else if (Array.isArray(secondContent)) {
|
|
14
|
+
return [...firstContent, ...secondContent];
|
|
15
|
+
// If the first content is a list and second is a string
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
// Otherwise, add the second content as a new element of the list
|
|
19
|
+
return [...firstContent, { type: "text", text: secondContent }];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Base class for all types of messages in a conversation. It includes
|
|
24
|
+
* properties like `content`, `name`, and `additional_kwargs`. It also
|
|
25
|
+
* includes methods like `toDict()` and `_getType()`.
|
|
26
|
+
*/
|
|
27
|
+
export class BaseMessage extends Serializable {
|
|
28
|
+
get lc_aliases() {
|
|
29
|
+
// exclude snake case conversion to pascal case
|
|
30
|
+
return {
|
|
31
|
+
additional_kwargs: "additional_kwargs",
|
|
32
|
+
response_metadata: "response_metadata",
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @deprecated
|
|
37
|
+
* Use {@link BaseMessage.content} instead.
|
|
38
|
+
*/
|
|
39
|
+
get text() {
|
|
40
|
+
return typeof this.content === "string" ? this.content : "";
|
|
41
|
+
}
|
|
42
|
+
constructor(fields,
|
|
43
|
+
/** @deprecated */
|
|
44
|
+
kwargs) {
|
|
45
|
+
if (typeof fields === "string") {
|
|
46
|
+
// eslint-disable-next-line no-param-reassign
|
|
47
|
+
fields = {
|
|
48
|
+
content: fields,
|
|
49
|
+
additional_kwargs: kwargs,
|
|
50
|
+
response_metadata: {},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
// Make sure the default value for additional_kwargs is passed into super() for serialization
|
|
54
|
+
if (!fields.additional_kwargs) {
|
|
55
|
+
// eslint-disable-next-line no-param-reassign
|
|
56
|
+
fields.additional_kwargs = {};
|
|
57
|
+
}
|
|
58
|
+
if (!fields.response_metadata) {
|
|
59
|
+
// eslint-disable-next-line no-param-reassign
|
|
60
|
+
fields.response_metadata = {};
|
|
61
|
+
}
|
|
62
|
+
super(fields);
|
|
63
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
64
|
+
enumerable: true,
|
|
65
|
+
configurable: true,
|
|
66
|
+
writable: true,
|
|
67
|
+
value: ["langchain_core", "messages"]
|
|
68
|
+
});
|
|
69
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
70
|
+
enumerable: true,
|
|
71
|
+
configurable: true,
|
|
72
|
+
writable: true,
|
|
73
|
+
value: true
|
|
74
|
+
});
|
|
75
|
+
/** The content of the message. */
|
|
76
|
+
Object.defineProperty(this, "content", {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
configurable: true,
|
|
79
|
+
writable: true,
|
|
80
|
+
value: void 0
|
|
81
|
+
});
|
|
82
|
+
/** The name of the message sender in a multi-user chat. */
|
|
83
|
+
Object.defineProperty(this, "name", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: void 0
|
|
88
|
+
});
|
|
89
|
+
/** Additional keyword arguments */
|
|
90
|
+
Object.defineProperty(this, "additional_kwargs", {
|
|
91
|
+
enumerable: true,
|
|
92
|
+
configurable: true,
|
|
93
|
+
writable: true,
|
|
94
|
+
value: void 0
|
|
95
|
+
});
|
|
96
|
+
/** Response metadata. For example: response headers, logprobs, token counts. */
|
|
97
|
+
Object.defineProperty(this, "response_metadata", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
configurable: true,
|
|
100
|
+
writable: true,
|
|
101
|
+
value: void 0
|
|
102
|
+
});
|
|
103
|
+
this.name = fields.name;
|
|
104
|
+
this.content = fields.content;
|
|
105
|
+
this.additional_kwargs = fields.additional_kwargs;
|
|
106
|
+
this.response_metadata = fields.response_metadata;
|
|
107
|
+
}
|
|
108
|
+
toDict() {
|
|
109
|
+
return {
|
|
110
|
+
type: this._getType(),
|
|
111
|
+
data: this.toJSON()
|
|
112
|
+
.kwargs,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export function isOpenAIToolCallArray(value) {
|
|
117
|
+
return (Array.isArray(value) &&
|
|
118
|
+
value.every((v) => typeof v.index === "number"));
|
|
119
|
+
}
|
|
120
|
+
export function _mergeDicts(
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
122
|
+
left,
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
124
|
+
right
|
|
125
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
126
|
+
) {
|
|
127
|
+
const merged = { ...left };
|
|
128
|
+
for (const [key, value] of Object.entries(right)) {
|
|
129
|
+
if (merged[key] == null) {
|
|
130
|
+
merged[key] = value;
|
|
131
|
+
}
|
|
132
|
+
else if (value == null) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
else if (typeof merged[key] !== typeof value ||
|
|
136
|
+
Array.isArray(merged[key]) !== Array.isArray(value)) {
|
|
137
|
+
throw new Error(`field[${key}] already exists in the message chunk, but with a different type.`);
|
|
138
|
+
}
|
|
139
|
+
else if (typeof merged[key] === "string") {
|
|
140
|
+
merged[key] = merged[key] + value;
|
|
141
|
+
}
|
|
142
|
+
else if (!Array.isArray(merged[key]) && typeof merged[key] === "object") {
|
|
143
|
+
merged[key] = _mergeDicts(merged[key], value);
|
|
144
|
+
}
|
|
145
|
+
else if (Array.isArray(merged[key])) {
|
|
146
|
+
merged[key] = _mergeLists(merged[key], value);
|
|
147
|
+
}
|
|
148
|
+
else if (merged[key] === value) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
console.warn(`field[${key}] already exists in this message chunk and value has unsupported type.`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return merged;
|
|
156
|
+
}
|
|
157
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
158
|
+
export function _mergeLists(left, right) {
|
|
159
|
+
if (left === undefined && right === undefined) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
else if (left === undefined || right === undefined) {
|
|
163
|
+
return left || right;
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
const merged = [...left];
|
|
167
|
+
for (const item of right) {
|
|
168
|
+
if (typeof item === "object" &&
|
|
169
|
+
"index" in item &&
|
|
170
|
+
typeof item.index === "number") {
|
|
171
|
+
const toMerge = merged.findIndex((leftItem) => leftItem.index === item.index);
|
|
172
|
+
if (toMerge !== -1) {
|
|
173
|
+
merged[toMerge] = _mergeDicts(merged[toMerge], item);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
merged.push(item);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
merged.push(item);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return merged;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Represents a chunk of a message, which can be concatenated with other
|
|
188
|
+
* message chunks. It includes a method `_merge_kwargs_dict()` for merging
|
|
189
|
+
* additional keyword arguments from another `BaseMessageChunk` into this
|
|
190
|
+
* one. It also overrides the `__add__()` method to support concatenation
|
|
191
|
+
* of `BaseMessageChunk` instances.
|
|
192
|
+
*/
|
|
193
|
+
export class BaseMessageChunk extends BaseMessage {
|
|
194
|
+
}
|
|
195
|
+
export function isBaseMessage(messageLike) {
|
|
196
|
+
return typeof messageLike?._getType === "function";
|
|
197
|
+
}
|
|
198
|
+
export function isBaseMessageChunk(messageLike) {
|
|
199
|
+
return (isBaseMessage(messageLike) &&
|
|
200
|
+
typeof messageLike.concat === "function");
|
|
201
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChatMessageChunk = exports.ChatMessage = void 0;
|
|
4
|
+
const base_js_1 = require("./base.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Represents a chat message in a conversation.
|
|
7
|
+
*/
|
|
8
|
+
class ChatMessage extends base_js_1.BaseMessage {
|
|
9
|
+
static lc_name() {
|
|
10
|
+
return "ChatMessage";
|
|
11
|
+
}
|
|
12
|
+
static _chatMessageClass() {
|
|
13
|
+
return ChatMessage;
|
|
14
|
+
}
|
|
15
|
+
constructor(fields, role) {
|
|
16
|
+
if (typeof fields === "string") {
|
|
17
|
+
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
|
|
18
|
+
fields = { content: fields, role: role };
|
|
19
|
+
}
|
|
20
|
+
super(fields);
|
|
21
|
+
Object.defineProperty(this, "role", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: void 0
|
|
26
|
+
});
|
|
27
|
+
this.role = fields.role;
|
|
28
|
+
}
|
|
29
|
+
_getType() {
|
|
30
|
+
return "generic";
|
|
31
|
+
}
|
|
32
|
+
static isInstance(message) {
|
|
33
|
+
return message._getType() === "generic";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.ChatMessage = ChatMessage;
|
|
37
|
+
/**
|
|
38
|
+
* Represents a chunk of a chat message, which can be concatenated with
|
|
39
|
+
* other chat message chunks.
|
|
40
|
+
*/
|
|
41
|
+
class ChatMessageChunk extends base_js_1.BaseMessageChunk {
|
|
42
|
+
static lc_name() {
|
|
43
|
+
return "ChatMessageChunk";
|
|
44
|
+
}
|
|
45
|
+
constructor(fields, role) {
|
|
46
|
+
if (typeof fields === "string") {
|
|
47
|
+
// eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
|
|
48
|
+
fields = { content: fields, role: role };
|
|
49
|
+
}
|
|
50
|
+
super(fields);
|
|
51
|
+
Object.defineProperty(this, "role", {
|
|
52
|
+
enumerable: true,
|
|
53
|
+
configurable: true,
|
|
54
|
+
writable: true,
|
|
55
|
+
value: void 0
|
|
56
|
+
});
|
|
57
|
+
this.role = fields.role;
|
|
58
|
+
}
|
|
59
|
+
_getType() {
|
|
60
|
+
return "generic";
|
|
61
|
+
}
|
|
62
|
+
concat(chunk) {
|
|
63
|
+
return new ChatMessageChunk({
|
|
64
|
+
content: (0, base_js_1.mergeContent)(this.content, chunk.content),
|
|
65
|
+
additional_kwargs: (0, base_js_1._mergeDicts)(this.additional_kwargs, chunk.additional_kwargs),
|
|
66
|
+
response_metadata: (0, base_js_1._mergeDicts)(this.response_metadata, chunk.response_metadata),
|
|
67
|
+
role: this.role,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.ChatMessageChunk = ChatMessageChunk;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { BaseMessage, BaseMessageChunk, type BaseMessageFields, type MessageType } from "./base.js";
|
|
2
|
+
export interface ChatMessageFieldsWithRole extends BaseMessageFields {
|
|
3
|
+
role: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Represents a chat message in a conversation.
|
|
7
|
+
*/
|
|
8
|
+
export declare class ChatMessage extends BaseMessage implements ChatMessageFieldsWithRole {
|
|
9
|
+
static lc_name(): string;
|
|
10
|
+
role: string;
|
|
11
|
+
static _chatMessageClass(): typeof ChatMessage;
|
|
12
|
+
constructor(content: string, role: string);
|
|
13
|
+
constructor(fields: ChatMessageFieldsWithRole);
|
|
14
|
+
_getType(): MessageType;
|
|
15
|
+
static isInstance(message: BaseMessage): message is ChatMessage;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Represents a chunk of a chat message, which can be concatenated with
|
|
19
|
+
* other chat message chunks.
|
|
20
|
+
*/
|
|
21
|
+
export declare class ChatMessageChunk extends BaseMessageChunk {
|
|
22
|
+
static lc_name(): string;
|
|
23
|
+
role: string;
|
|
24
|
+
constructor(content: string, role: string);
|
|
25
|
+
constructor(fields: ChatMessageFieldsWithRole);
|
|
26
|
+
_getType(): MessageType;
|
|
27
|
+
concat(chunk: ChatMessageChunk): ChatMessageChunk;
|
|
28
|
+
}
|