@digipair/skill-llm 0.94.0-8 → 0.95.0

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