langchain 0.0.178 → 0.0.180

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.
Files changed (84) hide show
  1. package/chains/combine_documents/reduce.cjs +1 -0
  2. package/chains/combine_documents/reduce.d.ts +1 -0
  3. package/chains/combine_documents/reduce.js +1 -0
  4. package/chat_models/bedrock/web.cjs +1 -0
  5. package/chat_models/bedrock/web.d.ts +1 -0
  6. package/chat_models/bedrock/web.js +1 -0
  7. package/chat_models/bedrock.cjs +1 -1
  8. package/chat_models/bedrock.d.ts +1 -1
  9. package/chat_models/bedrock.js +1 -1
  10. package/dist/cache/base.d.ts +1 -1
  11. package/dist/callbacks/index.d.ts +1 -1
  12. package/dist/chains/combine_documents/reduce.cjs +69 -0
  13. package/dist/chains/combine_documents/reduce.d.ts +28 -0
  14. package/dist/chains/combine_documents/reduce.js +64 -0
  15. package/dist/chat_models/baiduwenxin.cjs +16 -2
  16. package/dist/chat_models/baiduwenxin.js +16 -2
  17. package/dist/chat_models/bedrock/index.cjs +24 -0
  18. package/dist/chat_models/bedrock/index.d.ts +12 -0
  19. package/dist/chat_models/bedrock/index.js +18 -0
  20. package/dist/chat_models/{bedrock.cjs → bedrock/web.cjs} +44 -15
  21. package/dist/chat_models/{bedrock.d.ts → bedrock/web.d.ts} +14 -5
  22. package/dist/chat_models/{bedrock.js → bedrock/web.js} +42 -13
  23. package/dist/chat_models/iflytek_xinghuo/index.cjs +1 -1
  24. package/dist/chat_models/iflytek_xinghuo/index.js +1 -1
  25. package/dist/llms/bedrock/index.cjs +17 -0
  26. package/dist/llms/bedrock/index.d.ts +7 -0
  27. package/dist/llms/bedrock/index.js +13 -0
  28. package/dist/llms/{bedrock.cjs → bedrock/web.cjs} +46 -12
  29. package/dist/llms/{bedrock.d.ts → bedrock/web.d.ts} +17 -4
  30. package/dist/llms/{bedrock.js → bedrock/web.js} +46 -12
  31. package/dist/load/import_constants.cjs +3 -0
  32. package/dist/load/import_constants.js +3 -0
  33. package/dist/load/import_map.cjs +4 -2
  34. package/dist/load/import_map.d.ts +2 -0
  35. package/dist/load/import_map.js +2 -0
  36. package/dist/output_parsers/json.cjs +77 -0
  37. package/dist/output_parsers/json.d.ts +1 -0
  38. package/dist/output_parsers/json.js +73 -0
  39. package/dist/output_parsers/openai_functions.cjs +37 -2
  40. package/dist/output_parsers/openai_functions.d.ts +10 -5
  41. package/dist/output_parsers/openai_functions.js +38 -3
  42. package/dist/schema/index.cjs +33 -1
  43. package/dist/schema/index.d.ts +3 -1
  44. package/dist/schema/index.js +31 -0
  45. package/dist/schema/output_parser.cjs +63 -3
  46. package/dist/schema/output_parser.d.ts +16 -1
  47. package/dist/schema/output_parser.js +59 -0
  48. package/dist/schema/prompt_template.cjs +33 -0
  49. package/dist/schema/prompt_template.d.ts +12 -0
  50. package/dist/schema/prompt_template.js +29 -0
  51. package/dist/storage/convex.d.ts +21 -0
  52. package/dist/stores/message/cassandra.cjs +135 -0
  53. package/dist/stores/message/cassandra.d.ts +44 -0
  54. package/dist/stores/message/cassandra.js +131 -0
  55. package/dist/stores/message/convex.d.ts +21 -0
  56. package/dist/util/bedrock.cjs +13 -1
  57. package/dist/util/bedrock.d.ts +5 -2
  58. package/dist/util/bedrock.js +13 -1
  59. package/dist/util/fast-json-patch/index.cjs +1 -0
  60. package/dist/util/fast-json-patch/index.d.ts +1 -0
  61. package/dist/util/fast-json-patch/index.js +1 -0
  62. package/dist/util/fast-json-patch/src/duplex.cjs +237 -0
  63. package/dist/util/fast-json-patch/src/duplex.d.ts +23 -0
  64. package/dist/util/fast-json-patch/src/duplex.js +230 -0
  65. package/dist/vectorstores/cassandra.cjs +197 -47
  66. package/dist/vectorstores/cassandra.d.ts +47 -4
  67. package/dist/vectorstores/cassandra.js +197 -47
  68. package/dist/vectorstores/convex.d.ts +21 -0
  69. package/llms/bedrock/web.cjs +1 -0
  70. package/llms/bedrock/web.d.ts +1 -0
  71. package/llms/bedrock/web.js +1 -0
  72. package/llms/bedrock.cjs +1 -1
  73. package/llms/bedrock.d.ts +1 -1
  74. package/llms/bedrock.js +1 -1
  75. package/package.json +41 -1
  76. package/schema/prompt_template.cjs +1 -0
  77. package/schema/prompt_template.d.ts +1 -0
  78. package/schema/prompt_template.js +1 -0
  79. package/stores/message/cassandra.cjs +1 -0
  80. package/stores/message/cassandra.d.ts +1 -0
  81. package/stores/message/cassandra.js +1 -0
  82. package/dist/schema/runnable/remote.cjs +0 -225
  83. package/dist/schema/runnable/remote.d.ts +0 -28
  84. package/dist/schema/runnable/remote.js +0 -221
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ // Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/utils/parse_partial_json.py
3
+ // MIT License
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.parsePartialJson = void 0;
6
+ function parsePartialJson(s) {
7
+ // Attempt to parse the string as-is.
8
+ try {
9
+ return JSON.parse(s);
10
+ }
11
+ catch (error) {
12
+ // Pass
13
+ }
14
+ // Initialize variables.
15
+ let new_s = "";
16
+ const stack = [];
17
+ let isInsideString = false;
18
+ let escaped = false;
19
+ // Process each character in the string one at a time.
20
+ for (let char of s) {
21
+ if (isInsideString) {
22
+ if (char === '"' && !escaped) {
23
+ isInsideString = false;
24
+ }
25
+ else if (char === "\n" && !escaped) {
26
+ char = "\\n"; // Replace the newline character with the escape sequence.
27
+ }
28
+ else if (char === "\\") {
29
+ escaped = !escaped;
30
+ }
31
+ else {
32
+ escaped = false;
33
+ }
34
+ }
35
+ else {
36
+ if (char === '"') {
37
+ isInsideString = true;
38
+ escaped = false;
39
+ }
40
+ else if (char === "{") {
41
+ stack.push("}");
42
+ }
43
+ else if (char === "[") {
44
+ stack.push("]");
45
+ }
46
+ else if (char === "}" || char === "]") {
47
+ if (stack && stack[stack.length - 1] === char) {
48
+ stack.pop();
49
+ }
50
+ else {
51
+ // Mismatched closing character; the input is malformed.
52
+ return null;
53
+ }
54
+ }
55
+ }
56
+ // Append the processed character to the new string.
57
+ new_s += char;
58
+ }
59
+ // If we're still inside a string at the end of processing,
60
+ // we need to close the string.
61
+ if (isInsideString) {
62
+ new_s += '"';
63
+ }
64
+ // Close any remaining open structures in the reverse order that they were opened.
65
+ for (let i = stack.length - 1; i >= 0; i -= 1) {
66
+ new_s += stack[i];
67
+ }
68
+ // Attempt to parse the modified string as JSON.
69
+ try {
70
+ return JSON.parse(new_s);
71
+ }
72
+ catch (error) {
73
+ // If we still can't parse the string as JSON, return null to indicate failure.
74
+ return null;
75
+ }
76
+ }
77
+ exports.parsePartialJson = parsePartialJson;
@@ -0,0 +1 @@
1
+ export declare function parsePartialJson(s: string): any;
@@ -0,0 +1,73 @@
1
+ // Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/utils/parse_partial_json.py
2
+ // MIT License
3
+ export function parsePartialJson(s) {
4
+ // Attempt to parse the string as-is.
5
+ try {
6
+ return JSON.parse(s);
7
+ }
8
+ catch (error) {
9
+ // Pass
10
+ }
11
+ // Initialize variables.
12
+ let new_s = "";
13
+ const stack = [];
14
+ let isInsideString = false;
15
+ let escaped = false;
16
+ // Process each character in the string one at a time.
17
+ for (let char of s) {
18
+ if (isInsideString) {
19
+ if (char === '"' && !escaped) {
20
+ isInsideString = false;
21
+ }
22
+ else if (char === "\n" && !escaped) {
23
+ char = "\\n"; // Replace the newline character with the escape sequence.
24
+ }
25
+ else if (char === "\\") {
26
+ escaped = !escaped;
27
+ }
28
+ else {
29
+ escaped = false;
30
+ }
31
+ }
32
+ else {
33
+ if (char === '"') {
34
+ isInsideString = true;
35
+ escaped = false;
36
+ }
37
+ else if (char === "{") {
38
+ stack.push("}");
39
+ }
40
+ else if (char === "[") {
41
+ stack.push("]");
42
+ }
43
+ else if (char === "}" || char === "]") {
44
+ if (stack && stack[stack.length - 1] === char) {
45
+ stack.pop();
46
+ }
47
+ else {
48
+ // Mismatched closing character; the input is malformed.
49
+ return null;
50
+ }
51
+ }
52
+ }
53
+ // Append the processed character to the new string.
54
+ new_s += char;
55
+ }
56
+ // If we're still inside a string at the end of processing,
57
+ // we need to close the string.
58
+ if (isInsideString) {
59
+ new_s += '"';
60
+ }
61
+ // Close any remaining open structures in the reverse order that they were opened.
62
+ for (let i = stack.length - 1; i >= 0; i -= 1) {
63
+ new_s += stack[i];
64
+ }
65
+ // Attempt to parse the modified string as JSON.
66
+ try {
67
+ return JSON.parse(new_s);
68
+ }
69
+ catch (error) {
70
+ // If we still can't parse the string as JSON, return null to indicate failure.
71
+ return null;
72
+ }
73
+ }
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.JsonKeyOutputFunctionsParser = exports.JsonOutputFunctionsParser = exports.OutputFunctionsParser = void 0;
4
4
  const output_parser_js_1 = require("../schema/output_parser.cjs");
