@digipair/skill-llm 0.94.0-1 → 0.94.0-3

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/chat.esm.js ADDED
@@ -0,0 +1,2413 @@
1
+ import { a as BaseMessage, c as checkValidTemplate, b as BasePromptTemplate, r as renderTemplate, I as ImagePromptValue, P as PromptTemplate, H as HumanMessage, S as SystemMessage, i as isBaseMessage, d as coerceMessageLikeToMessage, C as ChatPromptValue, p as parseMustache, e as parseFString, B as BaseStringPromptTemplate, A as AIMessage, f as addLangChainErrorFields, R as Runnable } from './index.esm2.js';
2
+
3
+ function _assert_this_initialized$2(self) {
4
+ if (self === void 0) {
5
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
6
+ }
7
+ return self;
8
+ }
9
+ function _class_call_check$2(instance, Constructor) {
10
+ if (!(instance instanceof Constructor)) {
11
+ throw new TypeError("Cannot call a class as a function");
12
+ }
13
+ }
14
+ function _defineProperties$2(target, props) {
15
+ for(var i = 0; i < props.length; i++){
16
+ var descriptor = props[i];
17
+ descriptor.enumerable = descriptor.enumerable || false;
18
+ descriptor.configurable = true;
19
+ if ("value" in descriptor) descriptor.writable = true;
20
+ Object.defineProperty(target, descriptor.key, descriptor);
21
+ }
22
+ }
23
+ function _create_class$2(Constructor, protoProps, staticProps) {
24
+ if (protoProps) _defineProperties$2(Constructor.prototype, protoProps);
25
+ if (staticProps) _defineProperties$2(Constructor, staticProps);
26
+ return Constructor;
27
+ }
28
+ function _define_property$2(obj, key, value) {
29
+ if (key in obj) {
30
+ Object.defineProperty(obj, key, {
31
+ value: value,
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true
35
+ });
36
+ } else {
37
+ obj[key] = value;
38
+ }
39
+ return obj;
40
+ }
41
+ function _get(target, property, receiver) {
42
+ if (typeof Reflect !== "undefined" && Reflect.get) {
43
+ _get = Reflect.get;
44
+ } else {
45
+ _get = function get(target, property, receiver) {
46
+ var base = _super_prop_base(target, property);
47
+ if (!base) return;
48
+ var desc = Object.getOwnPropertyDescriptor(base, property);
49
+ if (desc.get) {
50
+ return desc.get.call(receiver || target);
51
+ }
52
+ return desc.value;
53
+ };
54
+ }
55
+ return _get(target, property, receiver || target);
56
+ }
57
+ function _get_prototype_of$2(o) {
58
+ _get_prototype_of$2 = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
59
+ return o.__proto__ || Object.getPrototypeOf(o);
60
+ };
61
+ return _get_prototype_of$2(o);
62
+ }
63
+ function _inherits$2(subClass, superClass) {
64
+ if (typeof superClass !== "function" && superClass !== null) {
65
+ throw new TypeError("Super expression must either be null or a function");
66
+ }
67
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
68
+ constructor: {
69
+ value: subClass,
70
+ writable: true,
71
+ configurable: true
72
+ }
73
+ });
74
+ if (superClass) _set_prototype_of$2(subClass, superClass);
75
+ }
76
+ function _object_spread$2(target) {
77
+ for(var i = 1; i < arguments.length; i++){
78
+ var source = arguments[i] != null ? arguments[i] : {};
79
+ var ownKeys = Object.keys(source);
80
+ if (typeof Object.getOwnPropertySymbols === "function") {
81
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
82
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
83
+ }));
84
+ }
85
+ ownKeys.forEach(function(key) {
86
+ _define_property$2(target, key, source[key]);
87
+ });
88
+ }
89
+ return target;
90
+ }
91
+ function ownKeys$2(object, enumerableOnly) {
92
+ var keys = Object.keys(object);
93
+ if (Object.getOwnPropertySymbols) {
94
+ var symbols = Object.getOwnPropertySymbols(object);
95
+ if (enumerableOnly) {
96
+ symbols = symbols.filter(function(sym) {
97
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
98
+ });
99
+ }
100
+ keys.push.apply(keys, symbols);
101
+ }
102
+ return keys;
103
+ }
104
+ function _object_spread_props$2(target, source) {
105
+ source = source != null ? source : {};
106
+ if (Object.getOwnPropertyDescriptors) {
107
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
108
+ } else {
109
+ ownKeys$2(Object(source)).forEach(function(key) {
110
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
111
+ });
112
+ }
113
+ return target;
114
+ }
115
+ function _possible_constructor_return$2(self, call) {
116
+ if (call && (_type_of$2(call) === "object" || typeof call === "function")) {
117
+ return call;
118
+ }
119
+ return _assert_this_initialized$2(self);
120
+ }
121
+ function _set_prototype_of$2(o, p) {
122
+ _set_prototype_of$2 = Object.setPrototypeOf || function setPrototypeOf(o, p) {
123
+ o.__proto__ = p;
124
+ return o;
125
+ };
126
+ return _set_prototype_of$2(o, p);
127
+ }
128
+ function _super_prop_base(object, property) {
129
+ while(!Object.prototype.hasOwnProperty.call(object, property)){
130
+ object = _get_prototype_of$2(object);
131
+ if (object === null) break;
132
+ }
133
+ return object;
134
+ }
135
+ function _type_of$2(obj) {
136
+ "@swc/helpers - typeof";
137
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
138
+ }
139
+ function _is_native_reflect_construct$2() {
140
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
141
+ if (Reflect.construct.sham) return false;
142
+ if (typeof Proxy === "function") return true;
143
+ try {
144
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
145
+ return true;
146
+ } catch (e) {
147
+ return false;
148
+ }
149
+ }
150
+ function _create_super$2(Derived) {
151
+ var hasNativeReflectConstruct = _is_native_reflect_construct$2();
152
+ return function _createSuperInternal() {
153
+ var Super = _get_prototype_of$2(Derived), result;
154
+ if (hasNativeReflectConstruct) {
155
+ var NewTarget = _get_prototype_of$2(this).constructor;
156
+ result = Reflect.construct(Super, arguments, NewTarget);
157
+ } else {
158
+ result = Super.apply(this, arguments);
159
+ }
160
+ return _possible_constructor_return$2(this, result);
161
+ };
162
+ }
163
+ /**
164
+ * Represents a chat message in a conversation.
165
+ */ var ChatMessage = /*#__PURE__*/ function(BaseMessage) {
166
+ _inherits$2(ChatMessage, BaseMessage);
167
+ var _super = _create_super$2(ChatMessage);
168
+ function ChatMessage(fields, role) {
169
+ _class_call_check$2(this, ChatMessage);
170
+ var _this;
171
+ if (typeof fields === "string") {
172
+ // eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
173
+ fields = {
174
+ content: fields,
175
+ role: role
176
+ };
177
+ }
178
+ _this = _super.call(this, fields);
179
+ Object.defineProperty(_assert_this_initialized$2(_this), "role", {
180
+ enumerable: true,
181
+ configurable: true,
182
+ writable: true,
183
+ value: void 0
184
+ });
185
+ _this.role = fields.role;
186
+ return _this;
187
+ }
188
+ _create_class$2(ChatMessage, [
189
+ {
190
+ key: "_getType",
191
+ value: function _getType() {
192
+ return "generic";
193
+ }
194
+ },
195
+ {
196
+ key: "_printableFields",
197
+ get: function get() {
198
+ return _object_spread_props$2(_object_spread$2({}, _get(_get_prototype_of$2(ChatMessage.prototype), "_printableFields", this)), {
199
+ role: this.role
200
+ });
201
+ }
202
+ }
203
+ ], [
204
+ {
205
+ key: "lc_name",
206
+ value: function lc_name() {
207
+ return "ChatMessage";
208
+ }
209
+ },
210
+ {
211
+ key: "_chatMessageClass",
212
+ value: function _chatMessageClass() {
213
+ return ChatMessage;
214
+ }
215
+ },
216
+ {
217
+ key: "isInstance",
218
+ value: function isInstance(message) {
219
+ return message._getType() === "generic";
220
+ }
221
+ }
222
+ ]);
223
+ return ChatMessage;
224
+ }(BaseMessage);
225
+
226
+ function _array_like_to_array$1(arr, len) {
227
+ if (len == null || len > arr.length) len = arr.length;
228
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
229
+ return arr2;
230
+ }
231
+ function _array_with_holes(arr) {
232
+ if (Array.isArray(arr)) return arr;
233
+ }
234
+ function _assert_this_initialized$1(self) {
235
+ if (self === void 0) {
236
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
237
+ }
238
+ return self;
239
+ }
240
+ function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
241
+ try {
242
+ var info = gen[key](arg);
243
+ var value = info.value;
244
+ } catch (error) {
245
+ reject(error);
246
+ return;
247
+ }
248
+ if (info.done) {
249
+ resolve(value);
250
+ } else {
251
+ Promise.resolve(value).then(_next, _throw);
252
+ }
253
+ }
254
+ function _async_to_generator$1(fn) {
255
+ return function() {
256
+ var self = this, args = arguments;
257
+ return new Promise(function(resolve, reject) {
258
+ var gen = fn.apply(self, args);
259
+ function _next(value) {
260
+ asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
261
+ }
262
+ function _throw(err) {
263
+ asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
264
+ }
265
+ _next(undefined);
266
+ });
267
+ };
268
+ }
269
+ function _class_call_check$1(instance, Constructor) {
270
+ if (!(instance instanceof Constructor)) {
271
+ throw new TypeError("Cannot call a class as a function");
272
+ }
273
+ }
274
+ function _defineProperties$1(target, props) {
275
+ for(var i = 0; i < props.length; i++){
276
+ var descriptor = props[i];
277
+ descriptor.enumerable = descriptor.enumerable || false;
278
+ descriptor.configurable = true;
279
+ if ("value" in descriptor) descriptor.writable = true;
280
+ Object.defineProperty(target, descriptor.key, descriptor);
281
+ }
282
+ }
283
+ function _create_class$1(Constructor, protoProps, staticProps) {
284
+ if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
285
+ if (staticProps) _defineProperties$1(Constructor, staticProps);
286
+ return Constructor;
287
+ }
288
+ function _define_property$1(obj, key, value) {
289
+ if (key in obj) {
290
+ Object.defineProperty(obj, key, {
291
+ value: value,
292
+ enumerable: true,
293
+ configurable: true,
294
+ writable: true
295
+ });
296
+ } else {
297
+ obj[key] = value;
298
+ }
299
+ return obj;
300
+ }
301
+ function _get_prototype_of$1(o) {
302
+ _get_prototype_of$1 = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
303
+ return o.__proto__ || Object.getPrototypeOf(o);
304
+ };
305
+ return _get_prototype_of$1(o);
306
+ }
307
+ function _inherits$1(subClass, superClass) {
308
+ if (typeof superClass !== "function" && superClass !== null) {
309
+ throw new TypeError("Super expression must either be null or a function");
310
+ }
311
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
312
+ constructor: {
313
+ value: subClass,
314
+ writable: true,
315
+ configurable: true
316
+ }
317
+ });
318
+ if (superClass) _set_prototype_of$1(subClass, superClass);
319
+ }
320
+ function _iterable_to_array_limit(arr, i) {
321
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
322
+ if (_i == null) return;
323
+ var _arr = [];
324
+ var _n = true;
325
+ var _d = false;
326
+ var _s, _e;
327
+ try {
328
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
329
+ _arr.push(_s.value);
330
+ if (i && _arr.length === i) break;
331
+ }
332
+ } catch (err) {
333
+ _d = true;
334
+ _e = err;
335
+ } finally{
336
+ try {
337
+ if (!_n && _i["return"] != null) _i["return"]();
338
+ } finally{
339
+ if (_d) throw _e;
340
+ }
341
+ }
342
+ return _arr;
343
+ }
344
+ function _non_iterable_rest() {
345
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
346
+ }
347
+ function _object_spread$1(target) {
348
+ for(var i = 1; i < arguments.length; i++){
349
+ var source = arguments[i] != null ? arguments[i] : {};
350
+ var ownKeys = Object.keys(source);
351
+ if (typeof Object.getOwnPropertySymbols === "function") {
352
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
353
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
354
+ }));
355
+ }
356
+ ownKeys.forEach(function(key) {
357
+ _define_property$1(target, key, source[key]);
358
+ });
359
+ }
360
+ return target;
361
+ }
362
+ function ownKeys$1(object, enumerableOnly) {
363
+ var keys = Object.keys(object);
364
+ if (Object.getOwnPropertySymbols) {
365
+ var symbols = Object.getOwnPropertySymbols(object);
366
+ if (enumerableOnly) {
367
+ symbols = symbols.filter(function(sym) {
368
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
369
+ });
370
+ }
371
+ keys.push.apply(keys, symbols);
372
+ }
373
+ return keys;
374
+ }
375
+ function _object_spread_props$1(target, source) {
376
+ source = source != null ? source : {};
377
+ if (Object.getOwnPropertyDescriptors) {
378
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
379
+ } else {
380
+ ownKeys$1(Object(source)).forEach(function(key) {
381
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
382
+ });
383
+ }
384
+ return target;
385
+ }
386
+ function _possible_constructor_return$1(self, call) {
387
+ if (call && (_type_of$1(call) === "object" || typeof call === "function")) {
388
+ return call;
389
+ }
390
+ return _assert_this_initialized$1(self);
391
+ }
392
+ function _set_prototype_of$1(o, p) {
393
+ _set_prototype_of$1 = Object.setPrototypeOf || function setPrototypeOf(o, p) {
394
+ o.__proto__ = p;
395
+ return o;
396
+ };
397
+ return _set_prototype_of$1(o, p);
398
+ }
399
+ function _sliced_to_array(arr, i) {
400
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array$1(arr, i) || _non_iterable_rest();
401
+ }
402
+ function _type_of$1(obj) {
403
+ "@swc/helpers - typeof";
404
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
405
+ }
406
+ function _unsupported_iterable_to_array$1(o, minLen) {
407
+ if (!o) return;
408
+ if (typeof o === "string") return _array_like_to_array$1(o, minLen);
409
+ var n = Object.prototype.toString.call(o).slice(8, -1);
410
+ if (n === "Object" && o.constructor) n = o.constructor.name;
411
+ if (n === "Map" || n === "Set") return Array.from(n);
412
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array$1(o, minLen);
413
+ }
414
+ function _is_native_reflect_construct$1() {
415
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
416
+ if (Reflect.construct.sham) return false;
417
+ if (typeof Proxy === "function") return true;
418
+ try {
419
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
420
+ return true;
421
+ } catch (e) {
422
+ return false;
423
+ }
424
+ }
425
+ function _create_super$1(Derived) {
426
+ var hasNativeReflectConstruct = _is_native_reflect_construct$1();
427
+ return function _createSuperInternal() {
428
+ var Super = _get_prototype_of$1(Derived), result;
429
+ if (hasNativeReflectConstruct) {
430
+ var NewTarget = _get_prototype_of$1(this).constructor;
431
+ result = Reflect.construct(Super, arguments, NewTarget);
432
+ } else {
433
+ result = Super.apply(this, arguments);
434
+ }
435
+ return _possible_constructor_return$1(this, result);
436
+ };
437
+ }
438
+ function _ts_generator$1(thisArg, body) {
439
+ var f, y, t, g, _ = {
440
+ label: 0,
441
+ sent: function() {
442
+ if (t[0] & 1) throw t[1];
443
+ return t[1];
444
+ },
445
+ trys: [],
446
+ ops: []
447
+ };
448
+ return g = {
449
+ next: verb(0),
450
+ "throw": verb(1),
451
+ "return": verb(2)
452
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
453
+ return this;
454
+ }), g;
455
+ function verb(n) {
456
+ return function(v) {
457
+ return step([
458
+ n,
459
+ v
460
+ ]);
461
+ };
462
+ }
463
+ function step(op) {
464
+ if (f) throw new TypeError("Generator is already executing.");
465
+ while(_)try {
466
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
467
+ if (y = 0, t) op = [
468
+ op[0] & 2,
469
+ t.value
470
+ ];
471
+ switch(op[0]){
472
+ case 0:
473
+ case 1:
474
+ t = op;
475
+ break;
476
+ case 4:
477
+ _.label++;
478
+ return {
479
+ value: op[1],
480
+ done: false
481
+ };
482
+ case 5:
483
+ _.label++;
484
+ y = op[1];
485
+ op = [
486
+ 0
487
+ ];
488
+ continue;
489
+ case 7:
490
+ op = _.ops.pop();
491
+ _.trys.pop();
492
+ continue;
493
+ default:
494
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
495
+ _ = 0;
496
+ continue;
497
+ }
498
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
499
+ _.label = op[1];
500
+ break;
501
+ }
502
+ if (op[0] === 6 && _.label < t[1]) {
503
+ _.label = t[1];
504
+ t = op;
505
+ break;
506
+ }
507
+ if (t && _.label < t[2]) {
508
+ _.label = t[2];
509
+ _.ops.push(op);
510
+ break;
511
+ }
512
+ if (t[2]) _.ops.pop();
513
+ _.trys.pop();
514
+ continue;
515
+ }
516
+ op = body.call(thisArg, _);
517
+ } catch (e) {
518
+ op = [
519
+ 6,
520
+ e
521
+ ];
522
+ y = 0;
523
+ } finally{
524
+ f = t = 0;
525
+ }
526
+ if (op[0] & 5) throw op[1];
527
+ return {
528
+ value: op[0] ? op[1] : void 0,
529
+ done: true
530
+ };
531
+ }
532
+ }
533
+ /**
534
+ * An image prompt template for a multimodal model.
535
+ */ var ImagePromptTemplate = /*#__PURE__*/ function(BasePromptTemplate) {
536
+ _inherits$1(ImagePromptTemplate, BasePromptTemplate);
537
+ var _super = _create_super$1(ImagePromptTemplate);
538
+ function ImagePromptTemplate(input) {
539
+ _class_call_check$1(this, ImagePromptTemplate);
540
+ var _this;
541
+ _this = _super.call(this, input);
542
+ Object.defineProperty(_assert_this_initialized$1(_this), "lc_namespace", {
543
+ enumerable: true,
544
+ configurable: true,
545
+ writable: true,
546
+ value: [
547
+ "langchain_core",
548
+ "prompts",
549
+ "image"
550
+ ]
551
+ });
552
+ Object.defineProperty(_assert_this_initialized$1(_this), "template", {
553
+ enumerable: true,
554
+ configurable: true,
555
+ writable: true,
556
+ value: void 0
557
+ });
558
+ Object.defineProperty(_assert_this_initialized$1(_this), "templateFormat", {
559
+ enumerable: true,
560
+ configurable: true,
561
+ writable: true,
562
+ value: "f-string"
563
+ });
564
+ Object.defineProperty(_assert_this_initialized$1(_this), "validateTemplate", {
565
+ enumerable: true,
566
+ configurable: true,
567
+ writable: true,
568
+ value: true
569
+ });
570
+ /**
571
+ * Additional fields which should be included inside
572
+ * the message content array if using a complex message
573
+ * content.
574
+ */ // eslint-disable-next-line @typescript-eslint/no-explicit-any
575
+ Object.defineProperty(_assert_this_initialized$1(_this), "additionalContentFields", {
576
+ enumerable: true,
577
+ configurable: true,
578
+ writable: true,
579
+ value: void 0
580
+ });
581
+ _this.template = input.template;
582
+ var _input_templateFormat;
583
+ _this.templateFormat = (_input_templateFormat = input.templateFormat) !== null && _input_templateFormat !== void 0 ? _input_templateFormat : _this.templateFormat;
584
+ var _input_validateTemplate;
585
+ _this.validateTemplate = (_input_validateTemplate = input.validateTemplate) !== null && _input_validateTemplate !== void 0 ? _input_validateTemplate : _this.validateTemplate;
586
+ _this.additionalContentFields = input.additionalContentFields;
587
+ if (_this.validateTemplate) {
588
+ var totalInputVariables = _this.inputVariables;
589
+ if (_this.partialVariables) {
590
+ totalInputVariables = totalInputVariables.concat(Object.keys(_this.partialVariables));
591
+ }
592
+ checkValidTemplate([
593
+ {
594
+ type: "image_url",
595
+ image_url: _this.template
596
+ }
597
+ ], _this.templateFormat, totalInputVariables);
598
+ }
599
+ return _this;
600
+ }
601
+ _create_class$1(ImagePromptTemplate, [
602
+ {
603
+ key: "_getPromptType",
604
+ value: function _getPromptType() {
605
+ return "prompt";
606
+ }
607
+ },
608
+ {
609
+ key: "partial",
610
+ value: /**
611
+ * Partially applies values to the prompt template.
612
+ * @param values The values to be partially applied to the prompt template.
613
+ * @returns A new instance of ImagePromptTemplate with the partially applied values.
614
+ */ function partial(values) {
615
+ var _this = this;
616
+ return _async_to_generator$1(function() {
617
+ var newInputVariables, _this_partialVariables, newPartialVariables, promptDict;
618
+ return _ts_generator$1(this, function(_state) {
619
+ newInputVariables = _this.inputVariables.filter(function(iv) {
620
+ return !(iv in values);
621
+ });
622
+ newPartialVariables = _object_spread$1({}, (_this_partialVariables = _this.partialVariables) !== null && _this_partialVariables !== void 0 ? _this_partialVariables : {}, values);
623
+ promptDict = _object_spread_props$1(_object_spread$1({}, _this), {
624
+ inputVariables: newInputVariables,
625
+ partialVariables: newPartialVariables
626
+ });
627
+ return [
628
+ 2,
629
+ new ImagePromptTemplate(promptDict)
630
+ ];
631
+ });
632
+ })();
633
+ }
634
+ },
635
+ {
636
+ key: "format",
637
+ value: /**
638
+ * Formats the prompt template with the provided values.
639
+ * @param values The values to be used to format the prompt template.
640
+ * @returns A promise that resolves to a string which is the formatted prompt.
641
+ */ function format(values) {
642
+ var _this = this;
643
+ return _async_to_generator$1(function() {
644
+ var formatted, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, key, value, url, detail, output;
645
+ return _ts_generator$1(this, function(_state) {
646
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
647
+ formatted = {};
648
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
649
+ try {
650
+ for(_iterator = Object.entries(_this.template)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
651
+ _step_value = _sliced_to_array(_step.value, 2), key = _step_value[0], value = _step_value[1];
652
+ if (typeof value === "string") {
653
+ formatted[key] = renderTemplate(value, _this.templateFormat, values);
654
+ } else {
655
+ formatted[key] = value;
656
+ }
657
+ }
658
+ } catch (err) {
659
+ _didIteratorError = true;
660
+ _iteratorError = err;
661
+ } finally{
662
+ try {
663
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
664
+ _iterator.return();
665
+ }
666
+ } finally{
667
+ if (_didIteratorError) {
668
+ throw _iteratorError;
669
+ }
670
+ }
671
+ }
672
+ url = values.url || formatted.url;
673
+ detail = values.detail || formatted.detail;
674
+ if (!url) {
675
+ throw new Error("Must provide either an image URL.");
676
+ }
677
+ if (typeof url !== "string") {
678
+ throw new Error("url must be a string.");
679
+ }
680
+ output = {
681
+ url: url
682
+ };
683
+ if (detail) {
684
+ output.detail = detail;
685
+ }
686
+ return [
687
+ 2,
688
+ output
689
+ ];
690
+ });
691
+ })();
692
+ }
693
+ },
694
+ {
695
+ key: "formatPromptValue",
696
+ value: /**
697
+ * Formats the prompt given the input values and returns a formatted
698
+ * prompt value.
699
+ * @param values The input values to format the prompt.
700
+ * @returns A Promise that resolves to a formatted prompt value.
701
+ */ function formatPromptValue(values) {
702
+ var _this = this;
703
+ return _async_to_generator$1(function() {
704
+ var formattedPrompt;
705
+ return _ts_generator$1(this, function(_state) {
706
+ switch(_state.label){
707
+ case 0:
708
+ return [
709
+ 4,
710
+ _this.format(values)
711
+ ];
712
+ case 1:
713
+ formattedPrompt = _state.sent();
714
+ return [
715
+ 2,
716
+ new ImagePromptValue(formattedPrompt)
717
+ ];
718
+ }
719
+ });
720
+ })();
721
+ }
722
+ }
723
+ ], [
724
+ {
725
+ key: "lc_name",
726
+ value: function lc_name() {
727
+ return "ImagePromptTemplate";
728
+ }
729
+ }
730
+ ]);
731
+ return ImagePromptTemplate;
732
+ }(BasePromptTemplate);
733
+
734
+ // Default generic "any" values are for backwards compatibility.
735
+ // Replace with "string" when we are comfortable with a breaking change.
736
+ function _array_like_to_array(arr, len) {
737
+ if (len == null || len > arr.length) len = arr.length;
738
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
739
+ return arr2;
740
+ }
741
+ function _array_without_holes(arr) {
742
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
743
+ }
744
+ function _assert_this_initialized(self) {
745
+ if (self === void 0) {
746
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
747
+ }
748
+ return self;
749
+ }
750
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
751
+ try {
752
+ var info = gen[key](arg);
753
+ var value = info.value;
754
+ } catch (error) {
755
+ reject(error);
756
+ return;
757
+ }
758
+ if (info.done) {
759
+ resolve(value);
760
+ } else {
761
+ Promise.resolve(value).then(_next, _throw);
762
+ }
763
+ }
764
+ function _async_to_generator(fn) {
765
+ return function() {
766
+ var self = this, args = arguments;
767
+ return new Promise(function(resolve, reject) {
768
+ var gen = fn.apply(self, args);
769
+ function _next(value) {
770
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
771
+ }
772
+ function _throw(err) {
773
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
774
+ }
775
+ _next(undefined);
776
+ });
777
+ };
778
+ }
779
+ function _class_call_check(instance, Constructor) {
780
+ if (!(instance instanceof Constructor)) {
781
+ throw new TypeError("Cannot call a class as a function");
782
+ }
783
+ }
784
+ function _defineProperties(target, props) {
785
+ for(var i = 0; i < props.length; i++){
786
+ var descriptor = props[i];
787
+ descriptor.enumerable = descriptor.enumerable || false;
788
+ descriptor.configurable = true;
789
+ if ("value" in descriptor) descriptor.writable = true;
790
+ Object.defineProperty(target, descriptor.key, descriptor);
791
+ }
792
+ }
793
+ function _create_class(Constructor, protoProps, staticProps) {
794
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
795
+ if (staticProps) _defineProperties(Constructor, staticProps);
796
+ return Constructor;
797
+ }
798
+ function _define_property(obj, key, value) {
799
+ if (key in obj) {
800
+ Object.defineProperty(obj, key, {
801
+ value: value,
802
+ enumerable: true,
803
+ configurable: true,
804
+ writable: true
805
+ });
806
+ } else {
807
+ obj[key] = value;
808
+ }
809
+ return obj;
810
+ }
811
+ function _get_prototype_of(o) {
812
+ _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
813
+ return o.__proto__ || Object.getPrototypeOf(o);
814
+ };
815
+ return _get_prototype_of(o);
816
+ }
817
+ function _inherits(subClass, superClass) {
818
+ if (typeof superClass !== "function" && superClass !== null) {
819
+ throw new TypeError("Super expression must either be null or a function");
820
+ }
821
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
822
+ constructor: {
823
+ value: subClass,
824
+ writable: true,
825
+ configurable: true
826
+ }
827
+ });
828
+ if (superClass) _set_prototype_of(subClass, superClass);
829
+ }
830
+ function _instanceof(left, right) {
831
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
832
+ return !!right[Symbol.hasInstance](left);
833
+ } else {
834
+ return left instanceof right;
835
+ }
836
+ }
837
+ function _iterable_to_array(iter) {
838
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
839
+ }
840
+ function _non_iterable_spread() {
841
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
842
+ }
843
+ function _object_spread(target) {
844
+ for(var i = 1; i < arguments.length; i++){
845
+ var source = arguments[i] != null ? arguments[i] : {};
846
+ var ownKeys = Object.keys(source);
847
+ if (typeof Object.getOwnPropertySymbols === "function") {
848
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
849
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
850
+ }));
851
+ }
852
+ ownKeys.forEach(function(key) {
853
+ _define_property(target, key, source[key]);
854
+ });
855
+ }
856
+ return target;
857
+ }
858
+ function ownKeys(object, enumerableOnly) {
859
+ var keys = Object.keys(object);
860
+ if (Object.getOwnPropertySymbols) {
861
+ var symbols = Object.getOwnPropertySymbols(object);
862
+ if (enumerableOnly) {
863
+ symbols = symbols.filter(function(sym) {
864
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
865
+ });
866
+ }
867
+ keys.push.apply(keys, symbols);
868
+ }
869
+ return keys;
870
+ }
871
+ function _object_spread_props(target, source) {
872
+ source = source != null ? source : {};
873
+ if (Object.getOwnPropertyDescriptors) {
874
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
875
+ } else {
876
+ ownKeys(Object(source)).forEach(function(key) {
877
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
878
+ });
879
+ }
880
+ return target;
881
+ }
882
+ function _possible_constructor_return(self, call) {
883
+ if (call && (_type_of(call) === "object" || typeof call === "function")) {
884
+ return call;
885
+ }
886
+ return _assert_this_initialized(self);
887
+ }
888
+ function _set_prototype_of(o, p) {
889
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
890
+ o.__proto__ = p;
891
+ return o;
892
+ };
893
+ return _set_prototype_of(o, p);
894
+ }
895
+ function _to_consumable_array(arr) {
896
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
897
+ }
898
+ function _type_of(obj) {
899
+ "@swc/helpers - typeof";
900
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
901
+ }
902
+ function _unsupported_iterable_to_array(o, minLen) {
903
+ if (!o) return;
904
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
905
+ var n = Object.prototype.toString.call(o).slice(8, -1);
906
+ if (n === "Object" && o.constructor) n = o.constructor.name;
907
+ if (n === "Map" || n === "Set") return Array.from(n);
908
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
909
+ }
910
+ function _is_native_reflect_construct() {
911
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
912
+ if (Reflect.construct.sham) return false;
913
+ if (typeof Proxy === "function") return true;
914
+ try {
915
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
916
+ return true;
917
+ } catch (e) {
918
+ return false;
919
+ }
920
+ }
921
+ function _create_super(Derived) {
922
+ var hasNativeReflectConstruct = _is_native_reflect_construct();
923
+ return function _createSuperInternal() {
924
+ var Super = _get_prototype_of(Derived), result;
925
+ if (hasNativeReflectConstruct) {
926
+ var NewTarget = _get_prototype_of(this).constructor;
927
+ result = Reflect.construct(Super, arguments, NewTarget);
928
+ } else {
929
+ result = Super.apply(this, arguments);
930
+ }
931
+ return _possible_constructor_return(this, result);
932
+ };
933
+ }
934
+ function _ts_generator(thisArg, body) {
935
+ var f, y, t, g, _ = {
936
+ label: 0,
937
+ sent: function() {
938
+ if (t[0] & 1) throw t[1];
939
+ return t[1];
940
+ },
941
+ trys: [],
942
+ ops: []
943
+ };
944
+ return g = {
945
+ next: verb(0),
946
+ "throw": verb(1),
947
+ "return": verb(2)
948
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
949
+ return this;
950
+ }), g;
951
+ function verb(n) {
952
+ return function(v) {
953
+ return step([
954
+ n,
955
+ v
956
+ ]);
957
+ };
958
+ }
959
+ function step(op) {
960
+ if (f) throw new TypeError("Generator is already executing.");
961
+ while(_)try {
962
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
963
+ if (y = 0, t) op = [
964
+ op[0] & 2,
965
+ t.value
966
+ ];
967
+ switch(op[0]){
968
+ case 0:
969
+ case 1:
970
+ t = op;
971
+ break;
972
+ case 4:
973
+ _.label++;
974
+ return {
975
+ value: op[1],
976
+ done: false
977
+ };
978
+ case 5:
979
+ _.label++;
980
+ y = op[1];
981
+ op = [
982
+ 0
983
+ ];
984
+ continue;
985
+ case 7:
986
+ op = _.ops.pop();
987
+ _.trys.pop();
988
+ continue;
989
+ default:
990
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
991
+ _ = 0;
992
+ continue;
993
+ }
994
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
995
+ _.label = op[1];
996
+ break;
997
+ }
998
+ if (op[0] === 6 && _.label < t[1]) {
999
+ _.label = t[1];
1000
+ t = op;
1001
+ break;
1002
+ }
1003
+ if (t && _.label < t[2]) {
1004
+ _.label = t[2];
1005
+ _.ops.push(op);
1006
+ break;
1007
+ }
1008
+ if (t[2]) _.ops.pop();
1009
+ _.trys.pop();
1010
+ continue;
1011
+ }
1012
+ op = body.call(thisArg, _);
1013
+ } catch (e) {
1014
+ op = [
1015
+ 6,
1016
+ e
1017
+ ];
1018
+ y = 0;
1019
+ } finally{
1020
+ f = t = 0;
1021
+ }
1022
+ if (op[0] & 5) throw op[1];
1023
+ return {
1024
+ value: op[0] ? op[1] : void 0,
1025
+ done: true
1026
+ };
1027
+ }
1028
+ }
1029
+ function _ts_values(o) {
1030
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
1031
+ if (m) return m.call(o);
1032
+ if (o && typeof o.length === "number") return {
1033
+ next: function() {
1034
+ if (o && i >= o.length) o = void 0;
1035
+ return {
1036
+ value: o && o[i++],
1037
+ done: !o
1038
+ };
1039
+ }
1040
+ };
1041
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
1042
+ }
1043
+ /**
1044
+ * Abstract class that serves as a base for creating message prompt
1045
+ * templates. It defines how to format messages for different roles in a
1046
+ * conversation.
1047
+ */ var BaseMessagePromptTemplate = /*#__PURE__*/ function(Runnable) {
1048
+ _inherits(BaseMessagePromptTemplate, Runnable);
1049
+ var _super = _create_super(BaseMessagePromptTemplate);
1050
+ function BaseMessagePromptTemplate() {
1051
+ _class_call_check(this, BaseMessagePromptTemplate);
1052
+ var _this;
1053
+ _this = _super.call.apply(_super, [
1054
+ this
1055
+ ].concat(Array.prototype.slice.call(arguments)));
1056
+ Object.defineProperty(_assert_this_initialized(_this), "lc_namespace", {
1057
+ enumerable: true,
1058
+ configurable: true,
1059
+ writable: true,
1060
+ value: [
1061
+ "langchain_core",
1062
+ "prompts",
1063
+ "chat"
1064
+ ]
1065
+ });
1066
+ Object.defineProperty(_assert_this_initialized(_this), "lc_serializable", {
1067
+ enumerable: true,
1068
+ configurable: true,
1069
+ writable: true,
1070
+ value: true
1071
+ });
1072
+ return _this;
1073
+ }
1074
+ _create_class(BaseMessagePromptTemplate, [
1075
+ {
1076
+ key: "invoke",
1077
+ value: /**
1078
+ * Calls the formatMessages method with the provided input and options.
1079
+ * @param input Input for the formatMessages method
1080
+ * @param options Optional BaseCallbackConfig
1081
+ * @returns Formatted output messages
1082
+ */ function invoke(input, options) {
1083
+ var _this = this;
1084
+ return _async_to_generator(function() {
1085
+ return _ts_generator(this, function(_state) {
1086
+ return [
1087
+ 2,
1088
+ _this._callWithConfig(function(input) {
1089
+ return _this.formatMessages(input);
1090
+ }, input, _object_spread_props(_object_spread({}, options), {
1091
+ runType: "prompt"
1092
+ }))
1093
+ ];
1094
+ });
1095
+ })();
1096
+ }
1097
+ }
1098
+ ]);
1099
+ return BaseMessagePromptTemplate;
1100
+ }(Runnable);
1101
+ /**
1102
+ * Class that represents a placeholder for messages in a chat prompt. It
1103
+ * extends the BaseMessagePromptTemplate.
1104
+ */ var MessagesPlaceholder = /*#__PURE__*/ function(BaseMessagePromptTemplate) {
1105
+ _inherits(MessagesPlaceholder, BaseMessagePromptTemplate);
1106
+ var _super = _create_super(MessagesPlaceholder);
1107
+ function MessagesPlaceholder(fields) {
1108
+ _class_call_check(this, MessagesPlaceholder);
1109
+ var _this;
1110
+ if (typeof fields === "string") {
1111
+ // eslint-disable-next-line no-param-reassign
1112
+ fields = {
1113
+ variableName: fields
1114
+ };
1115
+ }
1116
+ _this = _super.call(this, fields);
1117
+ Object.defineProperty(_assert_this_initialized(_this), "variableName", {
1118
+ enumerable: true,
1119
+ configurable: true,
1120
+ writable: true,
1121
+ value: void 0
1122
+ });
1123
+ Object.defineProperty(_assert_this_initialized(_this), "optional", {
1124
+ enumerable: true,
1125
+ configurable: true,
1126
+ writable: true,
1127
+ value: void 0
1128
+ });
1129
+ _this.variableName = fields.variableName;
1130
+ var _fields_optional;
1131
+ _this.optional = (_fields_optional = fields.optional) !== null && _fields_optional !== void 0 ? _fields_optional : false;
1132
+ return _this;
1133
+ }
1134
+ _create_class(MessagesPlaceholder, [
1135
+ {
1136
+ key: "inputVariables",
1137
+ get: function get() {
1138
+ return [
1139
+ this.variableName
1140
+ ];
1141
+ }
1142
+ },
1143
+ {
1144
+ key: "formatMessages",
1145
+ value: function formatMessages(values) {
1146
+ var _this = this;
1147
+ return _async_to_generator(function() {
1148
+ var input, error, formattedMessages, readableInput, error1;
1149
+ return _ts_generator(this, function(_state) {
1150
+ input = values[_this.variableName];
1151
+ if (_this.optional && !input) {
1152
+ return [
1153
+ 2,
1154
+ []
1155
+ ];
1156
+ } else if (!input) {
1157
+ error = new Error('Field "'.concat(_this.variableName, '" in prompt uses a MessagesPlaceholder, which expects an array of BaseMessages as an input value. Received: undefined'));
1158
+ error.name = "InputFormatError";
1159
+ throw error;
1160
+ }
1161
+ try {
1162
+ if (Array.isArray(input)) {
1163
+ formattedMessages = input.map(coerceMessageLikeToMessage);
1164
+ } else {
1165
+ formattedMessages = [
1166
+ coerceMessageLikeToMessage(input)
1167
+ ];
1168
+ }
1169
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1170
+ } catch (e) {
1171
+ readableInput = typeof input === "string" ? input : JSON.stringify(input, null, 2);
1172
+ error1 = new Error([
1173
+ 'Field "'.concat(_this.variableName, '" in prompt uses a MessagesPlaceholder, which expects an array of BaseMessages or coerceable values as input.'),
1174
+ "Received value: ".concat(readableInput),
1175
+ "Additional message: ".concat(e.message)
1176
+ ].join("\n\n"));
1177
+ error1.name = "InputFormatError";
1178
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1179
+ error1.lc_error_code = e.lc_error_code;
1180
+ throw error1;
1181
+ }
1182
+ return [
1183
+ 2,
1184
+ formattedMessages
1185
+ ];
1186
+ });
1187
+ })();
1188
+ }
1189
+ }
1190
+ ], [
1191
+ {
1192
+ key: "lc_name",
1193
+ value: function lc_name() {
1194
+ return "MessagesPlaceholder";
1195
+ }
1196
+ }
1197
+ ]);
1198
+ return MessagesPlaceholder;
1199
+ }(BaseMessagePromptTemplate);
1200
+ /**
1201
+ * Abstract class that serves as a base for creating message string prompt
1202
+ * templates. It extends the BaseMessagePromptTemplate.
1203
+ */ var BaseMessageStringPromptTemplate = /*#__PURE__*/ function(BaseMessagePromptTemplate) {
1204
+ _inherits(BaseMessageStringPromptTemplate, BaseMessagePromptTemplate);
1205
+ var _super = _create_super(BaseMessageStringPromptTemplate);
1206
+ function BaseMessageStringPromptTemplate(fields) {
1207
+ _class_call_check(this, BaseMessageStringPromptTemplate);
1208
+ var _this;
1209
+ if (!("prompt" in fields)) {
1210
+ // eslint-disable-next-line no-param-reassign
1211
+ fields = {
1212
+ prompt: fields
1213
+ };
1214
+ }
1215
+ _this = _super.call(this, fields);
1216
+ Object.defineProperty(_assert_this_initialized(_this), "prompt", {
1217
+ enumerable: true,
1218
+ configurable: true,
1219
+ writable: true,
1220
+ value: void 0
1221
+ });
1222
+ _this.prompt = fields.prompt;
1223
+ return _this;
1224
+ }
1225
+ _create_class(BaseMessageStringPromptTemplate, [
1226
+ {
1227
+ key: "inputVariables",
1228
+ get: function get() {
1229
+ return this.prompt.inputVariables;
1230
+ }
1231
+ },
1232
+ {
1233
+ key: "formatMessages",
1234
+ value: function formatMessages(values) {
1235
+ var _this = this;
1236
+ return _async_to_generator(function() {
1237
+ return _ts_generator(this, function(_state) {
1238
+ switch(_state.label){
1239
+ case 0:
1240
+ return [
1241
+ 4,
1242
+ _this.format(values)
1243
+ ];
1244
+ case 1:
1245
+ return [
1246
+ 2,
1247
+ [
1248
+ _state.sent()
1249
+ ]
1250
+ ];
1251
+ }
1252
+ });
1253
+ })();
1254
+ }
1255
+ }
1256
+ ]);
1257
+ return BaseMessageStringPromptTemplate;
1258
+ }(BaseMessagePromptTemplate);
1259
+ /**
1260
+ * Abstract class that serves as a base for creating chat prompt
1261
+ * templates. It extends the BasePromptTemplate.
1262
+ */ var BaseChatPromptTemplate = /*#__PURE__*/ function(BasePromptTemplate) {
1263
+ _inherits(BaseChatPromptTemplate, BasePromptTemplate);
1264
+ var _super = _create_super(BaseChatPromptTemplate);
1265
+ function BaseChatPromptTemplate(input) {
1266
+ _class_call_check(this, BaseChatPromptTemplate);
1267
+ return _super.call(this, input);
1268
+ }
1269
+ _create_class(BaseChatPromptTemplate, [
1270
+ {
1271
+ key: "format",
1272
+ value: function format(values) {
1273
+ var _this = this;
1274
+ return _async_to_generator(function() {
1275
+ return _ts_generator(this, function(_state) {
1276
+ switch(_state.label){
1277
+ case 0:
1278
+ return [
1279
+ 4,
1280
+ _this.formatPromptValue(values)
1281
+ ];
1282
+ case 1:
1283
+ return [
1284
+ 2,
1285
+ _state.sent().toString()
1286
+ ];
1287
+ }
1288
+ });
1289
+ })();
1290
+ }
1291
+ },
1292
+ {
1293
+ key: "formatPromptValue",
1294
+ value: function formatPromptValue(values) {
1295
+ var _this = this;
1296
+ return _async_to_generator(function() {
1297
+ var resultMessages;
1298
+ return _ts_generator(this, function(_state) {
1299
+ switch(_state.label){
1300
+ case 0:
1301
+ return [
1302
+ 4,
1303
+ _this.formatMessages(values)
1304
+ ];
1305
+ case 1:
1306
+ resultMessages = _state.sent();
1307
+ return [
1308
+ 2,
1309
+ new ChatPromptValue(resultMessages)
1310
+ ];
1311
+ }
1312
+ });
1313
+ })();
1314
+ }
1315
+ }
1316
+ ]);
1317
+ return BaseChatPromptTemplate;
1318
+ }(BasePromptTemplate);
1319
+ /**
1320
+ * Class that represents a chat message prompt template. It extends the
1321
+ * BaseMessageStringPromptTemplate.
1322
+ */ var ChatMessagePromptTemplate = /*#__PURE__*/ function(BaseMessageStringPromptTemplate) {
1323
+ _inherits(ChatMessagePromptTemplate, BaseMessageStringPromptTemplate);
1324
+ var _super = _create_super(ChatMessagePromptTemplate);
1325
+ function ChatMessagePromptTemplate(fields, role) {
1326
+ _class_call_check(this, ChatMessagePromptTemplate);
1327
+ var _this;
1328
+ if (!("prompt" in fields)) {
1329
+ // eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
1330
+ fields = {
1331
+ prompt: fields,
1332
+ role: role
1333
+ };
1334
+ }
1335
+ _this = _super.call(this, fields);
1336
+ Object.defineProperty(_assert_this_initialized(_this), "role", {
1337
+ enumerable: true,
1338
+ configurable: true,
1339
+ writable: true,
1340
+ value: void 0
1341
+ });
1342
+ _this.role = fields.role;
1343
+ return _this;
1344
+ }
1345
+ _create_class(ChatMessagePromptTemplate, [
1346
+ {
1347
+ key: "format",
1348
+ value: function format(values) {
1349
+ var _this = this;
1350
+ return _async_to_generator(function() {
1351
+ var _;
1352
+ return _ts_generator(this, function(_state) {
1353
+ switch(_state.label){
1354
+ case 0:
1355
+ _ = ChatMessage.bind;
1356
+ return [
1357
+ 4,
1358
+ _this.prompt.format(values)
1359
+ ];
1360
+ case 1:
1361
+ return [
1362
+ 2,
1363
+ new (_.apply(ChatMessage, [
1364
+ void 0,
1365
+ _state.sent(),
1366
+ _this.role
1367
+ ]))
1368
+ ];
1369
+ }
1370
+ });
1371
+ })();
1372
+ }
1373
+ }
1374
+ ], [
1375
+ {
1376
+ key: "lc_name",
1377
+ value: function lc_name() {
1378
+ return "ChatMessagePromptTemplate";
1379
+ }
1380
+ },
1381
+ {
1382
+ key: "fromTemplate",
1383
+ value: function fromTemplate(template, role, options) {
1384
+ return new this(PromptTemplate.fromTemplate(template, {
1385
+ templateFormat: options === null || options === void 0 ? void 0 : options.templateFormat
1386
+ }), role);
1387
+ }
1388
+ }
1389
+ ]);
1390
+ return ChatMessagePromptTemplate;
1391
+ }(BaseMessageStringPromptTemplate);
1392
+ var _StringImageMessagePromptTemplate = /*#__PURE__*/ function(BaseMessagePromptTemplate) {
1393
+ _inherits(_StringImageMessagePromptTemplate, BaseMessagePromptTemplate);
1394
+ var _super = _create_super(_StringImageMessagePromptTemplate);
1395
+ function _StringImageMessagePromptTemplate(/** @TODO When we come up with a better way to type prompt templates, fix this */ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1396
+ fields, additionalOptions) {
1397
+ _class_call_check(this, _StringImageMessagePromptTemplate);
1398
+ var _this;
1399
+ if (!("prompt" in fields)) {
1400
+ // eslint-disable-next-line no-param-reassign
1401
+ fields = {
1402
+ prompt: fields
1403
+ };
1404
+ }
1405
+ _this = _super.call(this, fields);
1406
+ Object.defineProperty(_assert_this_initialized(_this), "lc_namespace", {
1407
+ enumerable: true,
1408
+ configurable: true,
1409
+ writable: true,
1410
+ value: [
1411
+ "langchain_core",
1412
+ "prompts",
1413
+ "chat"
1414
+ ]
1415
+ });
1416
+ Object.defineProperty(_assert_this_initialized(_this), "lc_serializable", {
1417
+ enumerable: true,
1418
+ configurable: true,
1419
+ writable: true,
1420
+ value: true
1421
+ });
1422
+ Object.defineProperty(_assert_this_initialized(_this), "inputVariables", {
1423
+ enumerable: true,
1424
+ configurable: true,
1425
+ writable: true,
1426
+ value: []
1427
+ });
1428
+ Object.defineProperty(_assert_this_initialized(_this), "additionalOptions", {
1429
+ enumerable: true,
1430
+ configurable: true,
1431
+ writable: true,
1432
+ value: {}
1433
+ });
1434
+ Object.defineProperty(_assert_this_initialized(_this), "prompt", {
1435
+ enumerable: true,
1436
+ configurable: true,
1437
+ writable: true,
1438
+ value: void 0
1439
+ });
1440
+ Object.defineProperty(_assert_this_initialized(_this), "messageClass", {
1441
+ enumerable: true,
1442
+ configurable: true,
1443
+ writable: true,
1444
+ value: void 0
1445
+ });
1446
+ // ChatMessage contains role field, others don't.
1447
+ // Because of this, we have a separate class property for ChatMessage.
1448
+ Object.defineProperty(_assert_this_initialized(_this), "chatMessageClass", {
1449
+ enumerable: true,
1450
+ configurable: true,
1451
+ writable: true,
1452
+ value: void 0
1453
+ });
1454
+ _this.prompt = fields.prompt;
1455
+ if (Array.isArray(_this.prompt)) {
1456
+ var inputVariables = [];
1457
+ _this.prompt.forEach(function(prompt) {
1458
+ if ("inputVariables" in prompt) {
1459
+ inputVariables = inputVariables.concat(prompt.inputVariables);
1460
+ }
1461
+ });
1462
+ _this.inputVariables = inputVariables;
1463
+ } else {
1464
+ _this.inputVariables = _this.prompt.inputVariables;
1465
+ }
1466
+ _this.additionalOptions = additionalOptions !== null && additionalOptions !== void 0 ? additionalOptions : _this.additionalOptions;
1467
+ return _this;
1468
+ }
1469
+ _create_class(_StringImageMessagePromptTemplate, [
1470
+ {
1471
+ key: "createMessage",
1472
+ value: function createMessage(content) {
1473
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1474
+ var constructor = this.constructor;
1475
+ if (constructor._messageClass()) {
1476
+ var MsgClass = constructor._messageClass();
1477
+ return new MsgClass({
1478
+ content: content
1479
+ });
1480
+ } else if (constructor.chatMessageClass) {
1481
+ var MsgClass1 = constructor.chatMessageClass();
1482
+ // Assuming ChatMessage constructor also takes a content argument
1483
+ return new MsgClass1({
1484
+ content: content,
1485
+ role: this.getRoleFromMessageClass(MsgClass1.lc_name())
1486
+ });
1487
+ } else {
1488
+ throw new Error("No message class defined");
1489
+ }
1490
+ }
1491
+ },
1492
+ {
1493
+ key: "getRoleFromMessageClass",
1494
+ value: function getRoleFromMessageClass(name) {
1495
+ switch(name){
1496
+ case "HumanMessage":
1497
+ return "human";
1498
+ case "AIMessage":
1499
+ return "ai";
1500
+ case "SystemMessage":
1501
+ return "system";
1502
+ case "ChatMessage":
1503
+ return "chat";
1504
+ default:
1505
+ throw new Error("Invalid message class name");
1506
+ }
1507
+ }
1508
+ },
1509
+ {
1510
+ key: "format",
1511
+ value: function format(input) {
1512
+ var _this = this;
1513
+ return _async_to_generator(function() {
1514
+ var text, content, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, prompt, inputs, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, item, formatted, additionalContentFields, formatted1, additionalContentFields1, err;
1515
+ return _ts_generator(this, function(_state) {
1516
+ switch(_state.label){
1517
+ case 0:
1518
+ if (!_instanceof(_this.prompt, BaseStringPromptTemplate)) return [
1519
+ 3,
1520
+ 2
1521
+ ];
1522
+ return [
1523
+ 4,
1524
+ _this.prompt.format(input)
1525
+ ];
1526
+ case 1:
1527
+ text = _state.sent();
1528
+ return [
1529
+ 2,
1530
+ _this.createMessage(text)
1531
+ ];
1532
+ case 2:
1533
+ content = [];
1534
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1535
+ _state.label = 3;
1536
+ case 3:
1537
+ _state.trys.push([
1538
+ 3,
1539
+ 10,
1540
+ 11,
1541
+ 12
1542
+ ]);
1543
+ _iterator = _this.prompt[Symbol.iterator]();
1544
+ _state.label = 4;
1545
+ case 4:
1546
+ if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
1547
+ 3,
1548
+ 9
1549
+ ];
1550
+ prompt = _step.value;
1551
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1552
+ inputs = {};
1553
+ if (!("inputVariables" in prompt)) {
1554
+ throw new Error("Prompt ".concat(prompt, " does not have inputVariables defined."));
1555
+ }
1556
+ _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
1557
+ try {
1558
+ for(_iterator1 = prompt.inputVariables[Symbol.iterator](); !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
1559
+ item = _step1.value;
1560
+ if (!inputs) {
1561
+ inputs = _define_property({}, item, input[item]);
1562
+ }
1563
+ inputs = _object_spread_props(_object_spread({}, inputs), _define_property({}, item, input[item]));
1564
+ }
1565
+ } catch (err) {
1566
+ _didIteratorError1 = true;
1567
+ _iteratorError1 = err;
1568
+ } finally{
1569
+ try {
1570
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
1571
+ _iterator1.return();
1572
+ }
1573
+ } finally{
1574
+ if (_didIteratorError1) {
1575
+ throw _iteratorError1;
1576
+ }
1577
+ }
1578
+ }
1579
+ if (!_instanceof(prompt, BaseStringPromptTemplate)) return [
1580
+ 3,
1581
+ 6
1582
+ ];
1583
+ return [
1584
+ 4,
1585
+ prompt.format(inputs)
1586
+ ];
1587
+ case 5:
1588
+ formatted = _state.sent();
1589
+ additionalContentFields = void 0;
1590
+ if ("additionalContentFields" in prompt) {
1591
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1592
+ additionalContentFields = prompt.additionalContentFields;
1593
+ }
1594
+ content.push(_object_spread_props(_object_spread({}, additionalContentFields), {
1595
+ type: "text",
1596
+ text: formatted
1597
+ }));
1598
+ return [
1599
+ 3,
1600
+ 8
1601
+ ];
1602
+ case 6:
1603
+ if (!_instanceof(prompt, ImagePromptTemplate)) return [
1604
+ 3,
1605
+ 8
1606
+ ];
1607
+ return [
1608
+ 4,
1609
+ prompt.format(inputs)
1610
+ ];
1611
+ case 7:
1612
+ formatted1 = _state.sent();
1613
+ additionalContentFields1 = void 0;
1614
+ if ("additionalContentFields" in prompt) {
1615
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1616
+ additionalContentFields1 = prompt.additionalContentFields;
1617
+ }
1618
+ content.push(_object_spread_props(_object_spread({}, additionalContentFields1), {
1619
+ type: "image_url",
1620
+ image_url: formatted1
1621
+ }));
1622
+ _state.label = 8;
1623
+ case 8:
1624
+ _iteratorNormalCompletion = true;
1625
+ return [
1626
+ 3,
1627
+ 4
1628
+ ];
1629
+ case 9:
1630
+ return [
1631
+ 3,
1632
+ 12
1633
+ ];
1634
+ case 10:
1635
+ err = _state.sent();
1636
+ _didIteratorError = true;
1637
+ _iteratorError = err;
1638
+ return [
1639
+ 3,
1640
+ 12
1641
+ ];
1642
+ case 11:
1643
+ try {
1644
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1645
+ _iterator.return();
1646
+ }
1647
+ } finally{
1648
+ if (_didIteratorError) {
1649
+ throw _iteratorError;
1650
+ }
1651
+ }
1652
+ return [
1653
+ 7
1654
+ ];
1655
+ case 12:
1656
+ return [
1657
+ 2,
1658
+ _this.createMessage(content)
1659
+ ];
1660
+ case 13:
1661
+ return [
1662
+ 2
1663
+ ];
1664
+ }
1665
+ });
1666
+ })();
1667
+ }
1668
+ },
1669
+ {
1670
+ key: "formatMessages",
1671
+ value: function formatMessages(values) {
1672
+ var _this = this;
1673
+ return _async_to_generator(function() {
1674
+ return _ts_generator(this, function(_state) {
1675
+ switch(_state.label){
1676
+ case 0:
1677
+ return [
1678
+ 4,
1679
+ _this.format(values)
1680
+ ];
1681
+ case 1:
1682
+ return [
1683
+ 2,
1684
+ [
1685
+ _state.sent()
1686
+ ]
1687
+ ];
1688
+ }
1689
+ });
1690
+ })();
1691
+ }
1692
+ }
1693
+ ], [
1694
+ {
1695
+ key: "_messageClass",
1696
+ value: function _messageClass() {
1697
+ throw new Error("Can not invoke _messageClass from inside _StringImageMessagePromptTemplate");
1698
+ }
1699
+ },
1700
+ {
1701
+ key: "fromTemplate",
1702
+ value: function fromTemplate(template, additionalOptions) {
1703
+ if (typeof template === "string") {
1704
+ return new this(PromptTemplate.fromTemplate(template, additionalOptions));
1705
+ }
1706
+ var prompt = [];
1707
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1708
+ try {
1709
+ for(var _iterator = template[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
1710
+ var item = _step.value;
1711
+ if (typeof item === "string" || typeof item === "object" && "text" in item) {
1712
+ var text = "";
1713
+ if (typeof item === "string") {
1714
+ text = item;
1715
+ } else if (typeof item.text === "string") {
1716
+ var _item_text;
1717
+ text = (_item_text = item.text) !== null && _item_text !== void 0 ? _item_text : "";
1718
+ }
1719
+ var options = _object_spread({}, additionalOptions, typeof item !== "string" ? {
1720
+ additionalContentFields: item
1721
+ } : {});
1722
+ prompt.push(PromptTemplate.fromTemplate(text, options));
1723
+ } else if (typeof item === "object" && "image_url" in item) {
1724
+ var _item_image_url;
1725
+ var imgTemplate = (_item_image_url = item.image_url) !== null && _item_image_url !== void 0 ? _item_image_url : "";
1726
+ var imgTemplateObject = void 0;
1727
+ var inputVariables = [];
1728
+ if (typeof imgTemplate === "string") {
1729
+ var parsedTemplate = void 0;
1730
+ if ((additionalOptions === null || additionalOptions === void 0 ? void 0 : additionalOptions.templateFormat) === "mustache") {
1731
+ parsedTemplate = parseMustache(imgTemplate);
1732
+ } else {
1733
+ parsedTemplate = parseFString(imgTemplate);
1734
+ }
1735
+ var variables = parsedTemplate.flatMap(function(item) {
1736
+ return item.type === "variable" ? [
1737
+ item.name
1738
+ ] : [];
1739
+ });
1740
+ var _variables_length;
1741
+ if (((_variables_length = variables === null || variables === void 0 ? void 0 : variables.length) !== null && _variables_length !== void 0 ? _variables_length : 0) > 0) {
1742
+ if (variables.length > 1) {
1743
+ throw new Error("Only one format variable allowed per image template.\nGot: ".concat(variables, "\nFrom: ").concat(imgTemplate));
1744
+ }
1745
+ inputVariables = [
1746
+ variables[0]
1747
+ ];
1748
+ } else {
1749
+ inputVariables = [];
1750
+ }
1751
+ imgTemplate = {
1752
+ url: imgTemplate
1753
+ };
1754
+ imgTemplateObject = new ImagePromptTemplate({
1755
+ template: imgTemplate,
1756
+ inputVariables: inputVariables,
1757
+ templateFormat: additionalOptions === null || additionalOptions === void 0 ? void 0 : additionalOptions.templateFormat,
1758
+ additionalContentFields: item
1759
+ });
1760
+ } else if (typeof imgTemplate === "object") {
1761
+ if ("url" in imgTemplate) {
1762
+ var parsedTemplate1 = void 0;
1763
+ if ((additionalOptions === null || additionalOptions === void 0 ? void 0 : additionalOptions.templateFormat) === "mustache") {
1764
+ parsedTemplate1 = parseMustache(imgTemplate.url);
1765
+ } else {
1766
+ parsedTemplate1 = parseFString(imgTemplate.url);
1767
+ }
1768
+ inputVariables = parsedTemplate1.flatMap(function(item) {
1769
+ return item.type === "variable" ? [
1770
+ item.name
1771
+ ] : [];
1772
+ });
1773
+ } else {
1774
+ inputVariables = [];
1775
+ }
1776
+ imgTemplateObject = new ImagePromptTemplate({
1777
+ template: imgTemplate,
1778
+ inputVariables: inputVariables,
1779
+ templateFormat: additionalOptions === null || additionalOptions === void 0 ? void 0 : additionalOptions.templateFormat,
1780
+ additionalContentFields: item
1781
+ });
1782
+ } else {
1783
+ throw new Error("Invalid image template");
1784
+ }
1785
+ prompt.push(imgTemplateObject);
1786
+ }
1787
+ }
1788
+ } catch (err) {
1789
+ _didIteratorError = true;
1790
+ _iteratorError = err;
1791
+ } finally{
1792
+ try {
1793
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
1794
+ _iterator.return();
1795
+ }
1796
+ } finally{
1797
+ if (_didIteratorError) {
1798
+ throw _iteratorError;
1799
+ }
1800
+ }
1801
+ }
1802
+ return new this({
1803
+ prompt: prompt,
1804
+ additionalOptions: additionalOptions
1805
+ });
1806
+ }
1807
+ }
1808
+ ]);
1809
+ return _StringImageMessagePromptTemplate;
1810
+ }(BaseMessagePromptTemplate);
1811
+ /**
1812
+ * Class that represents a human message prompt template. It extends the
1813
+ * BaseMessageStringPromptTemplate.
1814
+ * @example
1815
+ * ```typescript
1816
+ * const message = HumanMessagePromptTemplate.fromTemplate("{text}");
1817
+ * const formatted = await message.format({ text: "Hello world!" });
1818
+ *
1819
+ * const chatPrompt = ChatPromptTemplate.fromMessages([message]);
1820
+ * const formattedChatPrompt = await chatPrompt.invoke({
1821
+ * text: "Hello world!",
1822
+ * });
1823
+ * ```
1824
+ */ var HumanMessagePromptTemplate = /*#__PURE__*/ function(_StringImageMessagePromptTemplate) {
1825
+ _inherits(HumanMessagePromptTemplate, _StringImageMessagePromptTemplate);
1826
+ var _super = _create_super(HumanMessagePromptTemplate);
1827
+ function HumanMessagePromptTemplate() {
1828
+ _class_call_check(this, HumanMessagePromptTemplate);
1829
+ return _super.apply(this, arguments);
1830
+ }
1831
+ _create_class(HumanMessagePromptTemplate, null, [
1832
+ {
1833
+ key: "_messageClass",
1834
+ value: function _messageClass() {
1835
+ return HumanMessage;
1836
+ }
1837
+ },
1838
+ {
1839
+ key: "lc_name",
1840
+ value: function lc_name() {
1841
+ return "HumanMessagePromptTemplate";
1842
+ }
1843
+ }
1844
+ ]);
1845
+ return HumanMessagePromptTemplate;
1846
+ }(_StringImageMessagePromptTemplate);
1847
+ /**
1848
+ * Class that represents an AI message prompt template. It extends the
1849
+ * BaseMessageStringPromptTemplate.
1850
+ */ var AIMessagePromptTemplate = /*#__PURE__*/ function(_StringImageMessagePromptTemplate) {
1851
+ _inherits(AIMessagePromptTemplate, _StringImageMessagePromptTemplate);
1852
+ var _super = _create_super(AIMessagePromptTemplate);
1853
+ function AIMessagePromptTemplate() {
1854
+ _class_call_check(this, AIMessagePromptTemplate);
1855
+ return _super.apply(this, arguments);
1856
+ }
1857
+ _create_class(AIMessagePromptTemplate, null, [
1858
+ {
1859
+ key: "_messageClass",
1860
+ value: function _messageClass() {
1861
+ return AIMessage;
1862
+ }
1863
+ },
1864
+ {
1865
+ key: "lc_name",
1866
+ value: function lc_name() {
1867
+ return "AIMessagePromptTemplate";
1868
+ }
1869
+ }
1870
+ ]);
1871
+ return AIMessagePromptTemplate;
1872
+ }(_StringImageMessagePromptTemplate);
1873
+ /**
1874
+ * Class that represents a system message prompt template. It extends the
1875
+ * BaseMessageStringPromptTemplate.
1876
+ * @example
1877
+ * ```typescript
1878
+ * const message = SystemMessagePromptTemplate.fromTemplate("{text}");
1879
+ * const formatted = await message.format({ text: "Hello world!" });
1880
+ *
1881
+ * const chatPrompt = ChatPromptTemplate.fromMessages([message]);
1882
+ * const formattedChatPrompt = await chatPrompt.invoke({
1883
+ * text: "Hello world!",
1884
+ * });
1885
+ * ```
1886
+ */ var SystemMessagePromptTemplate = /*#__PURE__*/ function(_StringImageMessagePromptTemplate) {
1887
+ _inherits(SystemMessagePromptTemplate, _StringImageMessagePromptTemplate);
1888
+ var _super = _create_super(SystemMessagePromptTemplate);
1889
+ function SystemMessagePromptTemplate() {
1890
+ _class_call_check(this, SystemMessagePromptTemplate);
1891
+ return _super.apply(this, arguments);
1892
+ }
1893
+ _create_class(SystemMessagePromptTemplate, null, [
1894
+ {
1895
+ key: "_messageClass",
1896
+ value: function _messageClass() {
1897
+ return SystemMessage;
1898
+ }
1899
+ },
1900
+ {
1901
+ key: "lc_name",
1902
+ value: function lc_name() {
1903
+ return "SystemMessagePromptTemplate";
1904
+ }
1905
+ }
1906
+ ]);
1907
+ return SystemMessagePromptTemplate;
1908
+ }(_StringImageMessagePromptTemplate);
1909
+ function _isBaseMessagePromptTemplate(baseMessagePromptTemplateLike) {
1910
+ return typeof baseMessagePromptTemplateLike.formatMessages === "function";
1911
+ }
1912
+ function _coerceMessagePromptTemplateLike(messagePromptTemplateLike, extra) {
1913
+ if (_isBaseMessagePromptTemplate(messagePromptTemplateLike) || isBaseMessage(messagePromptTemplateLike)) {
1914
+ return messagePromptTemplateLike;
1915
+ }
1916
+ if (Array.isArray(messagePromptTemplateLike) && messagePromptTemplateLike[0] === "placeholder") {
1917
+ var messageContent = messagePromptTemplateLike[1];
1918
+ if (typeof messageContent !== "string" || messageContent[0] !== "{" || messageContent[messageContent.length - 1] !== "}") {
1919
+ throw new Error('Invalid placeholder template: "'.concat(messagePromptTemplateLike[1], '". Expected a variable name surrounded by curly braces.'));
1920
+ }
1921
+ var variableName = messageContent.slice(1, -1);
1922
+ return new MessagesPlaceholder({
1923
+ variableName: variableName,
1924
+ optional: true
1925
+ });
1926
+ }
1927
+ var message = coerceMessageLikeToMessage(messagePromptTemplateLike);
1928
+ var templateData;
1929
+ if (typeof message.content === "string") {
1930
+ templateData = message.content;
1931
+ } else {
1932
+ // Assuming message.content is an array of complex objects, transform it.
1933
+ templateData = message.content.map(function(item) {
1934
+ if ("text" in item) {
1935
+ return _object_spread_props(_object_spread({}, item), {
1936
+ text: item.text
1937
+ });
1938
+ } else if ("image_url" in item) {
1939
+ return _object_spread_props(_object_spread({}, item), {
1940
+ image_url: item.image_url
1941
+ });
1942
+ } else {
1943
+ return item;
1944
+ }
1945
+ });
1946
+ }
1947
+ if (message._getType() === "human") {
1948
+ return HumanMessagePromptTemplate.fromTemplate(templateData, extra);
1949
+ } else if (message._getType() === "ai") {
1950
+ return AIMessagePromptTemplate.fromTemplate(templateData, extra);
1951
+ } else if (message._getType() === "system") {
1952
+ return SystemMessagePromptTemplate.fromTemplate(templateData, extra);
1953
+ } else if (ChatMessage.isInstance(message)) {
1954
+ return ChatMessagePromptTemplate.fromTemplate(message.content, message.role, extra);
1955
+ } else {
1956
+ throw new Error('Could not coerce message prompt template from input. Received message type: "'.concat(message._getType(), '".'));
1957
+ }
1958
+ }
1959
+ function isMessagesPlaceholder(x) {
1960
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1961
+ return x.constructor.lc_name() === "MessagesPlaceholder";
1962
+ }
1963
+ /**
1964
+ * Class that represents a chat prompt. It extends the
1965
+ * BaseChatPromptTemplate and uses an array of BaseMessagePromptTemplate
1966
+ * instances to format a series of messages for a conversation.
1967
+ * @example
1968
+ * ```typescript
1969
+ * const message = SystemMessagePromptTemplate.fromTemplate("{text}");
1970
+ * const chatPrompt = ChatPromptTemplate.fromMessages([
1971
+ * ["ai", "You are a helpful assistant."],
1972
+ * message,
1973
+ * ]);
1974
+ * const formattedChatPrompt = await chatPrompt.invoke({
1975
+ * text: "Hello world!",
1976
+ * });
1977
+ * ```
1978
+ */ var ChatPromptTemplate = /*#__PURE__*/ function(BaseChatPromptTemplate) {
1979
+ _inherits(ChatPromptTemplate, BaseChatPromptTemplate);
1980
+ var _super = _create_super(ChatPromptTemplate);
1981
+ function ChatPromptTemplate(input) {
1982
+ _class_call_check(this, ChatPromptTemplate);
1983
+ var _this;
1984
+ _this = _super.call(this, input);
1985
+ Object.defineProperty(_assert_this_initialized(_this), "promptMessages", {
1986
+ enumerable: true,
1987
+ configurable: true,
1988
+ writable: true,
1989
+ value: void 0
1990
+ });
1991
+ Object.defineProperty(_assert_this_initialized(_this), "validateTemplate", {
1992
+ enumerable: true,
1993
+ configurable: true,
1994
+ writable: true,
1995
+ value: true
1996
+ });
1997
+ Object.defineProperty(_assert_this_initialized(_this), "templateFormat", {
1998
+ enumerable: true,
1999
+ configurable: true,
2000
+ writable: true,
2001
+ value: "f-string"
2002
+ });
2003
+ // If input is mustache and validateTemplate is not defined, set it to false
2004
+ if (input.templateFormat === "mustache" && input.validateTemplate === undefined) {
2005
+ _this.validateTemplate = false;
2006
+ }
2007
+ Object.assign(_assert_this_initialized(_this), input);
2008
+ if (_this.validateTemplate) {
2009
+ var inputVariablesMessages = new Set();
2010
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2011
+ try {
2012
+ for(var _iterator = _this.promptMessages[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2013
+ var promptMessage = _step.value;
2014
+ // eslint-disable-next-line no-instanceof/no-instanceof
2015
+ if (_instanceof(promptMessage, BaseMessage)) continue;
2016
+ var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
2017
+ try {
2018
+ for(var _iterator1 = promptMessage.inputVariables[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
2019
+ var inputVariable = _step1.value;
2020
+ inputVariablesMessages.add(inputVariable);
2021
+ }
2022
+ } catch (err) {
2023
+ _didIteratorError1 = true;
2024
+ _iteratorError1 = err;
2025
+ } finally{
2026
+ try {
2027
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
2028
+ _iterator1.return();
2029
+ }
2030
+ } finally{
2031
+ if (_didIteratorError1) {
2032
+ throw _iteratorError1;
2033
+ }
2034
+ }
2035
+ }
2036
+ }
2037
+ } catch (err) {
2038
+ _didIteratorError = true;
2039
+ _iteratorError = err;
2040
+ } finally{
2041
+ try {
2042
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2043
+ _iterator.return();
2044
+ }
2045
+ } finally{
2046
+ if (_didIteratorError) {
2047
+ throw _iteratorError;
2048
+ }
2049
+ }
2050
+ }
2051
+ var totalInputVariables = _this.inputVariables;
2052
+ var inputVariablesInstance = new Set(_this.partialVariables ? totalInputVariables.concat(Object.keys(_this.partialVariables)) : totalInputVariables);
2053
+ var difference = new Set(_to_consumable_array(inputVariablesInstance).filter(function(x) {
2054
+ return !inputVariablesMessages.has(x);
2055
+ }));
2056
+ if (difference.size > 0) {
2057
+ throw new Error("Input variables `".concat(_to_consumable_array(difference), "` are not used in any of the prompt messages."));
2058
+ }
2059
+ var otherDifference = new Set(_to_consumable_array(inputVariablesMessages).filter(function(x) {
2060
+ return !inputVariablesInstance.has(x);
2061
+ }));
2062
+ if (otherDifference.size > 0) {
2063
+ throw new Error("Input variables `".concat(_to_consumable_array(otherDifference), "` are used in prompt messages but not in the prompt template."));
2064
+ }
2065
+ }
2066
+ return _this;
2067
+ }
2068
+ _create_class(ChatPromptTemplate, [
2069
+ {
2070
+ key: "lc_aliases",
2071
+ get: function get() {
2072
+ return {
2073
+ promptMessages: "messages"
2074
+ };
2075
+ }
2076
+ },
2077
+ {
2078
+ key: "_getPromptType",
2079
+ value: function _getPromptType() {
2080
+ return "chat";
2081
+ }
2082
+ },
2083
+ {
2084
+ key: "_parseImagePrompts",
2085
+ value: function _parseImagePrompts(message, inputValues) {
2086
+ var _this = this;
2087
+ return _async_to_generator(function() {
2088
+ var formattedMessageContent;
2089
+ return _ts_generator(this, function(_state) {
2090
+ switch(_state.label){
2091
+ case 0:
2092
+ if (typeof message.content === "string") {
2093
+ return [
2094
+ 2,
2095
+ message
2096
+ ];
2097
+ }
2098
+ return [
2099
+ 4,
2100
+ Promise.all(message.content.map(function() {
2101
+ var _ref = _async_to_generator(function(item) {
2102
+ var imageUrl, promptTemplatePlaceholder, formattedUrl;
2103
+ return _ts_generator(this, function(_state) {
2104
+ switch(_state.label){
2105
+ case 0:
2106
+ if (item.type !== "image_url") {
2107
+ return [
2108
+ 2,
2109
+ item
2110
+ ];
2111
+ }
2112
+ imageUrl = "";
2113
+ if (typeof item.image_url === "string") {
2114
+ imageUrl = item.image_url;
2115
+ } else {
2116
+ imageUrl = item.image_url.url;
2117
+ }
2118
+ promptTemplatePlaceholder = PromptTemplate.fromTemplate(imageUrl, {
2119
+ templateFormat: _this.templateFormat
2120
+ });
2121
+ return [
2122
+ 4,
2123
+ promptTemplatePlaceholder.format(inputValues)
2124
+ ];
2125
+ case 1:
2126
+ formattedUrl = _state.sent();
2127
+ if (typeof item.image_url !== "string" && "url" in item.image_url) {
2128
+ // eslint-disable-next-line no-param-reassign
2129
+ item.image_url.url = formattedUrl;
2130
+ } else {
2131
+ // eslint-disable-next-line no-param-reassign
2132
+ item.image_url = formattedUrl;
2133
+ }
2134
+ return [
2135
+ 2,
2136
+ item
2137
+ ];
2138
+ }
2139
+ });
2140
+ });
2141
+ return function(item) {
2142
+ return _ref.apply(this, arguments);
2143
+ };
2144
+ }()))
2145
+ ];
2146
+ case 1:
2147
+ formattedMessageContent = _state.sent();
2148
+ // eslint-disable-next-line no-param-reassign
2149
+ message.content = formattedMessageContent;
2150
+ return [
2151
+ 2,
2152
+ message
2153
+ ];
2154
+ }
2155
+ });
2156
+ })();
2157
+ }
2158
+ },
2159
+ {
2160
+ key: "formatMessages",
2161
+ value: function formatMessages(values) {
2162
+ var _this = this;
2163
+ return _async_to_generator(function() {
2164
+ var allValues, resultMessages, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step, err;
2165
+ return _ts_generator(this, function(_state) {
2166
+ switch(_state.label){
2167
+ case 0:
2168
+ return [
2169
+ 4,
2170
+ _this.mergePartialAndUserVariables(values)
2171
+ ];
2172
+ case 1:
2173
+ allValues = _state.sent();
2174
+ resultMessages = [];
2175
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2176
+ _state.label = 2;
2177
+ case 2:
2178
+ _state.trys.push([
2179
+ 2,
2180
+ 7,
2181
+ 8,
2182
+ 9
2183
+ ]);
2184
+ _loop = function() {
2185
+ var promptMessage, _, inputValues, message;
2186
+ return _ts_generator(this, function(_state) {
2187
+ switch(_state.label){
2188
+ case 0:
2189
+ promptMessage = _step.value;
2190
+ if (!_instanceof(promptMessage, BaseMessage)) return [
2191
+ 3,
2192
+ 2
2193
+ ];
2194
+ _ = resultMessages.push;
2195
+ return [
2196
+ 4,
2197
+ _this._parseImagePrompts(promptMessage, allValues)
2198
+ ];
2199
+ case 1:
2200
+ _.apply(resultMessages, [
2201
+ _state.sent()
2202
+ ]);
2203
+ return [
2204
+ 3,
2205
+ 4
2206
+ ];
2207
+ case 2:
2208
+ inputValues = promptMessage.inputVariables.reduce(function(acc, inputVariable) {
2209
+ if (!(inputVariable in allValues) && !(isMessagesPlaceholder(promptMessage) && promptMessage.optional)) {
2210
+ var error = addLangChainErrorFields(new Error("Missing value for input variable `".concat(inputVariable.toString(), "`")), "INVALID_PROMPT_INPUT");
2211
+ throw error;
2212
+ }
2213
+ acc[inputVariable] = allValues[inputVariable];
2214
+ return acc;
2215
+ }, {});
2216
+ return [
2217
+ 4,
2218
+ promptMessage.formatMessages(inputValues)
2219
+ ];
2220
+ case 3:
2221
+ message = _state.sent();
2222
+ resultMessages = resultMessages.concat(message);
2223
+ _state.label = 4;
2224
+ case 4:
2225
+ return [
2226
+ 2
2227
+ ];
2228
+ }
2229
+ });
2230
+ };
2231
+ _iterator = _this.promptMessages[Symbol.iterator]();
2232
+ _state.label = 3;
2233
+ case 3:
2234
+ if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
2235
+ 3,
2236
+ 6
2237
+ ];
2238
+ return [
2239
+ 5,
2240
+ _ts_values(_loop())
2241
+ ];
2242
+ case 4:
2243
+ _state.sent();
2244
+ _state.label = 5;
2245
+ case 5:
2246
+ _iteratorNormalCompletion = true;
2247
+ return [
2248
+ 3,
2249
+ 3
2250
+ ];
2251
+ case 6:
2252
+ return [
2253
+ 3,
2254
+ 9
2255
+ ];
2256
+ case 7:
2257
+ err = _state.sent();
2258
+ _didIteratorError = true;
2259
+ _iteratorError = err;
2260
+ return [
2261
+ 3,
2262
+ 9
2263
+ ];
2264
+ case 8:
2265
+ try {
2266
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2267
+ _iterator.return();
2268
+ }
2269
+ } finally{
2270
+ if (_didIteratorError) {
2271
+ throw _iteratorError;
2272
+ }
2273
+ }
2274
+ return [
2275
+ 7
2276
+ ];
2277
+ case 9:
2278
+ return [
2279
+ 2,
2280
+ resultMessages
2281
+ ];
2282
+ }
2283
+ });
2284
+ })();
2285
+ }
2286
+ },
2287
+ {
2288
+ key: "partial",
2289
+ value: function partial(values) {
2290
+ var _this = this;
2291
+ return _async_to_generator(function() {
2292
+ var newInputVariables, _this_partialVariables, newPartialVariables, promptDict;
2293
+ return _ts_generator(this, function(_state) {
2294
+ // This is implemented in a way it doesn't require making
2295
+ // BaseMessagePromptTemplate aware of .partial()
2296
+ newInputVariables = _this.inputVariables.filter(function(iv) {
2297
+ return !(iv in values);
2298
+ });
2299
+ newPartialVariables = _object_spread({}, (_this_partialVariables = _this.partialVariables) !== null && _this_partialVariables !== void 0 ? _this_partialVariables : {}, values);
2300
+ promptDict = _object_spread_props(_object_spread({}, _this), {
2301
+ inputVariables: newInputVariables,
2302
+ partialVariables: newPartialVariables
2303
+ });
2304
+ return [
2305
+ 2,
2306
+ new ChatPromptTemplate(promptDict)
2307
+ ];
2308
+ });
2309
+ })();
2310
+ }
2311
+ }
2312
+ ], [
2313
+ {
2314
+ key: "lc_name",
2315
+ value: function lc_name() {
2316
+ return "ChatPromptTemplate";
2317
+ }
2318
+ },
2319
+ {
2320
+ key: "fromTemplate",
2321
+ value: function fromTemplate(template, options) {
2322
+ var prompt = PromptTemplate.fromTemplate(template, options);
2323
+ var humanTemplate = new HumanMessagePromptTemplate({
2324
+ prompt: prompt
2325
+ });
2326
+ return this.fromMessages([
2327
+ humanTemplate
2328
+ ]);
2329
+ }
2330
+ },
2331
+ {
2332
+ key: "fromMessages",
2333
+ value: /**
2334
+ * Create a chat model-specific prompt from individual chat messages
2335
+ * or message-like tuples.
2336
+ * @param promptMessages Messages to be passed to the chat model
2337
+ * @returns A new ChatPromptTemplate
2338
+ */ function fromMessages(promptMessages, extra) {
2339
+ var flattenedMessages = promptMessages.reduce(function(acc, promptMessage) {
2340
+ return acc.concat(// eslint-disable-next-line no-instanceof/no-instanceof
2341
+ _instanceof(promptMessage, ChatPromptTemplate) ? promptMessage.promptMessages : [
2342
+ _coerceMessagePromptTemplateLike(promptMessage, extra)
2343
+ ]);
2344
+ }, []);
2345
+ var flattenedPartialVariables = promptMessages.reduce(function(acc, promptMessage) {
2346
+ return(// eslint-disable-next-line no-instanceof/no-instanceof
2347
+ _instanceof(promptMessage, ChatPromptTemplate) ? Object.assign(acc, promptMessage.partialVariables) : acc);
2348
+ }, Object.create(null));
2349
+ var inputVariables = new Set();
2350
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
2351
+ try {
2352
+ for(var _iterator = flattenedMessages[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
2353
+ var promptMessage = _step.value;
2354
+ // eslint-disable-next-line no-instanceof/no-instanceof
2355
+ if (_instanceof(promptMessage, BaseMessage)) continue;
2356
+ var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
2357
+ try {
2358
+ for(var _iterator1 = promptMessage.inputVariables[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
2359
+ var inputVariable = _step1.value;
2360
+ if (inputVariable in flattenedPartialVariables) {
2361
+ continue;
2362
+ }
2363
+ inputVariables.add(inputVariable);
2364
+ }
2365
+ } catch (err) {
2366
+ _didIteratorError1 = true;
2367
+ _iteratorError1 = err;
2368
+ } finally{
2369
+ try {
2370
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
2371
+ _iterator1.return();
2372
+ }
2373
+ } finally{
2374
+ if (_didIteratorError1) {
2375
+ throw _iteratorError1;
2376
+ }
2377
+ }
2378
+ }
2379
+ }
2380
+ } catch (err) {
2381
+ _didIteratorError = true;
2382
+ _iteratorError = err;
2383
+ } finally{
2384
+ try {
2385
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
2386
+ _iterator.return();
2387
+ }
2388
+ } finally{
2389
+ if (_didIteratorError) {
2390
+ throw _iteratorError;
2391
+ }
2392
+ }
2393
+ }
2394
+ return new this(_object_spread_props(_object_spread({}, extra), {
2395
+ inputVariables: _to_consumable_array(inputVariables),
2396
+ promptMessages: flattenedMessages,
2397
+ partialVariables: flattenedPartialVariables,
2398
+ templateFormat: extra === null || extra === void 0 ? void 0 : extra.templateFormat
2399
+ }));
2400
+ }
2401
+ },
2402
+ {
2403
+ key: "fromPromptMessages",
2404
+ value: /** @deprecated Renamed to .fromMessages */ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2405
+ function fromPromptMessages(promptMessages) {
2406
+ return this.fromMessages(promptMessages);
2407
+ }
2408
+ }
2409
+ ]);
2410
+ return ChatPromptTemplate;
2411
+ }(BaseChatPromptTemplate);
2412
+
2413
+ export { BaseChatPromptTemplate as B, ChatPromptTemplate as C, HumanMessagePromptTemplate as H, SystemMessagePromptTemplate as S };