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