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