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