medpromptjs 0.3.0 → 0.3.2
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/index.d.ts +1 -0
- package/dist/llm_loop/base.d.ts +5 -3
- package/dist/llm_loop/index.d.ts +3 -0
- package/dist/medpromptjs.cjs.development.js +428 -2
- 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 +428 -3
- package/dist/medpromptjs.esm.js.map +1 -1
- package/package.json +5 -3
- package/src/index.ts +1 -0
- package/src/llm_loop/base.ts +107 -13
- package/src/llm_loop/index.ts +3 -0
- package/src/llm_loop/map_doc.ts +11 -4
- package/src/llm_loop/map_query.ts +5 -27
- package/src/llm_loop/reduce.ts +17 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "medpromptjs",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "A library of langchain base classes for building medical applications.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Bell Eapen",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"start": "dts watch",
|
|
33
33
|
"test": "dts test --verbose --testPathIgnorePatterns=./test/e2e/*",
|
|
34
34
|
"e2e": "dts test --verbose ./test/e2e --detectOpenHandles",
|
|
35
|
-
"docs": "typedoc --out docs src"
|
|
35
|
+
"docs": "typedoc --plugin typedoc-plugin-markdown --out docs src"
|
|
36
36
|
},
|
|
37
37
|
"husky": {
|
|
38
38
|
"hooks": {
|
|
@@ -75,11 +75,13 @@
|
|
|
75
75
|
"supertest": "^7.1.1",
|
|
76
76
|
"ts-mockito": "^2.6.1",
|
|
77
77
|
"tslib": "^2.6.2",
|
|
78
|
-
"typedoc": "^0.28.
|
|
78
|
+
"typedoc": "^0.28.5",
|
|
79
|
+
"typedoc-plugin-markdown": "^4.6.3",
|
|
79
80
|
"typescript": "^5.3.3"
|
|
80
81
|
},
|
|
81
82
|
"dependencies": {
|
|
82
83
|
"@langchain/community": "0.3.44",
|
|
84
|
+
"@langchain/google-genai": "^0.2.9",
|
|
83
85
|
"cors": "^2.8.5",
|
|
84
86
|
"dotenv": "^16.3.1",
|
|
85
87
|
"express": "^5.1.0",
|
package/src/index.ts
CHANGED
package/src/llm_loop/base.ts
CHANGED
|
@@ -15,25 +15,60 @@ export class LLMLoop extends BaseChain {
|
|
|
15
15
|
mapDoc: MapDoc = new MapDoc(this.container, '', '');
|
|
16
16
|
reduceChain: ReduceChain = new ReduceChain(this.container, '', '');
|
|
17
17
|
|
|
18
|
-
async checkAssertion(expression: string, context:
|
|
18
|
+
async checkAssertion(expression: string, context: any): Promise<boolean> {
|
|
19
|
+
let _expression: string = '';
|
|
20
|
+
try {
|
|
21
|
+
// Check if expression is a valid JSON string
|
|
22
|
+
_expression = this.printValues(JSON.parse(expression));
|
|
23
|
+
} catch (error) {
|
|
24
|
+
_expression = expression; // If not, use the expression as is
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
log.info(`Checking assertion with expression: ${_expression}`);
|
|
28
|
+
// Initialize context and content
|
|
29
|
+
let _context: any = undefined;
|
|
30
|
+
let _content: string = '';
|
|
31
|
+
try {
|
|
32
|
+
_context = JSON.parse(context);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
_context = context; // If not, use the context as is
|
|
35
|
+
log.warn(`Context is not a valid JSON string: ${context}`);
|
|
36
|
+
log.warn(`Using context as a string: ${_context}`);
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
// Check if context is a valid JSON string
|
|
40
|
+
_context = JSON.parse(context);
|
|
41
|
+
_context.forEach((element: any) => {
|
|
42
|
+
element.content.forEach((contentItem: any) => {
|
|
43
|
+
_content += atob(contentItem.attachment.data.value);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
} catch (error) {
|
|
47
|
+
_content = context; // If not, use the context as is
|
|
48
|
+
log.warn(`Context is not a valid JSON : ${context}`);
|
|
49
|
+
log.warn(`Using context as a string: ${_content}`);
|
|
50
|
+
}
|
|
51
|
+
_content = this.findDatesAndConvertToTimeElapsed(
|
|
52
|
+
_content.replace(/(\r\n|\n|\r)/gm, ' ')
|
|
53
|
+
);
|
|
54
|
+
log.info(`Checking assertion with context: ${_content}`);
|
|
19
55
|
const _input = {
|
|
20
56
|
input: {
|
|
21
|
-
expression:
|
|
22
|
-
context:
|
|
23
|
-
this.camelToString(context)
|
|
24
|
-
),
|
|
57
|
+
expression: _expression,
|
|
58
|
+
context: _content,
|
|
25
59
|
},
|
|
26
60
|
};
|
|
27
|
-
|
|
28
|
-
|
|
61
|
+
const result = await this.chain(_input);
|
|
62
|
+
log.info(`Assertion check result: ${result}`);
|
|
63
|
+
return result;
|
|
29
64
|
}
|
|
30
65
|
|
|
31
|
-
async checkMention(expression: string, context:
|
|
66
|
+
async checkMention(expression: string, context: any): Promise<boolean> {
|
|
32
67
|
if (expression && context) return true; // Placeholder for assertion check logic
|
|
33
68
|
return false;
|
|
34
69
|
}
|
|
35
70
|
|
|
36
|
-
async checkNegation(expression: string, context:
|
|
71
|
+
async checkNegation(expression: string, context: any): Promise<boolean> {
|
|
37
72
|
if (expression && context) return true; // Placeholder for assertion check logic
|
|
38
73
|
return false;
|
|
39
74
|
}
|
|
@@ -77,6 +112,9 @@ export class LLMLoop extends BaseChain {
|
|
|
77
112
|
'certainly',
|
|
78
113
|
'definitely',
|
|
79
114
|
'indeed',
|
|
115
|
+
'did have',
|
|
116
|
+
'showed',
|
|
117
|
+
'revealed',
|
|
80
118
|
];
|
|
81
119
|
const lowerStr = str.toLowerCase();
|
|
82
120
|
return positiveKeywords.some((keyword) => lowerStr.includes(keyword));
|
|
@@ -114,9 +152,44 @@ export class LLMLoop extends BaseChain {
|
|
|
114
152
|
}
|
|
115
153
|
|
|
116
154
|
printValues(obj: any) {
|
|
155
|
+
log.info(`Processing Rule: ${obj.name} for ${obj.context}`);
|
|
156
|
+
// For each obj.expression:arg:where
|
|
157
|
+
if (obj.expression && obj.expression.arg && obj.expression.arg.where) {
|
|
158
|
+
// for each args in obj.expression.arg.where, extract the nested key called "value"
|
|
159
|
+
const statements = this.extractObjectsByKey(
|
|
160
|
+
obj.expression.arg.where,
|
|
161
|
+
'value'
|
|
162
|
+
);
|
|
163
|
+
return statements.join(' ').replace(/,/g, ' ');
|
|
164
|
+
}
|
|
165
|
+
// return statements array as a string seperated by space
|
|
166
|
+
return '';
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
extractObjectsByKey(obj: any, key: string): any[] {
|
|
170
|
+
const results: any[] = [];
|
|
171
|
+
|
|
172
|
+
function traverse(obj: any) {
|
|
173
|
+
for (const currentKey in obj) {
|
|
174
|
+
if (obj.hasOwnProperty(currentKey)) {
|
|
175
|
+
if (currentKey === key) {
|
|
176
|
+
results.push(obj[currentKey]);
|
|
177
|
+
}
|
|
178
|
+
if (typeof obj[currentKey] === 'object' && obj[currentKey] !== null) {
|
|
179
|
+
traverse(obj[currentKey]);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
traverse(obj);
|
|
186
|
+
return results;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
_printValues(obj: any) {
|
|
117
190
|
for (var key in obj) {
|
|
118
191
|
if (typeof obj[key] === 'object') {
|
|
119
|
-
this.
|
|
192
|
+
this._printValues(obj[key]);
|
|
120
193
|
} else {
|
|
121
194
|
this.string_expression += obj[key] + ' ';
|
|
122
195
|
}
|
|
@@ -170,7 +243,7 @@ export class LLMLoop extends BaseChain {
|
|
|
170
243
|
return Promise.all(
|
|
171
244
|
textChunks.map((chunk) =>
|
|
172
245
|
this.mapDoc.chain({
|
|
173
|
-
input: { document: chunk,
|
|
246
|
+
input: { document: chunk, statements: input.input.expression },
|
|
174
247
|
})
|
|
175
248
|
)
|
|
176
249
|
);
|
|
@@ -182,13 +255,34 @@ export class LLMLoop extends BaseChain {
|
|
|
182
255
|
const sequence = RunnableSequence.from([
|
|
183
256
|
parallelChain,
|
|
184
257
|
async (results: any) => {
|
|
185
|
-
|
|
258
|
+
/* results is an array: [{ query: ... }, { documents: [...] }]
|
|
259
|
+
{
|
|
260
|
+
"0": {
|
|
261
|
+
"input": {
|
|
262
|
+
"expression": "0 Visual foot exam showed active infection?",
|
|
263
|
+
"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. "
|
|
264
|
+
},
|
|
265
|
+
"query": "The text indicates a question about whether a visual foot exam revealed an active infection.\n"
|
|
266
|
+
},
|
|
267
|
+
"1": {
|
|
268
|
+
"input": {
|
|
269
|
+
"expression": "0 Visual foot exam showed active infection?",
|
|
270
|
+
"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. "
|
|
271
|
+
},
|
|
272
|
+
"documents": [
|
|
273
|
+
"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"
|
|
274
|
+
]
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
*/
|
|
278
|
+
log.info(`Results from parallel chain: ${JSON.stringify(results)}`);
|
|
279
|
+
const expression = results[0].input.expression;
|
|
186
280
|
const query = results[0].query;
|
|
187
281
|
const documents = results[1].documents;
|
|
188
282
|
return this.reduceChain.chain({
|
|
189
283
|
input: {
|
|
190
|
-
query: query,
|
|
191
284
|
facts: documents,
|
|
285
|
+
query: query + ' ' + expression,
|
|
192
286
|
},
|
|
193
287
|
});
|
|
194
288
|
},
|
package/src/llm_loop/index.ts
CHANGED
package/src/llm_loop/map_doc.ts
CHANGED
|
@@ -4,9 +4,16 @@ export class MapDoc extends BaseChain {
|
|
|
4
4
|
name: string = 'MapDoc';
|
|
5
5
|
description: string = 'Map a set of documents to facts.';
|
|
6
6
|
template: string = `
|
|
7
|
-
You will be given a document and
|
|
8
|
-
|
|
9
|
-
Do not include
|
|
7
|
+
You will be given a document and few statements.\n
|
|
8
|
+
Extract facts from the document that are relevant to the statements.\n
|
|
9
|
+
Do not include any irrelevant information or context.\n
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Example:\n
|
|
13
|
+
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
|
|
14
|
+
statements: diagnosis of myocardial infarction. currently on beta-blockers.\n
|
|
15
|
+
facts: The patient was diagnosed with myocardial infarction and is currently on beta-blockers.\n
|
|
10
16
|
document: {document} \n
|
|
11
|
-
|
|
17
|
+
statements: {statements}\n
|
|
18
|
+
facts: `;
|
|
12
19
|
}
|
|
@@ -4,33 +4,11 @@ export class MapQuery extends BaseChain {
|
|
|
4
4
|
name: string = 'MapQuery';
|
|
5
5
|
description: string = 'Map a CQL query to a natural language.';
|
|
6
6
|
template: string = `
|
|
7
|
-
|
|
8
|
-
You should give a single line answer>> as in the example below.
|
|
9
|
-
Example:
|
|
7
|
+
You are an assistant that can convert statements to a natural language query as in the example below.\n
|
|
10
8
|
|
|
11
|
-
CQL: exists (
|
|
12
|
-
[DocumentReference] D
|
|
13
|
-
where D.allergies="Penicillin"
|
|
14
|
-
and D.complaint="Headache"
|
|
15
|
-
and D.complaint="Weakness" or D.complaint="Numbness"
|
|
16
|
-
and D.findings="Intact sensation to light touch"
|
|
17
|
-
)
|
|
18
9
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
where D.diagnosis="Diverticulitis"
|
|
24
|
-
and D.complaint="Fever"
|
|
25
|
-
and D.procedure="Colon resection"
|
|
26
|
-
and not D.finding="Fluid collection"
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
answer>> Diverticulitis diagnosis, Fever complaint, Colon resection procedure, and no Fluid collection finding?
|
|
30
|
-
|
|
31
|
-
Now convert the following CQL query to a natural language.
|
|
32
|
-
CQL: {expression}
|
|
33
|
-
|
|
34
|
-
answer>>
|
|
35
|
-
`;
|
|
10
|
+
Statements: Visual foot exam in the last month. Visual foot exam showed active infection.
|
|
11
|
+
query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?
|
|
12
|
+
Statements: {expression}
|
|
13
|
+
query: `;
|
|
36
14
|
}
|
package/src/llm_loop/reduce.ts
CHANGED
|
@@ -4,9 +4,22 @@ export class ReduceChain extends BaseChain {
|
|
|
4
4
|
name: string = 'Reduce';
|
|
5
5
|
description: string = 'Reduce a set of documents to binary answer.';
|
|
6
6
|
template: string = `
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
Say yes if the facts mentions all aspects of the query, else say no.\n
|
|
8
|
+
|
|
9
|
+
Example:
|
|
10
|
+
facts: Patient had a laproscopy 27 days back. The findings were normal. \n
|
|
11
|
+
query: Did the patient have a laproscopy this month?\n
|
|
12
|
+
answer: YES. The patient had a laproscopy 27 days back.\n
|
|
13
|
+
facts: The patient is a diabetic and hypertensive. He is on metformin and amlodipine. \n
|
|
14
|
+
query: Is the patient on heparin and metformin?\n
|
|
15
|
+
answer: NO. The patient is on heparin but not on metformin.\n
|
|
16
|
+
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
|
|
17
|
+
query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?\n
|
|
18
|
+
answer: YES. The patient had a visual foot exam in the last month and there is evidence of active infection.\n
|
|
19
|
+
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
|
|
20
|
+
query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?\n
|
|
21
|
+
answer: NO. The patient did not have a visual foot exam in the last month but there is evidence of active infection.\n
|
|
10
22
|
facts: {facts} \n
|
|
11
|
-
|
|
23
|
+
query: {query}\n
|
|
24
|
+
answer: `;
|
|
12
25
|
}
|