medpromptjs 0.3.1 → 0.4.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.
@@ -35,283 +35,149 @@ function _asyncToGenerator(n) {
35
35
  });
36
36
  };
37
37
  }
38
+ function _defineProperties(e, r) {
39
+ for (var t = 0; t < r.length; t++) {
40
+ var o = r[t];
41
+ o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
42
+ }
43
+ }
44
+ function _createClass(e, r, t) {
45
+ return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
46
+ writable: !1
47
+ }), e;
48
+ }
38
49
  function _inheritsLoose(t, o) {
39
50
  t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o);
40
51
  }
41
- function _regeneratorRuntime() {
42
- _regeneratorRuntime = function () {
43
- return r;
44
- };
45
- var t,
46
- r = {},
47
- e = Object.prototype,
48
- n = e.hasOwnProperty,
49
- o = "function" == typeof Symbol ? Symbol : {},
50
- i = o.iterator || "@@iterator",
51
- a = o.asyncIterator || "@@asyncIterator",
52
- u = o.toStringTag || "@@toStringTag";
53
- function c(t, r, e, n) {
54
- return Object.defineProperty(t, r, {
55
- value: e,
56
- enumerable: !n,
57
- configurable: !n,
58
- writable: !n
59
- });
60
- }
61
- try {
62
- c({}, "");
63
- } catch (t) {
64
- c = function (t, r, e) {
65
- return t[r] = e;
66
- };
67
- }
68
- function h(r, e, n, o) {
69
- var i = e && e.prototype instanceof Generator ? e : Generator,
70
- a = Object.create(i.prototype);
71
- return c(a, "_invoke", function (r, e, n) {
72
- var o = 1;
73
- return function (i, a) {
74
- if (3 === o) throw Error("Generator is already running");
75
- if (4 === o) {
76
- if ("throw" === i) throw a;
77
- return {
78
- value: t,
79
- done: !0
80
- };
81
- }
82
- for (n.method = i, n.arg = a;;) {
83
- var u = n.delegate;
84
- if (u) {
85
- var c = d(u, n);
86
- if (c) {
87
- if (c === f) continue;
88
- return c;
89
- }
52
+ function _regenerator() {
53
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
54
+ var e,
55
+ t,
56
+ r = "function" == typeof Symbol ? Symbol : {},
57
+ n = r.iterator || "@@iterator",
58
+ o = r.toStringTag || "@@toStringTag";
59
+ function i(r, n, o, i) {
60
+ var c = n && n.prototype instanceof Generator ? n : Generator,
61
+ u = Object.create(c.prototype);
62
+ return _regeneratorDefine(u, "_invoke", function (r, n, o) {
63
+ var i,
64
+ c,
65
+ u,
66
+ f = 0,
67
+ p = o || [],
68
+ y = !1,
69
+ G = {
70
+ p: 0,
71
+ n: 0,
72
+ v: e,
73
+ a: d,
74
+ f: d.bind(e, 4),
75
+ d: function (t, r) {
76
+ return i = t, c = 0, u = e, G.n = r, a;
90
77
  }
91
- if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
92
- if (1 === o) throw o = 4, n.arg;
93
- n.dispatchException(n.arg);
94
- } else "return" === n.method && n.abrupt("return", n.arg);
95
- o = 3;
96
- var h = s(r, e, n);
97
- if ("normal" === h.type) {
98
- if (o = n.done ? 4 : 2, h.arg === f) continue;
99
- return {
100
- value: h.arg,
101
- done: n.done
102
- };
78
+ };
79
+ function d(r, n) {
80
+ for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) {
81
+ var o,
82
+ i = p[t],
83
+ d = G.p,
84
+ l = i[2];
85
+ r > 3 ? (o = l === n) && (c = i[4] || 3, u = i[5] === e ? i[3] : i[5], i[4] = 3, i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0));
86
+ }
87
+ if (o || r > 1) return a;
88
+ throw y = !0, n;
89
+ }
90
+ return function (o, p, l) {
91
+ if (f > 1) throw TypeError("Generator is already running");
92
+ for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) {
93
+ i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u);
94
+ try {
95
+ if (f = 2, i) {
96
+ if (c || (o = "next"), t = i[o]) {
97
+ if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object");
98
+ if (!t.done) return t;
99
+ u = t.value, c < 2 && (c = 0);
100
+ } else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1);
101
+ i = e;
102
+ } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break;
103
+ } catch (t) {
104
+ i = e, c = 1, u = t;
105
+ } finally {
106
+ f = 1;
103
107
  }
104
- "throw" === h.type && (o = 4, n.method = "throw", n.arg = h.arg);
105
108
  }
109
+ return {
110
+ value: t,
111
+ done: y
112
+ };
106
113
  };
107
- }(r, n, new Context(o || [])), !0), a;
108
- }
109
- function s(t, r, e) {
110
- try {
111
- return {
112
- type: "normal",
113
- arg: t.call(r, e)
114
- };
115
- } catch (t) {
116
- return {
117
- type: "throw",
118
- arg: t
119
- };
120
- }
114
+ }(r, o, i), !0), u;
121
115
  }
122
- r.wrap = h;
123
- var f = {};
116
+ var a = {};
124
117
  function Generator() {}
125
118
  function GeneratorFunction() {}
126
119
  function GeneratorFunctionPrototype() {}
127
- var l = {};
128
- c(l, i, function () {
129
- return this;
130
- });
131
- var p = Object.getPrototypeOf,
132
- y = p && p(p(x([])));
133
- y && y !== e && n.call(y, i) && (l = y);
134
- var v = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(l);
135
- function g(t) {
136
- ["next", "throw", "return"].forEach(function (r) {
137
- c(t, r, function (t) {
138
- return this._invoke(r, t);
139
- });
140
- });
120
+ t = Object.getPrototypeOf;
121
+ var c = [][n] ? t(t([][n]())) : (_regeneratorDefine(t = {}, n, function () {
122
+ return this;
123
+ }), t),
124
+ u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c);
125
+ function f(e) {
126
+ return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e;
141
127
  }
142
- function AsyncIterator(t, r) {
143
- function e(o, i, a, u) {
144
- var c = s(t[o], t, i);
145
- if ("throw" !== c.type) {
146
- var h = c.arg,
147
- f = h.value;
148
- return f && "object" == typeof f && n.call(f, "__await") ? r.resolve(f.__await).then(function (t) {
149
- e("next", t, a, u);
150
- }, function (t) {
151
- e("throw", t, a, u);
152
- }) : r.resolve(f).then(function (t) {
153
- h.value = t, a(h);
154
- }, function (t) {
155
- return e("throw", t, a, u);
156
- });
157
- }
158
- u(c.arg);
159
- }
160
- var o;
161
- c(this, "_invoke", function (t, n) {
162
- function i() {
163
- return new r(function (r, o) {
164
- e(t, n, r, o);
165
- });
166
- }
167
- return o = o ? o.then(i, i) : i();
168
- }, !0);
169
- }
170
- function d(r, e) {
171
- var n = e.method,
172
- o = r.i[n];
173
- if (o === t) return e.delegate = null, "throw" === n && r.i.return && (e.method = "return", e.arg = t, d(r, e), "throw" === e.method) || "return" !== n && (e.method = "throw", e.arg = new TypeError("The iterator does not provide a '" + n + "' method")), f;
174
- var i = s(o, r.i, e.arg);
175
- if ("throw" === i.type) return e.method = "throw", e.arg = i.arg, e.delegate = null, f;
176
- var a = i.arg;
177
- return a ? a.done ? (e[r.r] = a.value, e.next = r.n, "return" !== e.method && (e.method = "next", e.arg = t), e.delegate = null, f) : a : (e.method = "throw", e.arg = new TypeError("iterator result is not an object"), e.delegate = null, f);
178
- }
179
- function w(t) {
180
- this.tryEntries.push(t);
181
- }
182
- function m(r) {
183
- var e = r[4] || {};
184
- e.type = "normal", e.arg = t, r[4] = e;
185
- }
186
- function Context(t) {
187
- this.tryEntries = [[-1]], t.forEach(w, this), this.reset(!0);
188
- }
189
- function x(r) {
190
- if (null != r) {
191
- var e = r[i];
192
- if (e) return e.call(r);
193
- if ("function" == typeof r.next) return r;
194
- if (!isNaN(r.length)) {
195
- var o = -1,
196
- a = function e() {
197
- for (; ++o < r.length;) if (n.call(r, o)) return e.value = r[o], e.done = !1, e;
198
- return e.value = t, e.done = !0, e;
199
- };
200
- return a.next = a;
201
- }
202
- }
203
- throw new TypeError(typeof r + " is not iterable");
204
- }
205
- return GeneratorFunction.prototype = GeneratorFunctionPrototype, c(v, "constructor", GeneratorFunctionPrototype), c(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = c(GeneratorFunctionPrototype, u, "GeneratorFunction"), r.isGeneratorFunction = function (t) {
206
- var r = "function" == typeof t && t.constructor;
207
- return !!r && (r === GeneratorFunction || "GeneratorFunction" === (r.displayName || r.name));
208
- }, r.mark = function (t) {
209
- return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, c(t, u, "GeneratorFunction")), t.prototype = Object.create(v), t;
210
- }, r.awrap = function (t) {
211
- return {
212
- __await: t
213
- };
214
- }, g(AsyncIterator.prototype), c(AsyncIterator.prototype, a, function () {
215
- return this;
216
- }), r.AsyncIterator = AsyncIterator, r.async = function (t, e, n, o, i) {
217
- void 0 === i && (i = Promise);
218
- var a = new AsyncIterator(h(t, e, n, o), i);
219
- return r.isGeneratorFunction(e) ? a : a.next().then(function (t) {
220
- return t.done ? t.value : a.next();
221
- });
222
- }, g(v), c(v, u, "Generator"), c(v, i, function () {
128
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine(u), _regeneratorDefine(u, o, "Generator"), _regeneratorDefine(u, n, function () {
223
129
  return this;
224
- }), c(v, "toString", function () {
130
+ }), _regeneratorDefine(u, "toString", function () {
225
131
  return "[object Generator]";
226
- }), r.keys = function (t) {
227
- var r = Object(t),
228
- e = [];
229
- for (var n in r) e.unshift(n);
230
- return function t() {
231
- for (; e.length;) if ((n = e.pop()) in r) return t.value = n, t.done = !1, t;
232
- return t.done = !0, t;
132
+ }), (_regenerator = function () {
133
+ return {
134
+ w: i,
135
+ m: f
233
136
  };
234
- }, r.values = x, Context.prototype = {
235
- constructor: Context,
236
- reset: function (r) {
237
- if (this.prev = this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(m), !r) for (var e in this) "t" === e.charAt(0) && n.call(this, e) && !isNaN(+e.slice(1)) && (this[e] = t);
238
- },
239
- stop: function () {
240
- this.done = !0;
241
- var t = this.tryEntries[0][4];
242
- if ("throw" === t.type) throw t.arg;
243
- return this.rval;
244
- },
245
- dispatchException: function (r) {
246
- if (this.done) throw r;
247
- var e = this;
248
- function n(t) {
249
- a.type = "throw", a.arg = r, e.next = t;
250
- }
251
- for (var o = e.tryEntries.length - 1; o >= 0; --o) {
252
- var i = this.tryEntries[o],
253
- a = i[4],
254
- u = this.prev,
255
- c = i[1],
256
- h = i[2];
257
- if (-1 === i[0]) return n("end"), !1;
258
- if (!c && !h) throw Error("try statement without catch or finally");
259
- if (null != i[0] && i[0] <= u) {
260
- if (u < c) return this.method = "next", this.arg = t, n(c), !0;
261
- if (u < h) return n(h), !1;
262
- }
263
- }
264
- },
265
- abrupt: function (t, r) {
266
- for (var e = this.tryEntries.length - 1; e >= 0; --e) {
267
- var n = this.tryEntries[e];
268
- if (n[0] > -1 && n[0] <= this.prev && this.prev < n[2]) {
269
- var o = n;
270
- break;
271
- }
272
- }
273
- o && ("break" === t || "continue" === t) && o[0] <= r && r <= o[2] && (o = null);
274
- var i = o ? o[4] : {};
275
- return i.type = t, i.arg = r, o ? (this.method = "next", this.next = o[2], f) : this.complete(i);
276
- },
277
- complete: function (t, r) {
278
- if ("throw" === t.type) throw t.arg;
279
- return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && r && (this.next = r), f;
280
- },
281
- finish: function (t) {
282
- for (var r = this.tryEntries.length - 1; r >= 0; --r) {
283
- var e = this.tryEntries[r];
284
- if (e[2] === t) return this.complete(e[4], e[3]), m(e), f;
285
- }
286
- },
287
- catch: function (t) {
288
- for (var r = this.tryEntries.length - 1; r >= 0; --r) {
289
- var e = this.tryEntries[r];
290
- if (e[0] === t) {
291
- var n = e[4];
292
- if ("throw" === n.type) {
293
- var o = n.arg;
294
- m(e);
295
- }
296
- return o;
297
- }
137
+ })();
138
+ }
139
+ function _regeneratorDefine(e, r, n, t) {
140
+ var i = Object.defineProperty;
141
+ try {
142
+ i({}, "", {});
143
+ } catch (e) {
144
+ i = 0;
145
+ }
146
+ _regeneratorDefine = function (e, r, n, t) {
147
+ if (r) i ? i(e, r, {
148
+ value: n,
149
+ enumerable: !t,
150
+ configurable: !t,
151
+ writable: !t
152
+ }) : e[r] = n;else {
153
+ function o(r, n) {
154
+ _regeneratorDefine(e, r, function (e) {
155
+ return this._invoke(r, n, e);
156
+ });
298
157
  }
299
- throw Error("illegal catch attempt");
300
- },
301
- delegateYield: function (r, e, n) {
302
- return this.delegate = {
303
- i: x(r),
304
- r: e,
305
- n: n
306
- }, "next" === this.method && (this.arg = t), f;
307
- }
308
- }, r;
158
+ o("next", 0), o("throw", 1), o("return", 2);
159
+ }
160
+ }, _regeneratorDefine(e, r, n, t);
309
161
  }
310
162
  function _setPrototypeOf(t, e) {
311
163
  return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
312
164
  return t.__proto__ = e, t;
313
165
  }, _setPrototypeOf(t, e);
314
166
  }
167
+ function _toPrimitive(t, r) {
168
+ if ("object" != typeof t || !t) return t;
169
+ var e = t[Symbol.toPrimitive];
170
+ if (void 0 !== e) {
171
+ var i = e.call(t, r || "default");
172
+ if ("object" != typeof i) return i;
173
+ throw new TypeError("@@toPrimitive must return a primitive value.");
174
+ }
175
+ return ("string" === r ? String : Number)(t);
176
+ }
177
+ function _toPropertyKey(t) {
178
+ var i = _toPrimitive(t, "string");
179
+ return "symbol" == typeof i ? i : i + "";
180
+ }
315
181
 
316
182
  /**
317
183
  * Copyright 2024 Bell Eapen
@@ -337,38 +203,41 @@ var mydi = function mydi(c, m, b) {
337
203
  };
338
204
 
339
205
  var BaseChain = /*#__PURE__*/function () {
340
- function BaseChain(container, name, description, template) {
341
- if (template === void 0) {
342
- template = "\n Summarize the following text:\n {input}\n ";
343
- }
206
+ function BaseChain(container) {
344
207
  this.container = void 0;
345
- this.tools = void 0;
346
- this.name = void 0;
347
- this.description = void 0;
208
+ this._name = '';
209
+ this._description = '';
210
+ this._template = '';
348
211
  this.prompt = void 0;
349
212
  this.llm = void 0;
350
- this.template = void 0;
351
213
  this.chat_model = void 0;
352
214
  this.runnable = void 0;
353
215
  this.container = container;
354
- this.name = name === '' ? this.camelize(this.constructor.name) : name;
355
- this.description = description === '' ? this.snake_case(this.constructor.name) : description;
356
- this.template = template;
357
- this.tools = this.resolve('tools');
358
216
  this.llm = this.resolve('main-llm');
359
217
  this.chat_model = false;
360
- try {
361
- this.chat_model = this.resolve('chat_model');
362
- } catch (e) {}
218
+ this.initialize();
219
+ }
220
+ // Getters and setters
221
+ var _proto = BaseChain.prototype;
222
+ _proto.initialize = function initialize() {
223
+ if (this._name === '') {
224
+ this._name = this.camelize(this.constructor.name);
225
+ }
226
+ if (this._description === '') {
227
+ this._description = this.snake_case(this.constructor.name);
228
+ }
229
+ if (this._template === '') {
230
+ this._template = "{{input}}";
231
+ }
232
+ this.prompt = this.prompt || prompts.PromptTemplate.fromTemplate(this._template);
363
233
  if (this.chat_model) {
364
- this.prompt = prompts.ChatPromptTemplate.fromTemplate(this.template);
234
+ this.prompt = prompts.ChatPromptTemplate.fromTemplate(this._template);
365
235
  } else {
366
- this.prompt = prompts.PromptTemplate.fromTemplate(this.template);
236
+ this.prompt = prompts.PromptTemplate.fromTemplate(this._template);
367
237
  }
368
- }
369
- var _proto = BaseChain.prototype;
238
+ };
370
239
  _proto.resolve = function resolve(name) {
371
- return mydi(this.container, this.name, name);
240
+ return mydi(this.container, this._name, name);
372
241
  };
373
242
  _proto.camelize = function camelize(str) {
374
243
  return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
@@ -386,30 +255,27 @@ var BaseChain = /*#__PURE__*/function () {
386
255
  _proto.chain =
387
256
  /*#__PURE__*/
388
257
  function () {
389
- var _chain = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
258
+ var _chain = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(input) {
390
259
  var response, aiMessage;
391
- return _regeneratorRuntime().wrap(function _callee$(_context) {
392
- while (1) switch (_context.prev = _context.next) {
260
+ return _regenerator().w(function (_context) {
261
+ while (1) switch (_context.n) {
393
262
  case 0:
394
263
  this.runnable = runnables.RunnableSequence.from([new runnables.RunnablePassthrough(), this.prompt, this.llm]);
395
264
  if (!this.chat_model) {
396
- _context.next = 7;
265
+ _context.n = 2;
397
266
  break;
398
267
  }
399
- _context.next = 4;
268
+ _context.n = 1;
400
269
  return this.runnable.invoke(input);
401
- case 4:
402
- response = _context.sent;
270
+ case 1:
271
+ response = _context.v;
403
272
  aiMessage = new messages.AIMessage(response);
404
- return _context.abrupt("return", aiMessage.content);
405
- case 7:
406
- _context.next = 9;
273
+ return _context.a(2, aiMessage.content);
274
+ case 2:
275
+ _context.n = 3;
407
276
  return this.runnable.invoke(input);
408
- case 9:
409
- return _context.abrupt("return", _context.sent);
410
- case 10:
411
- case "end":
412
- return _context.stop();
277
+ case 3:
278
+ return _context.a(2, _context.v);
413
279
  }
414
280
  }, _callee, this);
415
281
  }));
@@ -418,26 +284,78 @@ var BaseChain = /*#__PURE__*/function () {
418
284
  }
419
285
  return chain;
420
286
  }();
421
- return BaseChain;
287
+ return _createClass(BaseChain, [{
288
+ key: "name",
289
+ get: function get() {
290
+ return this._name;
291
+ },
292
+ set: function set(value) {
293
+ this._name = value === '' ? this.camelize(this.constructor.name) : value;
294
+ }
295
+ }, {
296
+ key: "description",
297
+ get: function get() {
298
+ return this._description;
299
+ },
300
+ set: function set(value) {
301
+ this._description = value === '' ? this.snake_case(this.constructor.name) : value;
302
+ }
303
+ }, {
304
+ key: "template",
305
+ get: function get() {
306
+ return this._template;
307
+ },
308
+ set: function set(value) {
309
+ this._template = value;
310
+ try {
311
+ this.chat_model = this.resolve('chat_model');
312
+ } catch (e) {}
313
+ if (this.chat_model) {
314
+ this.prompt = prompts.ChatPromptTemplate.fromTemplate(this._template);
315
+ } else {
316
+ this.prompt = prompts.PromptTemplate.fromTemplate(this._template);
317
+ }
318
+ }
319
+ }]);
422
320
  }();
423
321
 
424
322
  var BaseAgent = /*#__PURE__*/function () {
425
323
  function BaseAgent(container) {
426
324
  this.container = void 0;
427
- this.tools = void 0;
428
- this.name = void 0;
325
+ this._name = '';
326
+ this._description = '';
327
+ this._template = "\n ## Question: {question}\n ## Answer: {answer}\n Given the above question and answer, generate a chain of thought explanation for the answer.\n First, start with the model generated chain of thought explanation.\n End the chain of though explanation with:\n Therefore, the answer is {answer}.\n ";
328
+ this._tools = [];
429
329
  this.prompt = void 0;
430
330
  this.llm = void 0;
431
- this.template = "\n ## Question: {question}\n ## Answer: {answer}\n Given the above question and answer, generate a chain of thought explanation for the answer.\n First, start with the model generated chain of thought explanation.\n End the chain of though explanation with:\n Therefore, the answer is {answer}.\n ";
331
+ this.chat_model = void 0;
332
+ this.runnable = void 0;
432
333
  this.container = container;
433
- this.name = this.camelize(this.constructor.name);
434
- this.tools = this.resolve('tools');
435
- this.prompt = this.resolve('prompt') !== '' ? this.resolve('prompt') : prompts.PromptTemplate.fromTemplate(this.template);
436
334
  this.llm = this.resolve('main-llm');
335
+ this.chat_model = false;
336
+ this.initialize();
437
337
  }
338
+ // Getters and setters
438
339
  var _proto = BaseAgent.prototype;
340
+ _proto.initialize = function initialize() {
341
+ if (this._name === '') {
342
+ this._name = this.camelize(this.constructor.name);
343
+ }
344
+ if (this._description === '') {
345
+ this._description = this.snake_case(this.constructor.name);
346
+ }
347
+ if (this._template === '') {
348
+ this._template = "{{input}}";
349
+ }
350
+ this.prompt = this.prompt || prompts.PromptTemplate.fromTemplate(this._template);
351
+ if (this.chat_model) {
352
+ this.prompt = prompts.ChatPromptTemplate.fromTemplate(this._template);
353
+ } else {
354
+ this.prompt = prompts.PromptTemplate.fromTemplate(this._template);
355
+ }
356
+ };
439
357
  _proto.resolve = function resolve(name) {
440
- return mydi(this.container, this.name, name);
358
+ return mydi(this.container, this._name, name);
441
359
  };
442
360
  _proto.camelize = function camelize(str) {
443
361
  return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
@@ -450,32 +368,29 @@ var BaseAgent = /*#__PURE__*/function () {
450
368
  }).replace(/\s+/g, '');
451
369
  };
452
370
  _proto.run = /*#__PURE__*/function () {
453
- var _run = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
371
+ var _run = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(input) {
454
372
  var agent, agentExecutor;
455
- return _regeneratorRuntime().wrap(function _callee$(_context) {
456
- while (1) switch (_context.prev = _context.next) {
373
+ return _regenerator().w(function (_context) {
374
+ while (1) switch (_context.n) {
457
375
  case 0:
458
- _context.next = 2;
376
+ _context.n = 1;
459
377
  return agents.createStructuredChatAgent({
460
378
  llm: this.llm,
461
- tools: this.tools,
379
+ tools: this._tools,
462
380
  prompt: this.prompt
463
381
  });
464
- case 2:
465
- agent = _context.sent;
382
+ case 1:
383
+ agent = _context.v;
466
384
  agentExecutor = new agents.AgentExecutor({
467
385
  agent: agent,
468
386
  tools: this.tools,
469
387
  // verbose: true,
470
388
  handleParsingErrors: 'Please try again, paying close attention to the final answer'
471
389
  });
472
- _context.next = 6;
390
+ _context.n = 2;
473
391
  return agentExecutor.invoke(input);
474
- case 6:
475
- return _context.abrupt("return", _context.sent);
476
- case 7:
477
- case "end":
478
- return _context.stop();
392
+ case 2:
393
+ return _context.a(2, _context.v);
479
394
  }
480
395
  }, _callee, this);
481
396
  }));
@@ -484,7 +399,48 @@ var BaseAgent = /*#__PURE__*/function () {
484
399
  }
485
400
  return run;
486
401
  }();
487
- return BaseAgent;
402
+ return _createClass(BaseAgent, [{
403
+ key: "name",
404
+ get: function get() {
405
+ return this._name;
406
+ },
407
+ set: function set(value) {
408
+ this._name = value === '' ? this.camelize(this.constructor.name) : value;
409
+ }
410
+ }, {
411
+ key: "description",
412
+ get: function get() {
413
+ return this._description;
414
+ },
415
+ set: function set(value) {
416
+ this._description = value === '' ? this.snake_case(this.constructor.name) : value;
417
+ }
418
+ }, {
419
+ key: "template",
420
+ get: function get() {
421
+ return this._template;
422
+ },
423
+ set: function set(value) {
424
+ this._template = value;
425
+ try {
426
+ this.chat_model = this.resolve('chat_model');
427
+ } catch (e) {}
428
+ if (this.chat_model) {
429
+ this.prompt = prompts.ChatPromptTemplate.fromTemplate(this._template);
430
+ } else {
431
+ this.prompt = prompts.PromptTemplate.fromTemplate(this._template);
432
+ }
433
+ }
434
+ // Getters and setters for tools
435
+ }, {
436
+ key: "tools",
437
+ get: function get() {
438
+ return this._tools;
439
+ },
440
+ set: function set(value) {
441
+ this._tools = value;
442
+ }
443
+ }]);
488
444
  }();
489
445
 
490
446
  var BaseTool = /*#__PURE__*/function (_DynamicStructuredToo) {
@@ -504,14 +460,11 @@ var BaseTool = /*#__PURE__*/function (_DynamicStructuredToo) {
504
460
  _this.description = void 0;
505
461
  _this.schema = void 0;
506
462
  _this.func = /*#__PURE__*/function () {
507
- var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(args) {
508
- return _regeneratorRuntime().wrap(function _callee$(_context) {
509
- while (1) switch (_context.prev = _context.next) {
463
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(args) {
464
+ return _regenerator().w(function (_context) {
465
+ while (1) switch (_context.n) {
510
466
  case 0:
511
- return _context.abrupt("return", 'override this');
512
- case 1:
513
- case "end":
514
- return _context.stop();
467
+ return _context.a(2, 'override this');
515
468
  }
516
469
  }, _callee);
517
470
  }));
@@ -543,54 +496,6 @@ var BaseTool = /*#__PURE__*/function (_DynamicStructuredToo) {
543
496
  return BaseTool;
544
497
  }(tools.DynamicStructuredTool);
545
498
 
546
- var MapQuery = /*#__PURE__*/function (_BaseChain) {
547
- function MapQuery() {
548
- var _this;
549
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
550
- args[_key] = arguments[_key];
551
- }
552
- _this = _BaseChain.call.apply(_BaseChain, [this].concat(args)) || this;
553
- _this.name = 'MapQuery';
554
- _this.description = 'Map a CQL query to a natural language.';
555
- _this.template = "\n You are an assistant that can convert a CQL query to a natural language.\nYou should give a single line answer>> as in the example below.\nExample:\n\nCQL: exists (\n [DocumentReference] D\n where D.allergies=\"Penicillin\"\n and D.complaint=\"Headache\"\n and D.complaint=\"Weakness\" or D.complaint=\"Numbness\"\n and D.findings=\"Intact sensation to light touch\"\n )\n\nanswer>> Penicillin allergy, Headache, Weakness, Numbness, and Intact sensation to light touch as findings?\n\nCQL: exists (\n [DocumentReference] D\n where D.diagnosis=\"Diverticulitis\"\n and D.complaint=\"Fever\"\n and D.procedure=\"Colon resection\"\n and not D.finding=\"Fluid collection\"\n )\n\nanswer>> Diverticulitis diagnosis, Fever complaint, Colon resection procedure, and no Fluid collection finding?\n\nNow convert the following CQL query to a natural language.\nCQL: {expression}\n\nanswer>>\n ";
556
- return _this;
557
- }
558
- _inheritsLoose(MapQuery, _BaseChain);
559
- return MapQuery;
560
- }(BaseChain);
561
-
562
- var MapDoc = /*#__PURE__*/function (_BaseChain) {
563
- function MapDoc() {
564
- var _this;
565
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
566
- args[_key] = arguments[_key];
567
- }
568
- _this = _BaseChain.call.apply(_BaseChain, [this].concat(args)) || this;
569
- _this.name = 'MapDoc';
570
- _this.description = 'Map a set of documents to facts.';
571
- _this.template = "\n You will be given a document and a question.\n\n Summarize the document chunk commenting on: {query} \n\n Do not include absent or negative mentions.\n\n document: {document} \n\n Summary:: ";
572
- return _this;
573
- }
574
- _inheritsLoose(MapDoc, _BaseChain);
575
- return MapDoc;
576
- }(BaseChain);
577
-
578
- var ReduceChain = /*#__PURE__*/function (_BaseChain) {
579
- function ReduceChain() {
580
- var _this;
581
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
582
- args[_key] = arguments[_key];
583
- }
584
- _this = _BaseChain.call.apply(_BaseChain, [this].concat(args)) || this;
585
- _this.name = 'Reduce';
586
- _this.description = 'Reduce a set of documents to binary answer.';
587
- _this.template = "\n You will be given facts and a question to answer with ONLY a yes or no.\n\n Do not include the facts in the answer.\n\n Say yes if the facts mentions all aspects of the question, else say no.\n\n facts: {facts} \n\n question: Does the facts mention {question} Say YES OR NO Only::";
588
- return _this;
589
- }
590
- _inheritsLoose(ReduceChain, _BaseChain);
591
- return ReduceChain;
592
- }(BaseChain);
593
-
594
499
  var log = /*#__PURE__*/new tslog.Logger();
595
500
  var LLMLoop = /*#__PURE__*/function (_BaseChain) {
596
501
  function LLMLoop() {
@@ -600,25 +505,21 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
600
505
  }
601
506
  _this = _BaseChain.call.apply(_BaseChain, [this].concat(args)) || this;
602
507
  // Implement the LLM loop logic here
603
- _this.string_expression = '';
604
- _this.name = 'LLMLoop';
605
- _this.description = 'Base LLM Loop.';
606
- _this.mapQuery = new MapQuery(_this.container, '', '');
607
- _this.mapDoc = new MapDoc(_this.container, '', '');
608
- _this.reduceChain = new ReduceChain(_this.container, '', '');
508
+ _this.stringExpression = '';
509
+ _this._mapQueryTemplate = "\n You are an assistant that can convert statements to a natural language query as in the example below.\n\n\n\n Statements: Visual foot exam in the last month. Visual foot exam showed active infection.\n query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?\n Statements: {expression}\n query: ";
510
+ _this._mapDocTemplate = "\n You will be given a document and few statements.\n\n Extract facts from the document that are relevant to the statements.\n\n Do not include any irrelevant information or context.\n\n\n\n Example:\n\n document: The patient is a 45-year-old male with a history of hypertension. He presented with chest pain and was diagnosed with myocardial infarction. He was treated with aspirin and beta-blockers.\n\n statements: diagnosis of myocardial infarction. currently on beta-blockers.\n\n facts: The patient was diagnosed with myocardial infarction and is currently on beta-blockers.\n\n document: {document} \n\n statements: {statements}\n\n facts: ";
511
+ _this._reduceChainTemplate = "\n Say yes if the facts mentions all aspects of the query, else say no.\n\n\n Example:\n facts: Patient had a laproscopy 27 days back. The findings were normal. \n\n query: Did the patient have a laproscopy this month?\n\n answer: YES. The patient had a laproscopy 27 days back.\n\n facts: The patient is a diabetic and hypertensive. He is on metformin and amlodipine. \n\n query: Is the patient on heparin and metformin?\n\n answer: NO. The patient is on heparin but not on metformin.\n\n facts: A visual foot examination was performed 26 days ago to assess skin integrity, circulation, and structural abnormalities. The exam revealed normal skin condition, nail health, circulation, and absence of edema and deformities. However, there were signs of an active infection with erythema and swelling. \n\n query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?\n\n answer: YES. The patient had a visual foot exam in the last month and there is evidence of active infection.\n\n facts: A visual foot examination was performed 126 days ago to assess skin integrity, circulation, and structural abnormalities. The exam revealed normal skin condition, nail health, circulation, and absence of edema and deformities. However, there were signs of an active infection with erythema and swelling. \n\n query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?\n\n answer: NO. The patient did not have a visual foot exam in the last month but there is evidence of active infection.\n\n facts: {facts} \n\n query: {query}\n\n answer: ";
609
512
  return _this;
610
513
  }
611
514
  _inheritsLoose(LLMLoop, _BaseChain);
612
515
  var _proto = LLMLoop.prototype;
613
516
  _proto.checkAssertion = /*#__PURE__*/function () {
614
- var _checkAssertion = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(expression, context) {
615
- var _expression, _context, _content, _input;
616
- return _regeneratorRuntime().wrap(function _callee$(_context2) {
617
- while (1) switch (_context2.prev = _context2.next) {
517
+ var _checkAssertion = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(expression, context) {
518
+ var _expression, _context, _content, _input, result;
519
+ return _regenerator().w(function (_context2) {
520
+ while (1) switch (_context2.n) {
618
521
  case 0:
619
522
  _expression = '';
620
- _context = undefined;
621
- _content = '';
622
523
  try {
623
524
  // Check if expression is a valid JSON string
624
525
  _expression = this.printValues(JSON.parse(expression));
@@ -626,26 +527,43 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
626
527
  _expression = expression; // If not, use the expression as is
627
528
  }
628
529
  log.info("Checking assertion with expression: " + _expression);
530
+ // Initialize context and content
531
+ _context = undefined;
532
+ _content = '';
533
+ try {
534
+ _context = JSON.parse(context);
535
+ } catch (error) {
536
+ _context = context; // If not, use the context as is
537
+ log.warn("Context is not a valid JSON string: " + context);
538
+ log.warn("Using context as a string: " + _context);
539
+ }
629
540
  try {
630
541
  // Check if context is a valid JSON string
631
542
  _context = JSON.parse(context);
632
543
  _context.forEach(function (element) {
633
- _content += atob(element.content[0].attachment.data.value);
544
+ element.content.forEach(function (contentItem) {
545
+ _content += atob(contentItem.attachment.data.value);
546
+ });
634
547
  });
635
548
  } catch (error) {
636
549
  _content = context; // If not, use the context as is
550
+ log.warn("Context is not a valid JSON : " + context);
551
+ log.warn("Using context as a string: " + _content);
637
552
  }
638
553
  _content = this.findDatesAndConvertToTimeElapsed(_content.replace(/(\r\n|\n|\r)/gm, ' '));
554
+ log.info("Checking assertion with context: " + _content);
639
555
  _input = {
640
556
  input: {
641
557
  expression: _expression,
642
558
  context: _content
643
559
  }
644
560
  };
645
- return _context2.abrupt("return", this.chain(_input));
646
- case 9:
647
- case "end":
648
- return _context2.stop();
561
+ _context2.n = 1;
562
+ return this.chain(_input);
563
+ case 1:
564
+ result = _context2.v;
565
+ log.info("Assertion check result: " + result);
566
+ return _context2.a(2, result);
649
567
  }
650
568
  }, _callee, this);
651
569
  }));
@@ -655,20 +573,17 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
655
573
  return checkAssertion;
656
574
  }();
657
575
  _proto.checkMention = /*#__PURE__*/function () {
658
- var _checkMention = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(expression, context) {
659
- return _regeneratorRuntime().wrap(function _callee2$(_context3) {
660
- while (1) switch (_context3.prev = _context3.next) {
576
+ var _checkMention = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(expression, context) {
577
+ return _regenerator().w(function (_context3) {
578
+ while (1) switch (_context3.n) {
661
579
  case 0:
662
580
  if (!(expression && context)) {
663
- _context3.next = 2;
581
+ _context3.n = 1;
664
582
  break;
665
583
  }
666
- return _context3.abrupt("return", true);
667
- case 2:
668
- return _context3.abrupt("return", false);
669
- case 3:
670
- case "end":
671
- return _context3.stop();
584
+ return _context3.a(2, true);
585
+ case 1:
586
+ return _context3.a(2, false);
672
587
  }
673
588
  }, _callee2);
674
589
  }));
@@ -678,20 +593,17 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
678
593
  return checkMention;
679
594
  }();
680
595
  _proto.checkNegation = /*#__PURE__*/function () {
681
- var _checkNegation = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(expression, context) {
682
- return _regeneratorRuntime().wrap(function _callee3$(_context4) {
683
- while (1) switch (_context4.prev = _context4.next) {
596
+ var _checkNegation = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(expression, context) {
597
+ return _regenerator().w(function (_context4) {
598
+ while (1) switch (_context4.n) {
684
599
  case 0:
685
600
  if (!(expression && context)) {
686
- _context4.next = 2;
601
+ _context4.n = 1;
687
602
  break;
688
603
  }
689
- return _context4.abrupt("return", true);
690
- case 2:
691
- return _context4.abrupt("return", false);
692
- case 3:
693
- case "end":
694
- return _context4.stop();
604
+ return _context4.a(2, true);
605
+ case 1:
606
+ return _context4.a(2, false);
695
607
  }
696
608
  }, _callee3);
697
609
  }));
@@ -719,17 +631,17 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
719
631
  };
720
632
  _proto.stringToBoolean = function stringToBoolean(str) {
721
633
  log.info("Converting string to boolean: " + str);
722
- var positiveKeywords = ['true', 'contains', 'confirms', 'yes', 'affirmative', 'matches', 'exists', 'present', 'absolutely', 'certainly', 'definitely', 'indeed'];
634
+ var positiveKeywords = ['true', 'contains', 'confirms', 'yes', 'affirmative', 'matches', 'exists', 'present', 'absolutely', 'certainly', 'definitely', 'indeed', 'did have', 'showed', 'revealed'];
723
635
  var lowerStr = str.toLowerCase();
724
636
  return positiveKeywords.some(function (keyword) {
725
637
  return lowerStr.includes(keyword);
726
638
  });
727
639
  };
728
640
  _proto.textSplitter = /*#__PURE__*/function () {
729
- var _textSplitter = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(text, chunkSize, chunkOverlap) {
641
+ var _textSplitter = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(text, chunkSize, chunkOverlap) {
730
642
  var textSplitter, texts;
731
- return _regeneratorRuntime().wrap(function _callee4$(_context5) {
732
- while (1) switch (_context5.prev = _context5.next) {
643
+ return _regenerator().w(function (_context5) {
644
+ while (1) switch (_context5.n) {
733
645
  case 0:
734
646
  if (chunkSize === void 0) {
735
647
  chunkSize = 3900;
@@ -741,17 +653,17 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
741
653
  chunkSize: chunkSize,
742
654
  chunkOverlap: chunkOverlap
743
655
  });
744
- _context5.next = 5;
656
+ _context5.n = 1;
745
657
  return textSplitter.splitText(text);
746
- case 5:
747
- texts = _context5.sent;
658
+ case 1:
659
+ texts = _context5.v;
748
660
  if (!(texts.length === 0)) {
749
- _context5.next = 9;
661
+ _context5.n = 2;
750
662
  break;
751
663
  }
752
664
  log.warn('No text chunks created. Please check the input text.');
753
- return _context5.abrupt("return", []);
754
- case 9:
665
+ return _context5.a(2, []);
666
+ case 2:
755
667
  if (texts.length > 100) {
756
668
  log.warn("Warning: More than 100 text chunks created (" + texts.length + "). This may affect performance.");
757
669
  }
@@ -759,10 +671,7 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
759
671
  log.warn("Warning: Chunk size is less than 1000 characters (" + chunkSize + "). This may lead to very small chunks.");
760
672
  }
761
673
  log.info("Text split into " + texts.length + " chunks with size " + chunkSize + " and overlap " + chunkOverlap);
762
- return _context5.abrupt("return", texts);
763
- case 13:
764
- case "end":
765
- return _context5.stop();
674
+ return _context5.a(2, texts);
766
675
  }
767
676
  }, _callee4);
768
677
  }));
@@ -772,50 +681,85 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
772
681
  return textSplitter;
773
682
  }();
774
683
  _proto.printValues = function printValues(obj) {
684
+ log.info("Processing Rule: " + obj.name + " for " + obj.context);
685
+ // For each obj.expression:arg:where
686
+ if (obj.expression && obj.expression.arg && obj.expression.arg.where) {
687
+ // for each args in obj.expression.arg.where, extract the nested key called "value"
688
+ var statements = this.extractObjectsByKey(obj.expression.arg.where, 'value');
689
+ return statements.join(' ').replace(/,/g, ' ');
690
+ }
691
+ // return statements array as a string seperated by space
692
+ return '';
693
+ };
694
+ _proto.extractObjectsByKey = function extractObjectsByKey(obj, key) {
695
+ var results = [];
696
+ function traverse(obj) {
697
+ for (var currentKey in obj) {
698
+ if (obj.hasOwnProperty(currentKey)) {
699
+ if (currentKey === key) {
700
+ results.push(obj[currentKey]);
701
+ }
702
+ if (typeof obj[currentKey] === 'object' && obj[currentKey] !== null) {
703
+ traverse(obj[currentKey]);
704
+ }
705
+ }
706
+ }
707
+ }
708
+ traverse(obj);
709
+ return results;
710
+ };
711
+ _proto._printValues = function _printValues(obj) {
775
712
  var _this2 = this;
776
713
  for (var key in obj) {
777
714
  if (typeof obj[key] === 'object') {
778
- this.printValues(obj[key]);
715
+ this._printValues(obj[key]);
779
716
  } else {
780
- this.string_expression += obj[key] + ' ';
717
+ this.stringExpression += obj[key] + ' ';
781
718
  }
782
719
  }
783
720
  var _eliminate = ['true', 'false', 'null', 'undefined', 'String', 'Number', 'Object', 'Array', 'Boolean', 'value', 'Type', 'Specifier', 'Named'];
784
721
  _eliminate.forEach(function (element) {
785
722
  var regex = new RegExp(element, 'g');
786
- _this2.string_expression = _this2.string_expression.replace(regex, '');
723
+ _this2.stringExpression = _this2.stringExpression.replace(regex, '');
787
724
  });
788
- this.string_expression = this.string_expression.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');
789
- return this.camelToString(this.string_expression);
725
+ this.stringExpression = this.stringExpression.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');
726
+ return this.camelToString(this.stringExpression);
790
727
  };
791
728
  _proto.chain = /*#__PURE__*/function () {
792
- var _chain = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(input) {
729
+ var _chain = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(input) {
793
730
  var _this3 = this;
794
- var _yield$import, RunnableSequence, RunnableParallel, RunnablePassthrough, mapQueryChain, mapDocChain, parallelChain, sequence;
795
- return _regeneratorRuntime().wrap(function _callee8$(_context9) {
796
- while (1) switch (_context9.prev = _context9.next) {
731
+ var _yield$import, RunnableSequence, RunnableParallel, RunnablePassthrough, mapQuery, mapDoc, reduceChain, mapQueryChain, mapDocChain, parallelChain, sequence;
732
+ return _regenerator().w(function (_context9) {
733
+ while (1) switch (_context9.n) {
797
734
  case 0:
798
- _context9.next = 2;
735
+ _context9.n = 1;
799
736
  return import('@langchain/core/runnables');
800
- case 2:
801
- _yield$import = _context9.sent;
737
+ case 1:
738
+ _yield$import = _context9.v;
802
739
  RunnableSequence = _yield$import.RunnableSequence;
803
740
  RunnableParallel = _yield$import.RunnableParallel;
804
741
  RunnablePassthrough = _yield$import.RunnablePassthrough;
742
+ mapQuery = new BaseChain(this.container);
743
+ mapDoc = new BaseChain(this.container);
744
+ reduceChain = new BaseChain(this.container);
745
+ mapQuery.name = 'MapQuery';
746
+ mapQuery.description = 'Map the expression to a natural language query.';
747
+ mapQuery.template = this._mapQueryTemplate;
748
+ mapDoc.name = 'MapDoc';
749
+ mapDoc.description = 'Map the document to a set of facts.';
750
+ mapDoc.template = this._mapDocTemplate;
751
+ reduceChain.name = 'ReduceChain';
752
+ reduceChain.description = 'Reduce a set of documents to binary answer.';
753
+ reduceChain.template = this._reduceChainTemplate;
805
754
  mapQueryChain = RunnablePassthrough.assign({
806
755
  query: function () {
807
- var _query = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(input) {
808
- return _regeneratorRuntime().wrap(function _callee5$(_context6) {
809
- while (1) switch (_context6.prev = _context6.next) {
756
+ var _query = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(input) {
757
+ return _regenerator().w(function (_context6) {
758
+ while (1) switch (_context6.n) {
810
759
  case 0:
811
- return _context6.abrupt("return", _this3.mapQuery.chain({
812
- input: {
813
- expression: input.input.expression
814
- }
760
+ return _context6.a(2, mapQuery.chain({
761
+ expression: input.input.expression
815
762
  }));
816
- case 1:
817
- case "end":
818
- return _context6.stop();
819
763
  }
820
764
  }, _callee5);
821
765
  }));
@@ -827,26 +771,21 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
827
771
  });
828
772
  mapDocChain = RunnablePassthrough.assign({
829
773
  documents: function () {
830
- var _documents = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(input) {
774
+ var _documents = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(input) {
831
775
  var textChunks;
832
- return _regeneratorRuntime().wrap(function _callee6$(_context7) {
833
- while (1) switch (_context7.prev = _context7.next) {
776
+ return _regenerator().w(function (_context7) {
777
+ while (1) switch (_context7.n) {
834
778
  case 0:
835
- _context7.next = 2;
779
+ _context7.n = 1;
836
780
  return _this3.textSplitter(input.input.context);
837
- case 2:
838
- textChunks = _context7.sent;
839
- return _context7.abrupt("return", Promise.all(textChunks.map(function (chunk) {
840
- return _this3.mapDoc.chain({
841
- input: {
842
- document: chunk,
843
- question: input.input.expression
844
- }
781
+ case 1:
782
+ textChunks = _context7.v;
783
+ return _context7.a(2, Promise.all(textChunks.map(function (chunk) {
784
+ return mapDoc.chain({
785
+ document: chunk,
786
+ statements: input.input.expression
845
787
  });
846
788
  })));
847
- case 4:
848
- case "end":
849
- return _context7.stop();
850
789
  }
851
790
  }, _callee6);
852
791
  }));
@@ -858,23 +797,39 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
858
797
  });
859
798
  parallelChain = RunnableParallel.from([mapQueryChain, mapDocChain]);
860
799
  sequence = RunnableSequence.from([parallelChain, (/*#__PURE__*/function () {
861
- var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(results) {
862
- var query, documents;
863
- return _regeneratorRuntime().wrap(function _callee7$(_context8) {
864
- while (1) switch (_context8.prev = _context8.next) {
800
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(results) {
801
+ var expression, query, documents;
802
+ return _regenerator().w(function (_context8) {
803
+ while (1) switch (_context8.n) {
865
804
  case 0:
866
- // results is an array: [{ query: ... }, { documents: [...] }]
805
+ /* results is an array: [{ query: ... }, { documents: [...] }]
806
+ {
807
+ "0": {
808
+ "input": {
809
+ "expression": "0 Visual foot exam showed active infection?",
810
+ "context": "Procedure: The patient underwent a visual foot examination on 26 days ago. to assess skin integrity, circulation, and structural abnormalities. Findings: - Skin Condition: No signs of dryness, cracking, or ulceration were observed. No abnormal lesions or discoloration present. - Nail Health: Toenails appeared intact with no evidence of fungal infection or ingrown nails. - Circulation: Normal coloration, with no signs of cyanosis or pallor. Capillary refill time within normal limits. - Edema: No visible swelling in the feet or ankles. - Deformities: No structural abnormalities such as bunions, hammertoes, or Charcot foot deformities noted. - Infections: Showed signs of active infection with erythema and swelling. - Sensation: No visible signs of neuropathy, though further testing may be required to assess sensory deficits. "
811
+ },
812
+ "query": "The text indicates a question about whether a visual foot exam revealed an active infection.\n"
813
+ },
814
+ "1": {
815
+ "input": {
816
+ "expression": "0 Visual foot exam showed active infection?",
817
+ "context": "Procedure: The patient underwent a visual foot examination on 26 days ago. to assess skin integrity, circulation, and structural abnormalities. Findings: - Skin Condition: No signs of dryness, cracking, or ulceration were observed. No abnormal lesions or discoloration present. - Nail Health: Toenails appeared intact with no evidence of fungal infection or ingrown nails. - Circulation: Normal coloration, with no signs of cyanosis or pallor. Capillary refill time within normal limits. - Edema: No visible swelling in the feet or ankles. - Deformities: No structural abnormalities such as bunions, hammertoes, or Charcot foot deformities noted. - Infections: Showed signs of active infection with erythema and swelling. - Sensation: No visible signs of neuropathy, though further testing may be required to assess sensory deficits. "
818
+ },
819
+ "documents": [
820
+ "26 days ago, a visual foot examination revealed no dryness, cracking, ulceration, lesions, discoloration, fungal infection, ingrown nails, cyanosis, pallor, edema, bunions, hammertoes, or Charcot foot deformities. Circulation and capillary refill were normal. However, the exam showed signs"
821
+ ]
822
+ }
823
+ }
824
+ */
825
+ log.info("Results from parallel chain: " + JSON.stringify(results));
826
+ expression = results[0].input.expression;
867
827
  query = results[0].query;
868
828
  documents = results[1].documents;
869
- return _context8.abrupt("return", _this3.reduceChain.chain({
870
- input: {
871
- query: query,
872
- facts: documents
873
- }
829
+ return _context8.a(2, reduceChain.chain({
830
+ facts: documents,
831
+ query: query + ' ' + expression
874
832
  }));
875
- case 3:
876
- case "end":
877
- return _context8.stop();
878
833
  }
879
834
  }, _callee7);
880
835
  }));
@@ -884,19 +839,42 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
884
839
  }()), function (result) {
885
840
  return _this3.stringToBoolean(result);
886
841
  }]);
887
- return _context9.abrupt("return", sequence.invoke(input));
888
- case 11:
889
- case "end":
890
- return _context9.stop();
842
+ return _context9.a(2, sequence.invoke(input));
891
843
  }
892
- }, _callee8);
844
+ }, _callee8, this);
893
845
  }));
894
846
  function chain(_x0) {
895
847
  return _chain.apply(this, arguments);
896
848
  }
897
849
  return chain;
898
850
  }();
899
- return LLMLoop;
851
+ return _createClass(LLMLoop, [{
852
+ key: "mapQueryTemplate",
853
+ get:
854
+ // Add getters and setters for the templates if needed
855
+ function get() {
856
+ return this._mapQueryTemplate;
857
+ },
858
+ set: function set(template) {
859
+ this._mapQueryTemplate = template;
860
+ }
861
+ }, {
862
+ key: "mapDocTemplate",
863
+ get: function get() {
864
+ return this._mapDocTemplate;
865
+ },
866
+ set: function set(template) {
867
+ this._mapDocTemplate = template;
868
+ }
869
+ }, {
870
+ key: "reduceChainTemplate",
871
+ get: function get() {
872
+ return this._reduceChainTemplate;
873
+ },
874
+ set: function set(template) {
875
+ this._reduceChainTemplate = template;
876
+ }
877
+ }]);
900
878
  }(BaseChain);
901
879
 
902
880
  exports.BaseAgent = BaseAgent;