medpromptjs 0.2.3 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chain.d.ts +4 -3
- package/dist/index.d.ts +1 -0
- package/dist/llm_loop/base.d.ts +21 -0
- package/dist/llm_loop/index.d.ts +4 -0
- package/dist/llm_loop/map_doc.d.ts +6 -0
- package/dist/llm_loop/map_query.d.ts +6 -0
- package/dist/llm_loop/reduce.d.ts +6 -0
- package/dist/medpromptjs.cjs.development.js +468 -78
- 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 +469 -80
- package/dist/medpromptjs.esm.js.map +1 -1
- package/package.json +13 -10
- package/src/chain.ts +27 -13
- package/src/index.ts +1 -0
- package/src/llm_loop/base.ts +225 -0
- package/src/llm_loop/index.ts +4 -0
- package/src/llm_loop/map_doc.ts +12 -0
- package/src/llm_loop/map_query.ts +36 -0
- package/src/llm_loop/reduce.ts +12 -0
package/dist/medpromptjs.esm.js
CHANGED
|
@@ -1,85 +1,10 @@
|
|
|
1
|
-
import { PromptTemplate } from '@langchain/core/prompts';
|
|
1
|
+
import { ChatPromptTemplate, PromptTemplate } from '@langchain/core/prompts';
|
|
2
2
|
import { RunnableSequence, RunnablePassthrough } from '@langchain/core/runnables';
|
|
3
|
+
import { AIMessage } from '@langchain/core/messages';
|
|
3
4
|
import { AgentExecutor, createStructuredChatAgent } from 'langchain/agents';
|
|
4
5
|
import { DynamicStructuredTool } from 'langchain/tools';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* Copyright 2024 Bell Eapen
|
|
8
|
-
*
|
|
9
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
10
|
-
* you may not use this file except in compliance with the License.
|
|
11
|
-
* You may obtain a copy of the License at
|
|
12
|
-
*
|
|
13
|
-
* https://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
-
*
|
|
15
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
16
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18
|
-
* See the License for the specific language governing permissions and
|
|
19
|
-
* limitations under the License.
|
|
20
|
-
*/
|
|
21
|
-
var mydi = function mydi(c, m, b) {
|
|
22
|
-
try {
|
|
23
|
-
return c.resolve(m + '_' + b);
|
|
24
|
-
} catch (e) {
|
|
25
|
-
return c.resolve(b);
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Copyright 2024 Bell Eapen
|
|
31
|
-
*
|
|
32
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
33
|
-
* you may not use this file except in compliance with the License.
|
|
34
|
-
* You may obtain a copy of the License at
|
|
35
|
-
*
|
|
36
|
-
* https://www.apache.org/licenses/LICENSE-2.0
|
|
37
|
-
*
|
|
38
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
39
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
40
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
41
|
-
* See the License for the specific language governing permissions and
|
|
42
|
-
* limitations under the License.
|
|
43
|
-
*/
|
|
44
|
-
var BaseChain = /*#__PURE__*/function () {
|
|
45
|
-
function BaseChain(container, name, description, template) {
|
|
46
|
-
this.container = void 0;
|
|
47
|
-
this.tools = void 0;
|
|
48
|
-
this.name = void 0;
|
|
49
|
-
this.description = void 0;
|
|
50
|
-
this.prompt = void 0;
|
|
51
|
-
this.llm = void 0;
|
|
52
|
-
this.template = "\n Summarize the following text:\n {input}\n ";
|
|
53
|
-
this.container = container;
|
|
54
|
-
this.name = name === '' ? this.camelize(this.constructor.name) : name;
|
|
55
|
-
this.description = description === '' ? this.snake_case(this.constructor.name) : description;
|
|
56
|
-
this.tools = this.resolve('tools');
|
|
57
|
-
this.prompt = this.resolve('prompt') !== '' ? this.resolve('prompt') : PromptTemplate.fromTemplate(this.template);
|
|
58
|
-
this.llm = this.resolve('main-llm');
|
|
59
|
-
}
|
|
60
|
-
var _proto = BaseChain.prototype;
|
|
61
|
-
_proto.resolve = function resolve(name) {
|
|
62
|
-
return mydi(this.container, this.name, name);
|
|
63
|
-
};
|
|
64
|
-
_proto.camelize = function camelize(str) {
|
|
65
|
-
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
66
|
-
return index === 0 ? word.toLowerCase() : word.toUpperCase();
|
|
67
|
-
}).replace(/\s+/g, '');
|
|
68
|
-
};
|
|
69
|
-
_proto.snake_case = function snake_case(str) {
|
|
70
|
-
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
71
|
-
return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();
|
|
72
|
-
}).replace(/\s+/g, '');
|
|
73
|
-
}
|
|
74
|
-
// https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/
|
|
75
|
-
//! Override this method
|
|
76
|
-
;
|
|
77
|
-
_proto.chain = function chain(input) {
|
|
78
|
-
var _chain = RunnableSequence.from([new RunnablePassthrough(), this.prompt, this.llm]);
|
|
79
|
-
return _chain.invoke(input);
|
|
80
|
-
};
|
|
81
|
-
return BaseChain;
|
|
82
|
-
}();
|
|
6
|
+
import { CharacterTextSplitter } from '@langchain/textsplitters';
|
|
7
|
+
import { Logger } from 'tslog';
|
|
83
8
|
|
|
84
9
|
function asyncGeneratorStep(n, t, e, r, o, a, c) {
|
|
85
10
|
try {
|
|
@@ -384,6 +309,114 @@ function _setPrototypeOf(t, e) {
|
|
|
384
309
|
}, _setPrototypeOf(t, e);
|
|
385
310
|
}
|
|
386
311
|
|
|
312
|
+
/**
|
|
313
|
+
* Copyright 2024 Bell Eapen
|
|
314
|
+
*
|
|
315
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
316
|
+
* you may not use this file except in compliance with the License.
|
|
317
|
+
* You may obtain a copy of the License at
|
|
318
|
+
*
|
|
319
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
320
|
+
*
|
|
321
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
322
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
323
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
324
|
+
* See the License for the specific language governing permissions and
|
|
325
|
+
* limitations under the License.
|
|
326
|
+
*/
|
|
327
|
+
var mydi = function mydi(c, m, b) {
|
|
328
|
+
try {
|
|
329
|
+
return c.resolve(m + '_' + b);
|
|
330
|
+
} catch (e) {
|
|
331
|
+
return c.resolve(b);
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
var BaseChain = /*#__PURE__*/function () {
|
|
336
|
+
function BaseChain(container, name, description, template) {
|
|
337
|
+
if (template === void 0) {
|
|
338
|
+
template = "\n Summarize the following text:\n {input}\n ";
|
|
339
|
+
}
|
|
340
|
+
this.container = void 0;
|
|
341
|
+
this.tools = void 0;
|
|
342
|
+
this.name = void 0;
|
|
343
|
+
this.description = void 0;
|
|
344
|
+
this.prompt = void 0;
|
|
345
|
+
this.llm = void 0;
|
|
346
|
+
this.template = void 0;
|
|
347
|
+
this.chat_model = void 0;
|
|
348
|
+
this.runnable = void 0;
|
|
349
|
+
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
|
+
this.llm = this.resolve('main-llm');
|
|
355
|
+
this.chat_model = false;
|
|
356
|
+
try {
|
|
357
|
+
this.chat_model = this.resolve('chat_model');
|
|
358
|
+
} catch (e) {}
|
|
359
|
+
if (this.chat_model) {
|
|
360
|
+
this.prompt = ChatPromptTemplate.fromTemplate(this.template);
|
|
361
|
+
} else {
|
|
362
|
+
this.prompt = PromptTemplate.fromTemplate(this.template);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
var _proto = BaseChain.prototype;
|
|
366
|
+
_proto.resolve = function resolve(name) {
|
|
367
|
+
return mydi(this.container, this.name, name);
|
|
368
|
+
};
|
|
369
|
+
_proto.camelize = function camelize(str) {
|
|
370
|
+
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
371
|
+
return index === 0 ? word.toLowerCase() : word.toUpperCase();
|
|
372
|
+
}).replace(/\s+/g, '');
|
|
373
|
+
};
|
|
374
|
+
_proto.snake_case = function snake_case(str) {
|
|
375
|
+
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
376
|
+
return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();
|
|
377
|
+
}).replace(/\s+/g, '');
|
|
378
|
+
}
|
|
379
|
+
// https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/
|
|
380
|
+
//! Override this method
|
|
381
|
+
;
|
|
382
|
+
_proto.chain =
|
|
383
|
+
/*#__PURE__*/
|
|
384
|
+
function () {
|
|
385
|
+
var _chain = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
|
|
386
|
+
var response, aiMessage;
|
|
387
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
388
|
+
while (1) switch (_context.prev = _context.next) {
|
|
389
|
+
case 0:
|
|
390
|
+
this.runnable = RunnableSequence.from([new RunnablePassthrough(), this.prompt, this.llm]);
|
|
391
|
+
if (!this.chat_model) {
|
|
392
|
+
_context.next = 7;
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
_context.next = 4;
|
|
396
|
+
return this.runnable.invoke(input);
|
|
397
|
+
case 4:
|
|
398
|
+
response = _context.sent;
|
|
399
|
+
aiMessage = new AIMessage(response);
|
|
400
|
+
return _context.abrupt("return", aiMessage.content);
|
|
401
|
+
case 7:
|
|
402
|
+
_context.next = 9;
|
|
403
|
+
return this.runnable.invoke(input);
|
|
404
|
+
case 9:
|
|
405
|
+
return _context.abrupt("return", _context.sent);
|
|
406
|
+
case 10:
|
|
407
|
+
case "end":
|
|
408
|
+
return _context.stop();
|
|
409
|
+
}
|
|
410
|
+
}, _callee, this);
|
|
411
|
+
}));
|
|
412
|
+
function chain(_x) {
|
|
413
|
+
return _chain.apply(this, arguments);
|
|
414
|
+
}
|
|
415
|
+
return chain;
|
|
416
|
+
}();
|
|
417
|
+
return BaseChain;
|
|
418
|
+
}();
|
|
419
|
+
|
|
387
420
|
var BaseAgent = /*#__PURE__*/function () {
|
|
388
421
|
function BaseAgent(container) {
|
|
389
422
|
this.container = void 0;
|
|
@@ -506,5 +539,361 @@ var BaseTool = /*#__PURE__*/function (_DynamicStructuredToo) {
|
|
|
506
539
|
return BaseTool;
|
|
507
540
|
}(DynamicStructuredTool);
|
|
508
541
|
|
|
509
|
-
|
|
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
|
+
var log = /*#__PURE__*/new Logger();
|
|
591
|
+
var LLMLoop = /*#__PURE__*/function (_BaseChain) {
|
|
592
|
+
function LLMLoop() {
|
|
593
|
+
var _this;
|
|
594
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
595
|
+
args[_key] = arguments[_key];
|
|
596
|
+
}
|
|
597
|
+
_this = _BaseChain.call.apply(_BaseChain, [this].concat(args)) || this;
|
|
598
|
+
// Implement the LLM loop logic here
|
|
599
|
+
_this.string_expression = '';
|
|
600
|
+
_this.name = 'LLMLoop';
|
|
601
|
+
_this.description = 'Base LLM Loop.';
|
|
602
|
+
_this.mapQuery = new MapQuery(_this.container, '', '');
|
|
603
|
+
_this.mapDoc = new MapDoc(_this.container, '', '');
|
|
604
|
+
_this.reduceChain = new ReduceChain(_this.container, '', '');
|
|
605
|
+
return _this;
|
|
606
|
+
}
|
|
607
|
+
_inheritsLoose(LLMLoop, _BaseChain);
|
|
608
|
+
var _proto = LLMLoop.prototype;
|
|
609
|
+
_proto.checkAssertion = /*#__PURE__*/function () {
|
|
610
|
+
var _checkAssertion = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(expression, context) {
|
|
611
|
+
var _expression, _context, _content, _input;
|
|
612
|
+
return _regeneratorRuntime().wrap(function _callee$(_context2) {
|
|
613
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
614
|
+
case 0:
|
|
615
|
+
_expression = '';
|
|
616
|
+
_context = undefined;
|
|
617
|
+
_content = '';
|
|
618
|
+
try {
|
|
619
|
+
// Check if expression is a valid JSON string
|
|
620
|
+
_expression = this.printValues(JSON.parse(expression));
|
|
621
|
+
} catch (error) {
|
|
622
|
+
_expression = expression; // If not, use the expression as is
|
|
623
|
+
}
|
|
624
|
+
log.info("Checking assertion with expression: " + _expression);
|
|
625
|
+
try {
|
|
626
|
+
// Check if context is a valid JSON string
|
|
627
|
+
_context = JSON.parse(context);
|
|
628
|
+
_context.forEach(function (element) {
|
|
629
|
+
_content += atob(element.content[0].attachment.data.value);
|
|
630
|
+
});
|
|
631
|
+
} catch (error) {
|
|
632
|
+
_content = context; // If not, use the context as is
|
|
633
|
+
}
|
|
634
|
+
_content = this.findDatesAndConvertToTimeElapsed(_content.replace(/(\r\n|\n|\r)/gm, ' '));
|
|
635
|
+
_input = {
|
|
636
|
+
input: {
|
|
637
|
+
expression: _expression,
|
|
638
|
+
context: _content
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
return _context2.abrupt("return", this.chain(_input));
|
|
642
|
+
case 9:
|
|
643
|
+
case "end":
|
|
644
|
+
return _context2.stop();
|
|
645
|
+
}
|
|
646
|
+
}, _callee, this);
|
|
647
|
+
}));
|
|
648
|
+
function checkAssertion(_x, _x2) {
|
|
649
|
+
return _checkAssertion.apply(this, arguments);
|
|
650
|
+
}
|
|
651
|
+
return checkAssertion;
|
|
652
|
+
}();
|
|
653
|
+
_proto.checkMention = /*#__PURE__*/function () {
|
|
654
|
+
var _checkMention = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(expression, context) {
|
|
655
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context3) {
|
|
656
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
657
|
+
case 0:
|
|
658
|
+
if (!(expression && context)) {
|
|
659
|
+
_context3.next = 2;
|
|
660
|
+
break;
|
|
661
|
+
}
|
|
662
|
+
return _context3.abrupt("return", true);
|
|
663
|
+
case 2:
|
|
664
|
+
return _context3.abrupt("return", false);
|
|
665
|
+
case 3:
|
|
666
|
+
case "end":
|
|
667
|
+
return _context3.stop();
|
|
668
|
+
}
|
|
669
|
+
}, _callee2);
|
|
670
|
+
}));
|
|
671
|
+
function checkMention(_x3, _x4) {
|
|
672
|
+
return _checkMention.apply(this, arguments);
|
|
673
|
+
}
|
|
674
|
+
return checkMention;
|
|
675
|
+
}();
|
|
676
|
+
_proto.checkNegation = /*#__PURE__*/function () {
|
|
677
|
+
var _checkNegation = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(expression, context) {
|
|
678
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context4) {
|
|
679
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
680
|
+
case 0:
|
|
681
|
+
if (!(expression && context)) {
|
|
682
|
+
_context4.next = 2;
|
|
683
|
+
break;
|
|
684
|
+
}
|
|
685
|
+
return _context4.abrupt("return", true);
|
|
686
|
+
case 2:
|
|
687
|
+
return _context4.abrupt("return", false);
|
|
688
|
+
case 3:
|
|
689
|
+
case "end":
|
|
690
|
+
return _context4.stop();
|
|
691
|
+
}
|
|
692
|
+
}, _callee3);
|
|
693
|
+
}));
|
|
694
|
+
function checkNegation(_x5, _x6) {
|
|
695
|
+
return _checkNegation.apply(this, arguments);
|
|
696
|
+
}
|
|
697
|
+
return checkNegation;
|
|
698
|
+
}();
|
|
699
|
+
_proto.findDatesAndConvertToTimeElapsed = function findDatesAndConvertToTimeElapsed(text) {
|
|
700
|
+
// Regular expression to match dates in format of 'mm/dd/yyyy' or 'mm-dd-yyyy'
|
|
701
|
+
var dateRegex = /(\d{1,2}[-/]\d{1,2}[-/]\d{4})/g;
|
|
702
|
+
var matches;
|
|
703
|
+
var currentDate = new Date();
|
|
704
|
+
while ((matches = dateRegex.exec(text)) !== null) {
|
|
705
|
+
var date = new Date(matches[0]);
|
|
706
|
+
var timeElapsed = (currentDate.getTime() - date.getTime()) / (1000 * 60 * 60 * 24);
|
|
707
|
+
text = text.replace(matches[0], Math.floor(timeElapsed).toString() + ' days ago.');
|
|
708
|
+
}
|
|
709
|
+
return text;
|
|
710
|
+
};
|
|
711
|
+
_proto.camelToString = function camelToString(camelCase) {
|
|
712
|
+
return camelCase.replace(/([A-Z])/g, ' $1').replace(/^./, function (str) {
|
|
713
|
+
return str.toUpperCase();
|
|
714
|
+
});
|
|
715
|
+
};
|
|
716
|
+
_proto.stringToBoolean = function stringToBoolean(str) {
|
|
717
|
+
log.info("Converting string to boolean: " + str);
|
|
718
|
+
var positiveKeywords = ['true', 'contains', 'confirms', 'yes', 'affirmative', 'matches', 'exists', 'present', 'absolutely', 'certainly', 'definitely', 'indeed'];
|
|
719
|
+
var lowerStr = str.toLowerCase();
|
|
720
|
+
return positiveKeywords.some(function (keyword) {
|
|
721
|
+
return lowerStr.includes(keyword);
|
|
722
|
+
});
|
|
723
|
+
};
|
|
724
|
+
_proto.textSplitter = /*#__PURE__*/function () {
|
|
725
|
+
var _textSplitter = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(text, chunkSize, chunkOverlap) {
|
|
726
|
+
var textSplitter, texts;
|
|
727
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context5) {
|
|
728
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
729
|
+
case 0:
|
|
730
|
+
if (chunkSize === void 0) {
|
|
731
|
+
chunkSize = 3900;
|
|
732
|
+
}
|
|
733
|
+
if (chunkOverlap === void 0) {
|
|
734
|
+
chunkOverlap = 0;
|
|
735
|
+
}
|
|
736
|
+
textSplitter = new CharacterTextSplitter({
|
|
737
|
+
chunkSize: chunkSize,
|
|
738
|
+
chunkOverlap: chunkOverlap
|
|
739
|
+
});
|
|
740
|
+
_context5.next = 5;
|
|
741
|
+
return textSplitter.splitText(text);
|
|
742
|
+
case 5:
|
|
743
|
+
texts = _context5.sent;
|
|
744
|
+
if (!(texts.length === 0)) {
|
|
745
|
+
_context5.next = 9;
|
|
746
|
+
break;
|
|
747
|
+
}
|
|
748
|
+
log.warn('No text chunks created. Please check the input text.');
|
|
749
|
+
return _context5.abrupt("return", []);
|
|
750
|
+
case 9:
|
|
751
|
+
if (texts.length > 100) {
|
|
752
|
+
log.warn("Warning: More than 100 text chunks created (" + texts.length + "). This may affect performance.");
|
|
753
|
+
}
|
|
754
|
+
if (chunkSize < 1000) {
|
|
755
|
+
log.warn("Warning: Chunk size is less than 1000 characters (" + chunkSize + "). This may lead to very small chunks.");
|
|
756
|
+
}
|
|
757
|
+
log.info("Text split into " + texts.length + " chunks with size " + chunkSize + " and overlap " + chunkOverlap);
|
|
758
|
+
return _context5.abrupt("return", texts);
|
|
759
|
+
case 13:
|
|
760
|
+
case "end":
|
|
761
|
+
return _context5.stop();
|
|
762
|
+
}
|
|
763
|
+
}, _callee4);
|
|
764
|
+
}));
|
|
765
|
+
function textSplitter(_x7, _x8, _x9) {
|
|
766
|
+
return _textSplitter.apply(this, arguments);
|
|
767
|
+
}
|
|
768
|
+
return textSplitter;
|
|
769
|
+
}();
|
|
770
|
+
_proto.printValues = function printValues(obj) {
|
|
771
|
+
var _this2 = this;
|
|
772
|
+
for (var key in obj) {
|
|
773
|
+
if (typeof obj[key] === 'object') {
|
|
774
|
+
this.printValues(obj[key]);
|
|
775
|
+
} else {
|
|
776
|
+
this.string_expression += obj[key] + ' ';
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
var _eliminate = ['true', 'false', 'null', 'undefined', 'String', 'Number', 'Object', 'Array', 'Boolean', 'value', 'Type', 'Specifier', 'Named'];
|
|
780
|
+
_eliminate.forEach(function (element) {
|
|
781
|
+
var regex = new RegExp(element, 'g');
|
|
782
|
+
_this2.string_expression = _this2.string_expression.replace(regex, '');
|
|
783
|
+
});
|
|
784
|
+
this.string_expression = this.string_expression.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');
|
|
785
|
+
return this.camelToString(this.string_expression);
|
|
786
|
+
};
|
|
787
|
+
_proto.chain = /*#__PURE__*/function () {
|
|
788
|
+
var _chain = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(input) {
|
|
789
|
+
var _this3 = this;
|
|
790
|
+
var _yield$import, RunnableSequence, RunnableParallel, RunnablePassthrough, mapQueryChain, mapDocChain, parallelChain, sequence;
|
|
791
|
+
return _regeneratorRuntime().wrap(function _callee8$(_context9) {
|
|
792
|
+
while (1) switch (_context9.prev = _context9.next) {
|
|
793
|
+
case 0:
|
|
794
|
+
_context9.next = 2;
|
|
795
|
+
return import('@langchain/core/runnables');
|
|
796
|
+
case 2:
|
|
797
|
+
_yield$import = _context9.sent;
|
|
798
|
+
RunnableSequence = _yield$import.RunnableSequence;
|
|
799
|
+
RunnableParallel = _yield$import.RunnableParallel;
|
|
800
|
+
RunnablePassthrough = _yield$import.RunnablePassthrough;
|
|
801
|
+
mapQueryChain = RunnablePassthrough.assign({
|
|
802
|
+
query: function () {
|
|
803
|
+
var _query = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(input) {
|
|
804
|
+
return _regeneratorRuntime().wrap(function _callee5$(_context6) {
|
|
805
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
806
|
+
case 0:
|
|
807
|
+
return _context6.abrupt("return", _this3.mapQuery.chain({
|
|
808
|
+
input: {
|
|
809
|
+
expression: input.input.expression
|
|
810
|
+
}
|
|
811
|
+
}));
|
|
812
|
+
case 1:
|
|
813
|
+
case "end":
|
|
814
|
+
return _context6.stop();
|
|
815
|
+
}
|
|
816
|
+
}, _callee5);
|
|
817
|
+
}));
|
|
818
|
+
function query(_x1) {
|
|
819
|
+
return _query.apply(this, arguments);
|
|
820
|
+
}
|
|
821
|
+
return query;
|
|
822
|
+
}()
|
|
823
|
+
});
|
|
824
|
+
mapDocChain = RunnablePassthrough.assign({
|
|
825
|
+
documents: function () {
|
|
826
|
+
var _documents = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(input) {
|
|
827
|
+
var textChunks;
|
|
828
|
+
return _regeneratorRuntime().wrap(function _callee6$(_context7) {
|
|
829
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
830
|
+
case 0:
|
|
831
|
+
_context7.next = 2;
|
|
832
|
+
return _this3.textSplitter(input.input.context);
|
|
833
|
+
case 2:
|
|
834
|
+
textChunks = _context7.sent;
|
|
835
|
+
return _context7.abrupt("return", Promise.all(textChunks.map(function (chunk) {
|
|
836
|
+
return _this3.mapDoc.chain({
|
|
837
|
+
input: {
|
|
838
|
+
document: chunk,
|
|
839
|
+
question: input.input.expression
|
|
840
|
+
}
|
|
841
|
+
});
|
|
842
|
+
})));
|
|
843
|
+
case 4:
|
|
844
|
+
case "end":
|
|
845
|
+
return _context7.stop();
|
|
846
|
+
}
|
|
847
|
+
}, _callee6);
|
|
848
|
+
}));
|
|
849
|
+
function documents(_x10) {
|
|
850
|
+
return _documents.apply(this, arguments);
|
|
851
|
+
}
|
|
852
|
+
return documents;
|
|
853
|
+
}()
|
|
854
|
+
});
|
|
855
|
+
parallelChain = RunnableParallel.from([mapQueryChain, mapDocChain]);
|
|
856
|
+
sequence = RunnableSequence.from([parallelChain, (/*#__PURE__*/function () {
|
|
857
|
+
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(results) {
|
|
858
|
+
var query, documents;
|
|
859
|
+
return _regeneratorRuntime().wrap(function _callee7$(_context8) {
|
|
860
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
861
|
+
case 0:
|
|
862
|
+
// results is an array: [{ query: ... }, { documents: [...] }]
|
|
863
|
+
query = results[0].query;
|
|
864
|
+
documents = results[1].documents;
|
|
865
|
+
return _context8.abrupt("return", _this3.reduceChain.chain({
|
|
866
|
+
input: {
|
|
867
|
+
query: query,
|
|
868
|
+
facts: documents
|
|
869
|
+
}
|
|
870
|
+
}));
|
|
871
|
+
case 3:
|
|
872
|
+
case "end":
|
|
873
|
+
return _context8.stop();
|
|
874
|
+
}
|
|
875
|
+
}, _callee7);
|
|
876
|
+
}));
|
|
877
|
+
return function (_x11) {
|
|
878
|
+
return _ref.apply(this, arguments);
|
|
879
|
+
};
|
|
880
|
+
}()), function (result) {
|
|
881
|
+
return _this3.stringToBoolean(result);
|
|
882
|
+
}]);
|
|
883
|
+
return _context9.abrupt("return", sequence.invoke(input));
|
|
884
|
+
case 11:
|
|
885
|
+
case "end":
|
|
886
|
+
return _context9.stop();
|
|
887
|
+
}
|
|
888
|
+
}, _callee8);
|
|
889
|
+
}));
|
|
890
|
+
function chain(_x0) {
|
|
891
|
+
return _chain.apply(this, arguments);
|
|
892
|
+
}
|
|
893
|
+
return chain;
|
|
894
|
+
}();
|
|
895
|
+
return LLMLoop;
|
|
896
|
+
}(BaseChain);
|
|
897
|
+
|
|
898
|
+
export { BaseAgent, BaseChain, BaseTool, LLMLoop };
|
|
510
899
|
//# sourceMappingURL=medpromptjs.esm.js.map
|