medpromptjs 0.3.2 → 0.4.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.
@@ -1,19 +1,73 @@
1
- import { BaseChain } from '../chain';
2
- import { MapQuery } from './map_query';
1
+ import { BaseChain } from './chain';
3
2
  import { CharacterTextSplitter } from '@langchain/textsplitters';
4
- import { ReduceChain } from './reduce';
5
- import { MapDoc } from './map_doc';
6
3
  import { Logger, ILogObj } from 'tslog';
7
4
 
8
5
  const log: Logger<ILogObj> = new Logger();
9
6
  export class LLMLoop extends BaseChain {
10
7
  // Implement the LLM loop logic here
11
- string_expression: string = '';
12
- name: string = 'LLMLoop';
13
- description: string = 'Base LLM Loop.';
14
- mapQuery: MapQuery = new MapQuery(this.container, '', '');
15
- mapDoc: MapDoc = new MapDoc(this.container, '', '');
16
- reduceChain: ReduceChain = new ReduceChain(this.container, '', '');
8
+ stringExpression: string = '';
9
+ _mapQueryTemplate: string = `
10
+ You are an assistant that can convert statements to a natural language query as in the example below.\n
11
+
12
+
13
+ Statements: Visual foot exam in the last month. Visual foot exam showed active infection.
14
+ query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?
15
+ Statements: {expression}
16
+ query: `;
17
+
18
+ _mapDocTemplate: string = `
19
+ You will be given a document and few statements.\n
20
+ Extract facts from the document that are relevant to the statements.\n
21
+ Do not include any irrelevant information or context.\n
22
+
23
+
24
+ Example:\n
25
+ 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
26
+ statements: diagnosis of myocardial infarction. currently on beta-blockers.\n
27
+ facts: The patient was diagnosed with myocardial infarction and is currently on beta-blockers.\n
28
+ document: {document} \n
29
+ statements: {statements}\n
30
+ facts: `;
31
+
32
+ _reduceChainTemplate: string = `
33
+ Say yes if the facts mentions all aspects of the query, else say no.\n
34
+
35
+ Example:
36
+ facts: Patient had a laproscopy 27 days back. The findings were normal. \n
37
+ query: Did the patient have a laproscopy this month?\n
38
+ answer: YES. The patient had a laproscopy 27 days back.\n
39
+ facts: The patient is a diabetic and hypertensive. He is on metformin and amlodipine. \n
40
+ query: Is the patient on heparin and metformin?\n
41
+ answer: NO. The patient is on heparin but not on metformin.\n
42
+ 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
43
+ query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?\n
44
+ answer: YES. The patient had a visual foot exam in the last month and there is evidence of active infection.\n
45
+ 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
46
+ query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?\n
47
+ answer: NO. The patient did not have a visual foot exam in the last month but there is evidence of active infection.\n
48
+ facts: {facts} \n
49
+ query: {query}\n
50
+ answer: `;
51
+
52
+ // Add getters and setters for the templates if needed
53
+ get mapQueryTemplate(): string {
54
+ return this._mapQueryTemplate;
55
+ }
56
+ set mapQueryTemplate(template: string) {
57
+ this._mapQueryTemplate = template;
58
+ }
59
+ get mapDocTemplate(): string {
60
+ return this._mapDocTemplate;
61
+ }
62
+ set mapDocTemplate(template: string) {
63
+ this._mapDocTemplate = template;
64
+ }
65
+ get reduceChainTemplate(): string {
66
+ return this._reduceChainTemplate;
67
+ }
68
+ set reduceChainTemplate(template: string) {
69
+ this._reduceChainTemplate = template;
70
+ }
17
71
 
18
72
  async checkAssertion(expression: string, context: any): Promise<boolean> {
19
73
  let _expression: string = '';
@@ -191,7 +245,7 @@ export class LLMLoop extends BaseChain {
191
245
  if (typeof obj[key] === 'object') {
192
246
  this._printValues(obj[key]);
193
247
  } else {
194
- this.string_expression += obj[key] + ' ';
248
+ this.stringExpression += obj[key] + ' ';
195
249
  }
196
250
  }
197
251
  const _eliminate = [
@@ -211,13 +265,13 @@ export class LLMLoop extends BaseChain {
211
265
  ];
212
266
  _eliminate.forEach((element) => {
213
267
  const regex = new RegExp(element, 'g');
214
- this.string_expression = this.string_expression.replace(regex, '');
268
+ this.stringExpression = this.stringExpression.replace(regex, '');
215
269
  });
216
- this.string_expression = this.string_expression.replace(
270
+ this.stringExpression = this.stringExpression.replace(
217
271
  /(?:https?|ftp):\/\/[\n\S]+/g,
218
272
  ''
219
273
  );
220
- return this.camelToString(this.string_expression);
274
+ return this.camelToString(this.stringExpression);
221
275
  }
222
276
 
223
277
  async chain(input: any) {
@@ -230,11 +284,21 @@ export class LLMLoop extends BaseChain {
230
284
  const { RunnableSequence, RunnableParallel, RunnablePassthrough } =
231
285
  await import('@langchain/core/runnables');
232
286
 
287
+ const mapQuery: BaseChain = new BaseChain(this.container);
288
+ const mapDoc: BaseChain = new BaseChain(this.container);
289
+ const reduceChain: BaseChain = new BaseChain(this.container);
290
+ mapQuery.name = 'MapQuery';
291
+ mapQuery.description = 'Map the expression to a natural language query.';
292
+ mapQuery.template = this._mapQueryTemplate;
293
+ mapDoc.name = 'MapDoc';
294
+ mapDoc.description = 'Map the document to a set of facts.';
295
+ mapDoc.template = this._mapDocTemplate;
296
+ reduceChain.name = 'ReduceChain';
297
+ reduceChain.description = 'Reduce a set of documents to binary answer.';
298
+ reduceChain.template = this._reduceChainTemplate;
233
299
  const mapQueryChain = RunnablePassthrough.assign({
234
300
  query: async (input: any) =>
235
- this.mapQuery.chain({
236
- input: { expression: input.input.expression },
237
- }),
301
+ mapQuery.chain({ expression: input.input.expression }),
238
302
  });
239
303
 
240
304
  const mapDocChain = RunnablePassthrough.assign({
@@ -242,8 +306,9 @@ export class LLMLoop extends BaseChain {
242
306
  const textChunks = await this.textSplitter(input.input.context);
243
307
  return Promise.all(
244
308
  textChunks.map((chunk) =>
245
- this.mapDoc.chain({
246
- input: { document: chunk, statements: input.input.expression },
309
+ mapDoc.chain({
310
+ document: chunk,
311
+ statements: input.input.expression,
247
312
  })
248
313
  )
249
314
  );
@@ -279,11 +344,9 @@ export class LLMLoop extends BaseChain {
279
344
  const expression = results[0].input.expression;
280
345
  const query = results[0].query;
281
346
  const documents = results[1].documents;
282
- return this.reduceChain.chain({
283
- input: {
284
- facts: documents,
285
- query: query + ' ' + expression,
286
- },
347
+ return reduceChain.chain({
348
+ facts: documents,
349
+ query: query + ' ' + expression,
287
350
  });
288
351
  },
289
352
  (result: any) => this.stringToBoolean(result),
package/src/tool.ts CHANGED
@@ -18,6 +18,7 @@ import { DynamicStructuredTool } from 'langchain/tools';
18
18
  import mydi from './mydi';
19
19
 
20
20
  export class BaseTool extends DynamicStructuredTool {
21
+ /** @hidden */
21
22
  container: any;
22
23
  name: string;
23
24
  description: string;
@@ -65,4 +66,14 @@ export class BaseTool extends DynamicStructuredTool {
65
66
  })
66
67
  .replace(/\s+/g, '');
67
68
  }
69
+
70
+ /**
71
+ * @param input - The input to process in batch.
72
+ * @param config - The configuration for the batch.
73
+ * @returns A promise resolving to an array of results.
74
+ */
75
+ async _batchWithConfig(input: any, config: any): Promise<any[]> {
76
+ // Call the parent implementation or provide your own
77
+ return super._batchWithConfig(input, config);
78
+ }
68
79
  }
@@ -1,4 +0,0 @@
1
- export { MapQuery } from './map_query';
2
- export { MapDoc } from './map_doc';
3
- export { ReduceChain } from './reduce';
4
- export { LLMLoop } from './base';
@@ -1,6 +0,0 @@
1
- import { BaseChain } from '../chain';
2
- export declare class MapDoc extends BaseChain {
3
- name: string;
4
- description: string;
5
- template: string;
6
- }
@@ -1,6 +0,0 @@
1
- import { BaseChain } from '../chain';
2
- export declare class MapQuery extends BaseChain {
3
- name: string;
4
- description: string;
5
- template: string;
6
- }
@@ -1,6 +0,0 @@
1
- import { BaseChain } from '../chain';
2
- export declare class ReduceChain extends BaseChain {
3
- name: string;
4
- description: string;
5
- template: string;
6
- }
@@ -1,4 +0,0 @@
1
- export { MapQuery } from './map_query';
2
- export { MapDoc } from './map_doc';
3
- export { ReduceChain } from './reduce';
4
- export { LLMLoop } from './base';
@@ -1,19 +0,0 @@
1
- import { BaseChain } from '../chain';
2
-
3
- export class MapDoc extends BaseChain {
4
- name: string = 'MapDoc';
5
- description: string = 'Map a set of documents to facts.';
6
- template: string = `
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
16
- document: {document} \n
17
- statements: {statements}\n
18
- facts: `;
19
- }
@@ -1,14 +0,0 @@
1
- import { BaseChain } from '../chain';
2
-
3
- export class MapQuery extends BaseChain {
4
- name: string = 'MapQuery';
5
- description: string = 'Map a CQL query to a natural language.';
6
- template: string = `
7
- You are an assistant that can convert statements to a natural language query as in the example below.\n
8
-
9
-
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: `;
14
- }
@@ -1,25 +0,0 @@
1
- import { BaseChain } from '../chain';
2
-
3
- export class ReduceChain extends BaseChain {
4
- name: string = 'Reduce';
5
- description: string = 'Reduce a set of documents to binary answer.';
6
- template: string = `
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
22
- facts: {facts} \n
23
- query: {query}\n
24
- answer: `;
25
- }