medpromptjs 0.4.4 → 0.5.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/README.md +40 -0
- package/dist/agent.d.ts +6 -21
- package/dist/chain.d.ts +2 -2
- package/dist/embedding.d.ts +53 -0
- package/dist/index.d.ts +4 -0
- package/dist/llm.d.ts +91 -0
- package/dist/medpromptjs.cjs.development.js +629 -772
- 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 +630 -775
- package/dist/medpromptjs.esm.js.map +1 -1
- package/package.json +5 -5
package/dist/medpromptjs.esm.js
CHANGED
|
@@ -1,177 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ChatPromptTemplate, PromptTemplate } from '@langchain/core/prompts';
|
|
2
2
|
import { RunnableSequence, RunnablePassthrough } from '@langchain/core/runnables';
|
|
3
3
|
import { AIMessage } from '@langchain/core/messages';
|
|
4
|
-
import {
|
|
4
|
+
import { StringOutputParser } from '@langchain/core/output_parsers';
|
|
5
|
+
import { createStructuredChatAgent, AgentExecutor } from 'langchain/agents';
|
|
5
6
|
import { DynamicStructuredTool } from 'langchain/tools';
|
|
6
7
|
import { CharacterTextSplitter } from '@langchain/textsplitters';
|
|
7
8
|
import { Logger } from 'tslog';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
try {
|
|
11
|
-
var i = n[a](c),
|
|
12
|
-
u = i.value;
|
|
13
|
-
} catch (n) {
|
|
14
|
-
return void e(n);
|
|
15
|
-
}
|
|
16
|
-
i.done ? t(u) : Promise.resolve(u).then(r, o);
|
|
17
|
-
}
|
|
18
|
-
function _asyncToGenerator(n) {
|
|
19
|
-
return function () {
|
|
20
|
-
var t = this,
|
|
21
|
-
e = arguments;
|
|
22
|
-
return new Promise(function (r, o) {
|
|
23
|
-
var a = n.apply(t, e);
|
|
24
|
-
function _next(n) {
|
|
25
|
-
asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
|
|
26
|
-
}
|
|
27
|
-
function _throw(n) {
|
|
28
|
-
asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
|
|
29
|
-
}
|
|
30
|
-
_next(void 0);
|
|
31
|
-
});
|
|
32
|
-
};
|
|
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
|
-
}
|
|
45
|
-
function _inheritsLoose(t, o) {
|
|
46
|
-
t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o);
|
|
47
|
-
}
|
|
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;
|
|
73
|
-
}
|
|
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) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = 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;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return {
|
|
106
|
-
value: t,
|
|
107
|
-
done: y
|
|
108
|
-
};
|
|
109
|
-
};
|
|
110
|
-
}(r, o, i), !0), u;
|
|
111
|
-
}
|
|
112
|
-
var a = {};
|
|
113
|
-
function Generator() {}
|
|
114
|
-
function GeneratorFunction() {}
|
|
115
|
-
function GeneratorFunctionPrototype() {}
|
|
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;
|
|
123
|
-
}
|
|
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 () {
|
|
125
|
-
return this;
|
|
126
|
-
}), _regeneratorDefine(u, "toString", function () {
|
|
127
|
-
return "[object Generator]";
|
|
128
|
-
}), (_regenerator = function () {
|
|
129
|
-
return {
|
|
130
|
-
w: i,
|
|
131
|
-
m: f
|
|
132
|
-
};
|
|
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
|
-
function o(r, n) {
|
|
144
|
-
_regeneratorDefine(e, r, function (e) {
|
|
145
|
-
return this._invoke(r, n, e);
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
r ? i ? i(e, r, {
|
|
149
|
-
value: n,
|
|
150
|
-
enumerable: !t,
|
|
151
|
-
configurable: !t,
|
|
152
|
-
writable: !t
|
|
153
|
-
}) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2));
|
|
154
|
-
}, _regeneratorDefine(e, r, n, t);
|
|
155
|
-
}
|
|
156
|
-
function _setPrototypeOf(t, e) {
|
|
157
|
-
return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
|
|
158
|
-
return t.__proto__ = e, t;
|
|
159
|
-
}, _setPrototypeOf(t, e);
|
|
160
|
-
}
|
|
161
|
-
function _toPrimitive(t, r) {
|
|
162
|
-
if ("object" != typeof t || !t) return t;
|
|
163
|
-
var e = t[Symbol.toPrimitive];
|
|
164
|
-
if (void 0 !== e) {
|
|
165
|
-
var i = e.call(t, r || "default");
|
|
166
|
-
if ("object" != typeof i) return i;
|
|
167
|
-
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
168
|
-
}
|
|
169
|
-
return ("string" === r ? String : Number)(t);
|
|
170
|
-
}
|
|
171
|
-
function _toPropertyKey(t) {
|
|
172
|
-
var i = _toPrimitive(t, "string");
|
|
173
|
-
return "symbol" == typeof i ? i : i + "";
|
|
174
|
-
}
|
|
9
|
+
import { LLM } from '@langchain/core/language_models/llms';
|
|
10
|
+
import { Embeddings } from '@langchain/core/embeddings';
|
|
175
11
|
|
|
176
12
|
/**
|
|
177
13
|
* Copyright 2024 Bell Eapen
|
|
@@ -188,16 +24,36 @@ function _toPropertyKey(t) {
|
|
|
188
24
|
* See the License for the specific language governing permissions and
|
|
189
25
|
* limitations under the License.
|
|
190
26
|
*/
|
|
191
|
-
|
|
27
|
+
const mydi = (c, m, b) => {
|
|
192
28
|
try {
|
|
193
29
|
return c.resolve(m + '_' + b);
|
|
194
|
-
} catch (e) {
|
|
30
|
+
} catch (e) {}
|
|
31
|
+
try {
|
|
32
|
+
return c.resolve(m + '-' + b);
|
|
33
|
+
} catch (e) {}
|
|
34
|
+
try {
|
|
195
35
|
return c.resolve(b);
|
|
196
|
-
}
|
|
36
|
+
} catch (e) {}
|
|
37
|
+
return null;
|
|
197
38
|
};
|
|
198
39
|
|
|
199
|
-
|
|
200
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Copyright 2024 Bell Eapen
|
|
42
|
+
*
|
|
43
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
44
|
+
* you may not use this file except in compliance with the License.
|
|
45
|
+
* You may obtain a copy of the License at
|
|
46
|
+
*
|
|
47
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
48
|
+
*
|
|
49
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
50
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
51
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
52
|
+
* See the License for the specific language governing permissions and
|
|
53
|
+
* limitations under the License.
|
|
54
|
+
*/
|
|
55
|
+
class BaseChain {
|
|
56
|
+
constructor(container) {
|
|
201
57
|
this.container = void 0;
|
|
202
58
|
this._name = '';
|
|
203
59
|
this._description = '';
|
|
@@ -207,283 +63,165 @@ var BaseChain = /*#__PURE__*/function () {
|
|
|
207
63
|
this.chat_model = void 0;
|
|
208
64
|
this.runnable = void 0;
|
|
209
65
|
this.container = container;
|
|
210
|
-
this.
|
|
211
|
-
this.
|
|
212
|
-
this.
|
|
66
|
+
this._name = this.camelize(this.constructor.name);
|
|
67
|
+
this._description = this.snake_case(this.constructor.name);
|
|
68
|
+
this.llm = this.resolve('llm');
|
|
69
|
+
this._template = this.resolve('template');
|
|
70
|
+
this.chat_model = this.resolve('chat-model', false);
|
|
71
|
+
if (this.chat_model) {
|
|
72
|
+
this.prompt = ChatPromptTemplate.fromTemplate(this._template);
|
|
73
|
+
} else {
|
|
74
|
+
this.prompt = PromptTemplate.fromTemplate(this._template);
|
|
75
|
+
}
|
|
213
76
|
}
|
|
214
77
|
// Getters and setters
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
78
|
+
get name() {
|
|
79
|
+
return this._name;
|
|
80
|
+
}
|
|
81
|
+
set name(value) {
|
|
82
|
+
this._name = value === '' ? this.camelize(this.constructor.name) : value;
|
|
83
|
+
}
|
|
84
|
+
get description() {
|
|
85
|
+
return this._description;
|
|
86
|
+
}
|
|
87
|
+
set description(value) {
|
|
88
|
+
this._description = value === '' ? this.snake_case(this.constructor.name) : value;
|
|
89
|
+
}
|
|
90
|
+
get template() {
|
|
91
|
+
return this._template;
|
|
92
|
+
}
|
|
93
|
+
set template(value) {
|
|
94
|
+
this._template = value;
|
|
227
95
|
if (this.chat_model) {
|
|
228
96
|
this.prompt = ChatPromptTemplate.fromTemplate(this._template);
|
|
229
97
|
} else {
|
|
230
98
|
this.prompt = PromptTemplate.fromTemplate(this._template);
|
|
231
99
|
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return mydi(this.container, this.
|
|
235
|
-
}
|
|
236
|
-
|
|
100
|
+
}
|
|
101
|
+
resolve(name, defaultValue = null) {
|
|
102
|
+
return mydi(this.container, this.camelize(this.constructor.name), name) || defaultValue;
|
|
103
|
+
}
|
|
104
|
+
camelize(str) {
|
|
237
105
|
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
238
106
|
return index === 0 ? word.toLowerCase() : word.toUpperCase();
|
|
239
107
|
}).replace(/\s+/g, '');
|
|
240
|
-
}
|
|
241
|
-
|
|
108
|
+
}
|
|
109
|
+
snake_case(str) {
|
|
242
110
|
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
243
111
|
return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();
|
|
244
112
|
}).replace(/\s+/g, '');
|
|
245
113
|
}
|
|
246
114
|
// https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/
|
|
247
115
|
//! Override this method
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if (!this.chat_model) {
|
|
259
|
-
_context.n = 2;
|
|
260
|
-
break;
|
|
261
|
-
}
|
|
262
|
-
_context.n = 1;
|
|
263
|
-
return this.runnable.invoke(input);
|
|
264
|
-
case 1:
|
|
265
|
-
response = _context.v;
|
|
266
|
-
aiMessage = new AIMessage(response);
|
|
267
|
-
return _context.a(2, aiMessage.content);
|
|
268
|
-
case 2:
|
|
269
|
-
_context.n = 3;
|
|
270
|
-
return this.runnable.invoke(input);
|
|
271
|
-
case 3:
|
|
272
|
-
return _context.a(2, _context.v);
|
|
273
|
-
}
|
|
274
|
-
}, _callee, this);
|
|
275
|
-
}));
|
|
276
|
-
function chain(_x) {
|
|
277
|
-
return _chain.apply(this, arguments);
|
|
278
|
-
}
|
|
279
|
-
return chain;
|
|
280
|
-
}();
|
|
281
|
-
return _createClass(BaseChain, [{
|
|
282
|
-
key: "name",
|
|
283
|
-
get: function get() {
|
|
284
|
-
return this._name;
|
|
285
|
-
},
|
|
286
|
-
set: function set(value) {
|
|
287
|
-
this._name = value === '' ? this.camelize(this.constructor.name) : value;
|
|
288
|
-
}
|
|
289
|
-
}, {
|
|
290
|
-
key: "description",
|
|
291
|
-
get: function get() {
|
|
292
|
-
return this._description;
|
|
293
|
-
},
|
|
294
|
-
set: function set(value) {
|
|
295
|
-
this._description = value === '' ? this.snake_case(this.constructor.name) : value;
|
|
296
|
-
}
|
|
297
|
-
}, {
|
|
298
|
-
key: "template",
|
|
299
|
-
get: function get() {
|
|
300
|
-
return this._template;
|
|
301
|
-
},
|
|
302
|
-
set: function set(value) {
|
|
303
|
-
this._template = value;
|
|
304
|
-
try {
|
|
305
|
-
this.chat_model = this.resolve('chat_model');
|
|
306
|
-
} catch (e) {}
|
|
307
|
-
if (this.chat_model) {
|
|
308
|
-
this.prompt = ChatPromptTemplate.fromTemplate(this._template);
|
|
309
|
-
} else {
|
|
310
|
-
this.prompt = PromptTemplate.fromTemplate(this._template);
|
|
311
|
-
}
|
|
116
|
+
async chain(input) {
|
|
117
|
+
const outputParser = new StringOutputParser();
|
|
118
|
+
this.runnable = RunnableSequence.from([{
|
|
119
|
+
input: new RunnablePassthrough()
|
|
120
|
+
// context: async () => loadContextFromStore(),
|
|
121
|
+
}, this.prompt, this.llm, outputParser]);
|
|
122
|
+
if (this.chat_model) {
|
|
123
|
+
const response = await this.runnable.invoke(input);
|
|
124
|
+
const aiMessage = new AIMessage(response);
|
|
125
|
+
return aiMessage.content;
|
|
312
126
|
}
|
|
313
|
-
|
|
314
|
-
}
|
|
127
|
+
return await this.runnable.invoke(input);
|
|
128
|
+
}
|
|
129
|
+
async invoke(input) {
|
|
130
|
+
return this.chain(input);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
315
133
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Copyright 2024 Bell Eapen
|
|
136
|
+
*
|
|
137
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
138
|
+
* you may not use this file except in compliance with the License.
|
|
139
|
+
* You may obtain a copy of the License at
|
|
140
|
+
*
|
|
141
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
142
|
+
*
|
|
143
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
144
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
145
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
146
|
+
* See the License for the specific language governing permissions and
|
|
147
|
+
* limitations under the License.
|
|
148
|
+
*/
|
|
149
|
+
class BaseAgent extends BaseChain {
|
|
150
|
+
constructor(...args) {
|
|
151
|
+
super(...args);
|
|
322
152
|
this._tools = [];
|
|
323
|
-
this.prompt = void 0;
|
|
324
|
-
this.llm = void 0;
|
|
325
|
-
this.chat_model = void 0;
|
|
326
|
-
this.runnable = void 0;
|
|
327
|
-
this.container = container;
|
|
328
|
-
this.llm = this.resolve('main-llm');
|
|
329
|
-
this.chat_model = false;
|
|
330
|
-
this.initialize();
|
|
331
153
|
}
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
}).replace(/\s+/g, '');
|
|
358
|
-
};
|
|
359
|
-
_proto.snake_case = function snake_case(str) {
|
|
360
|
-
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
361
|
-
return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();
|
|
362
|
-
}).replace(/\s+/g, '');
|
|
363
|
-
};
|
|
364
|
-
_proto.run = /*#__PURE__*/function () {
|
|
365
|
-
var _run = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(input) {
|
|
366
|
-
var agent, agentExecutor;
|
|
367
|
-
return _regenerator().w(function (_context) {
|
|
368
|
-
while (1) switch (_context.n) {
|
|
369
|
-
case 0:
|
|
370
|
-
_context.n = 1;
|
|
371
|
-
return createStructuredChatAgent({
|
|
372
|
-
llm: this.llm,
|
|
373
|
-
tools: this._tools,
|
|
374
|
-
prompt: this.prompt
|
|
375
|
-
});
|
|
376
|
-
case 1:
|
|
377
|
-
agent = _context.v;
|
|
378
|
-
agentExecutor = new AgentExecutor({
|
|
379
|
-
agent: agent,
|
|
380
|
-
tools: this.tools,
|
|
381
|
-
// verbose: true,
|
|
382
|
-
handleParsingErrors: 'Please try again, paying close attention to the final answer'
|
|
383
|
-
});
|
|
384
|
-
_context.n = 2;
|
|
385
|
-
return agentExecutor.invoke(input);
|
|
386
|
-
case 2:
|
|
387
|
-
return _context.a(2, _context.v);
|
|
388
|
-
}
|
|
389
|
-
}, _callee, this);
|
|
390
|
-
}));
|
|
391
|
-
function run(_x) {
|
|
392
|
-
return _run.apply(this, arguments);
|
|
393
|
-
}
|
|
394
|
-
return run;
|
|
395
|
-
}();
|
|
396
|
-
return _createClass(BaseAgent, [{
|
|
397
|
-
key: "name",
|
|
398
|
-
get: function get() {
|
|
399
|
-
return this._name;
|
|
400
|
-
},
|
|
401
|
-
set: function set(value) {
|
|
402
|
-
this._name = value === '' ? this.camelize(this.constructor.name) : value;
|
|
403
|
-
}
|
|
404
|
-
}, {
|
|
405
|
-
key: "description",
|
|
406
|
-
get: function get() {
|
|
407
|
-
return this._description;
|
|
408
|
-
},
|
|
409
|
-
set: function set(value) {
|
|
410
|
-
this._description = value === '' ? this.snake_case(this.constructor.name) : value;
|
|
411
|
-
}
|
|
412
|
-
}, {
|
|
413
|
-
key: "template",
|
|
414
|
-
get: function get() {
|
|
415
|
-
return this._template;
|
|
416
|
-
},
|
|
417
|
-
set: function set(value) {
|
|
418
|
-
this._template = value;
|
|
419
|
-
try {
|
|
420
|
-
this.chat_model = this.resolve('chat_model');
|
|
421
|
-
} catch (e) {}
|
|
422
|
-
if (this.chat_model) {
|
|
423
|
-
this.prompt = ChatPromptTemplate.fromTemplate(this._template);
|
|
424
|
-
} else {
|
|
425
|
-
this.prompt = PromptTemplate.fromTemplate(this._template);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
// Getters and setters for tools
|
|
429
|
-
}, {
|
|
430
|
-
key: "tools",
|
|
431
|
-
get: function get() {
|
|
432
|
-
return this._tools;
|
|
433
|
-
},
|
|
434
|
-
set: function set(value) {
|
|
435
|
-
this._tools = value;
|
|
436
|
-
}
|
|
437
|
-
}]);
|
|
438
|
-
}();
|
|
154
|
+
get tools() {
|
|
155
|
+
return this._tools;
|
|
156
|
+
}
|
|
157
|
+
set tools(value) {
|
|
158
|
+
this._tools = value;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Run the agent using the provided input.
|
|
162
|
+
* This uses createStructuredChatAgent and AgentExecutor from langchain/agents.
|
|
163
|
+
*/
|
|
164
|
+
async run(input) {
|
|
165
|
+
const agent = await createStructuredChatAgent({
|
|
166
|
+
llm: this.llm,
|
|
167
|
+
tools: this._tools,
|
|
168
|
+
prompt: this.prompt
|
|
169
|
+
});
|
|
170
|
+
const agentExecutor = new AgentExecutor({
|
|
171
|
+
agent: agent,
|
|
172
|
+
tools: this.tools,
|
|
173
|
+
// verbose: true,
|
|
174
|
+
handleParsingErrors: 'Please try again, paying close attention to the final answer'
|
|
175
|
+
});
|
|
176
|
+
return await agentExecutor.invoke(input);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
439
179
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Copyright 2024 Bell Eapen
|
|
182
|
+
*
|
|
183
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
184
|
+
* you may not use this file except in compliance with the License.
|
|
185
|
+
* You may obtain a copy of the License at
|
|
186
|
+
*
|
|
187
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
188
|
+
*
|
|
189
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
190
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
191
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
192
|
+
* See the License for the specific language governing permissions and
|
|
193
|
+
* limitations under the License.
|
|
194
|
+
*/
|
|
195
|
+
class BaseTool extends DynamicStructuredTool {
|
|
196
|
+
constructor(container, name, description, schema, func = null) {
|
|
197
|
+
super({
|
|
447
198
|
name: name,
|
|
448
199
|
description: description,
|
|
449
200
|
schema: schema,
|
|
450
201
|
func: func
|
|
451
|
-
})
|
|
202
|
+
});
|
|
452
203
|
/** @hidden */
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
return function (_x) {
|
|
467
|
-
return _ref.apply(this, arguments);
|
|
468
|
-
};
|
|
469
|
-
}();
|
|
470
|
-
_this.container = container;
|
|
471
|
-
_this.name = name === '' ? _this.camelize(_this.constructor.name) : name;
|
|
472
|
-
_this.description = description === '' ? _this.snake_case(_this.constructor.name) : description;
|
|
473
|
-
_this.schema = schema;
|
|
474
|
-
return _this;
|
|
475
|
-
}
|
|
476
|
-
_inheritsLoose(BaseTool, _DynamicStructuredToo);
|
|
477
|
-
var _proto = BaseTool.prototype;
|
|
478
|
-
_proto.resolve = function resolve(name) {
|
|
204
|
+
this.container = void 0;
|
|
205
|
+
this.name = void 0;
|
|
206
|
+
this.description = void 0;
|
|
207
|
+
this.schema = void 0;
|
|
208
|
+
this.func = async args => {
|
|
209
|
+
return 'override this';
|
|
210
|
+
};
|
|
211
|
+
this.container = container;
|
|
212
|
+
this.name = name === '' ? this.camelize(this.constructor.name) : name;
|
|
213
|
+
this.description = description === '' ? this.snake_case(this.constructor.name) : description;
|
|
214
|
+
this.schema = schema;
|
|
215
|
+
}
|
|
216
|
+
resolve(name) {
|
|
479
217
|
return mydi(this.container, this.name, name);
|
|
480
|
-
}
|
|
481
|
-
|
|
218
|
+
}
|
|
219
|
+
camelize(str) {
|
|
482
220
|
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
483
221
|
return index === 0 ? word.toLowerCase() : word.toUpperCase();
|
|
484
222
|
}).replace(/\s+/g, '');
|
|
485
|
-
}
|
|
486
|
-
|
|
223
|
+
}
|
|
224
|
+
snake_case(str) {
|
|
487
225
|
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
488
226
|
return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();
|
|
489
227
|
}).replace(/\s+/g, '');
|
|
@@ -492,225 +230,189 @@ var BaseTool = /*#__PURE__*/function (_DynamicStructuredToo) {
|
|
|
492
230
|
* @param input - The input to process in batch.
|
|
493
231
|
* @param config - The configuration for the batch.
|
|
494
232
|
* @returns A promise resolving to an array of results.
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
while (1) switch (_context2.n) {
|
|
502
|
-
case 0:
|
|
503
|
-
return _context2.a(2, _DynamicStructuredToo.prototype._batchWithConfig.call(this, input, config));
|
|
504
|
-
}
|
|
505
|
-
}, _callee2, this);
|
|
506
|
-
}));
|
|
507
|
-
function _batchWithConfig(_x2, _x3) {
|
|
508
|
-
return _batchWithConfig2.apply(this, arguments);
|
|
509
|
-
}
|
|
510
|
-
return _batchWithConfig;
|
|
511
|
-
}();
|
|
512
|
-
return BaseTool;
|
|
513
|
-
}(DynamicStructuredTool);
|
|
233
|
+
*/
|
|
234
|
+
async _batchWithConfig(input, config) {
|
|
235
|
+
// Call the parent implementation or provide your own
|
|
236
|
+
return super._batchWithConfig(input, config);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
514
239
|
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
520
|
-
args[_key] = arguments[_key];
|
|
521
|
-
}
|
|
522
|
-
_this = _BaseChain.call.apply(_BaseChain, [this].concat(args)) || this;
|
|
240
|
+
const log = /*#__PURE__*/new Logger();
|
|
241
|
+
class LLMLoop extends BaseChain {
|
|
242
|
+
constructor(...args) {
|
|
243
|
+
super(...args);
|
|
523
244
|
// Implement the LLM loop logic here
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
245
|
+
this.stringExpression = '';
|
|
246
|
+
this._mapQueryTemplate = `
|
|
247
|
+
You are an assistant that can convert statements to a natural language query as in the example below.\n
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
Statements: Visual foot exam in the last month. Visual foot exam showed active infection.
|
|
251
|
+
query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?
|
|
252
|
+
Statements: {expression}
|
|
253
|
+
query: `;
|
|
254
|
+
this._mapDocTemplate = `
|
|
255
|
+
You will be given a document and few statements.\n
|
|
256
|
+
Extract facts from the document that are relevant to the statements.\n
|
|
257
|
+
Do not include any irrelevant information or context.\n
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
Example:\n
|
|
261
|
+
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
|
|
262
|
+
statements: diagnosis of myocardial infarction. currently on beta-blockers.\n
|
|
263
|
+
facts: The patient was diagnosed with myocardial infarction and is currently on beta-blockers.\n
|
|
264
|
+
document: {document} \n
|
|
265
|
+
statements: {statements}\n
|
|
266
|
+
facts: `;
|
|
267
|
+
this._reduceChainTemplate = `
|
|
268
|
+
Say yes if the facts mentions all aspects of the query, else say no.\n
|
|
269
|
+
|
|
270
|
+
Example:
|
|
271
|
+
facts: Patient had a laproscopy 27 days back. The findings were normal. \n
|
|
272
|
+
query: Did the patient have a laproscopy this month?\n
|
|
273
|
+
answer: YES. The patient had a laproscopy 27 days back.\n
|
|
274
|
+
facts: The patient is a diabetic and hypertensive. He is on metformin and amlodipine. \n
|
|
275
|
+
query: Is the patient on heparin and metformin?\n
|
|
276
|
+
answer: NO. The patient is on heparin but not on metformin.\n
|
|
277
|
+
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
|
|
278
|
+
query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?\n
|
|
279
|
+
answer: YES. The patient had a visual foot exam in the last month and there is evidence of active infection.\n
|
|
280
|
+
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
|
|
281
|
+
query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?\n
|
|
282
|
+
answer: NO. The patient did not have a visual foot exam in the last month but there is evidence of active infection.\n
|
|
283
|
+
facts: {facts} \n
|
|
284
|
+
query: {query}\n
|
|
285
|
+
answer: `;
|
|
286
|
+
}
|
|
287
|
+
// Add getters and setters for the templates if needed
|
|
288
|
+
get mapQueryTemplate() {
|
|
289
|
+
return this._mapQueryTemplate;
|
|
290
|
+
}
|
|
291
|
+
set mapQueryTemplate(template) {
|
|
292
|
+
this._mapQueryTemplate = template;
|
|
293
|
+
}
|
|
294
|
+
get mapDocTemplate() {
|
|
295
|
+
return this._mapDocTemplate;
|
|
296
|
+
}
|
|
297
|
+
set mapDocTemplate(template) {
|
|
298
|
+
this._mapDocTemplate = template;
|
|
299
|
+
}
|
|
300
|
+
get reduceChainTemplate() {
|
|
301
|
+
return this._reduceChainTemplate;
|
|
302
|
+
}
|
|
303
|
+
set reduceChainTemplate(template) {
|
|
304
|
+
this._reduceChainTemplate = template;
|
|
305
|
+
}
|
|
306
|
+
async checkAssertion(expression, context) {
|
|
307
|
+
let _expression = '';
|
|
308
|
+
try {
|
|
309
|
+
// Check if expression is a valid JSON string
|
|
310
|
+
_expression = this.printValues(JSON.parse(expression));
|
|
311
|
+
} catch (error) {
|
|
312
|
+
_expression = expression; // If not, use the expression as is
|
|
313
|
+
}
|
|
314
|
+
log.info(`Checking assertion with expression: ${_expression}`);
|
|
315
|
+
// Initialize context and content
|
|
316
|
+
let _context = undefined;
|
|
317
|
+
let _content = '';
|
|
318
|
+
try {
|
|
319
|
+
_context = JSON.parse(context);
|
|
320
|
+
} catch (error) {
|
|
321
|
+
_context = context; // If not, use the context as is
|
|
322
|
+
log.warn(`Context is not a valid JSON string: ${context}`);
|
|
323
|
+
log.warn(`Using context as a string: ${_context}`);
|
|
324
|
+
}
|
|
325
|
+
try {
|
|
326
|
+
// Check if context is a valid JSON string
|
|
327
|
+
_context = JSON.parse(context);
|
|
328
|
+
_context.forEach(element => {
|
|
329
|
+
element.content.forEach(contentItem => {
|
|
330
|
+
_content += atob(contentItem.attachment.data.value);
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
} catch (error) {
|
|
334
|
+
_content = context; // If not, use the context as is
|
|
335
|
+
log.warn(`Context is not a valid JSON : ${context}`);
|
|
336
|
+
log.warn(`Using context as a string: ${_content}`);
|
|
337
|
+
}
|
|
338
|
+
_content = this.findDatesAndConvertToTimeElapsed(_content.replace(/(\r\n|\n|\r)/gm, ' '));
|
|
339
|
+
log.info(`Checking assertion with context: ${_content}`);
|
|
340
|
+
const _input = {
|
|
341
|
+
input: {
|
|
342
|
+
expression: _expression,
|
|
343
|
+
context: _content
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
const result = await this.chain(_input);
|
|
347
|
+
log.info(`Assertion check result: ${result}`);
|
|
348
|
+
return result;
|
|
349
|
+
}
|
|
350
|
+
async checkMention(expression, context) {
|
|
351
|
+
if (expression && context) return true; // Placeholder for assertion check logic
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
async checkNegation(expression, context) {
|
|
355
|
+
if (expression && context) return true; // Placeholder for assertion check logic
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
findDatesAndConvertToTimeElapsed(text) {
|
|
632
359
|
// Regular expression to match dates in format of 'mm/dd/yyyy' or 'mm-dd-yyyy'
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
360
|
+
const dateRegex = /(\d{1,2}[-/]\d{1,2}[-/]\d{4})/g;
|
|
361
|
+
let matches;
|
|
362
|
+
let currentDate = new Date();
|
|
636
363
|
while ((matches = dateRegex.exec(text)) !== null) {
|
|
637
|
-
|
|
638
|
-
|
|
364
|
+
let date = new Date(matches[0]);
|
|
365
|
+
let timeElapsed = (currentDate.getTime() - date.getTime()) / (1000 * 60 * 60 * 24);
|
|
639
366
|
text = text.replace(matches[0], Math.floor(timeElapsed).toString() + ' days ago.');
|
|
640
367
|
}
|
|
641
368
|
return text;
|
|
642
|
-
}
|
|
643
|
-
|
|
369
|
+
}
|
|
370
|
+
camelToString(camelCase) {
|
|
644
371
|
return camelCase.replace(/([A-Z])/g, ' $1').replace(/^./, function (str) {
|
|
645
372
|
return str.toUpperCase();
|
|
646
373
|
});
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
log.info(
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
return positiveKeywords.some(
|
|
653
|
-
|
|
374
|
+
}
|
|
375
|
+
stringToBoolean(str) {
|
|
376
|
+
log.info(`Converting string to boolean: ${str}`);
|
|
377
|
+
const positiveKeywords = ['true', 'contains', 'confirms', 'yes', 'affirmative', 'matches', 'exists', 'present', 'absolutely', 'certainly', 'definitely', 'indeed', 'did have', 'showed', 'revealed'];
|
|
378
|
+
const lowerStr = str.toLowerCase();
|
|
379
|
+
return positiveKeywords.some(keyword => lowerStr.includes(keyword));
|
|
380
|
+
}
|
|
381
|
+
async textSplitter(text, chunkSize = 3900, chunkOverlap = 0) {
|
|
382
|
+
const textSplitter = new CharacterTextSplitter({
|
|
383
|
+
chunkSize: chunkSize,
|
|
384
|
+
chunkOverlap: chunkOverlap
|
|
654
385
|
});
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
return
|
|
660
|
-
while (1) switch (_context5.n) {
|
|
661
|
-
case 0:
|
|
662
|
-
if (chunkSize === void 0) {
|
|
663
|
-
chunkSize = 3900;
|
|
664
|
-
}
|
|
665
|
-
if (chunkOverlap === void 0) {
|
|
666
|
-
chunkOverlap = 0;
|
|
667
|
-
}
|
|
668
|
-
textSplitter = new CharacterTextSplitter({
|
|
669
|
-
chunkSize: chunkSize,
|
|
670
|
-
chunkOverlap: chunkOverlap
|
|
671
|
-
});
|
|
672
|
-
_context5.n = 1;
|
|
673
|
-
return textSplitter.splitText(text);
|
|
674
|
-
case 1:
|
|
675
|
-
texts = _context5.v;
|
|
676
|
-
if (!(texts.length === 0)) {
|
|
677
|
-
_context5.n = 2;
|
|
678
|
-
break;
|
|
679
|
-
}
|
|
680
|
-
log.warn('No text chunks created. Please check the input text.');
|
|
681
|
-
return _context5.a(2, []);
|
|
682
|
-
case 2:
|
|
683
|
-
if (texts.length > 100) {
|
|
684
|
-
log.warn("Warning: More than 100 text chunks created (" + texts.length + "). This may affect performance.");
|
|
685
|
-
}
|
|
686
|
-
if (chunkSize < 1000) {
|
|
687
|
-
log.warn("Warning: Chunk size is less than 1000 characters (" + chunkSize + "). This may lead to very small chunks.");
|
|
688
|
-
}
|
|
689
|
-
log.info("Text split into " + texts.length + " chunks with size " + chunkSize + " and overlap " + chunkOverlap);
|
|
690
|
-
return _context5.a(2, texts);
|
|
691
|
-
}
|
|
692
|
-
}, _callee4);
|
|
693
|
-
}));
|
|
694
|
-
function textSplitter(_x7, _x8, _x9) {
|
|
695
|
-
return _textSplitter.apply(this, arguments);
|
|
386
|
+
const texts = await textSplitter.splitText(text);
|
|
387
|
+
// Log the number of chunks created
|
|
388
|
+
if (texts.length === 0) {
|
|
389
|
+
log.warn('No text chunks created. Please check the input text.');
|
|
390
|
+
return [];
|
|
696
391
|
}
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
392
|
+
if (texts.length > 100) {
|
|
393
|
+
log.warn(`Warning: More than 100 text chunks created (${texts.length}). This may affect performance.`);
|
|
394
|
+
}
|
|
395
|
+
if (chunkSize < 1000) {
|
|
396
|
+
log.warn(`Warning: Chunk size is less than 1000 characters (${chunkSize}). This may lead to very small chunks.`);
|
|
397
|
+
}
|
|
398
|
+
log.info(`Text split into ${texts.length} chunks with size ${chunkSize} and overlap ${chunkOverlap}`);
|
|
399
|
+
return texts;
|
|
400
|
+
}
|
|
401
|
+
printValues(obj) {
|
|
402
|
+
log.info(`Processing Rule: ${obj.name} for ${obj.context}`);
|
|
701
403
|
// For each obj.expression:arg:where
|
|
702
404
|
if (obj.expression && obj.expression.arg && obj.expression.arg.where) {
|
|
703
405
|
// for each args in obj.expression.arg.where, extract the nested key called "value"
|
|
704
|
-
|
|
406
|
+
const statements = this.extractObjectsByKey(obj.expression.arg.where, 'value');
|
|
705
407
|
return statements.join(' ').replace(/,/g, ' ');
|
|
706
408
|
}
|
|
707
409
|
// return statements array as a string seperated by space
|
|
708
410
|
return '';
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
|
|
411
|
+
}
|
|
412
|
+
extractObjectsByKey(obj, key) {
|
|
413
|
+
const results = [];
|
|
712
414
|
function traverse(obj) {
|
|
713
|
-
for (
|
|
415
|
+
for (const currentKey in obj) {
|
|
714
416
|
if (obj.hasOwnProperty(currentKey)) {
|
|
715
417
|
if (currentKey === key) {
|
|
716
418
|
results.push(obj[currentKey]);
|
|
@@ -723,9 +425,8 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
|
|
|
723
425
|
}
|
|
724
426
|
traverse(obj);
|
|
725
427
|
return results;
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
var _this2 = this;
|
|
428
|
+
}
|
|
429
|
+
_printValues(obj) {
|
|
729
430
|
for (var key in obj) {
|
|
730
431
|
if (typeof obj[key] === 'object') {
|
|
731
432
|
this._printValues(obj[key]);
|
|
@@ -733,165 +434,319 @@ var LLMLoop = /*#__PURE__*/function (_BaseChain) {
|
|
|
733
434
|
this.stringExpression += obj[key] + ' ';
|
|
734
435
|
}
|
|
735
436
|
}
|
|
736
|
-
|
|
737
|
-
_eliminate.forEach(
|
|
738
|
-
|
|
739
|
-
|
|
437
|
+
const _eliminate = ['true', 'false', 'null', 'undefined', 'String', 'Number', 'Object', 'Array', 'Boolean', 'value', 'Type', 'Specifier', 'Named'];
|
|
438
|
+
_eliminate.forEach(element => {
|
|
439
|
+
const regex = new RegExp(element, 'g');
|
|
440
|
+
this.stringExpression = this.stringExpression.replace(regex, '');
|
|
740
441
|
});
|
|
741
442
|
this.stringExpression = this.stringExpression.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');
|
|
742
443
|
return this.camelToString(this.stringExpression);
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
444
|
+
}
|
|
445
|
+
async chain(input) {
|
|
446
|
+
// Create a chain using RunnableSequence, RunnablePassthrough and RunnableParallel
|
|
447
|
+
// input has two properties: expression and context
|
|
448
|
+
// expression goes through mapQuery and parallely
|
|
449
|
+
// context goes through textSplitter and mapDoc
|
|
450
|
+
// Finally reduceChain is called with the results of mapQuery and mapDoc
|
|
451
|
+
// LCEL-based implementation using RunnableSequence, RunnableParallel, and RunnablePassthrough
|
|
452
|
+
const {
|
|
453
|
+
RunnableSequence,
|
|
454
|
+
RunnableParallel,
|
|
455
|
+
RunnablePassthrough
|
|
456
|
+
} = await import('@langchain/core/runnables');
|
|
457
|
+
const mapQuery = new BaseChain(this.container);
|
|
458
|
+
const mapDoc = new BaseChain(this.container);
|
|
459
|
+
const reduceChain = new BaseChain(this.container);
|
|
460
|
+
mapQuery.name = 'MapQuery';
|
|
461
|
+
mapQuery.description = 'Map the expression to a natural language query.';
|
|
462
|
+
mapQuery.template = this.resolve('litl_mapquery_template', this._mapQueryTemplate);
|
|
463
|
+
mapDoc.name = 'MapDoc';
|
|
464
|
+
mapDoc.description = 'Map the document to a set of facts.';
|
|
465
|
+
mapDoc.template = this.resolve('litl_mapdoc_template', this._mapDocTemplate);
|
|
466
|
+
reduceChain.name = 'ReduceChain';
|
|
467
|
+
reduceChain.description = 'Reduce a set of documents to binary answer.';
|
|
468
|
+
reduceChain.template = this.resolve('litl_reduce_chain_template', this._reduceChainTemplate);
|
|
469
|
+
const mapQueryChain = RunnablePassthrough.assign({
|
|
470
|
+
query: async input => mapQuery.chain({
|
|
471
|
+
expression: input.input.expression
|
|
472
|
+
})
|
|
473
|
+
});
|
|
474
|
+
const mapDocChain = RunnablePassthrough.assign({
|
|
475
|
+
documents: async input => {
|
|
476
|
+
const textChunks = await this.textSplitter(input.input.context);
|
|
477
|
+
return Promise.all(textChunks.map(chunk => mapDoc.chain({
|
|
478
|
+
document: chunk,
|
|
479
|
+
statements: input.input.expression
|
|
480
|
+
})));
|
|
481
|
+
}
|
|
482
|
+
});
|
|
483
|
+
const parallelChain = RunnableParallel.from([mapQueryChain, mapDocChain]);
|
|
484
|
+
const sequence = RunnableSequence.from([parallelChain, async results => {
|
|
485
|
+
/* results is an array: [{ query: ... }, { documents: [...] }]
|
|
486
|
+
{
|
|
487
|
+
"0": {
|
|
488
|
+
"input": {
|
|
489
|
+
"expression": "0 Visual foot exam showed active infection?",
|
|
490
|
+
"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. "
|
|
491
|
+
},
|
|
492
|
+
"query": "The text indicates a question about whether a visual foot exam revealed an active infection.\n"
|
|
493
|
+
},
|
|
494
|
+
"1": {
|
|
495
|
+
"input": {
|
|
496
|
+
"expression": "0 Visual foot exam showed active infection?",
|
|
497
|
+
"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. "
|
|
498
|
+
},
|
|
499
|
+
"documents": [
|
|
500
|
+
"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"
|
|
501
|
+
]
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
*/
|
|
505
|
+
log.info(`Results from parallel chain: ${JSON.stringify(results)}`);
|
|
506
|
+
const expression = results[0].input.expression;
|
|
507
|
+
const query = results[0].query;
|
|
508
|
+
const documents = results[1].documents;
|
|
509
|
+
return reduceChain.chain({
|
|
510
|
+
facts: documents,
|
|
511
|
+
query: query + ' ' + expression
|
|
512
|
+
});
|
|
513
|
+
}, result => this.stringToBoolean(result)]);
|
|
514
|
+
return sequence.invoke(input);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Copyright 2024 Bell Eapen
|
|
520
|
+
*
|
|
521
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
522
|
+
* you may not use this file except in compliance with the License.
|
|
523
|
+
* You may obtain a copy of the License at
|
|
524
|
+
*
|
|
525
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
526
|
+
*
|
|
527
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
528
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
529
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
530
|
+
* See the License for the specific language governing permissions and
|
|
531
|
+
* limitations under the License.
|
|
532
|
+
*/
|
|
533
|
+
/**
|
|
534
|
+
* BaseLLM is a base class for LLMs that communicate with a remote API.
|
|
535
|
+
* It extends the LangChain LLM class and provides configurable parameters
|
|
536
|
+
* for model inference.
|
|
537
|
+
*/
|
|
538
|
+
class BaseLLM extends LLM {
|
|
539
|
+
constructor(fields) {
|
|
540
|
+
var _fields$apiKey, _fields$timeout, _fields$backend, _fields$temperature, _fields$topP, _fields$topK, _fields$nBatch, _fields$nThreads, _fields$nPredict, _fields$maxOutputToke, _fields$repeatLastN, _fields$repeatPenalty;
|
|
541
|
+
super(fields);
|
|
542
|
+
this.baseUrl = void 0;
|
|
543
|
+
this.model = void 0;
|
|
544
|
+
this.apiKey = void 0;
|
|
545
|
+
this.timeout = void 0;
|
|
546
|
+
this.backend = void 0;
|
|
547
|
+
this.temperature = void 0;
|
|
548
|
+
this.topP = void 0;
|
|
549
|
+
this.topK = void 0;
|
|
550
|
+
this.nBatch = void 0;
|
|
551
|
+
this.nThreads = void 0;
|
|
552
|
+
this.nPredict = void 0;
|
|
553
|
+
this.maxOutputTokens = void 0;
|
|
554
|
+
this.repeatLastN = void 0;
|
|
555
|
+
this.repeatPenalty = void 0;
|
|
556
|
+
this.baseUrl = fields.baseUrl;
|
|
557
|
+
this.model = fields.model;
|
|
558
|
+
this.apiKey = (_fields$apiKey = fields.apiKey) != null ? _fields$apiKey : '';
|
|
559
|
+
this.timeout = (_fields$timeout = fields.timeout) != null ? _fields$timeout : 60000;
|
|
560
|
+
this.backend = (_fields$backend = fields.backend) != null ? _fields$backend : 'dhti';
|
|
561
|
+
this.temperature = (_fields$temperature = fields.temperature) != null ? _fields$temperature : 0.1;
|
|
562
|
+
this.topP = (_fields$topP = fields.topP) != null ? _fields$topP : 0.8;
|
|
563
|
+
this.topK = (_fields$topK = fields.topK) != null ? _fields$topK : 40;
|
|
564
|
+
this.nBatch = (_fields$nBatch = fields.nBatch) != null ? _fields$nBatch : 8;
|
|
565
|
+
this.nThreads = (_fields$nThreads = fields.nThreads) != null ? _fields$nThreads : 4;
|
|
566
|
+
this.nPredict = (_fields$nPredict = fields.nPredict) != null ? _fields$nPredict : 256;
|
|
567
|
+
this.maxOutputTokens = (_fields$maxOutputToke = fields.maxOutputTokens) != null ? _fields$maxOutputToke : 512;
|
|
568
|
+
this.repeatLastN = (_fields$repeatLastN = fields.repeatLastN) != null ? _fields$repeatLastN : 64;
|
|
569
|
+
this.repeatPenalty = (_fields$repeatPenalty = fields.repeatPenalty) != null ? _fields$repeatPenalty : 1.18;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Get the model default parameters
|
|
573
|
+
*/
|
|
574
|
+
get modelDefaultParameters() {
|
|
575
|
+
return {
|
|
576
|
+
maxOutputTokens: this.maxOutputTokens,
|
|
577
|
+
nPredict: this.nPredict,
|
|
578
|
+
topK: this.topK,
|
|
579
|
+
topP: this.topP,
|
|
580
|
+
temperature: this.temperature,
|
|
581
|
+
nBatch: this.nBatch,
|
|
582
|
+
repeatPenalty: this.repeatPenalty,
|
|
583
|
+
repeatLastN: this.repeatLastN
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Get the identifying parameters
|
|
588
|
+
*/
|
|
589
|
+
get identifyingParams() {
|
|
590
|
+
return {
|
|
591
|
+
model: this.model,
|
|
592
|
+
baseUrl: this.baseUrl,
|
|
593
|
+
modelParameters: this.modelDefaultParameters
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Get the LLM type identifier
|
|
598
|
+
*/
|
|
599
|
+
_llmType() {
|
|
600
|
+
return 'dhti';
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Prepare the request payload for the API
|
|
604
|
+
*/
|
|
605
|
+
preparePayload(prompt) {
|
|
606
|
+
return {
|
|
607
|
+
model: this.model,
|
|
608
|
+
options: this.modelDefaultParameters,
|
|
609
|
+
messages: [{
|
|
610
|
+
role: 'user',
|
|
611
|
+
content: prompt
|
|
612
|
+
}]
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Make the API call to generate text
|
|
617
|
+
*/
|
|
618
|
+
async _call(prompt, _options, _runManager) {
|
|
619
|
+
const payload = this.preparePayload(prompt);
|
|
620
|
+
const headers = {
|
|
621
|
+
'Content-Type': 'application/json'
|
|
622
|
+
};
|
|
623
|
+
if (this.apiKey) {
|
|
624
|
+
headers['Authorization'] = `Bearer ${this.apiKey}`;
|
|
625
|
+
}
|
|
626
|
+
const controller = new AbortController();
|
|
627
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
628
|
+
try {
|
|
629
|
+
const response = await fetch(this.baseUrl, {
|
|
630
|
+
method: 'POST',
|
|
631
|
+
headers,
|
|
632
|
+
body: JSON.stringify(payload),
|
|
633
|
+
signal: controller.signal
|
|
634
|
+
});
|
|
635
|
+
clearTimeout(timeoutId);
|
|
636
|
+
if (!response.ok) {
|
|
637
|
+
const body = await response.text();
|
|
638
|
+
throw new Error(`API request failed: status=${response.status}; body=${body}`);
|
|
639
|
+
}
|
|
640
|
+
const data = await response.json();
|
|
641
|
+
// Expecting structure like: { "choices": [ { "message": { "role":"assistant","content":"..." } } ] }
|
|
642
|
+
// Adapt this path if the API differs
|
|
643
|
+
if (data.choices && Array.isArray(data.choices) && data.choices.length > 0) {
|
|
644
|
+
const choice = data.choices[0];
|
|
645
|
+
// support both "message" and direct "text"
|
|
646
|
+
if (typeof choice === 'object' && choice !== null && 'message' in choice && typeof choice.message === 'object' && choice.message !== null) {
|
|
647
|
+
const content = choice.message.content;
|
|
648
|
+
if (typeof content === 'string') {
|
|
649
|
+
return content;
|
|
650
|
+
}
|
|
651
|
+
} else if ('text' in choice) {
|
|
652
|
+
const text = choice.text;
|
|
653
|
+
if (typeof text === 'string') {
|
|
654
|
+
return text;
|
|
655
|
+
}
|
|
859
656
|
}
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
657
|
+
}
|
|
658
|
+
// Fallback: return raw JSON string for debugging
|
|
659
|
+
return JSON.stringify(data);
|
|
660
|
+
} catch (error) {
|
|
661
|
+
clearTimeout(timeoutId);
|
|
662
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
663
|
+
throw new Error(`API request timed out after ${this.timeout}ms`);
|
|
664
|
+
}
|
|
665
|
+
throw error;
|
|
864
666
|
}
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Copyright 2024 Bell Eapen
|
|
672
|
+
*
|
|
673
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
674
|
+
* you may not use this file except in compliance with the License.
|
|
675
|
+
* You may obtain a copy of the License at
|
|
676
|
+
*
|
|
677
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
678
|
+
*
|
|
679
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
680
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
681
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
682
|
+
* See the License for the specific language governing permissions and
|
|
683
|
+
* limitations under the License.
|
|
684
|
+
*/
|
|
685
|
+
/**
|
|
686
|
+
* BaseEmbedding is a base class for embeddings that communicate with a remote API.
|
|
687
|
+
* It extends the LangChain Embeddings class and provides methods for embedding
|
|
688
|
+
* documents and queries.
|
|
689
|
+
*/
|
|
690
|
+
class BaseEmbedding extends Embeddings {
|
|
691
|
+
constructor(fields) {
|
|
692
|
+
var _fields$apiKey;
|
|
693
|
+
super(fields);
|
|
694
|
+
this.baseUrl = void 0;
|
|
695
|
+
this.model = void 0;
|
|
696
|
+
this.apiKey = void 0;
|
|
697
|
+
this.baseUrl = fields.baseUrl;
|
|
698
|
+
this.model = fields.model;
|
|
699
|
+
this.apiKey = (_fields$apiKey = fields.apiKey) != null ? _fields$apiKey : '';
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Embed a list of documents
|
|
703
|
+
* @param texts - Array of strings to embed
|
|
704
|
+
* @returns Promise of array of embedding vectors
|
|
705
|
+
*/
|
|
706
|
+
async embedDocuments(texts) {
|
|
707
|
+
return this.getEmbeddings(texts);
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Embed a single query
|
|
711
|
+
* @param text - String to embed
|
|
712
|
+
* @returns Promise of embedding vector
|
|
713
|
+
*/
|
|
714
|
+
async embedQuery(text) {
|
|
715
|
+
const embeddings = await this.getEmbeddings([text]);
|
|
716
|
+
return embeddings[0];
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
* Helper method to get embeddings for a list of texts
|
|
720
|
+
* @param texts - Array of strings to embed
|
|
721
|
+
* @returns Promise of array of embedding vectors
|
|
722
|
+
*/
|
|
723
|
+
async getEmbeddings(texts) {
|
|
724
|
+
const headers = {
|
|
725
|
+
'Content-Type': 'application/json'
|
|
726
|
+
};
|
|
727
|
+
if (this.apiKey) {
|
|
728
|
+
headers['Authorization'] = `Bearer ${this.apiKey}`;
|
|
876
729
|
}
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
730
|
+
const payload = {
|
|
731
|
+
model: this.model,
|
|
732
|
+
input: texts
|
|
733
|
+
};
|
|
734
|
+
const response = await fetch(this.baseUrl, {
|
|
735
|
+
method: 'POST',
|
|
736
|
+
headers,
|
|
737
|
+
body: JSON.stringify(payload)
|
|
738
|
+
});
|
|
739
|
+
if (!response.ok) {
|
|
740
|
+
const body = await response.text();
|
|
741
|
+
throw new Error(`API request failed: status=${response.status}; body=${body}`);
|
|
884
742
|
}
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
return this._reduceChainTemplate;
|
|
889
|
-
},
|
|
890
|
-
set: function set(template) {
|
|
891
|
-
this._reduceChainTemplate = template;
|
|
743
|
+
const respJson = await response.json();
|
|
744
|
+
if (!('embeddings' in respJson)) {
|
|
745
|
+
throw new Error(`API response missing 'embeddings' key. Received keys: ${Object.keys(respJson).join(', ')}`);
|
|
892
746
|
}
|
|
893
|
-
|
|
894
|
-
}
|
|
747
|
+
return respJson.embeddings;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
895
750
|
|
|
896
|
-
export { BaseAgent, BaseChain, BaseTool, LLMLoop };
|
|
751
|
+
export { BaseAgent, BaseChain, BaseEmbedding, BaseLLM, BaseTool, LLMLoop };
|
|
897
752
|
//# sourceMappingURL=medpromptjs.esm.js.map
|