@digipair/skill-chatbot 0.29.6 → 0.30.1

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/few_shot.cjs.js DELETED
@@ -1,1159 +0,0 @@
1
- 'use strict';
2
-
3
- var index = require('./index.cjs2.js');
4
- require('@digipair/engine');
5
-
6
- // Default generic "any" values are for backwards compatibility.
7
- function _assert_this_initialized$1(self) {
8
- if (self === void 0) {
9
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
10
- }
11
- return self;
12
- }
13
- function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
14
- try {
15
- var info = gen[key](arg);
16
- var value = info.value;
17
- } catch (error) {
18
- reject(error);
19
- return;
20
- }
21
- if (info.done) {
22
- resolve(value);
23
- } else {
24
- Promise.resolve(value).then(_next, _throw);
25
- }
26
- }
27
- function _async_to_generator$1(fn) {
28
- return function() {
29
- var self = this, args = arguments;
30
- return new Promise(function(resolve, reject) {
31
- var gen = fn.apply(self, args);
32
- function _next(value) {
33
- asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
34
- }
35
- function _throw(err) {
36
- asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
37
- }
38
- _next(undefined);
39
- });
40
- };
41
- }
42
- function _class_call_check$1(instance, Constructor) {
43
- if (!(instance instanceof Constructor)) {
44
- throw new TypeError("Cannot call a class as a function");
45
- }
46
- }
47
- function _defineProperties$1(target, props) {
48
- for(var i = 0; i < props.length; i++){
49
- var descriptor = props[i];
50
- descriptor.enumerable = descriptor.enumerable || false;
51
- descriptor.configurable = true;
52
- if ("value" in descriptor) descriptor.writable = true;
53
- Object.defineProperty(target, descriptor.key, descriptor);
54
- }
55
- }
56
- function _create_class$1(Constructor, protoProps, staticProps) {
57
- if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
58
- if (staticProps) _defineProperties$1(Constructor, staticProps);
59
- return Constructor;
60
- }
61
- function _get_prototype_of$1(o) {
62
- _get_prototype_of$1 = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
63
- return o.__proto__ || Object.getPrototypeOf(o);
64
- };
65
- return _get_prototype_of$1(o);
66
- }
67
- function _inherits$1(subClass, superClass) {
68
- if (typeof superClass !== "function" && superClass !== null) {
69
- throw new TypeError("Super expression must either be null or a function");
70
- }
71
- subClass.prototype = Object.create(superClass && superClass.prototype, {
72
- constructor: {
73
- value: subClass,
74
- writable: true,
75
- configurable: true
76
- }
77
- });
78
- if (superClass) _set_prototype_of$1(subClass, superClass);
79
- }
80
- function _possible_constructor_return$1(self, call) {
81
- if (call && (_type_of$1(call) === "object" || typeof call === "function")) {
82
- return call;
83
- }
84
- return _assert_this_initialized$1(self);
85
- }
86
- function _set_prototype_of$1(o, p) {
87
- _set_prototype_of$1 = Object.setPrototypeOf || function setPrototypeOf(o, p) {
88
- o.__proto__ = p;
89
- return o;
90
- };
91
- return _set_prototype_of$1(o, p);
92
- }
93
- function _type_of$1(obj) {
94
- "@swc/helpers - typeof";
95
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
96
- }
97
- function _is_native_reflect_construct$1() {
98
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
99
- if (Reflect.construct.sham) return false;
100
- if (typeof Proxy === "function") return true;
101
- try {
102
- Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
103
- return true;
104
- } catch (e) {
105
- return false;
106
- }
107
- }
108
- function _create_super$1(Derived) {
109
- var hasNativeReflectConstruct = _is_native_reflect_construct$1();
110
- return function _createSuperInternal() {
111
- var Super = _get_prototype_of$1(Derived), result;
112
- if (hasNativeReflectConstruct) {
113
- var NewTarget = _get_prototype_of$1(this).constructor;
114
- result = Reflect.construct(Super, arguments, NewTarget);
115
- } else {
116
- result = Super.apply(this, arguments);
117
- }
118
- return _possible_constructor_return$1(this, result);
119
- };
120
- }
121
- function _ts_generator$1(thisArg, body) {
122
- var f, y, t, g, _ = {
123
- label: 0,
124
- sent: function() {
125
- if (t[0] & 1) throw t[1];
126
- return t[1];
127
- },
128
- trys: [],
129
- ops: []
130
- };
131
- return g = {
132
- next: verb(0),
133
- "throw": verb(1),
134
- "return": verb(2)
135
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
136
- return this;
137
- }), g;
138
- function verb(n) {
139
- return function(v) {
140
- return step([
141
- n,
142
- v
143
- ]);
144
- };
145
- }
146
- function step(op) {
147
- if (f) throw new TypeError("Generator is already executing.");
148
- while(_)try {
149
- 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;
150
- if (y = 0, t) op = [
151
- op[0] & 2,
152
- t.value
153
- ];
154
- switch(op[0]){
155
- case 0:
156
- case 1:
157
- t = op;
158
- break;
159
- case 4:
160
- _.label++;
161
- return {
162
- value: op[1],
163
- done: false
164
- };
165
- case 5:
166
- _.label++;
167
- y = op[1];
168
- op = [
169
- 0
170
- ];
171
- continue;
172
- case 7:
173
- op = _.ops.pop();
174
- _.trys.pop();
175
- continue;
176
- default:
177
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
178
- _ = 0;
179
- continue;
180
- }
181
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
182
- _.label = op[1];
183
- break;
184
- }
185
- if (op[0] === 6 && _.label < t[1]) {
186
- _.label = t[1];
187
- t = op;
188
- break;
189
- }
190
- if (t && _.label < t[2]) {
191
- _.label = t[2];
192
- _.ops.push(op);
193
- break;
194
- }
195
- if (t[2]) _.ops.pop();
196
- _.trys.pop();
197
- continue;
198
- }
199
- op = body.call(thisArg, _);
200
- } catch (e) {
201
- op = [
202
- 6,
203
- e
204
- ];
205
- y = 0;
206
- } finally{
207
- f = t = 0;
208
- }
209
- if (op[0] & 5) throw op[1];
210
- return {
211
- value: op[0] ? op[1] : void 0,
212
- done: true
213
- };
214
- }
215
- }
216
- /**
217
- * Abstract class that serves as a base for creating chat prompt
218
- * templates. It extends the BasePromptTemplate.
219
- */ var BaseChatPromptTemplate = /*#__PURE__*/ function(BasePromptTemplate) {
220
- _inherits$1(BaseChatPromptTemplate, BasePromptTemplate);
221
- var _super = _create_super$1(BaseChatPromptTemplate);
222
- function BaseChatPromptTemplate(input) {
223
- _class_call_check$1(this, BaseChatPromptTemplate);
224
- return _super.call(this, input);
225
- }
226
- _create_class$1(BaseChatPromptTemplate, [
227
- {
228
- key: "format",
229
- value: function format(values) {
230
- var _this = this;
231
- return _async_to_generator$1(function() {
232
- return _ts_generator$1(this, function(_state) {
233
- switch(_state.label){
234
- case 0:
235
- return [
236
- 4,
237
- _this.formatPromptValue(values)
238
- ];
239
- case 1:
240
- return [
241
- 2,
242
- _state.sent().toString()
243
- ];
244
- }
245
- });
246
- })();
247
- }
248
- },
249
- {
250
- key: "formatPromptValue",
251
- value: function formatPromptValue(values) {
252
- var _this = this;
253
- return _async_to_generator$1(function() {
254
- var resultMessages;
255
- return _ts_generator$1(this, function(_state) {
256
- switch(_state.label){
257
- case 0:
258
- return [
259
- 4,
260
- _this.formatMessages(values)
261
- ];
262
- case 1:
263
- resultMessages = _state.sent();
264
- return [
265
- 2,
266
- new index.ChatPromptValue(resultMessages)
267
- ];
268
- }
269
- });
270
- })();
271
- }
272
- }
273
- ]);
274
- return BaseChatPromptTemplate;
275
- }(index.BasePromptTemplate);
276
-
277
- function _array_like_to_array(arr, len) {
278
- if (len == null || len > arr.length) len = arr.length;
279
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
280
- return arr2;
281
- }
282
- function _array_without_holes(arr) {
283
- if (Array.isArray(arr)) return _array_like_to_array(arr);
284
- }
285
- function _assert_this_initialized(self) {
286
- if (self === void 0) {
287
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
288
- }
289
- return self;
290
- }
291
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
292
- try {
293
- var info = gen[key](arg);
294
- var value = info.value;
295
- } catch (error) {
296
- reject(error);
297
- return;
298
- }
299
- if (info.done) {
300
- resolve(value);
301
- } else {
302
- Promise.resolve(value).then(_next, _throw);
303
- }
304
- }
305
- function _async_to_generator(fn) {
306
- return function() {
307
- var self = this, args = arguments;
308
- return new Promise(function(resolve, reject) {
309
- var gen = fn.apply(self, args);
310
- function _next(value) {
311
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
312
- }
313
- function _throw(err) {
314
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
315
- }
316
- _next(undefined);
317
- });
318
- };
319
- }
320
- function _class_call_check(instance, Constructor) {
321
- if (!(instance instanceof Constructor)) {
322
- throw new TypeError("Cannot call a class as a function");
323
- }
324
- }
325
- function _defineProperties(target, props) {
326
- for(var i = 0; i < props.length; i++){
327
- var descriptor = props[i];
328
- descriptor.enumerable = descriptor.enumerable || false;
329
- descriptor.configurable = true;
330
- if ("value" in descriptor) descriptor.writable = true;
331
- Object.defineProperty(target, descriptor.key, descriptor);
332
- }
333
- }
334
- function _create_class(Constructor, protoProps, staticProps) {
335
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
336
- if (staticProps) _defineProperties(Constructor, staticProps);
337
- return Constructor;
338
- }
339
- function _define_property(obj, key, value) {
340
- if (key in obj) {
341
- Object.defineProperty(obj, key, {
342
- value: value,
343
- enumerable: true,
344
- configurable: true,
345
- writable: true
346
- });
347
- } else {
348
- obj[key] = value;
349
- }
350
- return obj;
351
- }
352
- function _get_prototype_of(o) {
353
- _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
354
- return o.__proto__ || Object.getPrototypeOf(o);
355
- };
356
- return _get_prototype_of(o);
357
- }
358
- function _inherits(subClass, superClass) {
359
- if (typeof superClass !== "function" && superClass !== null) {
360
- throw new TypeError("Super expression must either be null or a function");
361
- }
362
- subClass.prototype = Object.create(superClass && superClass.prototype, {
363
- constructor: {
364
- value: subClass,
365
- writable: true,
366
- configurable: true
367
- }
368
- });
369
- if (superClass) _set_prototype_of(subClass, superClass);
370
- }
371
- function _iterable_to_array(iter) {
372
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
373
- }
374
- function _non_iterable_spread() {
375
- throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
376
- }
377
- function _object_spread(target) {
378
- for(var i = 1; i < arguments.length; i++){
379
- var source = arguments[i] != null ? arguments[i] : {};
380
- var ownKeys = Object.keys(source);
381
- if (typeof Object.getOwnPropertySymbols === "function") {
382
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
383
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
384
- }));
385
- }
386
- ownKeys.forEach(function(key) {
387
- _define_property(target, key, source[key]);
388
- });
389
- }
390
- return target;
391
- }
392
- function ownKeys(object, enumerableOnly) {
393
- var keys = Object.keys(object);
394
- if (Object.getOwnPropertySymbols) {
395
- var symbols = Object.getOwnPropertySymbols(object);
396
- if (enumerableOnly) {
397
- symbols = symbols.filter(function(sym) {
398
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
399
- });
400
- }
401
- keys.push.apply(keys, symbols);
402
- }
403
- return keys;
404
- }
405
- function _object_spread_props(target, source) {
406
- source = source != null ? source : {};
407
- if (Object.getOwnPropertyDescriptors) {
408
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
409
- } else {
410
- ownKeys(Object(source)).forEach(function(key) {
411
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
412
- });
413
- }
414
- return target;
415
- }
416
- function _possible_constructor_return(self, call) {
417
- if (call && (_type_of(call) === "object" || typeof call === "function")) {
418
- return call;
419
- }
420
- return _assert_this_initialized(self);
421
- }
422
- function _set_prototype_of(o, p) {
423
- _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
424
- o.__proto__ = p;
425
- return o;
426
- };
427
- return _set_prototype_of(o, p);
428
- }
429
- function _to_consumable_array(arr) {
430
- return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
431
- }
432
- function _type_of(obj) {
433
- "@swc/helpers - typeof";
434
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
435
- }
436
- function _unsupported_iterable_to_array(o, minLen) {
437
- if (!o) return;
438
- if (typeof o === "string") return _array_like_to_array(o, minLen);
439
- var n = Object.prototype.toString.call(o).slice(8, -1);
440
- if (n === "Object" && o.constructor) n = o.constructor.name;
441
- if (n === "Map" || n === "Set") return Array.from(n);
442
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
443
- }
444
- function _is_native_reflect_construct() {
445
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
446
- if (Reflect.construct.sham) return false;
447
- if (typeof Proxy === "function") return true;
448
- try {
449
- Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
450
- return true;
451
- } catch (e) {
452
- return false;
453
- }
454
- }
455
- function _create_super(Derived) {
456
- var hasNativeReflectConstruct = _is_native_reflect_construct();
457
- return function _createSuperInternal() {
458
- var Super = _get_prototype_of(Derived), result;
459
- if (hasNativeReflectConstruct) {
460
- var NewTarget = _get_prototype_of(this).constructor;
461
- result = Reflect.construct(Super, arguments, NewTarget);
462
- } else {
463
- result = Super.apply(this, arguments);
464
- }
465
- return _possible_constructor_return(this, result);
466
- };
467
- }
468
- function _ts_generator(thisArg, body) {
469
- var f, y, t, g, _ = {
470
- label: 0,
471
- sent: function() {
472
- if (t[0] & 1) throw t[1];
473
- return t[1];
474
- },
475
- trys: [],
476
- ops: []
477
- };
478
- return g = {
479
- next: verb(0),
480
- "throw": verb(1),
481
- "return": verb(2)
482
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
483
- return this;
484
- }), g;
485
- function verb(n) {
486
- return function(v) {
487
- return step([
488
- n,
489
- v
490
- ]);
491
- };
492
- }
493
- function step(op) {
494
- if (f) throw new TypeError("Generator is already executing.");
495
- while(_)try {
496
- 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;
497
- if (y = 0, t) op = [
498
- op[0] & 2,
499
- t.value
500
- ];
501
- switch(op[0]){
502
- case 0:
503
- case 1:
504
- t = op;
505
- break;
506
- case 4:
507
- _.label++;
508
- return {
509
- value: op[1],
510
- done: false
511
- };
512
- case 5:
513
- _.label++;
514
- y = op[1];
515
- op = [
516
- 0
517
- ];
518
- continue;
519
- case 7:
520
- op = _.ops.pop();
521
- _.trys.pop();
522
- continue;
523
- default:
524
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
525
- _ = 0;
526
- continue;
527
- }
528
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
529
- _.label = op[1];
530
- break;
531
- }
532
- if (op[0] === 6 && _.label < t[1]) {
533
- _.label = t[1];
534
- t = op;
535
- break;
536
- }
537
- if (t && _.label < t[2]) {
538
- _.label = t[2];
539
- _.ops.push(op);
540
- break;
541
- }
542
- if (t[2]) _.ops.pop();
543
- _.trys.pop();
544
- continue;
545
- }
546
- op = body.call(thisArg, _);
547
- } catch (e) {
548
- op = [
549
- 6,
550
- e
551
- ];
552
- y = 0;
553
- } finally{
554
- f = t = 0;
555
- }
556
- if (op[0] & 5) throw op[1];
557
- return {
558
- value: op[0] ? op[1] : void 0,
559
- done: true
560
- };
561
- }
562
- }
563
- /**
564
- * Prompt template that contains few-shot examples.
565
- * @augments BasePromptTemplate
566
- * @augments FewShotPromptTemplateInput
567
- * @example
568
- * ```typescript
569
- * const examplePrompt = PromptTemplate.fromTemplate(
570
- * "Input: {input}\nOutput: {output}",
571
- * );
572
- *
573
- * const exampleSelector = await SemanticSimilarityExampleSelector.fromExamples(
574
- * [
575
- * { input: "happy", output: "sad" },
576
- * { input: "tall", output: "short" },
577
- * { input: "energetic", output: "lethargic" },
578
- * { input: "sunny", output: "gloomy" },
579
- * { input: "windy", output: "calm" },
580
- * ],
581
- * new OpenAIEmbeddings(),
582
- * HNSWLib,
583
- * { k: 1 },
584
- * );
585
- *
586
- * const dynamicPrompt = new FewShotPromptTemplate({
587
- * exampleSelector,
588
- * examplePrompt,
589
- * prefix: "Give the antonym of every input",
590
- * suffix: "Input: {adjective}\nOutput:",
591
- * inputVariables: ["adjective"],
592
- * });
593
- *
594
- * // Format the dynamic prompt with the input 'rainy'
595
- * console.log(await dynamicPrompt.format({ adjective: "rainy" }));
596
- *
597
- * ```
598
- */ var FewShotPromptTemplate = /*#__PURE__*/ function(BaseStringPromptTemplate) {
599
- _inherits(FewShotPromptTemplate, BaseStringPromptTemplate);
600
- var _super = _create_super(FewShotPromptTemplate);
601
- function FewShotPromptTemplate(input) {
602
- _class_call_check(this, FewShotPromptTemplate);
603
- var _this;
604
- _this = _super.call(this, input);
605
- Object.defineProperty(_assert_this_initialized(_this), "lc_serializable", {
606
- enumerable: true,
607
- configurable: true,
608
- writable: true,
609
- value: false
610
- });
611
- Object.defineProperty(_assert_this_initialized(_this), "examples", {
612
- enumerable: true,
613
- configurable: true,
614
- writable: true,
615
- value: void 0
616
- });
617
- Object.defineProperty(_assert_this_initialized(_this), "exampleSelector", {
618
- enumerable: true,
619
- configurable: true,
620
- writable: true,
621
- value: void 0
622
- });
623
- Object.defineProperty(_assert_this_initialized(_this), "examplePrompt", {
624
- enumerable: true,
625
- configurable: true,
626
- writable: true,
627
- value: void 0
628
- });
629
- Object.defineProperty(_assert_this_initialized(_this), "suffix", {
630
- enumerable: true,
631
- configurable: true,
632
- writable: true,
633
- value: ""
634
- });
635
- Object.defineProperty(_assert_this_initialized(_this), "exampleSeparator", {
636
- enumerable: true,
637
- configurable: true,
638
- writable: true,
639
- value: "\n\n"
640
- });
641
- Object.defineProperty(_assert_this_initialized(_this), "prefix", {
642
- enumerable: true,
643
- configurable: true,
644
- writable: true,
645
- value: ""
646
- });
647
- Object.defineProperty(_assert_this_initialized(_this), "templateFormat", {
648
- enumerable: true,
649
- configurable: true,
650
- writable: true,
651
- value: "f-string"
652
- });
653
- Object.defineProperty(_assert_this_initialized(_this), "validateTemplate", {
654
- enumerable: true,
655
- configurable: true,
656
- writable: true,
657
- value: true
658
- });
659
- Object.assign(_assert_this_initialized(_this), input);
660
- if (_this.examples !== undefined && _this.exampleSelector !== undefined) {
661
- throw new Error("Only one of 'examples' and 'example_selector' should be provided");
662
- }
663
- if (_this.examples === undefined && _this.exampleSelector === undefined) {
664
- throw new Error("One of 'examples' and 'example_selector' should be provided");
665
- }
666
- if (_this.validateTemplate) {
667
- var totalInputVariables = _this.inputVariables;
668
- if (_this.partialVariables) {
669
- totalInputVariables = totalInputVariables.concat(Object.keys(_this.partialVariables));
670
- }
671
- index.checkValidTemplate(_this.prefix + _this.suffix, _this.templateFormat, totalInputVariables);
672
- }
673
- return _this;
674
- }
675
- _create_class(FewShotPromptTemplate, [
676
- {
677
- key: "_getPromptType",
678
- value: function _getPromptType() {
679
- return "few_shot";
680
- }
681
- },
682
- {
683
- key: "getExamples",
684
- value: function getExamples(inputVariables) {
685
- var _this = this;
686
- return _async_to_generator(function() {
687
- return _ts_generator(this, function(_state) {
688
- if (_this.examples !== undefined) {
689
- return [
690
- 2,
691
- _this.examples
692
- ];
693
- }
694
- if (_this.exampleSelector !== undefined) {
695
- return [
696
- 2,
697
- _this.exampleSelector.selectExamples(inputVariables)
698
- ];
699
- }
700
- throw new Error("One of 'examples' and 'example_selector' should be provided");
701
- });
702
- })();
703
- }
704
- },
705
- {
706
- key: "partial",
707
- value: function partial(values) {
708
- var _this = this;
709
- return _async_to_generator(function() {
710
- var newInputVariables, _this_partialVariables, newPartialVariables, promptDict;
711
- return _ts_generator(this, function(_state) {
712
- newInputVariables = _this.inputVariables.filter(function(iv) {
713
- return !(iv in values);
714
- });
715
- newPartialVariables = _object_spread({}, (_this_partialVariables = _this.partialVariables) !== null && _this_partialVariables !== void 0 ? _this_partialVariables : {}, values);
716
- promptDict = _object_spread_props(_object_spread({}, _this), {
717
- inputVariables: newInputVariables,
718
- partialVariables: newPartialVariables
719
- });
720
- return [
721
- 2,
722
- new FewShotPromptTemplate(promptDict)
723
- ];
724
- });
725
- })();
726
- }
727
- },
728
- {
729
- key: "format",
730
- value: /**
731
- * Formats the prompt with the given values.
732
- * @param values The values to format the prompt with.
733
- * @returns A promise that resolves to a string representing the formatted prompt.
734
- */ function format(values) {
735
- var _this = this;
736
- return _async_to_generator(function() {
737
- var allValues, examples, exampleStrings, template;
738
- return _ts_generator(this, function(_state) {
739
- switch(_state.label){
740
- case 0:
741
- return [
742
- 4,
743
- _this.mergePartialAndUserVariables(values)
744
- ];
745
- case 1:
746
- allValues = _state.sent();
747
- return [
748
- 4,
749
- _this.getExamples(allValues)
750
- ];
751
- case 2:
752
- examples = _state.sent();
753
- return [
754
- 4,
755
- Promise.all(examples.map(function(example) {
756
- return _this.examplePrompt.format(example);
757
- }))
758
- ];
759
- case 3:
760
- exampleStrings = _state.sent();
761
- template = [
762
- _this.prefix
763
- ].concat(_to_consumable_array(exampleStrings), [
764
- _this.suffix
765
- ]).join(_this.exampleSeparator);
766
- return [
767
- 2,
768
- index.renderTemplate(template, _this.templateFormat, allValues)
769
- ];
770
- }
771
- });
772
- })();
773
- }
774
- },
775
- {
776
- key: "serialize",
777
- value: function serialize() {
778
- if (this.exampleSelector || !this.examples) {
779
- throw new Error("Serializing an example selector is not currently supported");
780
- }
781
- if (this.outputParser !== undefined) {
782
- throw new Error("Serializing an output parser is not currently supported");
783
- }
784
- return {
785
- _type: this._getPromptType(),
786
- input_variables: this.inputVariables,
787
- example_prompt: this.examplePrompt.serialize(),
788
- example_separator: this.exampleSeparator,
789
- suffix: this.suffix,
790
- prefix: this.prefix,
791
- template_format: this.templateFormat,
792
- examples: this.examples
793
- };
794
- }
795
- }
796
- ], [
797
- {
798
- key: "lc_name",
799
- value: function lc_name() {
800
- return "FewShotPromptTemplate";
801
- }
802
- },
803
- {
804
- key: "deserialize",
805
- value: function deserialize(data) {
806
- return _async_to_generator(function() {
807
- var example_prompt, examplePrompt, examples;
808
- return _ts_generator(this, function(_state) {
809
- switch(_state.label){
810
- case 0:
811
- example_prompt = data.example_prompt;
812
- if (!example_prompt) {
813
- throw new Error("Missing example prompt");
814
- }
815
- return [
816
- 4,
817
- index.PromptTemplate.deserialize(example_prompt)
818
- ];
819
- case 1:
820
- examplePrompt = _state.sent();
821
- if (Array.isArray(data.examples)) {
822
- examples = data.examples;
823
- } else {
824
- throw new Error("Invalid examples format. Only list or string are supported.");
825
- }
826
- return [
827
- 2,
828
- new FewShotPromptTemplate({
829
- inputVariables: data.input_variables,
830
- examplePrompt: examplePrompt,
831
- examples: examples,
832
- exampleSeparator: data.example_separator,
833
- prefix: data.prefix,
834
- suffix: data.suffix,
835
- templateFormat: data.template_format
836
- })
837
- ];
838
- }
839
- });
840
- })();
841
- }
842
- }
843
- ]);
844
- return FewShotPromptTemplate;
845
- }(index.BaseStringPromptTemplate);
846
- /**
847
- * Chat prompt template that contains few-shot examples.
848
- * @augments BasePromptTemplateInput
849
- * @augments FewShotChatMessagePromptTemplateInput
850
- */ var FewShotChatMessagePromptTemplate = /*#__PURE__*/ function(BaseChatPromptTemplate) {
851
- _inherits(FewShotChatMessagePromptTemplate, BaseChatPromptTemplate);
852
- var _super = _create_super(FewShotChatMessagePromptTemplate);
853
- function FewShotChatMessagePromptTemplate(fields) {
854
- _class_call_check(this, FewShotChatMessagePromptTemplate);
855
- var _this;
856
- _this = _super.call(this, fields);
857
- Object.defineProperty(_assert_this_initialized(_this), "lc_serializable", {
858
- enumerable: true,
859
- configurable: true,
860
- writable: true,
861
- value: true
862
- });
863
- Object.defineProperty(_assert_this_initialized(_this), "examples", {
864
- enumerable: true,
865
- configurable: true,
866
- writable: true,
867
- value: void 0
868
- });
869
- Object.defineProperty(_assert_this_initialized(_this), "exampleSelector", {
870
- enumerable: true,
871
- configurable: true,
872
- writable: true,
873
- value: void 0
874
- });
875
- Object.defineProperty(_assert_this_initialized(_this), "examplePrompt", {
876
- enumerable: true,
877
- configurable: true,
878
- writable: true,
879
- value: void 0
880
- });
881
- Object.defineProperty(_assert_this_initialized(_this), "suffix", {
882
- enumerable: true,
883
- configurable: true,
884
- writable: true,
885
- value: ""
886
- });
887
- Object.defineProperty(_assert_this_initialized(_this), "exampleSeparator", {
888
- enumerable: true,
889
- configurable: true,
890
- writable: true,
891
- value: "\n\n"
892
- });
893
- Object.defineProperty(_assert_this_initialized(_this), "prefix", {
894
- enumerable: true,
895
- configurable: true,
896
- writable: true,
897
- value: ""
898
- });
899
- Object.defineProperty(_assert_this_initialized(_this), "templateFormat", {
900
- enumerable: true,
901
- configurable: true,
902
- writable: true,
903
- value: "f-string"
904
- });
905
- Object.defineProperty(_assert_this_initialized(_this), "validateTemplate", {
906
- enumerable: true,
907
- configurable: true,
908
- writable: true,
909
- value: true
910
- });
911
- _this.examples = fields.examples;
912
- _this.examplePrompt = fields.examplePrompt;
913
- var _fields_exampleSeparator;
914
- _this.exampleSeparator = (_fields_exampleSeparator = fields.exampleSeparator) !== null && _fields_exampleSeparator !== void 0 ? _fields_exampleSeparator : "\n\n";
915
- _this.exampleSelector = fields.exampleSelector;
916
- var _fields_prefix;
917
- _this.prefix = (_fields_prefix = fields.prefix) !== null && _fields_prefix !== void 0 ? _fields_prefix : "";
918
- var _fields_suffix;
919
- _this.suffix = (_fields_suffix = fields.suffix) !== null && _fields_suffix !== void 0 ? _fields_suffix : "";
920
- var _fields_templateFormat;
921
- _this.templateFormat = (_fields_templateFormat = fields.templateFormat) !== null && _fields_templateFormat !== void 0 ? _fields_templateFormat : "f-string";
922
- var _fields_validateTemplate;
923
- _this.validateTemplate = (_fields_validateTemplate = fields.validateTemplate) !== null && _fields_validateTemplate !== void 0 ? _fields_validateTemplate : true;
924
- if (_this.examples !== undefined && _this.exampleSelector !== undefined) {
925
- throw new Error("Only one of 'examples' and 'example_selector' should be provided");
926
- }
927
- if (_this.examples === undefined && _this.exampleSelector === undefined) {
928
- throw new Error("One of 'examples' and 'example_selector' should be provided");
929
- }
930
- if (_this.validateTemplate) {
931
- var totalInputVariables = _this.inputVariables;
932
- if (_this.partialVariables) {
933
- totalInputVariables = totalInputVariables.concat(Object.keys(_this.partialVariables));
934
- }
935
- index.checkValidTemplate(_this.prefix + _this.suffix, _this.templateFormat, totalInputVariables);
936
- }
937
- return _this;
938
- }
939
- _create_class(FewShotChatMessagePromptTemplate, [
940
- {
941
- key: "_getPromptType",
942
- value: function _getPromptType() {
943
- return "few_shot_chat";
944
- }
945
- },
946
- {
947
- key: "getExamples",
948
- value: function getExamples(inputVariables) {
949
- var _this = this;
950
- return _async_to_generator(function() {
951
- return _ts_generator(this, function(_state) {
952
- if (_this.examples !== undefined) {
953
- return [
954
- 2,
955
- _this.examples
956
- ];
957
- }
958
- if (_this.exampleSelector !== undefined) {
959
- return [
960
- 2,
961
- _this.exampleSelector.selectExamples(inputVariables)
962
- ];
963
- }
964
- throw new Error("One of 'examples' and 'example_selector' should be provided");
965
- });
966
- })();
967
- }
968
- },
969
- {
970
- key: "formatMessages",
971
- value: /**
972
- * Formats the list of values and returns a list of formatted messages.
973
- * @param values The values to format the prompt with.
974
- * @returns A promise that resolves to a string representing the formatted prompt.
975
- */ function formatMessages(values) {
976
- var _this = this;
977
- return _async_to_generator(function() {
978
- var allValues, examples, messages, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, example, _messages, exampleMessages, err;
979
- return _ts_generator(this, function(_state) {
980
- switch(_state.label){
981
- case 0:
982
- return [
983
- 4,
984
- _this.mergePartialAndUserVariables(values)
985
- ];
986
- case 1:
987
- allValues = _state.sent();
988
- return [
989
- 4,
990
- _this.getExamples(allValues)
991
- ];
992
- case 2:
993
- examples = _state.sent();
994
- examples = examples.map(function(example) {
995
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
996
- var result = {};
997
- _this.examplePrompt.inputVariables.forEach(function(inputVariable) {
998
- result[inputVariable] = example[inputVariable];
999
- });
1000
- return result;
1001
- });
1002
- messages = [];
1003
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
1004
- _state.label = 3;
1005
- case 3:
1006
- _state.trys.push([
1007
- 3,
1008
- 8,
1009
- 9,
1010
- 10
1011
- ]);
1012
- _iterator = examples[Symbol.iterator]();
1013
- _state.label = 4;
1014
- case 4:
1015
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
1016
- 3,
1017
- 7
1018
- ];
1019
- example = _step.value;
1020
- return [
1021
- 4,
1022
- _this.examplePrompt.formatMessages(example)
1023
- ];
1024
- case 5:
1025
- exampleMessages = _state.sent();
1026
- (_messages = messages).push.apply(_messages, _to_consumable_array(exampleMessages));
1027
- _state.label = 6;
1028
- case 6:
1029
- _iteratorNormalCompletion = true;
1030
- return [
1031
- 3,
1032
- 4
1033
- ];
1034
- case 7:
1035
- return [
1036
- 3,
1037
- 10
1038
- ];
1039
- case 8:
1040
- err = _state.sent();
1041
- _didIteratorError = true;
1042
- _iteratorError = err;
1043
- return [
1044
- 3,
1045
- 10
1046
- ];
1047
- case 9:
1048
- try {
1049
- if (!_iteratorNormalCompletion && _iterator.return != null) {
1050
- _iterator.return();
1051
- }
1052
- } finally{
1053
- if (_didIteratorError) {
1054
- throw _iteratorError;
1055
- }
1056
- }
1057
- return [
1058
- 7
1059
- ];
1060
- case 10:
1061
- return [
1062
- 2,
1063
- messages
1064
- ];
1065
- }
1066
- });
1067
- })();
1068
- }
1069
- },
1070
- {
1071
- key: "format",
1072
- value: /**
1073
- * Formats the prompt with the given values.
1074
- * @param values The values to format the prompt with.
1075
- * @returns A promise that resolves to a string representing the formatted prompt.
1076
- */ function format(values) {
1077
- var _this = this;
1078
- return _async_to_generator(function() {
1079
- var allValues, examples, exampleMessages, exampleStrings, template;
1080
- return _ts_generator(this, function(_state) {
1081
- switch(_state.label){
1082
- case 0:
1083
- return [
1084
- 4,
1085
- _this.mergePartialAndUserVariables(values)
1086
- ];
1087
- case 1:
1088
- allValues = _state.sent();
1089
- return [
1090
- 4,
1091
- _this.getExamples(allValues)
1092
- ];
1093
- case 2:
1094
- examples = _state.sent();
1095
- return [
1096
- 4,
1097
- Promise.all(examples.map(function(example) {
1098
- return _this.examplePrompt.formatMessages(example);
1099
- }))
1100
- ];
1101
- case 3:
1102
- exampleMessages = _state.sent();
1103
- exampleStrings = exampleMessages.flat().map(function(message) {
1104
- return message.content;
1105
- });
1106
- template = [
1107
- _this.prefix
1108
- ].concat(_to_consumable_array(exampleStrings), [
1109
- _this.suffix
1110
- ]).join(_this.exampleSeparator);
1111
- return [
1112
- 2,
1113
- index.renderTemplate(template, _this.templateFormat, allValues)
1114
- ];
1115
- }
1116
- });
1117
- })();
1118
- }
1119
- },
1120
- {
1121
- key: "partial",
1122
- value: /**
1123
- * Partially formats the prompt with the given values.
1124
- * @param values The values to partially format the prompt with.
1125
- * @returns A promise that resolves to an instance of `FewShotChatMessagePromptTemplate` with the given values partially formatted.
1126
- */ function partial(values) {
1127
- var _this = this;
1128
- return _async_to_generator(function() {
1129
- var newInputVariables, _this_partialVariables, newPartialVariables, promptDict;
1130
- return _ts_generator(this, function(_state) {
1131
- newInputVariables = _this.inputVariables.filter(function(variable) {
1132
- return !(variable in values);
1133
- });
1134
- newPartialVariables = _object_spread({}, (_this_partialVariables = _this.partialVariables) !== null && _this_partialVariables !== void 0 ? _this_partialVariables : {}, values);
1135
- promptDict = _object_spread_props(_object_spread({}, _this), {
1136
- inputVariables: newInputVariables,
1137
- partialVariables: newPartialVariables
1138
- });
1139
- return [
1140
- 2,
1141
- new FewShotChatMessagePromptTemplate(promptDict)
1142
- ];
1143
- });
1144
- })();
1145
- }
1146
- }
1147
- ], [
1148
- {
1149
- key: "lc_name",
1150
- value: function lc_name() {
1151
- return "FewShotChatMessagePromptTemplate";
1152
- }
1153
- }
1154
- ]);
1155
- return FewShotChatMessagePromptTemplate;
1156
- }(BaseChatPromptTemplate);
1157
-
1158
- exports.FewShotChatMessagePromptTemplate = FewShotChatMessagePromptTemplate;
1159
- exports.FewShotPromptTemplate = FewShotPromptTemplate;