@digipair/skill-llm 0.7.3 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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
@@ -0,0 +1,864 @@
|
|
1
|
+
import { P as PromptTemplate, L as LLMChain, g as StuffDocumentsChain, f as BaseChain } from './index.esm2.js';
|
2
|
+
import { C as ChatPromptTemplate, S as SystemMessagePromptTemplate, H as HumanMessagePromptTemplate } from './chat.esm.js';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Abstract class that defines the interface for selecting a prompt for a
|
6
|
+
* given language model.
|
7
|
+
*/ function _array_like_to_array(arr, len) {
|
8
|
+
if (len == null || len > arr.length) len = arr.length;
|
9
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
10
|
+
return arr2;
|
11
|
+
}
|
12
|
+
function _array_with_holes(arr) {
|
13
|
+
if (Array.isArray(arr)) return arr;
|
14
|
+
}
|
15
|
+
function _assert_this_initialized$1(self) {
|
16
|
+
if (self === void 0) {
|
17
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
18
|
+
}
|
19
|
+
return self;
|
20
|
+
}
|
21
|
+
function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
|
22
|
+
try {
|
23
|
+
var info = gen[key](arg);
|
24
|
+
var value = info.value;
|
25
|
+
} catch (error) {
|
26
|
+
reject(error);
|
27
|
+
return;
|
28
|
+
}
|
29
|
+
if (info.done) {
|
30
|
+
resolve(value);
|
31
|
+
} else {
|
32
|
+
Promise.resolve(value).then(_next, _throw);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
function _async_to_generator$1(fn) {
|
36
|
+
return function() {
|
37
|
+
var self = this, args = arguments;
|
38
|
+
return new Promise(function(resolve, reject) {
|
39
|
+
var gen = fn.apply(self, args);
|
40
|
+
function _next(value) {
|
41
|
+
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
|
42
|
+
}
|
43
|
+
function _throw(err) {
|
44
|
+
asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
|
45
|
+
}
|
46
|
+
_next(undefined);
|
47
|
+
});
|
48
|
+
};
|
49
|
+
}
|
50
|
+
function _class_call_check$1(instance, Constructor) {
|
51
|
+
if (!(instance instanceof Constructor)) {
|
52
|
+
throw new TypeError("Cannot call a class as a function");
|
53
|
+
}
|
54
|
+
}
|
55
|
+
function _defineProperties$1(target, props) {
|
56
|
+
for(var i = 0; i < props.length; i++){
|
57
|
+
var descriptor = props[i];
|
58
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
59
|
+
descriptor.configurable = true;
|
60
|
+
if ("value" in descriptor) descriptor.writable = true;
|
61
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
62
|
+
}
|
63
|
+
}
|
64
|
+
function _create_class$1(Constructor, protoProps, staticProps) {
|
65
|
+
if (protoProps) _defineProperties$1(Constructor.prototype, protoProps);
|
66
|
+
if (staticProps) _defineProperties$1(Constructor, staticProps);
|
67
|
+
return Constructor;
|
68
|
+
}
|
69
|
+
function _get_prototype_of$1(o) {
|
70
|
+
_get_prototype_of$1 = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
|
71
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
72
|
+
};
|
73
|
+
return _get_prototype_of$1(o);
|
74
|
+
}
|
75
|
+
function _inherits$1(subClass, superClass) {
|
76
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
77
|
+
throw new TypeError("Super expression must either be null or a function");
|
78
|
+
}
|
79
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
80
|
+
constructor: {
|
81
|
+
value: subClass,
|
82
|
+
writable: true,
|
83
|
+
configurable: true
|
84
|
+
}
|
85
|
+
});
|
86
|
+
if (superClass) _set_prototype_of$1(subClass, superClass);
|
87
|
+
}
|
88
|
+
function _iterable_to_array_limit(arr, i) {
|
89
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
90
|
+
if (_i == null) return;
|
91
|
+
var _arr = [];
|
92
|
+
var _n = true;
|
93
|
+
var _d = false;
|
94
|
+
var _s, _e;
|
95
|
+
try {
|
96
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
97
|
+
_arr.push(_s.value);
|
98
|
+
if (i && _arr.length === i) break;
|
99
|
+
}
|
100
|
+
} catch (err) {
|
101
|
+
_d = true;
|
102
|
+
_e = err;
|
103
|
+
} finally{
|
104
|
+
try {
|
105
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
106
|
+
} finally{
|
107
|
+
if (_d) throw _e;
|
108
|
+
}
|
109
|
+
}
|
110
|
+
return _arr;
|
111
|
+
}
|
112
|
+
function _non_iterable_rest() {
|
113
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
114
|
+
}
|
115
|
+
function _possible_constructor_return$1(self, call) {
|
116
|
+
if (call && (_type_of$1(call) === "object" || typeof call === "function")) {
|
117
|
+
return call;
|
118
|
+
}
|
119
|
+
return _assert_this_initialized$1(self);
|
120
|
+
}
|
121
|
+
function _set_prototype_of$1(o, p) {
|
122
|
+
_set_prototype_of$1 = Object.setPrototypeOf || function setPrototypeOf(o, p) {
|
123
|
+
o.__proto__ = p;
|
124
|
+
return o;
|
125
|
+
};
|
126
|
+
return _set_prototype_of$1(o, p);
|
127
|
+
}
|
128
|
+
function _sliced_to_array(arr, i) {
|
129
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
130
|
+
}
|
131
|
+
function _type_of$1(obj) {
|
132
|
+
"@swc/helpers - typeof";
|
133
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
134
|
+
}
|
135
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
136
|
+
if (!o) return;
|
137
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
138
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
139
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
140
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
141
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
142
|
+
}
|
143
|
+
function _is_native_reflect_construct$1() {
|
144
|
+
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
145
|
+
if (Reflect.construct.sham) return false;
|
146
|
+
if (typeof Proxy === "function") return true;
|
147
|
+
try {
|
148
|
+
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
149
|
+
return true;
|
150
|
+
} catch (e) {
|
151
|
+
return false;
|
152
|
+
}
|
153
|
+
}
|
154
|
+
function _create_super$1(Derived) {
|
155
|
+
var hasNativeReflectConstruct = _is_native_reflect_construct$1();
|
156
|
+
return function _createSuperInternal() {
|
157
|
+
var Super = _get_prototype_of$1(Derived), result;
|
158
|
+
if (hasNativeReflectConstruct) {
|
159
|
+
var NewTarget = _get_prototype_of$1(this).constructor;
|
160
|
+
result = Reflect.construct(Super, arguments, NewTarget);
|
161
|
+
} else {
|
162
|
+
result = Super.apply(this, arguments);
|
163
|
+
}
|
164
|
+
return _possible_constructor_return$1(this, result);
|
165
|
+
};
|
166
|
+
}
|
167
|
+
function _ts_generator$1(thisArg, body) {
|
168
|
+
var f, y, t, g, _ = {
|
169
|
+
label: 0,
|
170
|
+
sent: function() {
|
171
|
+
if (t[0] & 1) throw t[1];
|
172
|
+
return t[1];
|
173
|
+
},
|
174
|
+
trys: [],
|
175
|
+
ops: []
|
176
|
+
};
|
177
|
+
return g = {
|
178
|
+
next: verb(0),
|
179
|
+
"throw": verb(1),
|
180
|
+
"return": verb(2)
|
181
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
182
|
+
return this;
|
183
|
+
}), g;
|
184
|
+
function verb(n) {
|
185
|
+
return function(v) {
|
186
|
+
return step([
|
187
|
+
n,
|
188
|
+
v
|
189
|
+
]);
|
190
|
+
};
|
191
|
+
}
|
192
|
+
function step(op) {
|
193
|
+
if (f) throw new TypeError("Generator is already executing.");
|
194
|
+
while(_)try {
|
195
|
+
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;
|
196
|
+
if (y = 0, t) op = [
|
197
|
+
op[0] & 2,
|
198
|
+
t.value
|
199
|
+
];
|
200
|
+
switch(op[0]){
|
201
|
+
case 0:
|
202
|
+
case 1:
|
203
|
+
t = op;
|
204
|
+
break;
|
205
|
+
case 4:
|
206
|
+
_.label++;
|
207
|
+
return {
|
208
|
+
value: op[1],
|
209
|
+
done: false
|
210
|
+
};
|
211
|
+
case 5:
|
212
|
+
_.label++;
|
213
|
+
y = op[1];
|
214
|
+
op = [
|
215
|
+
0
|
216
|
+
];
|
217
|
+
continue;
|
218
|
+
case 7:
|
219
|
+
op = _.ops.pop();
|
220
|
+
_.trys.pop();
|
221
|
+
continue;
|
222
|
+
default:
|
223
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
224
|
+
_ = 0;
|
225
|
+
continue;
|
226
|
+
}
|
227
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
228
|
+
_.label = op[1];
|
229
|
+
break;
|
230
|
+
}
|
231
|
+
if (op[0] === 6 && _.label < t[1]) {
|
232
|
+
_.label = t[1];
|
233
|
+
t = op;
|
234
|
+
break;
|
235
|
+
}
|
236
|
+
if (t && _.label < t[2]) {
|
237
|
+
_.label = t[2];
|
238
|
+
_.ops.push(op);
|
239
|
+
break;
|
240
|
+
}
|
241
|
+
if (t[2]) _.ops.pop();
|
242
|
+
_.trys.pop();
|
243
|
+
continue;
|
244
|
+
}
|
245
|
+
op = body.call(thisArg, _);
|
246
|
+
} catch (e) {
|
247
|
+
op = [
|
248
|
+
6,
|
249
|
+
e
|
250
|
+
];
|
251
|
+
y = 0;
|
252
|
+
} finally{
|
253
|
+
f = t = 0;
|
254
|
+
}
|
255
|
+
if (op[0] & 5) throw op[1];
|
256
|
+
return {
|
257
|
+
value: op[0] ? op[1] : void 0,
|
258
|
+
done: true
|
259
|
+
};
|
260
|
+
}
|
261
|
+
}
|
262
|
+
var BasePromptSelector = /*#__PURE__*/ function() {
|
263
|
+
function BasePromptSelector() {
|
264
|
+
_class_call_check$1(this, BasePromptSelector);
|
265
|
+
}
|
266
|
+
_create_class$1(BasePromptSelector, [
|
267
|
+
{
|
268
|
+
key: "getPromptAsync",
|
269
|
+
value: /**
|
270
|
+
* Asynchronous version of `getPrompt` that also accepts an options object
|
271
|
+
* for partial variables.
|
272
|
+
* @param llm The language model for which to get a prompt.
|
273
|
+
* @param options Optional object for partial variables.
|
274
|
+
* @returns A Promise that resolves to a prompt template.
|
275
|
+
*/ function getPromptAsync(llm, options) {
|
276
|
+
var _this = this;
|
277
|
+
return _async_to_generator$1(function() {
|
278
|
+
var prompt, _options_partialVariables;
|
279
|
+
return _ts_generator$1(this, function(_state) {
|
280
|
+
prompt = _this.getPrompt(llm);
|
281
|
+
return [
|
282
|
+
2,
|
283
|
+
prompt.partial((_options_partialVariables = options === null || options === void 0 ? void 0 : options.partialVariables) !== null && _options_partialVariables !== void 0 ? _options_partialVariables : {})
|
284
|
+
];
|
285
|
+
});
|
286
|
+
})();
|
287
|
+
}
|
288
|
+
}
|
289
|
+
]);
|
290
|
+
return BasePromptSelector;
|
291
|
+
}();
|
292
|
+
/**
|
293
|
+
* Concrete implementation of `BasePromptSelector` that selects a prompt
|
294
|
+
* based on a set of conditions. It has a default prompt that it returns
|
295
|
+
* if none of the conditions are met.
|
296
|
+
*/ var ConditionalPromptSelector = /*#__PURE__*/ function(BasePromptSelector) {
|
297
|
+
_inherits$1(ConditionalPromptSelector, BasePromptSelector);
|
298
|
+
var _super = _create_super$1(ConditionalPromptSelector);
|
299
|
+
function ConditionalPromptSelector(default_prompt) {
|
300
|
+
var conditionals = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [];
|
301
|
+
_class_call_check$1(this, ConditionalPromptSelector);
|
302
|
+
var _this;
|
303
|
+
_this = _super.call(this);
|
304
|
+
Object.defineProperty(_assert_this_initialized$1(_this), "defaultPrompt", {
|
305
|
+
enumerable: true,
|
306
|
+
configurable: true,
|
307
|
+
writable: true,
|
308
|
+
value: void 0
|
309
|
+
});
|
310
|
+
Object.defineProperty(_assert_this_initialized$1(_this), "conditionals", {
|
311
|
+
enumerable: true,
|
312
|
+
configurable: true,
|
313
|
+
writable: true,
|
314
|
+
value: void 0
|
315
|
+
});
|
316
|
+
_this.defaultPrompt = default_prompt;
|
317
|
+
_this.conditionals = conditionals;
|
318
|
+
return _this;
|
319
|
+
}
|
320
|
+
_create_class$1(ConditionalPromptSelector, [
|
321
|
+
{
|
322
|
+
/**
|
323
|
+
* Method that selects a prompt based on a set of conditions. If none of
|
324
|
+
* the conditions are met, it returns the default prompt.
|
325
|
+
* @param llm The language model for which to get a prompt.
|
326
|
+
* @returns A prompt template.
|
327
|
+
*/ key: "getPrompt",
|
328
|
+
value: function getPrompt(llm) {
|
329
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
330
|
+
try {
|
331
|
+
for(var _iterator = this.conditionals[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
332
|
+
var _step_value = _sliced_to_array(_step.value, 2), condition = _step_value[0], prompt = _step_value[1];
|
333
|
+
if (condition(llm)) {
|
334
|
+
return prompt;
|
335
|
+
}
|
336
|
+
}
|
337
|
+
} catch (err) {
|
338
|
+
_didIteratorError = true;
|
339
|
+
_iteratorError = err;
|
340
|
+
} finally{
|
341
|
+
try {
|
342
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
343
|
+
_iterator.return();
|
344
|
+
}
|
345
|
+
} finally{
|
346
|
+
if (_didIteratorError) {
|
347
|
+
throw _iteratorError;
|
348
|
+
}
|
349
|
+
}
|
350
|
+
}
|
351
|
+
return this.defaultPrompt;
|
352
|
+
}
|
353
|
+
}
|
354
|
+
]);
|
355
|
+
return ConditionalPromptSelector;
|
356
|
+
}(BasePromptSelector);
|
357
|
+
/**
|
358
|
+
* Type guard function that checks if a given language model is of type
|
359
|
+
* `BaseChatModel`.
|
360
|
+
*/ function isChatModel(llm) {
|
361
|
+
return llm._modelType() === "base_chat_model";
|
362
|
+
}
|
363
|
+
|
364
|
+
var DEFAULT_QA_PROMPT = /*#__PURE__*/ new PromptTemplate({
|
365
|
+
template: "Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.\n\n{context}\n\nQuestion: {question}\nHelpful Answer:",
|
366
|
+
inputVariables: [
|
367
|
+
"context",
|
368
|
+
"question"
|
369
|
+
]
|
370
|
+
});
|
371
|
+
var system_template = "Use the following pieces of context to answer the users question. \nIf you don't know the answer, just say that you don't know, don't try to make up an answer.\n----------------\n{context}";
|
372
|
+
var messages = [
|
373
|
+
/*#__PURE__*/ SystemMessagePromptTemplate.fromTemplate(system_template),
|
374
|
+
/*#__PURE__*/ HumanMessagePromptTemplate.fromTemplate("{question}")
|
375
|
+
];
|
376
|
+
var CHAT_PROMPT = /*#__PURE__*/ ChatPromptTemplate.fromMessages(messages);
|
377
|
+
var QA_PROMPT_SELECTOR = /*#__PURE__*/ new ConditionalPromptSelector(DEFAULT_QA_PROMPT, [
|
378
|
+
[
|
379
|
+
isChatModel,
|
380
|
+
CHAT_PROMPT
|
381
|
+
]
|
382
|
+
]);
|
383
|
+
|
384
|
+
/**
|
385
|
+
* Loads a StuffQAChain based on the provided parameters. It takes an LLM
|
386
|
+
* instance and StuffQAChainParams as parameters.
|
387
|
+
* @param llm An instance of BaseLanguageModel.
|
388
|
+
* @param params Parameters for creating a StuffQAChain.
|
389
|
+
* @returns A StuffQAChain instance.
|
390
|
+
*/ function loadQAStuffChain(llm) {
|
391
|
+
var params = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
392
|
+
var _params_prompt = params.prompt, prompt = _params_prompt === void 0 ? QA_PROMPT_SELECTOR.getPrompt(llm) : _params_prompt, verbose = params.verbose;
|
393
|
+
var llmChain = new LLMChain({
|
394
|
+
prompt: prompt,
|
395
|
+
llm: llm,
|
396
|
+
verbose: verbose
|
397
|
+
});
|
398
|
+
var chain = new StuffDocumentsChain({
|
399
|
+
llmChain: llmChain,
|
400
|
+
verbose: verbose
|
401
|
+
});
|
402
|
+
return chain;
|
403
|
+
}
|
404
|
+
|
405
|
+
function _assert_this_initialized(self) {
|
406
|
+
if (self === void 0) {
|
407
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
408
|
+
}
|
409
|
+
return self;
|
410
|
+
}
|
411
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
412
|
+
try {
|
413
|
+
var info = gen[key](arg);
|
414
|
+
var value = info.value;
|
415
|
+
} catch (error) {
|
416
|
+
reject(error);
|
417
|
+
return;
|
418
|
+
}
|
419
|
+
if (info.done) {
|
420
|
+
resolve(value);
|
421
|
+
} else {
|
422
|
+
Promise.resolve(value).then(_next, _throw);
|
423
|
+
}
|
424
|
+
}
|
425
|
+
function _async_to_generator(fn) {
|
426
|
+
return function() {
|
427
|
+
var self = this, args = arguments;
|
428
|
+
return new Promise(function(resolve, reject) {
|
429
|
+
var gen = fn.apply(self, args);
|
430
|
+
function _next(value) {
|
431
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
432
|
+
}
|
433
|
+
function _throw(err) {
|
434
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
435
|
+
}
|
436
|
+
_next(undefined);
|
437
|
+
});
|
438
|
+
};
|
439
|
+
}
|
440
|
+
function _class_call_check(instance, Constructor) {
|
441
|
+
if (!(instance instanceof Constructor)) {
|
442
|
+
throw new TypeError("Cannot call a class as a function");
|
443
|
+
}
|
444
|
+
}
|
445
|
+
function _defineProperties(target, props) {
|
446
|
+
for(var i = 0; i < props.length; i++){
|
447
|
+
var descriptor = props[i];
|
448
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
449
|
+
descriptor.configurable = true;
|
450
|
+
if ("value" in descriptor) descriptor.writable = true;
|
451
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
452
|
+
}
|
453
|
+
}
|
454
|
+
function _create_class(Constructor, protoProps, staticProps) {
|
455
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
456
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
457
|
+
return Constructor;
|
458
|
+
}
|
459
|
+
function _define_property(obj, key, value) {
|
460
|
+
if (key in obj) {
|
461
|
+
Object.defineProperty(obj, key, {
|
462
|
+
value: value,
|
463
|
+
enumerable: true,
|
464
|
+
configurable: true,
|
465
|
+
writable: true
|
466
|
+
});
|
467
|
+
} else {
|
468
|
+
obj[key] = value;
|
469
|
+
}
|
470
|
+
return obj;
|
471
|
+
}
|
472
|
+
function _get_prototype_of(o) {
|
473
|
+
_get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
|
474
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
475
|
+
};
|
476
|
+
return _get_prototype_of(o);
|
477
|
+
}
|
478
|
+
function _inherits(subClass, superClass) {
|
479
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
480
|
+
throw new TypeError("Super expression must either be null or a function");
|
481
|
+
}
|
482
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
483
|
+
constructor: {
|
484
|
+
value: subClass,
|
485
|
+
writable: true,
|
486
|
+
configurable: true
|
487
|
+
}
|
488
|
+
});
|
489
|
+
if (superClass) _set_prototype_of(subClass, superClass);
|
490
|
+
}
|
491
|
+
function _object_spread(target) {
|
492
|
+
for(var i = 1; i < arguments.length; i++){
|
493
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
494
|
+
var ownKeys = Object.keys(source);
|
495
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
496
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
497
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
498
|
+
}));
|
499
|
+
}
|
500
|
+
ownKeys.forEach(function(key) {
|
501
|
+
_define_property(target, key, source[key]);
|
502
|
+
});
|
503
|
+
}
|
504
|
+
return target;
|
505
|
+
}
|
506
|
+
function ownKeys(object, enumerableOnly) {
|
507
|
+
var keys = Object.keys(object);
|
508
|
+
if (Object.getOwnPropertySymbols) {
|
509
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
510
|
+
if (enumerableOnly) {
|
511
|
+
symbols = symbols.filter(function(sym) {
|
512
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
513
|
+
});
|
514
|
+
}
|
515
|
+
keys.push.apply(keys, symbols);
|
516
|
+
}
|
517
|
+
return keys;
|
518
|
+
}
|
519
|
+
function _object_spread_props(target, source) {
|
520
|
+
source = source != null ? source : {};
|
521
|
+
if (Object.getOwnPropertyDescriptors) {
|
522
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
523
|
+
} else {
|
524
|
+
ownKeys(Object(source)).forEach(function(key) {
|
525
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
526
|
+
});
|
527
|
+
}
|
528
|
+
return target;
|
529
|
+
}
|
530
|
+
function _possible_constructor_return(self, call) {
|
531
|
+
if (call && (_type_of(call) === "object" || typeof call === "function")) {
|
532
|
+
return call;
|
533
|
+
}
|
534
|
+
return _assert_this_initialized(self);
|
535
|
+
}
|
536
|
+
function _set_prototype_of(o, p) {
|
537
|
+
_set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
|
538
|
+
o.__proto__ = p;
|
539
|
+
return o;
|
540
|
+
};
|
541
|
+
return _set_prototype_of(o, p);
|
542
|
+
}
|
543
|
+
function _type_of(obj) {
|
544
|
+
"@swc/helpers - typeof";
|
545
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
546
|
+
}
|
547
|
+
function _is_native_reflect_construct() {
|
548
|
+
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
549
|
+
if (Reflect.construct.sham) return false;
|
550
|
+
if (typeof Proxy === "function") return true;
|
551
|
+
try {
|
552
|
+
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
|
553
|
+
return true;
|
554
|
+
} catch (e) {
|
555
|
+
return false;
|
556
|
+
}
|
557
|
+
}
|
558
|
+
function _create_super(Derived) {
|
559
|
+
var hasNativeReflectConstruct = _is_native_reflect_construct();
|
560
|
+
return function _createSuperInternal() {
|
561
|
+
var Super = _get_prototype_of(Derived), result;
|
562
|
+
if (hasNativeReflectConstruct) {
|
563
|
+
var NewTarget = _get_prototype_of(this).constructor;
|
564
|
+
result = Reflect.construct(Super, arguments, NewTarget);
|
565
|
+
} else {
|
566
|
+
result = Super.apply(this, arguments);
|
567
|
+
}
|
568
|
+
return _possible_constructor_return(this, result);
|
569
|
+
};
|
570
|
+
}
|
571
|
+
function _ts_generator(thisArg, body) {
|
572
|
+
var f, y, t, g, _ = {
|
573
|
+
label: 0,
|
574
|
+
sent: function() {
|
575
|
+
if (t[0] & 1) throw t[1];
|
576
|
+
return t[1];
|
577
|
+
},
|
578
|
+
trys: [],
|
579
|
+
ops: []
|
580
|
+
};
|
581
|
+
return g = {
|
582
|
+
next: verb(0),
|
583
|
+
"throw": verb(1),
|
584
|
+
"return": verb(2)
|
585
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
586
|
+
return this;
|
587
|
+
}), g;
|
588
|
+
function verb(n) {
|
589
|
+
return function(v) {
|
590
|
+
return step([
|
591
|
+
n,
|
592
|
+
v
|
593
|
+
]);
|
594
|
+
};
|
595
|
+
}
|
596
|
+
function step(op) {
|
597
|
+
if (f) throw new TypeError("Generator is already executing.");
|
598
|
+
while(_)try {
|
599
|
+
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;
|
600
|
+
if (y = 0, t) op = [
|
601
|
+
op[0] & 2,
|
602
|
+
t.value
|
603
|
+
];
|
604
|
+
switch(op[0]){
|
605
|
+
case 0:
|
606
|
+
case 1:
|
607
|
+
t = op;
|
608
|
+
break;
|
609
|
+
case 4:
|
610
|
+
_.label++;
|
611
|
+
return {
|
612
|
+
value: op[1],
|
613
|
+
done: false
|
614
|
+
};
|
615
|
+
case 5:
|
616
|
+
_.label++;
|
617
|
+
y = op[1];
|
618
|
+
op = [
|
619
|
+
0
|
620
|
+
];
|
621
|
+
continue;
|
622
|
+
case 7:
|
623
|
+
op = _.ops.pop();
|
624
|
+
_.trys.pop();
|
625
|
+
continue;
|
626
|
+
default:
|
627
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
628
|
+
_ = 0;
|
629
|
+
continue;
|
630
|
+
}
|
631
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
632
|
+
_.label = op[1];
|
633
|
+
break;
|
634
|
+
}
|
635
|
+
if (op[0] === 6 && _.label < t[1]) {
|
636
|
+
_.label = t[1];
|
637
|
+
t = op;
|
638
|
+
break;
|
639
|
+
}
|
640
|
+
if (t && _.label < t[2]) {
|
641
|
+
_.label = t[2];
|
642
|
+
_.ops.push(op);
|
643
|
+
break;
|
644
|
+
}
|
645
|
+
if (t[2]) _.ops.pop();
|
646
|
+
_.trys.pop();
|
647
|
+
continue;
|
648
|
+
}
|
649
|
+
op = body.call(thisArg, _);
|
650
|
+
} catch (e) {
|
651
|
+
op = [
|
652
|
+
6,
|
653
|
+
e
|
654
|
+
];
|
655
|
+
y = 0;
|
656
|
+
} finally{
|
657
|
+
f = t = 0;
|
658
|
+
}
|
659
|
+
if (op[0] & 5) throw op[1];
|
660
|
+
return {
|
661
|
+
value: op[0] ? op[1] : void 0,
|
662
|
+
done: true
|
663
|
+
};
|
664
|
+
}
|
665
|
+
}
|
666
|
+
/**
|
667
|
+
* Class that represents a VectorDBQAChain. It extends the `BaseChain`
|
668
|
+
* class and implements the `VectorDBQAChainInput` interface. It performs
|
669
|
+
* a similarity search using a vector store and combines the search
|
670
|
+
* results using a specified combine documents chain.
|
671
|
+
*
|
672
|
+
* @deprecated
|
673
|
+
* Switch to {@link https://js.langchain.com/docs/modules/chains/ | createRetrievalChain}
|
674
|
+
* Will be removed in 0.2.0
|
675
|
+
*/ var VectorDBQAChain = /*#__PURE__*/ function(BaseChain1) {
|
676
|
+
_inherits(VectorDBQAChain, BaseChain1);
|
677
|
+
var _super = _create_super(VectorDBQAChain);
|
678
|
+
function VectorDBQAChain(fields) {
|
679
|
+
_class_call_check(this, VectorDBQAChain);
|
680
|
+
var _this;
|
681
|
+
_this = _super.call(this, fields);
|
682
|
+
Object.defineProperty(_assert_this_initialized(_this), "k", {
|
683
|
+
enumerable: true,
|
684
|
+
configurable: true,
|
685
|
+
writable: true,
|
686
|
+
value: 4
|
687
|
+
});
|
688
|
+
Object.defineProperty(_assert_this_initialized(_this), "inputKey", {
|
689
|
+
enumerable: true,
|
690
|
+
configurable: true,
|
691
|
+
writable: true,
|
692
|
+
value: "query"
|
693
|
+
});
|
694
|
+
Object.defineProperty(_assert_this_initialized(_this), "vectorstore", {
|
695
|
+
enumerable: true,
|
696
|
+
configurable: true,
|
697
|
+
writable: true,
|
698
|
+
value: void 0
|
699
|
+
});
|
700
|
+
Object.defineProperty(_assert_this_initialized(_this), "combineDocumentsChain", {
|
701
|
+
enumerable: true,
|
702
|
+
configurable: true,
|
703
|
+
writable: true,
|
704
|
+
value: void 0
|
705
|
+
});
|
706
|
+
Object.defineProperty(_assert_this_initialized(_this), "returnSourceDocuments", {
|
707
|
+
enumerable: true,
|
708
|
+
configurable: true,
|
709
|
+
writable: true,
|
710
|
+
value: false
|
711
|
+
});
|
712
|
+
_this.vectorstore = fields.vectorstore;
|
713
|
+
_this.combineDocumentsChain = fields.combineDocumentsChain;
|
714
|
+
var _fields_inputKey;
|
715
|
+
_this.inputKey = (_fields_inputKey = fields.inputKey) !== null && _fields_inputKey !== void 0 ? _fields_inputKey : _this.inputKey;
|
716
|
+
var _fields_k;
|
717
|
+
_this.k = (_fields_k = fields.k) !== null && _fields_k !== void 0 ? _fields_k : _this.k;
|
718
|
+
var _fields_returnSourceDocuments;
|
719
|
+
_this.returnSourceDocuments = (_fields_returnSourceDocuments = fields.returnSourceDocuments) !== null && _fields_returnSourceDocuments !== void 0 ? _fields_returnSourceDocuments : _this.returnSourceDocuments;
|
720
|
+
return _this;
|
721
|
+
}
|
722
|
+
_create_class(VectorDBQAChain, [
|
723
|
+
{
|
724
|
+
key: "inputKeys",
|
725
|
+
get: function get() {
|
726
|
+
return [
|
727
|
+
this.inputKey
|
728
|
+
];
|
729
|
+
}
|
730
|
+
},
|
731
|
+
{
|
732
|
+
key: "outputKeys",
|
733
|
+
get: function get() {
|
734
|
+
return this.combineDocumentsChain.outputKeys.concat(this.returnSourceDocuments ? [
|
735
|
+
"sourceDocuments"
|
736
|
+
] : []);
|
737
|
+
}
|
738
|
+
},
|
739
|
+
{
|
740
|
+
key: "_call",
|
741
|
+
value: /** @ignore */ function _call(values, runManager) {
|
742
|
+
var _this = this;
|
743
|
+
return _async_to_generator(function() {
|
744
|
+
var question, docs, inputs, result;
|
745
|
+
return _ts_generator(this, function(_state) {
|
746
|
+
switch(_state.label){
|
747
|
+
case 0:
|
748
|
+
if (!(_this.inputKey in values)) {
|
749
|
+
throw new Error("Question key ".concat(_this.inputKey, " not found."));
|
750
|
+
}
|
751
|
+
question = values[_this.inputKey];
|
752
|
+
return [
|
753
|
+
4,
|
754
|
+
_this.vectorstore.similaritySearch(question, _this.k, values.filter, runManager === null || runManager === void 0 ? void 0 : runManager.getChild("vectorstore"))
|
755
|
+
];
|
756
|
+
case 1:
|
757
|
+
docs = _state.sent();
|
758
|
+
inputs = {
|
759
|
+
question: question,
|
760
|
+
input_documents: docs
|
761
|
+
};
|
762
|
+
return [
|
763
|
+
4,
|
764
|
+
_this.combineDocumentsChain.call(inputs, runManager === null || runManager === void 0 ? void 0 : runManager.getChild("combine_documents"))
|
765
|
+
];
|
766
|
+
case 2:
|
767
|
+
result = _state.sent();
|
768
|
+
if (_this.returnSourceDocuments) {
|
769
|
+
return [
|
770
|
+
2,
|
771
|
+
_object_spread_props(_object_spread({}, result), {
|
772
|
+
sourceDocuments: docs
|
773
|
+
})
|
774
|
+
];
|
775
|
+
}
|
776
|
+
return [
|
777
|
+
2,
|
778
|
+
result
|
779
|
+
];
|
780
|
+
}
|
781
|
+
});
|
782
|
+
})();
|
783
|
+
}
|
784
|
+
},
|
785
|
+
{
|
786
|
+
key: "_chainType",
|
787
|
+
value: function _chainType() {
|
788
|
+
return "vector_db_qa";
|
789
|
+
}
|
790
|
+
},
|
791
|
+
{
|
792
|
+
key: "serialize",
|
793
|
+
value: function serialize() {
|
794
|
+
return {
|
795
|
+
_type: this._chainType(),
|
796
|
+
combine_documents_chain: this.combineDocumentsChain.serialize(),
|
797
|
+
k: this.k
|
798
|
+
};
|
799
|
+
}
|
800
|
+
}
|
801
|
+
], [
|
802
|
+
{
|
803
|
+
key: "lc_name",
|
804
|
+
value: function lc_name() {
|
805
|
+
return "VectorDBQAChain";
|
806
|
+
}
|
807
|
+
},
|
808
|
+
{
|
809
|
+
key: "deserialize",
|
810
|
+
value: function deserialize(data, values) {
|
811
|
+
return _async_to_generator(function() {
|
812
|
+
var vectorstore, _, _tmp;
|
813
|
+
return _ts_generator(this, function(_state) {
|
814
|
+
switch(_state.label){
|
815
|
+
case 0:
|
816
|
+
if (!("vectorstore" in values)) {
|
817
|
+
throw new Error("Need to pass in a vectorstore to deserialize VectorDBQAChain");
|
818
|
+
}
|
819
|
+
vectorstore = values.vectorstore;
|
820
|
+
if (!data.combine_documents_chain) {
|
821
|
+
throw new Error("VectorDBQAChain must have combine_documents_chain in serialized data");
|
822
|
+
}
|
823
|
+
_ = VectorDBQAChain.bind;
|
824
|
+
_tmp = {};
|
825
|
+
return [
|
826
|
+
4,
|
827
|
+
BaseChain.deserialize(data.combine_documents_chain)
|
828
|
+
];
|
829
|
+
case 1:
|
830
|
+
return [
|
831
|
+
2,
|
832
|
+
new (_.apply(VectorDBQAChain, [
|
833
|
+
void 0,
|
834
|
+
(_tmp.combineDocumentsChain = _state.sent(), _tmp.k = data.k, _tmp.vectorstore = vectorstore, _tmp)
|
835
|
+
]))
|
836
|
+
];
|
837
|
+
}
|
838
|
+
});
|
839
|
+
})();
|
840
|
+
}
|
841
|
+
},
|
842
|
+
{
|
843
|
+
key: "fromLLM",
|
844
|
+
value: /**
|
845
|
+
* Static method that creates a VectorDBQAChain instance from a
|
846
|
+
* BaseLanguageModel and a vector store. It also accepts optional options
|
847
|
+
* to customize the chain.
|
848
|
+
* @param llm The BaseLanguageModel instance.
|
849
|
+
* @param vectorstore The vector store used for similarity search.
|
850
|
+
* @param options Optional options to customize the chain.
|
851
|
+
* @returns A new instance of VectorDBQAChain.
|
852
|
+
*/ function fromLLM(llm, vectorstore, options) {
|
853
|
+
var qaChain = loadQAStuffChain(llm);
|
854
|
+
return new this(_object_spread({
|
855
|
+
vectorstore: vectorstore,
|
856
|
+
combineDocumentsChain: qaChain
|
857
|
+
}, options));
|
858
|
+
}
|
859
|
+
}
|
860
|
+
]);
|
861
|
+
return VectorDBQAChain;
|
862
|
+
}(BaseChain);
|
863
|
+
|
864
|
+
export { VectorDBQAChain };
|