@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.
@@ -0,0 +1,480 @@
1
+ 'use strict';
2
+
3
+ var index = require('./index.cjs2.js');
4
+
5
+ var API_URL_RAW_PROMPT_TEMPLATE = "You are given the below API Documentation:\n{api_docs}\nUsing this documentation, generate the full API url to call for answering the user question.\nYou should build the API url in order to get a response that is as short as possible, while still getting the necessary information to answer the question. Pay attention to deliberately exclude any unnecessary pieces of data in the API call.\n\nQuestion:{question}\nAPI url:";
6
+ var API_URL_PROMPT_TEMPLATE = /* #__PURE__ */ new index.PromptTemplate({
7
+ inputVariables: [
8
+ "api_docs",
9
+ "question"
10
+ ],
11
+ template: API_URL_RAW_PROMPT_TEMPLATE
12
+ });
13
+ var API_RESPONSE_RAW_PROMPT_TEMPLATE = "".concat(API_URL_RAW_PROMPT_TEMPLATE, " {api_url}\n\nHere is the response from the API:\n\n{api_response}\n\nSummarize this response to answer the original question.\n\nSummary:");
14
+ var API_RESPONSE_PROMPT_TEMPLATE = /* #__PURE__ */ new index.PromptTemplate({
15
+ inputVariables: [
16
+ "api_docs",
17
+ "question",
18
+ "api_url",
19
+ "api_response"
20
+ ],
21
+ template: API_RESPONSE_RAW_PROMPT_TEMPLATE
22
+ });
23
+
24
+ function _assert_this_initialized(self) {
25
+ if (self === void 0) {
26
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
27
+ }
28
+ return self;
29
+ }
30
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
31
+ try {
32
+ var info = gen[key](arg);
33
+ var value = info.value;
34
+ } catch (error) {
35
+ reject(error);
36
+ return;
37
+ }
38
+ if (info.done) {
39
+ resolve(value);
40
+ } else {
41
+ Promise.resolve(value).then(_next, _throw);
42
+ }
43
+ }
44
+ function _async_to_generator(fn) {
45
+ return function() {
46
+ var self = this, args = arguments;
47
+ return new Promise(function(resolve, reject) {
48
+ var gen = fn.apply(self, args);
49
+ function _next(value) {
50
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
51
+ }
52
+ function _throw(err) {
53
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
54
+ }
55
+ _next(undefined);
56
+ });
57
+ };
58
+ }
59
+ function _class_call_check(instance, Constructor) {
60
+ if (!(instance instanceof Constructor)) {
61
+ throw new TypeError("Cannot call a class as a function");
62
+ }
63
+ }
64
+ function _defineProperties(target, props) {
65
+ for(var i = 0; i < props.length; i++){
66
+ var descriptor = props[i];
67
+ descriptor.enumerable = descriptor.enumerable || false;
68
+ descriptor.configurable = true;
69
+ if ("value" in descriptor) descriptor.writable = true;
70
+ Object.defineProperty(target, descriptor.key, descriptor);
71
+ }
72
+ }
73
+ function _create_class(Constructor, protoProps, staticProps) {
74
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
75
+ if (staticProps) _defineProperties(Constructor, staticProps);
76
+ return Constructor;
77
+ }
78
+ function _define_property(obj, key, value) {
79
+ if (key in obj) {
80
+ Object.defineProperty(obj, key, {
81
+ value: value,
82
+ enumerable: true,
83
+ configurable: true,
84
+ writable: true
85
+ });
86
+ } else {
87
+ obj[key] = value;
88
+ }
89
+ return obj;
90
+ }
91
+ function _get_prototype_of(o) {
92
+ _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
93
+ return o.__proto__ || Object.getPrototypeOf(o);
94
+ };
95
+ return _get_prototype_of(o);
96
+ }
97
+ function _inherits(subClass, superClass) {
98
+ if (typeof superClass !== "function" && superClass !== null) {
99
+ throw new TypeError("Super expression must either be null or a function");
100
+ }
101
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
102
+ constructor: {
103
+ value: subClass,
104
+ writable: true,
105
+ configurable: true
106
+ }
107
+ });
108
+ if (superClass) _set_prototype_of(subClass, superClass);
109
+ }
110
+ function _object_spread(target) {
111
+ for(var i = 1; i < arguments.length; i++){
112
+ var source = arguments[i] != null ? arguments[i] : {};
113
+ var ownKeys = Object.keys(source);
114
+ if (typeof Object.getOwnPropertySymbols === "function") {
115
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
116
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
117
+ }));
118
+ }
119
+ ownKeys.forEach(function(key) {
120
+ _define_property(target, key, source[key]);
121
+ });
122
+ }
123
+ return target;
124
+ }
125
+ function _possible_constructor_return(self, call) {
126
+ if (call && (_type_of(call) === "object" || typeof call === "function")) {
127
+ return call;
128
+ }
129
+ return _assert_this_initialized(self);
130
+ }
131
+ function _set_prototype_of(o, p) {
132
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
133
+ o.__proto__ = p;
134
+ return o;
135
+ };
136
+ return _set_prototype_of(o, p);
137
+ }
138
+ function _type_of(obj) {
139
+ "@swc/helpers - typeof";
140
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
141
+ }
142
+ function _is_native_reflect_construct() {
143
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
144
+ if (Reflect.construct.sham) return false;
145
+ if (typeof Proxy === "function") return true;
146
+ try {
147
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
148
+ return true;
149
+ } catch (e) {
150
+ return false;
151
+ }
152
+ }
153
+ function _create_super(Derived) {
154
+ var hasNativeReflectConstruct = _is_native_reflect_construct();
155
+ return function _createSuperInternal() {
156
+ var Super = _get_prototype_of(Derived), result;
157
+ if (hasNativeReflectConstruct) {
158
+ var NewTarget = _get_prototype_of(this).constructor;
159
+ result = Reflect.construct(Super, arguments, NewTarget);
160
+ } else {
161
+ result = Super.apply(this, arguments);
162
+ }
163
+ return _possible_constructor_return(this, result);
164
+ };
165
+ }
166
+ function _ts_generator(thisArg, body) {
167
+ var f, y, t, g, _ = {
168
+ label: 0,
169
+ sent: function() {
170
+ if (t[0] & 1) throw t[1];
171
+ return t[1];
172
+ },
173
+ trys: [],
174
+ ops: []
175
+ };
176
+ return g = {
177
+ next: verb(0),
178
+ "throw": verb(1),
179
+ "return": verb(2)
180
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
181
+ return this;
182
+ }), g;
183
+ function verb(n) {
184
+ return function(v) {
185
+ return step([
186
+ n,
187
+ v
188
+ ]);
189
+ };
190
+ }
191
+ function step(op) {
192
+ if (f) throw new TypeError("Generator is already executing.");
193
+ while(_)try {
194
+ 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;
195
+ if (y = 0, t) op = [
196
+ op[0] & 2,
197
+ t.value
198
+ ];
199
+ switch(op[0]){
200
+ case 0:
201
+ case 1:
202
+ t = op;
203
+ break;
204
+ case 4:
205
+ _.label++;
206
+ return {
207
+ value: op[1],
208
+ done: false
209
+ };
210
+ case 5:
211
+ _.label++;
212
+ y = op[1];
213
+ op = [
214
+ 0
215
+ ];
216
+ continue;
217
+ case 7:
218
+ op = _.ops.pop();
219
+ _.trys.pop();
220
+ continue;
221
+ default:
222
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
223
+ _ = 0;
224
+ continue;
225
+ }
226
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
227
+ _.label = op[1];
228
+ break;
229
+ }
230
+ if (op[0] === 6 && _.label < t[1]) {
231
+ _.label = t[1];
232
+ t = op;
233
+ break;
234
+ }
235
+ if (t && _.label < t[2]) {
236
+ _.label = t[2];
237
+ _.ops.push(op);
238
+ break;
239
+ }
240
+ if (t[2]) _.ops.pop();
241
+ _.trys.pop();
242
+ continue;
243
+ }
244
+ op = body.call(thisArg, _);
245
+ } catch (e) {
246
+ op = [
247
+ 6,
248
+ e
249
+ ];
250
+ y = 0;
251
+ } finally{
252
+ f = t = 0;
253
+ }
254
+ if (op[0] & 5) throw op[1];
255
+ return {
256
+ value: op[0] ? op[1] : void 0,
257
+ done: true
258
+ };
259
+ }
260
+ }
261
+ /**
262
+ * Class that extends BaseChain and represents a chain specifically
263
+ * designed for making API requests and processing API responses.
264
+ */ var APIChain = /*#__PURE__*/ function(BaseChain) {
265
+ _inherits(APIChain, BaseChain);
266
+ var _super = _create_super(APIChain);
267
+ function APIChain(fields) {
268
+ _class_call_check(this, APIChain);
269
+ var _this;
270
+ _this = _super.call(this, fields);
271
+ Object.defineProperty(_assert_this_initialized(_this), "apiAnswerChain", {
272
+ enumerable: true,
273
+ configurable: true,
274
+ writable: true,
275
+ value: void 0
276
+ });
277
+ Object.defineProperty(_assert_this_initialized(_this), "apiRequestChain", {
278
+ enumerable: true,
279
+ configurable: true,
280
+ writable: true,
281
+ value: void 0
282
+ });
283
+ Object.defineProperty(_assert_this_initialized(_this), "apiDocs", {
284
+ enumerable: true,
285
+ configurable: true,
286
+ writable: true,
287
+ value: void 0
288
+ });
289
+ Object.defineProperty(_assert_this_initialized(_this), "headers", {
290
+ enumerable: true,
291
+ configurable: true,
292
+ writable: true,
293
+ value: {}
294
+ });
295
+ Object.defineProperty(_assert_this_initialized(_this), "inputKey", {
296
+ enumerable: true,
297
+ configurable: true,
298
+ writable: true,
299
+ value: "question"
300
+ });
301
+ Object.defineProperty(_assert_this_initialized(_this), "outputKey", {
302
+ enumerable: true,
303
+ configurable: true,
304
+ writable: true,
305
+ value: "output"
306
+ });
307
+ _this.apiRequestChain = fields.apiRequestChain;
308
+ _this.apiAnswerChain = fields.apiAnswerChain;
309
+ _this.apiDocs = fields.apiDocs;
310
+ var _fields_inputKey;
311
+ _this.inputKey = (_fields_inputKey = fields.inputKey) !== null && _fields_inputKey !== void 0 ? _fields_inputKey : _this.inputKey;
312
+ var _fields_outputKey;
313
+ _this.outputKey = (_fields_outputKey = fields.outputKey) !== null && _fields_outputKey !== void 0 ? _fields_outputKey : _this.outputKey;
314
+ var _fields_headers;
315
+ _this.headers = (_fields_headers = fields.headers) !== null && _fields_headers !== void 0 ? _fields_headers : _this.headers;
316
+ return _this;
317
+ }
318
+ _create_class(APIChain, [
319
+ {
320
+ key: "inputKeys",
321
+ get: function get() {
322
+ return [
323
+ this.inputKey
324
+ ];
325
+ }
326
+ },
327
+ {
328
+ key: "outputKeys",
329
+ get: function get() {
330
+ return [
331
+ this.outputKey
332
+ ];
333
+ }
334
+ },
335
+ {
336
+ key: "_call",
337
+ value: /** @ignore */ function _call(values, runManager) {
338
+ var _this = this;
339
+ return _async_to_generator(function() {
340
+ var question, api_url, res, api_response, answer;
341
+ return _ts_generator(this, function(_state) {
342
+ switch(_state.label){
343
+ case 0:
344
+ question = values[_this.inputKey];
345
+ return [
346
+ 4,
347
+ _this.apiRequestChain.predict({
348
+ question: question,
349
+ api_docs: _this.apiDocs
350
+ }, runManager === null || runManager === void 0 ? void 0 : runManager.getChild("request"))
351
+ ];
352
+ case 1:
353
+ api_url = _state.sent();
354
+ return [
355
+ 4,
356
+ fetch(api_url, {
357
+ headers: _this.headers
358
+ })
359
+ ];
360
+ case 2:
361
+ res = _state.sent();
362
+ return [
363
+ 4,
364
+ res.text()
365
+ ];
366
+ case 3:
367
+ api_response = _state.sent();
368
+ return [
369
+ 4,
370
+ _this.apiAnswerChain.predict({
371
+ question: question,
372
+ api_docs: _this.apiDocs,
373
+ api_url: api_url,
374
+ api_response: api_response
375
+ }, runManager === null || runManager === void 0 ? void 0 : runManager.getChild("response"))
376
+ ];
377
+ case 4:
378
+ answer = _state.sent();
379
+ return [
380
+ 2,
381
+ _define_property({}, _this.outputKey, answer)
382
+ ];
383
+ }
384
+ });
385
+ })();
386
+ }
387
+ },
388
+ {
389
+ key: "_chainType",
390
+ value: function _chainType() {
391
+ return "api_chain";
392
+ }
393
+ },
394
+ {
395
+ key: "serialize",
396
+ value: function serialize() {
397
+ return {
398
+ _type: this._chainType(),
399
+ api_answer_chain: this.apiAnswerChain.serialize(),
400
+ api_request_chain: this.apiRequestChain.serialize(),
401
+ api_docs: this.apiDocs
402
+ };
403
+ }
404
+ }
405
+ ], [
406
+ {
407
+ key: "deserialize",
408
+ value: function deserialize(data) {
409
+ return _async_to_generator(function() {
410
+ var api_request_chain, api_answer_chain, api_docs, _, _tmp;
411
+ return _ts_generator(this, function(_state) {
412
+ switch(_state.label){
413
+ case 0:
414
+ api_request_chain = data.api_request_chain, api_answer_chain = data.api_answer_chain, api_docs = data.api_docs;
415
+ if (!api_request_chain) {
416
+ throw new Error("LLMChain must have api_request_chain");
417
+ }
418
+ if (!api_answer_chain) {
419
+ throw new Error("LLMChain must have api_answer_chain");
420
+ }
421
+ if (!api_docs) {
422
+ throw new Error("LLMChain must have api_docs");
423
+ }
424
+ _ = APIChain.bind;
425
+ _tmp = {};
426
+ return [
427
+ 4,
428
+ index.LLMChain.deserialize(api_answer_chain)
429
+ ];
430
+ case 1:
431
+ _tmp.apiAnswerChain = _state.sent();
432
+ return [
433
+ 4,
434
+ index.LLMChain.deserialize(api_request_chain)
435
+ ];
436
+ case 2:
437
+ return [
438
+ 2,
439
+ new (_.apply(APIChain, [
440
+ void 0,
441
+ (_tmp.apiRequestChain = _state.sent(), _tmp.apiDocs = api_docs, _tmp)
442
+ ]))
443
+ ];
444
+ }
445
+ });
446
+ })();
447
+ }
448
+ },
449
+ {
450
+ key: "fromLLMAndAPIDocs",
451
+ value: /**
452
+ * Static method to create a new APIChain from a BaseLanguageModel and API
453
+ * documentation.
454
+ * @param llm BaseLanguageModel instance.
455
+ * @param apiDocs API documentation.
456
+ * @param options Optional configuration options for the APIChain.
457
+ * @returns New APIChain instance.
458
+ */ function fromLLMAndAPIDocs(llm, apiDocs) {
459
+ var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : {};
460
+ var _options_apiUrlPrompt = options.apiUrlPrompt, apiUrlPrompt = _options_apiUrlPrompt === void 0 ? API_URL_PROMPT_TEMPLATE : _options_apiUrlPrompt, _options_apiResponsePrompt = options.apiResponsePrompt, apiResponsePrompt = _options_apiResponsePrompt === void 0 ? API_RESPONSE_PROMPT_TEMPLATE : _options_apiResponsePrompt;
461
+ var apiRequestChain = new index.LLMChain({
462
+ prompt: apiUrlPrompt,
463
+ llm: llm
464
+ });
465
+ var apiAnswerChain = new index.LLMChain({
466
+ prompt: apiResponsePrompt,
467
+ llm: llm
468
+ });
469
+ return new this(_object_spread({
470
+ apiAnswerChain: apiAnswerChain,
471
+ apiRequestChain: apiRequestChain,
472
+ apiDocs: apiDocs
473
+ }, options));
474
+ }
475
+ }
476
+ ]);
477
+ return APIChain;
478
+ }(index.BaseChain);
479
+
480
+ exports.APIChain = APIChain;