5
+ const index_js_1 = require("../util/fast-json-patch/index.cjs");
6
+ const json_js_1 = require("./json.cjs");
5
7
  /**
6
8
  * Class for parsing the output of an LLM. Can be configured to return
7
9
  * only the arguments of the function call in the output.
@@ -63,12 +65,12 @@ exports.OutputFunctionsParser = OutputFunctionsParser;
63
65
  * Class for parsing the output of an LLM into a JSON object. Uses an
64
66
  * instance of `OutputFunctionsParser` to parse the output.
65
67
  */
66
- class JsonOutputFunctionsParser extends output_parser_js_1.BaseLLMOutputParser {
68
+ class JsonOutputFunctionsParser extends output_parser_js_1.BaseCumulativeTransformOutputParser {
67
69
  static lc_name() {
68
70
  return "JsonOutputFunctionsParser";
69
71
  }
70
72
  constructor(config) {
71
- super();
73
+ super(config);
72
74
  Object.defineProperty(this, "lc_namespace", {
73
75
  enumerable: true,
74
76
  configurable: true,
@@ -96,6 +98,31 @@ class JsonOutputFunctionsParser extends output_parser_js_1.BaseLLMOutputParser {
96
98
  this.argsOnly = config?.argsOnly ?? this.argsOnly;
97
99
  this.outputParser = new OutputFunctionsParser(config);
98
100
  }
101
+ _diff(prev, next) {
102
+ if (!next) {
103
+ return undefined;
104
+ }
105
+ const ops = (0, index_js_1.compare)(prev ?? {}, next);
106
+ return ops;
107
+ }
108
+ async parsePartialResult(generations) {
109
+ const generation = generations[0];
110
+ if (!generation.message) {
111
+ return undefined;
112
+ }
113
+ const { message } = generation;
114
+ const functionCall = message.additional_kwargs.function_call;
115
+ if (!functionCall) {
116
+ return undefined;
117
+ }
118
+ if (this.argsOnly) {
119
+ return (0, json_js_1.parsePartialJson)(functionCall.arguments);
120
+ }
121
+ return {
122
+ ...functionCall,
123
+ arguments: (0, json_js_1.parsePartialJson)(functionCall.arguments),
124
+ };
125
+ }
99
126
  /**
100
127
  * Parses the output and returns a JSON object. If `argsOnly` is true,
101
128
  * only the arguments of the function call are returned.
@@ -114,6 +141,14 @@ class JsonOutputFunctionsParser extends output_parser_js_1.BaseLLMOutputParser {
114
141
  parsedResult.arguments = JSON.parse(parsedResult.arguments);
115
142
  return parsedResult;
116
143
  }
144
+ // This method would be called by the default implementation of `parse_result`
145
+ // but we're overriding that method so it's not needed.
146
+ async parse(_text) {
147
+ throw new Error("Not implemented.");
148
+ }
149
+ getFormatInstructions() {
150
+ return "";
151
+ }
117
152
  }
118
153
  exports.JsonOutputFunctionsParser = JsonOutputFunctionsParser;
119
154
  /**
@@ -1,7 +1,8 @@
1
1
  import { JsonSchema7ObjectType } from "zod-to-json-schema/src/parsers/object.js";
2
2
  import { ChatGeneration, Generation } from "../schema/index.js";
3
3
  import { Optional } from "../types/type-utils.js";
4
- import { BaseLLMOutputParser } from "../schema/output_parser.js";
4
+ import { BaseCumulativeTransformOutputParser, type BaseCumulativeTransformOutputParserInput, BaseLLMOutputParser } from "../schema/output_parser.js";
5
+ import { type Operation as JSONPatchOperation } from "../util/fast-json-patch/index.js";
5
6
  /**
6
7
  * Represents optional parameters for a function in a JSON Schema.
7
8
  */
@@ -16,7 +17,7 @@ export declare class OutputFunctionsParser extends BaseLLMOutputParser<string> {
16
17
  lc_serializable: boolean;
17
18
  argsOnly: boolean;
18
19
  constructor(config?: {
19
- argsOnly: boolean;
20
+ argsOnly?: boolean;
20
21
  });
21
22
  /**
22
23
  * Parses the output and returns a string representation of the function
@@ -30,15 +31,17 @@ export declare class OutputFunctionsParser extends BaseLLMOutputParser<string> {
30
31
  * Class for parsing the output of an LLM into a JSON object. Uses an
31
32
  * instance of `OutputFunctionsParser` to parse the output.
32
33
  */
33
- export declare class JsonOutputFunctionsParser extends BaseLLMOutputParser<object> {
34
+ export declare class JsonOutputFunctionsParser extends BaseCumulativeTransformOutputParser<object> {
34
35
  static lc_name(): string;
35
36
  lc_namespace: string[];
36
37
  lc_serializable: boolean;
37
38
  outputParser: OutputFunctionsParser;
38
39
  argsOnly: boolean;
39
40
  constructor(config?: {
40
- argsOnly: boolean;
41
- });
41
+ argsOnly?: boolean;
42
+ } & BaseCumulativeTransformOutputParserInput);
43
+ protected _diff(prev: JSONPatchOperation | undefined, next: JSONPatchOperation): object | undefined;
44
+ parsePartialResult(generations: ChatGeneration[]): Promise<object | undefined>;
42
45
  /**
43
46
  * Parses the output and returns a JSON object. If `argsOnly` is true,
44
47
  * only the arguments of the function call are returned.
@@ -46,6 +49,8 @@ export declare class JsonOutputFunctionsParser extends BaseLLMOutputParser<objec
46
49
  * @returns A JSON object representation of the function call or its arguments.
47
50
  */
48
51
  parseResult(generations: Generation[] | ChatGeneration[]): Promise<object>;
52
+ parse(_text: string): Promise<object>;
53
+ getFormatInstructions(): string;
49
54
  }
50
55
  /**
51
56
  * Class for parsing the output of an LLM into a JSON object and returning
@@ -1,4 +1,6 @@
1
- import { BaseLLMOutputParser } from "../schema/output_parser.js";
1
+ import { BaseCumulativeTransformOutputParser, BaseLLMOutputParser, } from "../schema/output_parser.js";
2
+ import { compare, } from "../util/fast-json-patch/index.js";
3
+ import { parsePartialJson } from "./json.js";
2
4
  /**
3
5
  * Class for parsing the output of an LLM. Can be configured to return
4
6
  * only the arguments of the function call in the output.
@@ -59,12 +61,12 @@ export class OutputFunctionsParser extends BaseLLMOutputParser {
59
61
  * Class for parsing the output of an LLM into a JSON object. Uses an
60
62
  * instance of `OutputFunctionsParser` to parse the output.
61
63
  */
62
- export class JsonOutputFunctionsParser extends BaseLLMOutputParser {
64
+ export class JsonOutputFunctionsParser extends BaseCumulativeTransformOutputParser {
63
65
  static lc_name() {
64
66
  return "JsonOutputFunctionsParser";
65
67
  }
66
68
  constructor(config) {
67
- super();
69
+ super(config);
68
70
  Object.defineProperty(this, "lc_namespace", {
69
71
  enumerable: true,
70
72
  configurable: true,
@@ -92,6 +94,31 @@ export class JsonOutputFunctionsParser extends BaseLLMOutputParser {
92
94
  this.argsOnly = config?.argsOnly ?? this.argsOnly;
93
95
  this.outputParser = new OutputFunctionsParser(config);
94
96
  }
97
+ _diff(prev, next) {
98
+ if (!next) {
99
+ return undefined;
100
+ }
101
+ const ops = compare(prev ?? {}, next);
102
+ return ops;
103
+ }
104
+ async parsePartialResult(generations) {
105
+ const generation = generations[0];
106
+ if (!generation.message) {
107
+ return undefined;
108
+ }
109
+ const { message } = generation;
110
+ const functionCall = message.additional_kwargs.function_call;
111
+ if (!functionCall) {
112
+ return undefined;
113
+ }
114
+ if (this.argsOnly) {
115
+ return parsePartialJson(functionCall.arguments);
116
+ }
117
+ return {
118
+ ...functionCall,
119
+ arguments: parsePartialJson(functionCall.arguments),
120
+ };
121
+ }
95
122
  /**
96
123
  * Parses the output and returns a JSON object. If `argsOnly` is true,
97
124
  * only the arguments of the function call are returned.
@@ -110,6 +137,14 @@ export class JsonOutputFunctionsParser extends BaseLLMOutputParser {
110
137
  parsedResult.arguments = JSON.parse(parsedResult.arguments);
111
138
  return parsedResult;
112
139
  }
140
+ // This method would be called by the default implementation of `parse_result`
141
+ // but we're overriding that method so it's not needed.
142
+ async parse(_text) {
143
+ throw new Error("Not implemented.");
144
+ }
145
+ getFormatInstructions() {
146
+ return "";
147
+ }
113
148
  }
114
149
  /**
115
150
  * Class for parsing the output of an LLM into a JSON object and returning
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Docstore = exports.BaseEntityStore = exports.BaseFileStore = exports.BaseCache = exports.BaseListChatMessageHistory = exports.BaseChatMessageHistory = exports.BasePromptValue = exports.mapStoredMessageToChatMessage = exports.ChatGenerationChunk = exports.ChatMessageChunk = exports.coerceMessageLikeToMessage = exports.isBaseMessage = exports.ChatMessage = exports.FunctionMessageChunk = exports.FunctionMessage = exports.SystemChatMessage = exports.AIChatMessage = exports.HumanChatMessage = exports.BaseChatMessage = exports.SystemMessageChunk = exports.SystemMessage = exports.AIMessageChunk = exports.AIMessage = exports.HumanMessageChunk = exports.HumanMessage = exports.BaseMessageChunk = exports.BaseMessage = exports.GenerationChunk = exports.RUN_KEY = void 0;
3
+ exports.Docstore = exports.BaseEntityStore = exports.BaseFileStore = exports.BaseCache = exports.BaseListChatMessageHistory = exports.BaseChatMessageHistory = exports.BasePromptValue = exports.mapStoredMessageToChatMessage = exports.ChatGenerationChunk = exports.ChatMessageChunk = exports.coerceMessageLikeToMessage = exports.isBaseMessageChunk = exports.isBaseMessage = exports.ChatMessage = exports.FunctionMessageChunk = exports.FunctionMessage = exports.SystemChatMessage = exports.AIChatMessage = exports.HumanChatMessage = exports.BaseChatMessage = exports.SystemMessageChunk = exports.SystemMessage = exports.AIMessageChunk = exports.AIMessage = exports.HumanMessageChunk = exports.HumanMessage = exports.BaseMessageChunk = exports.BaseMessage = exports.GenerationChunk = exports.RUN_KEY = void 0;
4
4
  const serializable_js_1 = require("../load/serializable.cjs");
5
5
  exports.RUN_KEY = "__run";
6
6
  /**
@@ -105,6 +105,33 @@ class BaseMessage extends serializable_js_1.Serializable {
105
105
  .kwargs,
106
106
  };
107
107
  }
108
+ toChunk() {
109
+ const type = this._getType();
110
+ if (type === "human") {
111
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
112
+ return new HumanMessageChunk({ ...this });
113
+ }
114
+ else if (type === "ai") {
115
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
116
+ return new AIMessageChunk({ ...this });
117
+ }
118
+ else if (type === "system") {
119
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
120
+ return new SystemMessageChunk({ ...this });
121
+ }
122
+ else if (type === "function") {
123
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
124
+ return new FunctionMessageChunk({ ...this });
125
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
126
+ }
127
+ else if (ChatMessage.isInstance(this)) {
128
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
129
+ return new ChatMessageChunk({ ...this });
130
+ }
131
+ else {
132
+ throw new Error("Unknown message type.");
133
+ }
134
+ }
108
135
  }
109
136
  exports.BaseMessage = BaseMessage;
110
137
  /**
@@ -326,6 +353,11 @@ function isBaseMessage(messageLike) {
326
353
  return typeof messageLike?._getType === "function";
327
354
  }
328
355
  exports.isBaseMessage = isBaseMessage;
356
+ function isBaseMessageChunk(messageLike) {
357
+ return (isBaseMessage(messageLike) &&
358
+ typeof messageLike.concat === "function");
359
+ }
360
+ exports.isBaseMessageChunk = isBaseMessageChunk;
329
361
  function coerceMessageLikeToMessage(messageLike) {
330
362
  if (typeof messageLike === "string") {
331
363
  return new HumanMessage(messageLike);
@@ -103,6 +103,7 @@ export declare abstract class BaseMessage extends Serializable implements BaseMe
103
103
  /** @deprecated */
104
104
  kwargs?: Record<string, unknown>);
105
105
  toDict(): StoredMessage;
106
+ toChunk(): BaseMessageChunk;
106
107
  }
107
108
  /**
108
109
  * Represents a chunk of a message, which can be concatenated with other
@@ -219,6 +220,7 @@ export type BaseMessageLike = BaseMessage | [
219
220
  string
220
221
  ] | string;
221
222
  export declare function isBaseMessage(messageLike?: unknown): messageLike is BaseMessage;
223
+ export declare function isBaseMessageChunk(messageLike?: unknown): messageLike is BaseMessageChunk;
222
224
  export declare function coerceMessageLikeToMessage(messageLike: BaseMessageLike): BaseMessage;
223
225
  /**
224
226
  * Represents a chunk of a chat message, which can be concatenated with
@@ -243,7 +245,7 @@ export declare class ChatGenerationChunk extends GenerationChunk implements Chat
243
245
  constructor(fields: ChatGenerationChunkFields);
244
246
  concat(chunk: ChatGenerationChunk): ChatGenerationChunk;
245
247
  }
246
- export declare function mapStoredMessageToChatMessage(message: StoredMessage): HumanMessage | AIMessage | SystemMessage | FunctionMessage | ChatMessage;
248
+ export declare function mapStoredMessageToChatMessage(message: StoredMessage): ChatMessage | HumanMessage | AIMessage | SystemMessage | FunctionMessage;
247
249
  export interface ChatResult {
248
250
  generations: ChatGeneration[];
249
251
  llmOutput?: Record<string, any>;
@@ -101,6 +101,33 @@ export class BaseMessage extends Serializable {
101
101
  .kwargs,
102
102
  };
103
103
  }
104
+ toChunk() {
105
+ const type = this._getType();
106
+ if (type === "human") {
107
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
108
+ return new HumanMessageChunk({ ...this });
109
+ }
110
+ else if (type === "ai") {
111
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
112
+ return new AIMessageChunk({ ...this });
113
+ }
114
+ else if (type === "system") {
115
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
116
+ return new SystemMessageChunk({ ...this });
117
+ }
118
+ else if (type === "function") {
119
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
120
+ return new FunctionMessageChunk({ ...this });
121
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
122
+ }
123
+ else if (ChatMessage.isInstance(this)) {
124
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
125
+ return new ChatMessageChunk({ ...this });
126
+ }
127
+ else {
128
+ throw new Error("Unknown message type.");
129
+ }
130
+ }
104
131
  }
105
132
  /**
106
133
  * Represents a chunk of a message, which can be concatenated with other
@@ -310,6 +337,10 @@ export class ChatMessage extends BaseMessage {
310
337
  export function isBaseMessage(messageLike) {
311
338
  return typeof messageLike?._getType === "function";
312
339
  }
340
+ export function isBaseMessageChunk(messageLike) {
341
+ return (isBaseMessage(messageLike) &&
342
+ typeof messageLike.concat === "function");
343
+ }
313
344
  export function coerceMessageLikeToMessage(messageLike) {
314
345
  if (typeof messageLike === "string") {
315
346
  return new HumanMessage(messageLike);
@@ -1,13 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OutputParserException = exports.BytesOutputParser = exports.StringOutputParser = exports.BaseTransformOutputParser = exports.BaseOutputParser = exports.BaseLLMOutputParser = void 0;
4
- const index_js_1 = require("./runnable/index.cjs");
3
+ exports.OutputParserException = exports.BytesOutputParser = exports.StringOutputParser = exports.BaseCumulativeTransformOutputParser = exports.BaseTransformOutputParser = exports.BaseOutputParser = exports.BaseLLMOutputParser = void 0;
4
+ const index_js_1 = require("./index.cjs");
5
+ const index_js_2 = require("./runnable/index.cjs");
6
+ const index_js_3 = require("../util/@cfworker/json-schema/index.cjs");
5
7
  /**
6
8
  * Abstract base class for parsing the output of a Large Language Model
7
9
  * (LLM) call. It provides methods for parsing the result of an LLM call
8
10
  * and invoking the parser with a given input.
9
11
  */
10
- class BaseLLMOutputParser extends index_js_1.Runnable {
12
+ class BaseLLMOutputParser extends index_js_2.Runnable {
11
13
  /**
12
14
  * Parses the result of an LLM call with a given prompt. By default, it
13
15
  * simply calls `parseResult`.
@@ -86,6 +88,64 @@ class BaseTransformOutputParser extends BaseOutputParser {
86
88
  }
87
89
  }
88
90
  exports.BaseTransformOutputParser = BaseTransformOutputParser;
91
+ /**
92
+ * A base class for output parsers that can handle streaming input. It
93
+ * extends the `BaseTransformOutputParser` class and provides a method for
94
+ * converting parsed outputs into a diff format.
95
+ */
96
+ class BaseCumulativeTransformOutputParser extends BaseTransformOutputParser {
97
+ constructor(fields) {
98
+ super(fields);
99
+ Object.defineProperty(this, "diff", {
100
+ enumerable: true,
101
+ configurable: true,
102
+ writable: true,
103
+ value: false
104
+ });
105
+ this.diff = fields?.diff ?? this.diff;
106
+ }
107
+ async *_transform(inputGenerator) {
108
+ let prevParsed;
109
+ let accGen;
110
+ for await (const chunk of inputGenerator) {
111
+ let chunkGen;
112
+ if ((0, index_js_1.isBaseMessageChunk)(chunk)) {
113
+ chunkGen = new index_js_1.ChatGenerationChunk({
114
+ message: chunk,
115
+ text: chunk.content,
116
+ });
117
+ }
118
+ else if ((0, index_js_1.isBaseMessage)(chunk)) {
119
+ chunkGen = new index_js_1.ChatGenerationChunk({
120
+ message: chunk.toChunk(),
121
+ text: chunk.content,
122
+ });
123
+ }
124
+ else {
125
+ chunkGen = new index_js_1.GenerationChunk({ text: chunk });
126
+ }
127
+ if (accGen === undefined) {
128
+ accGen = chunkGen;
129
+ }
130
+ else {
131
+ accGen = accGen.concat(chunkGen);
132
+ }
133
+ const parsed = await this.parsePartialResult([accGen]);
134
+ if (parsed !== undefined &&
135
+ parsed !== null &&
136
+ !(0, index_js_3.deepCompareStrict)(parsed, prevParsed)) {
137
+ if (this.diff) {
138
+ yield this._diff(prevParsed, parsed);
139
+ }
140
+ else {
141
+ yield parsed;
142
+ }
143
+ prevParsed = parsed;
144
+ }
145
+ }
146
+ }
147
+ }
148
+ exports.BaseCumulativeTransformOutputParser = BaseCumulativeTransformOutputParser;
89
149
  /**
90
150
  * OutputParser that parses LLMResult into the top likely string.
91
151
  */
@@ -77,7 +77,7 @@ export declare abstract class BaseOutputParser<T = unknown> extends BaseLLMOutpu
77
77
  * Class to parse the output of an LLM call that also allows streaming inputs.
78
78
  */
79
79
  export declare abstract class BaseTransformOutputParser<T = unknown> extends BaseOutputParser<T> {
80
- _transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
80
+ protected _transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
81
81
  /**
82
82
  * Transforms an asynchronous generator of input into an asynchronous
83
83
  * generator of parsed output.
@@ -87,6 +87,21 @@ export declare abstract class BaseTransformOutputParser<T = unknown> extends Bas
87
87
  */
88
88
  transform(inputGenerator: AsyncGenerator<string | BaseMessage>, options: BaseCallbackConfig): AsyncGenerator<T>;
89
89
  }
90
+ export type BaseCumulativeTransformOutputParserInput = {
91
+ diff?: boolean;
92
+ };
93
+ /**
94
+ * A base class for output parsers that can handle streaming input. It
95
+ * extends the `BaseTransformOutputParser` class and provides a method for
96
+ * converting parsed outputs into a diff format.
97
+ */
98
+ export declare abstract class BaseCumulativeTransformOutputParser<T = unknown> extends BaseTransformOutputParser<T> {
99
+ protected diff: boolean;
100
+ constructor(fields?: BaseCumulativeTransformOutputParserInput);
101
+ protected abstract _diff(prev: any | undefined, next: any): any;
102
+ abstract parsePartialResult(generations: Generation[] | ChatGeneration[]): Promise<T | undefined>;
103
+ protected _transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
104
+ }
90
105
  /**
91
106
  * OutputParser that parses LLMResult into the top likely string.
92
107
  */
@@ -1,4 +1,6 @@
1
+ import { isBaseMessage, isBaseMessageChunk, ChatGenerationChunk, GenerationChunk, } from "./index.js";
1
2
  import { Runnable } from "./runnable/index.js";
3
+ import { deepCompareStrict } from "../util/@cfworker/json-schema/index.js";
2
4
  /**
3
5
  * Abstract base class for parsing the output of a Large Language Model
4
6
  * (LLM) call. It provides methods for parsing the result of an LLM call
@@ -80,6 +82,63 @@ export class BaseTransformOutputParser extends BaseOutputParser {
80
82
  });
81
83
  }
82
84
  }
85
+ /**
86
+ * A base class for output parsers that can handle streaming input. It
87
+ * extends the `BaseTransformOutputParser` class and provides a method for
88
+ * converting parsed outputs into a diff format.
89
+ */
90
+ export class BaseCumulativeTransformOutputParser extends BaseTransformOutputParser {
91
+ constructor(fields) {
92
+ super(fields);
93
+ Object.defineProperty(this, "diff", {
94
+ enumerable: true,
95
+ configurable: true,
96
+ writable: true,
97
+ value: false
98
+ });
99
+ this.diff = fields?.diff ?? this.diff;
100
+ }
101
+ async *_transform(inputGenerator) {
102
+ let prevParsed;
103
+ let accGen;
104
+ for await (const chunk of inputGenerator) {
105
+ let chunkGen;
106
+ if (isBaseMessageChunk(chunk)) {
107
+ chunkGen = new ChatGenerationChunk({
108
+ message: chunk,
109
+ text: chunk.content,
110
+ });
111
+ }
112
+ else if (isBaseMessage(chunk)) {
113
+ chunkGen = new ChatGenerationChunk({
114
+ message: chunk.toChunk(),
115
+ text: chunk.content,
116
+ });
117
+ }
118
+ else {
119
+ chunkGen = new GenerationChunk({ text: chunk });
120
+ }
121
+ if (accGen === undefined) {
122
+ accGen = chunkGen;
123
+ }
124
+ else {
125
+ accGen = accGen.concat(chunkGen);
126
+ }
127
+ const parsed = await this.parsePartialResult([accGen]);
128
+ if (parsed !== undefined &&
129
+ parsed !== null &&
130
+ !deepCompareStrict(parsed, prevParsed)) {
131
+ if (this.diff) {
132
+ yield this._diff(prevParsed, parsed);
133
+ }
134
+ else {
135
+ yield parsed;
136
+ }
137
+ prevParsed = parsed;
138
+ }
139
+ }
140
+ }
141
+ }
83
142
  /**
84
143
  * OutputParser that parses LLMResult into the top likely string.
85
144
  */