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