medpromptjs 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chain.d.ts +4 -3
- package/dist/llm_loop/base.d.ts +21 -0
- package/dist/llm_loop/index.d.ts +1 -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 +109 -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 +110 -79
- package/dist/medpromptjs.esm.js.map +1 -1
- package/package.json +12 -10
- package/src/chain.ts +27 -13
- package/src/llm_loop/base.ts +200 -0
- package/src/llm_loop/index.ts +1 -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/chain.d.ts
CHANGED
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { PromptTemplate } from '@langchain/core/prompts';
|
|
17
16
|
import { LLM } from '@langchain/core/language_models/llms';
|
|
18
17
|
import type { ToolInterface } from '@langchain/core/tools';
|
|
19
18
|
export declare class BaseChain {
|
|
@@ -21,12 +20,14 @@ export declare class BaseChain {
|
|
|
21
20
|
tools: ToolInterface[];
|
|
22
21
|
name: string;
|
|
23
22
|
description: string;
|
|
24
|
-
prompt:
|
|
23
|
+
prompt: any;
|
|
25
24
|
llm: LLM;
|
|
26
25
|
template: string;
|
|
26
|
+
chat_model: boolean;
|
|
27
|
+
runnable: any;
|
|
27
28
|
constructor(container: any, name: string, description: string, template?: string);
|
|
28
29
|
resolve(name: string): any;
|
|
29
30
|
camelize(str: string): string;
|
|
30
31
|
snake_case(str: string): string;
|
|
31
|
-
chain(input: any): Promise<
|
|
32
|
+
chain(input: any): Promise<any>;
|
|
32
33
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { BaseChain } from '../chain';
|
|
2
|
+
import { MapQuery } from './map_query';
|
|
3
|
+
import { ReduceChain } from './reduce';
|
|
4
|
+
import { MapDoc } from './map_doc';
|
|
5
|
+
export declare class LLMLoop extends BaseChain {
|
|
6
|
+
string_expression: string;
|
|
7
|
+
name: string;
|
|
8
|
+
description: string;
|
|
9
|
+
mapQuery: MapQuery;
|
|
10
|
+
mapDoc: MapDoc;
|
|
11
|
+
reduceChain: ReduceChain;
|
|
12
|
+
checkAssertion(expression: string, context: string): Promise<boolean>;
|
|
13
|
+
checkMention(expression: string, context: string): Promise<boolean>;
|
|
14
|
+
checkNegation(expression: string, context: string): Promise<boolean>;
|
|
15
|
+
findDatesAndConvertToTimeElapsed(text: string): string;
|
|
16
|
+
camelToString(camelCase: string): string;
|
|
17
|
+
stringToBoolean(str: string): boolean;
|
|
18
|
+
textSplitter(text: string, chunkSize?: number, chunkOverlap?: number): Promise<string[]>;
|
|
19
|
+
printValues(obj: any): string;
|
|
20
|
+
chain(input: any): Promise<boolean>;
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MapQuery } from './map_query';
|
|
@@ -4,87 +4,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var prompts = require('@langchain/core/prompts');
|
|
6
6
|
var runnables = require('@langchain/core/runnables');
|
|
7
|
+
var messages = require('@langchain/core/messages');
|
|
7
8
|
var agents = require('langchain/agents');
|
|
8
9
|
var tools = require('langchain/tools');
|
|
9
10
|
|
|
10
|
-
/**
|
|
11
|
-
* Copyright 2024 Bell Eapen
|
|
12
|
-
*
|
|
13
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
14
|
-
* you may not use this file except in compliance with the License.
|
|
15
|
-
* You may obtain a copy of the License at
|
|
16
|
-
*
|
|
17
|
-
* https://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
-
*
|
|
19
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
20
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
21
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
22
|
-
* See the License for the specific language governing permissions and
|
|
23
|
-
* limitations under the License.
|
|
24
|
-
*/
|
|
25
|
-
var mydi = function mydi(c, m, b) {
|
|
26
|
-
try {
|
|
27
|
-
return c.resolve(m + '_' + b);
|
|
28
|
-
} catch (e) {
|
|
29
|
-
return c.resolve(b);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Copyright 2024 Bell Eapen
|
|
35
|
-
*
|
|
36
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
37
|
-
* you may not use this file except in compliance with the License.
|
|
38
|
-
* You may obtain a copy of the License at
|
|
39
|
-
*
|
|
40
|
-
* https://www.apache.org/licenses/LICENSE-2.0
|
|
41
|
-
*
|
|
42
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
43
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
44
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
45
|
-
* See the License for the specific language governing permissions and
|
|
46
|
-
* limitations under the License.
|
|
47
|
-
*/
|
|
48
|
-
var BaseChain = /*#__PURE__*/function () {
|
|
49
|
-
function BaseChain(container, name, description, template) {
|
|
50
|
-
this.container = void 0;
|
|
51
|
-
this.tools = void 0;
|
|
52
|
-
this.name = void 0;
|
|
53
|
-
this.description = void 0;
|
|
54
|
-
this.prompt = void 0;
|
|
55
|
-
this.llm = void 0;
|
|
56
|
-
this.template = "\n Summarize the following text:\n {input}\n ";
|
|
57
|
-
this.container = container;
|
|
58
|
-
this.name = name === '' ? this.camelize(this.constructor.name) : name;
|
|
59
|
-
this.description = description === '' ? this.snake_case(this.constructor.name) : description;
|
|
60
|
-
this.tools = this.resolve('tools');
|
|
61
|
-
this.prompt = this.resolve('prompt') !== '' ? this.resolve('prompt') : prompts.PromptTemplate.fromTemplate(this.template);
|
|
62
|
-
this.llm = this.resolve('main-llm');
|
|
63
|
-
}
|
|
64
|
-
var _proto = BaseChain.prototype;
|
|
65
|
-
_proto.resolve = function resolve(name) {
|
|
66
|
-
return mydi(this.container, this.name, name);
|
|
67
|
-
};
|
|
68
|
-
_proto.camelize = function camelize(str) {
|
|
69
|
-
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
70
|
-
return index === 0 ? word.toLowerCase() : word.toUpperCase();
|
|
71
|
-
}).replace(/\s+/g, '');
|
|
72
|
-
};
|
|
73
|
-
_proto.snake_case = function snake_case(str) {
|
|
74
|
-
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
75
|
-
return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();
|
|
76
|
-
}).replace(/\s+/g, '');
|
|
77
|
-
}
|
|
78
|
-
// https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/
|
|
79
|
-
//! Override this method
|
|
80
|
-
;
|
|
81
|
-
_proto.chain = function chain(input) {
|
|
82
|
-
var _chain = runnables.RunnableSequence.from([new runnables.RunnablePassthrough(), this.prompt, this.llm]);
|
|
83
|
-
return _chain.invoke(input);
|
|
84
|
-
};
|
|
85
|
-
return BaseChain;
|
|
86
|
-
}();
|
|
87
|
-
|
|
88
11
|
function asyncGeneratorStep(n, t, e, r, o, a, c) {
|
|
89
12
|
try {
|
|
90
13
|
var i = n[a](c),
|
|
@@ -388,6 +311,114 @@ function _setPrototypeOf(t, e) {
|
|
|
388
311
|
}, _setPrototypeOf(t, e);
|
|
389
312
|
}
|
|
390
313
|
|
|
314
|
+
/**
|
|
315
|
+
* Copyright 2024 Bell Eapen
|
|
316
|
+
*
|
|
317
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
318
|
+
* you may not use this file except in compliance with the License.
|
|
319
|
+
* You may obtain a copy of the License at
|
|
320
|
+
*
|
|
321
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
322
|
+
*
|
|
323
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
324
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
325
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
326
|
+
* See the License for the specific language governing permissions and
|
|
327
|
+
* limitations under the License.
|
|
328
|
+
*/
|
|
329
|
+
var mydi = function mydi(c, m, b) {
|
|
330
|
+
try {
|
|
331
|
+
return c.resolve(m + '_' + b);
|
|
332
|
+
} catch (e) {
|
|
333
|
+
return c.resolve(b);
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
var BaseChain = /*#__PURE__*/function () {
|
|
338
|
+
function BaseChain(container, name, description, template) {
|
|
339
|
+
if (template === void 0) {
|
|
340
|
+
template = "\n Summarize the following text:\n {input}\n ";
|
|
341
|
+
}
|
|
342
|
+
this.container = void 0;
|
|
343
|
+
this.tools = void 0;
|
|
344
|
+
this.name = void 0;
|
|
345
|
+
this.description = void 0;
|
|
346
|
+
this.prompt = void 0;
|
|
347
|
+
this.llm = void 0;
|
|
348
|
+
this.template = void 0;
|
|
349
|
+
this.chat_model = void 0;
|
|
350
|
+
this.runnable = void 0;
|
|
351
|
+
this.container = container;
|
|
352
|
+
this.name = name === '' ? this.camelize(this.constructor.name) : name;
|
|
353
|
+
this.description = description === '' ? this.snake_case(this.constructor.name) : description;
|
|
354
|
+
this.template = template;
|
|
355
|
+
this.tools = this.resolve('tools');
|
|
356
|
+
this.llm = this.resolve('main-llm');
|
|
357
|
+
this.chat_model = false;
|
|
358
|
+
try {
|
|
359
|
+
this.chat_model = this.resolve('chat_model');
|
|
360
|
+
} catch (e) {}
|
|
361
|
+
if (this.chat_model) {
|
|
362
|
+
this.prompt = prompts.ChatPromptTemplate.fromTemplate(this.template);
|
|
363
|
+
} else {
|
|
364
|
+
this.prompt = prompts.PromptTemplate.fromTemplate(this.template);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
var _proto = BaseChain.prototype;
|
|
368
|
+
_proto.resolve = function resolve(name) {
|
|
369
|
+
return mydi(this.container, this.name, name);
|
|
370
|
+
};
|
|
371
|
+
_proto.camelize = function camelize(str) {
|
|
372
|
+
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
373
|
+
return index === 0 ? word.toLowerCase() : word.toUpperCase();
|
|
374
|
+
}).replace(/\s+/g, '');
|
|
375
|
+
};
|
|
376
|
+
_proto.snake_case = function snake_case(str) {
|
|
377
|
+
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
378
|
+
return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();
|
|
379
|
+
}).replace(/\s+/g, '');
|
|
380
|
+
}
|
|
381
|
+
// https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/
|
|
382
|
+
//! Override this method
|
|
383
|
+
;
|
|
384
|
+
_proto.chain =
|
|
385
|
+
/*#__PURE__*/
|
|
386
|
+
function () {
|
|
387
|
+
var _chain = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
|
|
388
|
+
var response, aiMessage;
|
|
389
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
390
|
+
while (1) switch (_context.prev = _context.next) {
|
|
391
|
+
case 0:
|
|
392
|
+
this.runnable = runnables.RunnableSequence.from([new runnables.RunnablePassthrough(), this.prompt, this.llm]);
|
|
393
|
+
if (!this.chat_model) {
|
|
394
|
+
_context.next = 7;
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
_context.next = 4;
|
|
398
|
+
return this.runnable.invoke(input);
|
|
399
|
+
case 4:
|
|
400
|
+
response = _context.sent;
|
|
401
|
+
aiMessage = new messages.AIMessage(response);
|
|
402
|
+
return _context.abrupt("return", aiMessage.content);
|
|
403
|
+
case 7:
|
|
404
|
+
_context.next = 9;
|
|
405
|
+
return this.runnable.invoke(input);
|
|
406
|
+
case 9:
|
|
407
|
+
return _context.abrupt("return", _context.sent);
|
|
408
|
+
case 10:
|
|
409
|
+
case "end":
|
|
410
|
+
return _context.stop();
|
|
411
|
+
}
|
|
412
|
+
}, _callee, this);
|
|
413
|
+
}));
|
|
414
|
+
function chain(_x) {
|
|
415
|
+
return _chain.apply(this, arguments);
|
|
416
|
+
}
|
|
417
|
+
return chain;
|
|
418
|
+
}();
|
|
419
|
+
return BaseChain;
|
|
420
|
+
}();
|
|
421
|
+
|
|
391
422
|
var BaseAgent = /*#__PURE__*/function () {
|
|
392
423
|
function BaseAgent(container) {
|
|
393
424
|
this.container = void 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"medpromptjs.cjs.development.js","sources":["../src/mydi.ts","../src/chain.ts","../src/agent.ts","../src/tool.ts"],"sourcesContent":["/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nconst mydi = (c: any, m: string, b: string) => {\n try {\n return c.resolve(m + '_' + b);\n } catch (e) {\n return c.resolve(b);\n }\n};\n\nexport default mydi;\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\nimport {\n RunnablePassthrough,\n RunnableSequence,\n} from '@langchain/core/runnables';\n\nexport class BaseChain {\n container: any;\n tools: ToolInterface[];\n name: string;\n description: string;\n prompt: PromptTemplate;\n llm: LLM;\n template: string = `\n Summarize the following text:\n {input}\n `;\n\n constructor(\n container: any,\n name: string,\n description: string,\n template: string = ''\n ) {\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.tools = this.resolve('tools');\n this.prompt =\n this.resolve('prompt') !== ''\n ? this.resolve('prompt')\n : PromptTemplate.fromTemplate(this.template);\n this.llm = this.resolve('main-llm');\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n // https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/\n //! Override this method\n chain(input: any) {\n const _chain = RunnableSequence.from([\n new RunnablePassthrough(),\n this.prompt,\n this.llm,\n ]);\n return _chain.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport {\n AgentExecutor,\n createReactAgent,\n createStructuredChatAgent,\n} from 'langchain/agents';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\n\nexport class BaseAgent {\n container: any;\n tools: ToolInterface[];\n name: string;\n prompt: PromptTemplate;\n llm: LLM;\n template: string = `\n ## Question: {question}\n ## Answer: {answer}\n Given the above question and answer, generate a chain of thought explanation for the answer.\n First, start with the model generated chain of thought explanation.\n End the chain of though explanation with:\n Therefore, the answer is {answer}.\n `;\n\n constructor(container: any) {\n this.container = container;\n this.name = this.camelize(this.constructor.name);\n this.tools = this.resolve('tools');\n this.prompt =\n this.resolve('prompt') !== ''\n ? this.resolve('prompt')\n : PromptTemplate.fromTemplate(this.template);\n this.llm = this.resolve('main-llm');\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n async run(input: any) {\n const agent = await createStructuredChatAgent({\n llm: this.llm,\n tools: this.tools,\n prompt: this.prompt,\n });\n const agentExecutor = new AgentExecutor({\n agent: agent,\n tools: this.tools,\n // verbose: true,\n handleParsingErrors:\n 'Please try again, paying close attention to the final answer',\n });\n return await agentExecutor.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DynamicStructuredTool } from 'langchain/tools';\nimport mydi from './mydi';\n\nexport class BaseTool extends DynamicStructuredTool {\n container: any;\n name: string;\n description: string;\n schema: any;\n constructor(\n container: any,\n name: string,\n description: string,\n schema: any,\n func: any = null\n ) {\n super({\n name: name,\n description: description,\n schema: schema,\n func: func,\n });\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.schema = schema;\n }\n\n func = async (args: any) => {\n return 'override this';\n };\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n}\n"],"names":["mydi","c","m","b","resolve","e","BaseChain","container","name","description","template","tools","prompt","llm","camelize","constructor","snake_case","PromptTemplate","fromTemplate","_proto","prototype","str","replace","word","index","toLowerCase","toUpperCase","chain","input","_chain","RunnableSequence","from","RunnablePassthrough","invoke","BaseAgent","run","_run","_asyncToGenerator","_regeneratorRuntime","mark","_callee","agent","agentExecutor","wrap","_callee$","_context","prev","next","createStructuredChatAgent","sent","AgentExecutor","handleParsingErrors","abrupt","stop","_x","apply","arguments","BaseTool","_DynamicStructuredToo","schema","func","_this","call","_ref","args","_inheritsLoose","DynamicStructuredTool"],"mappings":";;;;;;;;;AAAA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ACO,SAAS,EAAE,IAAI,CAACC,IAAI,EAAEA,IAAI,CAAC,CAAA;GAC7C,CAAA;AAAAW,EAAAA,MAAA,CAEDL,QAAQ,GAAR,SAAAA,QAAQA,CAACO,GAAW,EAAA;IAClB,OAAOA,GAAG,CACPC,OAAO,CAAC,qBAAqB,EAAE,UAAUC,IAAI,EAAEC,KAAK,EAAA;AACnD,MAAA,OAAOA,KAAK,KAAK,CAAC,GAAGD,IAAI,CAACE,WAAW,EAAE,GAAGF,IAAI,CAACG,WAAW,EAAE,CAAA;AAC9D,KAAC,CAAC,CACDJ,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;GACvB,CAAA;AAAAH,EAAAA,MAAA,CAEDH,UAAU,GAAV,SAAAA,UAAUA,CAACK,GAAW,EAAA;IACpB,OAAOA,GAAG,CACPC,OAAO,CAAC,qBAAqB,EAAE,UAAUC,IAAI,EAAEC,KAAK,EAAA;AACnD,MAAA,OAAOA,KAAK,KAAK,CAAC,GAAGD,IAAI,CAACE,WAAW,EAAE,GAAG,GAAG,GAAGF,IAAI,CAACE,WAAW,EAAE,CAAA;AACpE,KAAC,CAAC,CACDH,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;GACvB,CAAA;AAAA,EAAA,OAAAmC,QAAA,CAAA;AAAA,CAAA,CA/C2BS,2BAAqB;;;;;;"}
|
|
1
|
+
{"version":3,"file":"medpromptjs.cjs.development.js","sources":["../src/mydi.ts","../src/chain.ts","../src/agent.ts","../src/tool.ts"],"sourcesContent":["/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nconst mydi = (c: any, m: string, b: string) => {\n try {\n return c.resolve(m + '_' + b);\n } catch (e) {\n return c.resolve(b);\n }\n};\n\nexport default mydi;\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\nimport {\n RunnablePassthrough,\n RunnableSequence,\n} from '@langchain/core/runnables';\nimport { AIMessage } from '@langchain/core/messages';\n\nexport class BaseChain {\n container: any;\n tools: ToolInterface[];\n name: string;\n description: string;\n prompt: any;\n llm: LLM;\n template: string;\n chat_model: boolean;\n runnable: any;\n\n constructor(\n container: any,\n name: string,\n description: string,\n template: string = `\n Summarize the following text:\n {input}\n `\n ) {\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.template = template;\n this.tools = this.resolve('tools');\n this.llm = this.resolve('main-llm');\n this.chat_model = false;\n try {\n this.chat_model = this.resolve('chat_model');\n } catch (e) {}\n if (this.chat_model) {\n this.prompt = ChatPromptTemplate.fromTemplate(this.template);\n } else {\n this.prompt = PromptTemplate.fromTemplate(this.template);\n }\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n // https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/\n //! Override this method\n async chain(input: any) {\n this.runnable = RunnableSequence.from([\n new RunnablePassthrough(),\n this.prompt,\n this.llm,\n ]);\n if (this.chat_model) {\n const response: string = await this.runnable.invoke(input);\n const aiMessage = new AIMessage(response);\n return aiMessage.content;\n }\n return await this.runnable.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport {\n AgentExecutor,\n createReactAgent,\n createStructuredChatAgent,\n} from 'langchain/agents';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\n\nexport class BaseAgent {\n container: any;\n tools: ToolInterface[];\n name: string;\n prompt: PromptTemplate;\n llm: LLM;\n template: string = `\n ## Question: {question}\n ## Answer: {answer}\n Given the above question and answer, generate a chain of thought explanation for the answer.\n First, start with the model generated chain of thought explanation.\n End the chain of though explanation with:\n Therefore, the answer is {answer}.\n `;\n\n constructor(container: any) {\n this.container = container;\n this.name = this.camelize(this.constructor.name);\n this.tools = this.resolve('tools');\n this.prompt =\n this.resolve('prompt') !== ''\n ? this.resolve('prompt')\n : PromptTemplate.fromTemplate(this.template);\n this.llm = this.resolve('main-llm');\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n async run(input: any) {\n const agent = await createStructuredChatAgent({\n llm: this.llm,\n tools: this.tools,\n prompt: this.prompt,\n });\n const agentExecutor = new AgentExecutor({\n agent: agent,\n tools: this.tools,\n // verbose: true,\n handleParsingErrors:\n 'Please try again, paying close attention to the final answer',\n });\n return await agentExecutor.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DynamicStructuredTool } from 'langchain/tools';\nimport mydi from './mydi';\n\nexport class BaseTool extends DynamicStructuredTool {\n container: any;\n name: string;\n description: string;\n schema: any;\n constructor(\n container: any,\n name: string,\n description: string,\n schema: any,\n func: any = null\n ) {\n super({\n name: name,\n description: description,\n schema: schema,\n func: func,\n });\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.schema = schema;\n }\n\n func = async (args: any) => {\n return 'override this';\n };\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n}\n"],"names":["mydi","c","m","b","resolve","e","BaseChain","container","name","description","template","tools","prompt","llm","chat_model","runnable","camelize","constructor","snake_case","ChatPromptTemplate","fromTemplate","PromptTemplate","_proto","prototype","str","replace","word","index","toLowerCase","toUpperCase","chain","_chain","_asyncToGenerator","_regeneratorRuntime","mark","_callee","input","response","aiMessage","wrap","_callee$","_context","prev","next","RunnableSequence","from","RunnablePassthrough","invoke","sent","AIMessage","abrupt","content","stop","_x","apply","arguments","BaseAgent","run","_run","agent","agentExecutor","createStructuredChatAgent","AgentExecutor","handleParsingErrors","BaseTool","_DynamicStructuredToo","schema","func","_this","call","_ref","args","_inheritsLoose","DynamicStructuredTool"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;AAcG;AAEH,IAAMA,IAAI,GAAG,SAAPA,IAAIA,CAAIC,CAAM,EAAEC,CAAS,EAAEC,CAAS,EAAI;EAC5C,IAAI;IACF,OAAOF,CAAC,CAACG,OAAO,CAACF,CAAC,GAAG,GAAG,GAAGC,CAAC,CAAC,CAAA;GAC9B,CAAC,OAAOE,CAAC,EAAE;AACV,IAAA,OAAOJ,CAAC,CAACG,OAAO,CAACD,CAAC,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I,EAAEA,IAAI,CAAC,CAAA;GAC7C,CAAA;AAAAc,EAAAA,MAAA,CAEDN,QAAQ,GAAR,SAAAA,QAAQA,CAACQ,GAAW,EAAA;IAClB,OAAOA,GAAG,CACPC,OAAO,CAAC,qBAAqB,EAAE,UAAUC,IAAI,EAAEC,KAAK,EAAA;AACnD,MAAA,OAAOA,KAAK,KAAK,CAAC,GAAGD,IAAI,CAACE,WAAW,EAAE,GAAGF,IAAI,CAACG,WAAW,EAAE,CAAA;AAC9D,KAAC,CAAC,CACDJ,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;GACvB,CAAA;AAAAH,EAAAA,MAAA,CAEDJ,UAAU,GAAV,SAAAA,UAAUA,CAACM,GAAW,EAAA;IACpB,OAAOA,GAAG,CACPC,OAAO,CAAC,qBAAqB,EAAE,UAAUC,IAAI,EAAEC,KAAK,EAAA;AACnD,MAAA,OAAOA,KAAK,KAAK,CAAC,GAAGD,IAAI,CAACE,WAAW,EAAE,GAAG,GAAG,GAAGF,IAAI,CAACE,WAAW,EAAE,CAAA;AACpE,KAAC,CAAC,CACDH,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;GACvB,CAAA;AAAA,EAAA,OAAAuC,QAAA,CAAA;AAAA,CAAA,CA/C2BS,2BAAqB;;;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@langchain/core/prompts"),e=require("@langchain/core/runnables"),r=require("langchain/agents"),n=require("langchain/tools"),o=function(t,e,r){try{return t.resolve(e+"_"+r)}catch(e){return t.resolve(r)}},i=function(){function r(e,r,n,o){this.container=void 0,this.tools=void 0,this.name=void 0,this.description=void 0,this.prompt=void 0,this.llm=void 0,this.template="\n Summarize the following text:\n {input}\n ",this.container=e,this.name=""===r?this.camelize(this.constructor.name):r,this.description=""===n?this.snake_case(this.constructor.name):n,this.tools=this.resolve("tools"),this.prompt=""!==this.resolve("prompt")?this.resolve("prompt"):t.PromptTemplate.fromTemplate(this.template),this.llm=this.resolve("main-llm")}var n=r.prototype;return n.resolve=function(t){return o(this.container,this.name,t)},n.camelize=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():t.toUpperCase()})).replace(/\s+/g,"")},n.snake_case=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():"_"+t.toLowerCase()})).replace(/\s+/g,"")},n.chain=function(t){return e.RunnableSequence.from([new e.RunnablePassthrough,this.prompt,this.llm]).invoke(t)},r}();function a(t,e,r,n,o,i,a){try{var s=t[i](a),u=s.value}catch(t){return void r(t)}s.done?e(u):Promise.resolve(u).then(n,o)}function s(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var i=t.apply(e,r);function s(t){a(i,n,o,s,u,"next",t)}function u(t){a(i,n,o,s,u,"throw",t)}s(void 0)}))}}function u(){u=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",a=o.asyncIterator||"@@asyncIterator",s=o.toStringTag||"@@toStringTag";function c(t,e,r,n){return Object.defineProperty(t,e,{value:r,enumerable:!n,configurable:!n,writable:!n})}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function h(e,r,n,o){var i=Object.create((r&&r.prototype instanceof f?r:f).prototype);return c(i,"_invoke",function(e,r,n){var o=1;return function(i,a){if(3===o)throw Error("Generator is already running");if(4===o){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var s=n.delegate;if(s){var u=_(s,n);if(u){if(u===p)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(1===o)throw o=4,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=3;var c=l(e,r,n);if("normal"===c.type){if(o=n.done?4:2,c.arg===p)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o=4,n.method="throw",n.arg=c.arg)}}}(e,n,new k(o||[])),!0),i}function l(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=h;var p={};function f(){}function v(){}function m(){}var d={};c(d,i,(function(){return this}));var g=Object.getPrototypeOf,y=g&&g(g(j([])));y&&y!==r&&n.call(y,i)&&(d=y);var w=m.prototype=f.prototype=Object.create(d);function b(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function x(t,e){function r(o,i,a,s){var u=l(t[o],t,i);if("throw"!==u.type){var c=u.arg,h=c.value;return h&&"object"==typeof h&&n.call(h,"__await")?e.resolve(h.__await).then((function(t){r("next",t,a,s)}),(function(t){r("throw",t,a,s)})):e.resolve(h).then((function(t){c.value=t,a(c)}),(function(t){return r("throw",t,a,s)}))}s(u.arg)}var o;c(this,"_invoke",(function(t,n){function i(){return new e((function(e,o){r(t,n,e,o)}))}return o=o?o.then(i,i):i()}),!0)}function _(e,r){var n=r.method,o=e.i[n];if(o===t)return r.delegate=null,"throw"===n&&e.i.return&&(r.method="return",r.arg=t,_(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),p;var i=l(o,e.i,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,p;var a=i.arg;return a?a.done?(r[e.r]=a.value,r.next=e.n,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,p):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,p)}function E(t){this.tryEntries.push(t)}function O(e){var r=e[4]||{};r.type="normal",r.arg=t,e[4]=r}function k(t){this.tryEntries=[[-1]],t.forEach(E,this),this.reset(!0)}function j(e){if(null!=e){var r=e[i];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,a=function r(){for(;++o<e.length;)if(n.call(e,o))return r.value=e[o],r.done=!1,r;return r.value=t,r.done=!0,r};return a.next=a}}throw new TypeError(typeof e+" is not iterable")}return v.prototype=m,c(w,"constructor",m),c(m,"constructor",v),v.displayName=c(m,s,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,m):(t.__proto__=m,c(t,s,"GeneratorFunction")),t.prototype=Object.create(w),t},e.awrap=function(t){return{__await:t}},b(x.prototype),c(x.prototype,a,(function(){return this})),e.AsyncIterator=x,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new x(h(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},b(w),c(w,s,"Generator"),c(w,i,(function(){return this})),c(w,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.unshift(n);return function t(){for(;r.length;)if((n=r.pop())in e)return t.value=n,t.done=!1,t;return t.done=!0,t}},e.values=j,k.prototype={constructor:k,reset:function(e){if(this.prev=this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(O),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0][4];if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function n(t){a.type="throw",a.arg=e,r.next=t}for(var o=r.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i[4],s=this.prev,u=i[1],c=i[2];if(-1===i[0])return n("end"),!1;if(!u&&!c)throw Error("try statement without catch or finally");if(null!=i[0]&&i[0]<=s){if(s<u)return this.method="next",this.arg=t,n(u),!0;if(s<c)return n(c),!1}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n[0]>-1&&n[0]<=this.prev&&this.prev<n[2]){var o=n;break}}o&&("break"===t||"continue"===t)&&o[0]<=e&&e<=o[2]&&(o=null);var i=o?o[4]:{};return i.type=t,i.arg=e,o?(this.method="next",this.next=o[2],p):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),p},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r[2]===t)return this.complete(r[4],r[3]),O(r),p}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r[0]===t){var n=r[4];if("throw"===n.type){var o=n.arg;O(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={i:j(e),r:r,n:n},"next"===this.method&&(this.arg=t),p}},e}function c(t,e){return c=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},c(t,e)}var h=function(){function e(e){this.container=void 0,this.tools=void 0,this.name=void 0,this.prompt=void 0,this.llm=void 0,this.template="\n ## Question: {question}\n ## Answer: {answer}\n Given the above question and answer, generate a chain of thought explanation for the answer.\n First, start with the model generated chain of thought explanation.\n End the chain of though explanation with:\n Therefore, the answer is {answer}.\n ",this.container=e,this.name=this.camelize(this.constructor.name),this.tools=this.resolve("tools"),this.prompt=""!==this.resolve("prompt")?this.resolve("prompt"):t.PromptTemplate.fromTemplate(this.template),this.llm=this.resolve("main-llm")}var n=e.prototype;return n.resolve=function(t){return o(this.container,this.name,t)},n.camelize=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():t.toUpperCase()})).replace(/\s+/g,"")},n.snake_case=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():"_"+t.toLowerCase()})).replace(/\s+/g,"")},n.run=function(){var t=s(u().mark((function t(e){var n;return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,r.createStructuredChatAgent({llm:this.llm,tools:this.tools,prompt:this.prompt});case 2:return n=new r.AgentExecutor({agent:t.sent,tools:this.tools,handleParsingErrors:"Please try again, paying close attention to the final answer"}),t.next=6,n.invoke(e);case 6:return t.abrupt("return",t.sent);case 7:case"end":return t.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}(),e}(),l=function(t){function e(e,r,n,o,i){var a;return void 0===i&&(i=null),(a=t.call(this,{name:r,description:n,schema:o,func:i})||this).container=void 0,a.name=void 0,a.description=void 0,a.schema=void 0,a.func=function(){var t=s(u().mark((function t(e){return u().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return","override this");case 1:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),a.container=e,a.name=""===r?a.camelize(a.constructor.name):r,a.description=""===n?a.snake_case(a.constructor.name):n,a.schema=o,a}var r,n;n=t,(r=e).prototype=Object.create(n.prototype),r.prototype.constructor=r,c(r,n);var i=e.prototype;return i.resolve=function(t){return o(this.container,this.name,t)},i.camelize=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():t.toUpperCase()})).replace(/\s+/g,"")},i.snake_case=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():"_"+t.toLowerCase()})).replace(/\s+/g,"")},e}(n.DynamicStructuredTool);exports.BaseAgent=h,exports.BaseChain=i,exports.BaseTool=l;
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@langchain/core/prompts"),e=require("@langchain/core/runnables"),r=require("@langchain/core/messages"),n=require("langchain/agents"),o=require("langchain/tools");function i(t,e,r,n,o,i,a){try{var s=t[i](a),u=s.value}catch(t){return void r(t)}s.done?e(u):Promise.resolve(u).then(n,o)}function a(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function s(t){i(a,n,o,s,u,"next",t)}function u(t){i(a,n,o,s,u,"throw",t)}s(void 0)}))}}function s(){s=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",a=o.asyncIterator||"@@asyncIterator",u=o.toStringTag||"@@toStringTag";function c(t,e,r,n){return Object.defineProperty(t,e,{value:r,enumerable:!n,configurable:!n,writable:!n})}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function h(e,r,n,o){var i=Object.create((r&&r.prototype instanceof f?r:f).prototype);return c(i,"_invoke",function(e,r,n){var o=1;return function(i,a){if(3===o)throw Error("Generator is already running");if(4===o){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var s=n.delegate;if(s){var u=_(s,n);if(u){if(u===p)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(1===o)throw o=4,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=3;var c=l(e,r,n);if("normal"===c.type){if(o=n.done?4:2,c.arg===p)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o=4,n.method="throw",n.arg=c.arg)}}}(e,n,new O(o||[])),!0),i}function l(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=h;var p={};function f(){}function v(){}function m(){}var d={};c(d,i,(function(){return this}));var g=Object.getPrototypeOf,w=g&&g(g(P([])));w&&w!==r&&n.call(w,i)&&(d=w);var y=m.prototype=f.prototype=Object.create(d);function b(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function x(t,e){function r(o,i,a,s){var u=l(t[o],t,i);if("throw"!==u.type){var c=u.arg,h=c.value;return h&&"object"==typeof h&&n.call(h,"__await")?e.resolve(h.__await).then((function(t){r("next",t,a,s)}),(function(t){r("throw",t,a,s)})):e.resolve(h).then((function(t){c.value=t,a(c)}),(function(t){return r("throw",t,a,s)}))}s(u.arg)}var o;c(this,"_invoke",(function(t,n){function i(){return new e((function(e,o){r(t,n,e,o)}))}return o=o?o.then(i,i):i()}),!0)}function _(e,r){var n=r.method,o=e.i[n];if(o===t)return r.delegate=null,"throw"===n&&e.i.return&&(r.method="return",r.arg=t,_(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),p;var i=l(o,e.i,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,p;var a=i.arg;return a?a.done?(r[e.r]=a.value,r.next=e.n,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,p):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,p)}function E(t){this.tryEntries.push(t)}function k(e){var r=e[4]||{};r.type="normal",r.arg=t,e[4]=r}function O(t){this.tryEntries=[[-1]],t.forEach(E,this),this.reset(!0)}function P(e){if(null!=e){var r=e[i];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,a=function r(){for(;++o<e.length;)if(n.call(e,o))return r.value=e[o],r.done=!1,r;return r.value=t,r.done=!0,r};return a.next=a}}throw new TypeError(typeof e+" is not iterable")}return v.prototype=m,c(y,"constructor",m),c(m,"constructor",v),v.displayName=c(m,u,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,m):(t.__proto__=m,c(t,u,"GeneratorFunction")),t.prototype=Object.create(y),t},e.awrap=function(t){return{__await:t}},b(x.prototype),c(x.prototype,a,(function(){return this})),e.AsyncIterator=x,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new x(h(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},b(y),c(y,u,"Generator"),c(y,i,(function(){return this})),c(y,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.unshift(n);return function t(){for(;r.length;)if((n=r.pop())in e)return t.value=n,t.done=!1,t;return t.done=!0,t}},e.values=P,O.prototype={constructor:O,reset:function(e){if(this.prev=this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(k),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0][4];if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function n(t){a.type="throw",a.arg=e,r.next=t}for(var o=r.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i[4],s=this.prev,u=i[1],c=i[2];if(-1===i[0])return n("end"),!1;if(!u&&!c)throw Error("try statement without catch or finally");if(null!=i[0]&&i[0]<=s){if(s<u)return this.method="next",this.arg=t,n(u),!0;if(s<c)return n(c),!1}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n[0]>-1&&n[0]<=this.prev&&this.prev<n[2]){var o=n;break}}o&&("break"===t||"continue"===t)&&o[0]<=e&&e<=o[2]&&(o=null);var i=o?o[4]:{};return i.type=t,i.arg=e,o?(this.method="next",this.next=o[2],p):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),p},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r[2]===t)return this.complete(r[4],r[3]),k(r),p}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r[0]===t){var n=r[4];if("throw"===n.type){var o=n.arg;k(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={i:P(e),r:r,n:n},"next"===this.method&&(this.arg=t),p}},e}function u(t,e){return u=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},u(t,e)}var c=function(t,e,r){try{return t.resolve(e+"_"+r)}catch(e){return t.resolve(r)}},h=function(){function n(e,r,n,o){void 0===o&&(o="\n Summarize the following text:\n {input}\n "),this.container=void 0,this.tools=void 0,this.name=void 0,this.description=void 0,this.prompt=void 0,this.llm=void 0,this.template=void 0,this.chat_model=void 0,this.runnable=void 0,this.container=e,this.name=""===r?this.camelize(this.constructor.name):r,this.description=""===n?this.snake_case(this.constructor.name):n,this.template=o,this.tools=this.resolve("tools"),this.llm=this.resolve("main-llm"),this.chat_model=!1;try{this.chat_model=this.resolve("chat_model")}catch(t){}this.prompt=this.chat_model?t.ChatPromptTemplate.fromTemplate(this.template):t.PromptTemplate.fromTemplate(this.template)}var o=n.prototype;return o.resolve=function(t){return c(this.container,this.name,t)},o.camelize=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():t.toUpperCase()})).replace(/\s+/g,"")},o.snake_case=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():"_"+t.toLowerCase()})).replace(/\s+/g,"")},o.chain=function(){var t=a(s().mark((function t(n){var o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(this.runnable=e.RunnableSequence.from([new e.RunnablePassthrough,this.prompt,this.llm]),!this.chat_model){t.next=7;break}return t.next=4,this.runnable.invoke(n);case 4:return o=new r.AIMessage(t.sent),t.abrupt("return",o.content);case 7:return t.next=9,this.runnable.invoke(n);case 9:return t.abrupt("return",t.sent);case 10:case"end":return t.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}(),n}(),l=function(){function e(e){this.container=void 0,this.tools=void 0,this.name=void 0,this.prompt=void 0,this.llm=void 0,this.template="\n ## Question: {question}\n ## Answer: {answer}\n Given the above question and answer, generate a chain of thought explanation for the answer.\n First, start with the model generated chain of thought explanation.\n End the chain of though explanation with:\n Therefore, the answer is {answer}.\n ",this.container=e,this.name=this.camelize(this.constructor.name),this.tools=this.resolve("tools"),this.prompt=""!==this.resolve("prompt")?this.resolve("prompt"):t.PromptTemplate.fromTemplate(this.template),this.llm=this.resolve("main-llm")}var r=e.prototype;return r.resolve=function(t){return c(this.container,this.name,t)},r.camelize=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():t.toUpperCase()})).replace(/\s+/g,"")},r.snake_case=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():"_"+t.toLowerCase()})).replace(/\s+/g,"")},r.run=function(){var t=a(s().mark((function t(e){var r;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,n.createStructuredChatAgent({llm:this.llm,tools:this.tools,prompt:this.prompt});case 2:return r=new n.AgentExecutor({agent:t.sent,tools:this.tools,handleParsingErrors:"Please try again, paying close attention to the final answer"}),t.next=6,r.invoke(e);case 6:return t.abrupt("return",t.sent);case 7:case"end":return t.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}(),e}(),p=function(t){function e(e,r,n,o,i){var u;return void 0===i&&(i=null),(u=t.call(this,{name:r,description:n,schema:o,func:i})||this).container=void 0,u.name=void 0,u.description=void 0,u.schema=void 0,u.func=function(){var t=a(s().mark((function t(e){return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.abrupt("return","override this");case 1:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),u.container=e,u.name=""===r?u.camelize(u.constructor.name):r,u.description=""===n?u.snake_case(u.constructor.name):n,u.schema=o,u}var r,n;n=t,(r=e).prototype=Object.create(n.prototype),r.prototype.constructor=r,u(r,n);var o=e.prototype;return o.resolve=function(t){return c(this.container,this.name,t)},o.camelize=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():t.toUpperCase()})).replace(/\s+/g,"")},o.snake_case=function(t){return t.replace(/(?:^\w|[A-Z]|\b\w)/g,(function(t,e){return 0===e?t.toLowerCase():"_"+t.toLowerCase()})).replace(/\s+/g,"")},e}(o.DynamicStructuredTool);exports.BaseAgent=l,exports.BaseChain=h,exports.BaseTool=p;
|
|
2
2
|
//# sourceMappingURL=medpromptjs.cjs.production.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"medpromptjs.cjs.production.min.js","sources":["../src/mydi.ts","../src/chain.ts","../src/agent.ts","../src/tool.ts"],"sourcesContent":["/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nconst mydi = (c: any, m: string, b: string) => {\n try {\n return c.resolve(m + '_' + b);\n } catch (e) {\n return c.resolve(b);\n }\n};\n\nexport default mydi;\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\nimport {\n RunnablePassthrough,\n RunnableSequence,\n} from '@langchain/core/runnables';\n\nexport class BaseChain {\n container: any;\n tools: ToolInterface[];\n name: string;\n description: string;\n prompt: PromptTemplate;\n llm: LLM;\n template: string = `\n Summarize the following text:\n {input}\n `;\n\n constructor(\n container: any,\n name: string,\n description: string,\n template: string = ''\n ) {\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.tools = this.resolve('tools');\n this.prompt =\n this.resolve('prompt') !== ''\n ? this.resolve('prompt')\n : PromptTemplate.fromTemplate(this.template);\n this.llm = this.resolve('main-llm');\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n // https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/\n //! Override this method\n chain(input: any) {\n const _chain = RunnableSequence.from([\n new RunnablePassthrough(),\n this.prompt,\n this.llm,\n ]);\n return _chain.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport {\n AgentExecutor,\n createReactAgent,\n createStructuredChatAgent,\n} from 'langchain/agents';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\n\nexport class BaseAgent {\n container: any;\n tools: ToolInterface[];\n name: string;\n prompt: PromptTemplate;\n llm: LLM;\n template: string = `\n ## Question: {question}\n ## Answer: {answer}\n Given the above question and answer, generate a chain of thought explanation for the answer.\n First, start with the model generated chain of thought explanation.\n End the chain of though explanation with:\n Therefore, the answer is {answer}.\n `;\n\n constructor(container: any) {\n this.container = container;\n this.name = this.camelize(this.constructor.name);\n this.tools = this.resolve('tools');\n this.prompt =\n this.resolve('prompt') !== ''\n ? this.resolve('prompt')\n : PromptTemplate.fromTemplate(this.template);\n this.llm = this.resolve('main-llm');\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n async run(input: any) {\n const agent = await createStructuredChatAgent({\n llm: this.llm,\n tools: this.tools,\n prompt: this.prompt,\n });\n const agentExecutor = new AgentExecutor({\n agent: agent,\n tools: this.tools,\n // verbose: true,\n handleParsingErrors:\n 'Please try again, paying close attention to the final answer',\n });\n return await agentExecutor.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DynamicStructuredTool } from 'langchain/tools';\nimport mydi from './mydi';\n\nexport class BaseTool extends DynamicStructuredTool {\n container: any;\n name: string;\n description: string;\n schema: any;\n constructor(\n container: any,\n name: string,\n description: string,\n schema: any,\n func: any = null\n ) {\n super({\n name: name,\n description: description,\n schema: schema,\n func: func,\n });\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.schema = schema;\n }\n\n func = async (args: any) => {\n return 'override this';\n };\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n}\n"],"names":["mydi","c","m","b","resolve","e","BaseChain","container","name","description","template","this","tools","prompt","llm","camelize","constructor","snake_case","PromptTemplate","fromTemplate","_proto","prototype","str","replace","word","index","toLowerCase","toUpperCase","chain","input","RunnableSequence","from","RunnablePassthrough","invoke","BaseAgent","run","_run","_asyncToGenerator","_regeneratorRuntime","mark","_callee","agentExecutor","wrap","_context","prev","next","createStructuredChatAgent","AgentExecutor","agent","sent","handleParsingErrors","abrupt","stop","_x","apply","arguments","BaseTool","_DynamicStructuredToo","schema","func","_this","call","_ref","args","DynamicStructuredTool"],"mappings":"+MAgBMA,EAAO,SAACC,EAAQC,EAAWC,GAC/B,IACE,OAAOF,EAAEG,QAAQF,EAAI,IAAMC,EAC5B,CAAC,MAAOE,GACP,OAAOJ,EAAEG,QAAQD,EACnB,CACF,ECGaG,EAAS,WAYpB,SAAAA,EACEC,EACAC,EACAC,EACAC,GAAqBC,KAfvBJ,eAAS,EAAAI,KACTC,WAAK,EAAAD,KACLH,UAAI,EAAAG,KACJF,iBAAW,EAAAE,KACXE,YAAM,E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VA,EAAcD,EAAKE,cAAgB,IAAMF,EAAKE,aACtD,IACAH,QAAQ,OAAQ,KACpBiC,CAAA,EA/C2BQ,EAAqBA"}
|
|
1
|
+
{"version":3,"file":"medpromptjs.cjs.production.min.js","sources":["../src/mydi.ts","../src/chain.ts","../src/agent.ts","../src/tool.ts"],"sourcesContent":["/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nconst mydi = (c: any, m: string, b: string) => {\n try {\n return c.resolve(m + '_' + b);\n } catch (e) {\n return c.resolve(b);\n }\n};\n\nexport default mydi;\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\nimport {\n RunnablePassthrough,\n RunnableSequence,\n} from '@langchain/core/runnables';\nimport { AIMessage } from '@langchain/core/messages';\n\nexport class BaseChain {\n container: any;\n tools: ToolInterface[];\n name: string;\n description: string;\n prompt: any;\n llm: LLM;\n template: string;\n chat_model: boolean;\n runnable: any;\n\n constructor(\n container: any,\n name: string,\n description: string,\n template: string = `\n Summarize the following text:\n {input}\n `\n ) {\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.template = template;\n this.tools = this.resolve('tools');\n this.llm = this.resolve('main-llm');\n this.chat_model = false;\n try {\n this.chat_model = this.resolve('chat_model');\n } catch (e) {}\n if (this.chat_model) {\n this.prompt = ChatPromptTemplate.fromTemplate(this.template);\n } else {\n this.prompt = PromptTemplate.fromTemplate(this.template);\n }\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n // https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/\n //! Override this method\n async chain(input: any) {\n this.runnable = RunnableSequence.from([\n new RunnablePassthrough(),\n this.prompt,\n this.llm,\n ]);\n if (this.chat_model) {\n const response: string = await this.runnable.invoke(input);\n const aiMessage = new AIMessage(response);\n return aiMessage.content;\n }\n return await this.runnable.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport {\n AgentExecutor,\n createReactAgent,\n createStructuredChatAgent,\n} from 'langchain/agents';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\n\nexport class BaseAgent {\n container: any;\n tools: ToolInterface[];\n name: string;\n prompt: PromptTemplate;\n llm: LLM;\n template: string = `\n ## Question: {question}\n ## Answer: {answer}\n Given the above question and answer, generate a chain of thought explanation for the answer.\n First, start with the model generated chain of thought explanation.\n End the chain of though explanation with:\n Therefore, the answer is {answer}.\n `;\n\n constructor(container: any) {\n this.container = container;\n this.name = this.camelize(this.constructor.name);\n this.tools = this.resolve('tools');\n this.prompt =\n this.resolve('prompt') !== ''\n ? this.resolve('prompt')\n : PromptTemplate.fromTemplate(this.template);\n this.llm = this.resolve('main-llm');\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n async run(input: any) {\n const agent = await createStructuredChatAgent({\n llm: this.llm,\n tools: this.tools,\n prompt: this.prompt,\n });\n const agentExecutor = new AgentExecutor({\n agent: agent,\n tools: this.tools,\n // verbose: true,\n handleParsingErrors:\n 'Please try again, paying close attention to the final answer',\n });\n return await agentExecutor.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DynamicStructuredTool } from 'langchain/tools';\nimport mydi from './mydi';\n\nexport class BaseTool extends DynamicStructuredTool {\n container: any;\n name: string;\n description: string;\n schema: any;\n constructor(\n container: any,\n name: string,\n description: string,\n schema: any,\n func: any = null\n ) {\n super({\n name: name,\n description: description,\n schema: schema,\n func: func,\n });\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.schema = schema;\n }\n\n func = async (args: any) => {\n return 'override this';\n };\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n}\n"],"names":["mydi","c","m","b","resolve","e","BaseChain","container","name","description","template","this","tools","prompt","llm","chat_model","runnable","camelize","constructor","snake_case","ChatPromptTemplate","fromTemplate","PromptTemplate","_proto","prototype","str","replace","word","index","toLowerCase","toUpperCase","chain","_chain","_asyncToGenerator","_regeneratorRuntime","mark","_callee","input","aiMessage","wrap","_context","prev","next","RunnableSequence","from","RunnablePassthrough","invoke","AIMessage","sent","abrupt","content","stop","_x","apply","arguments","BaseAgent","run","_run","agentExecutor","createStructuredChatAgent","AgentExecutor","agent","handleParsingErrors","BaseTool","_DynamicStructuredToo","schema","func","_this","call","_ref","args","DynamicStructuredTool"],"mappings":"y0MAgBA,IAAMA,EAAO,SAACC,EAAQC,EAAWC,GAC/B,IACE,OAAOF,EAAEG,QAAQF,EAAI,IAAMC,EAC5B,CAAC,MAAOE,GACP,OAAOJ,EAAEG,QAAQD,EACnB,CACF,ECIaG,EAAS,WAWpB,SAAAA,EACEC,E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AqBND,KAAI,WAAA,IAAAG,EAAApC,EAAAC,IAAAC,MAAG,SAAAC,EAAOkC,GAAS,OAAApC,IAAAK,MAAA,SAAAC,GAAA,cAAAA,EAAAC,KAAAD,EAAAE,MAAA,KAAA,EAAA,OAAAF,EAAAS,OAAA,SACd,iBAAe,KAAA,EAAA,IAAA,MAAA,OAAAT,EAAAW,OAAA,GAAAf,EACvB,KAAA,OAAA,SAAAgB,GAAA,OAAAiB,EAAAhB,MAAA1C,KAAA2C,UAAA,CAAA,CAFG,GAPFa,EAAK5D,UAAYA,EACjB4D,EAAK3D,KAAgB,KAATA,EAAc2D,EAAKlD,SAASkD,EAAKjD,YAAYV,MAAQA,EACjE2D,EAAK1D,YACa,KAAhBA,EAAqB0D,EAAKhD,WAAWgD,EAAKjD,YAAYV,MAAQC,EAChE0D,EAAKF,OAASA,EAAOE,CACvB,WAACH,KAAAD,yEAAA,IAAAxC,EAAAwC,EAAAvC,UAwBA,OAxBAD,EAMDnB,QAAA,SAAQI,GACN,OAAOR,EAAKW,KAAKJ,UAAWI,KAAKH,KAAMA,IACxCe,EAEDN,SAAA,SAASQ,GACP,OAAOA,EACJC,QAAQ,uBAAuB,SAAUC,EAAMC,GAC9C,OAAiB,IAAVA,EAAcD,EAAKE,cAAgBF,EAAKG,aAChD,IACAJ,QAAQ,OAAQ,KACpBH,EAEDJ,WAAA,SAAWM,GACT,OAAOA,EACJC,QAAQ,uBAAuB,SAAUC,EAAMC,GAC9C,OAAiB,IAAVA,EAAcD,EAAKE,cAAgB,IAAMF,EAAKE,aACtD,IACAH,QAAQ,OAAQ,KACpBqC,CAAA,EA/C2BQ,EAAqBA"}
|
package/dist/medpromptjs.esm.js
CHANGED
|
@@ -1,86 +1,9 @@
|
|
|
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
|
|
|
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
|
-
}();
|
|
83
|
-
|
|
84
7
|
function asyncGeneratorStep(n, t, e, r, o, a, c) {
|
|
85
8
|
try {
|
|
86
9
|
var i = n[a](c),
|
|
@@ -384,6 +307,114 @@ function _setPrototypeOf(t, e) {
|
|
|
384
307
|
}, _setPrototypeOf(t, e);
|
|
385
308
|
}
|
|
386
309
|
|
|
310
|
+
/**
|
|
311
|
+
* Copyright 2024 Bell Eapen
|
|
312
|
+
*
|
|
313
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
314
|
+
* you may not use this file except in compliance with the License.
|
|
315
|
+
* You may obtain a copy of the License at
|
|
316
|
+
*
|
|
317
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
318
|
+
*
|
|
319
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
320
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
321
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
322
|
+
* See the License for the specific language governing permissions and
|
|
323
|
+
* limitations under the License.
|
|
324
|
+
*/
|
|
325
|
+
var mydi = function mydi(c, m, b) {
|
|
326
|
+
try {
|
|
327
|
+
return c.resolve(m + '_' + b);
|
|
328
|
+
} catch (e) {
|
|
329
|
+
return c.resolve(b);
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
var BaseChain = /*#__PURE__*/function () {
|
|
334
|
+
function BaseChain(container, name, description, template) {
|
|
335
|
+
if (template === void 0) {
|
|
336
|
+
template = "\n Summarize the following text:\n {input}\n ";
|
|
337
|
+
}
|
|
338
|
+
this.container = void 0;
|
|
339
|
+
this.tools = void 0;
|
|
340
|
+
this.name = void 0;
|
|
341
|
+
this.description = void 0;
|
|
342
|
+
this.prompt = void 0;
|
|
343
|
+
this.llm = void 0;
|
|
344
|
+
this.template = void 0;
|
|
345
|
+
this.chat_model = void 0;
|
|
346
|
+
this.runnable = void 0;
|
|
347
|
+
this.container = container;
|
|
348
|
+
this.name = name === '' ? this.camelize(this.constructor.name) : name;
|
|
349
|
+
this.description = description === '' ? this.snake_case(this.constructor.name) : description;
|
|
350
|
+
this.template = template;
|
|
351
|
+
this.tools = this.resolve('tools');
|
|
352
|
+
this.llm = this.resolve('main-llm');
|
|
353
|
+
this.chat_model = false;
|
|
354
|
+
try {
|
|
355
|
+
this.chat_model = this.resolve('chat_model');
|
|
356
|
+
} catch (e) {}
|
|
357
|
+
if (this.chat_model) {
|
|
358
|
+
this.prompt = ChatPromptTemplate.fromTemplate(this.template);
|
|
359
|
+
} else {
|
|
360
|
+
this.prompt = PromptTemplate.fromTemplate(this.template);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
var _proto = BaseChain.prototype;
|
|
364
|
+
_proto.resolve = function resolve(name) {
|
|
365
|
+
return mydi(this.container, this.name, name);
|
|
366
|
+
};
|
|
367
|
+
_proto.camelize = function camelize(str) {
|
|
368
|
+
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
369
|
+
return index === 0 ? word.toLowerCase() : word.toUpperCase();
|
|
370
|
+
}).replace(/\s+/g, '');
|
|
371
|
+
};
|
|
372
|
+
_proto.snake_case = function snake_case(str) {
|
|
373
|
+
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
374
|
+
return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();
|
|
375
|
+
}).replace(/\s+/g, '');
|
|
376
|
+
}
|
|
377
|
+
// https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/
|
|
378
|
+
//! Override this method
|
|
379
|
+
;
|
|
380
|
+
_proto.chain =
|
|
381
|
+
/*#__PURE__*/
|
|
382
|
+
function () {
|
|
383
|
+
var _chain = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(input) {
|
|
384
|
+
var response, aiMessage;
|
|
385
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
386
|
+
while (1) switch (_context.prev = _context.next) {
|
|
387
|
+
case 0:
|
|
388
|
+
this.runnable = RunnableSequence.from([new RunnablePassthrough(), this.prompt, this.llm]);
|
|
389
|
+
if (!this.chat_model) {
|
|
390
|
+
_context.next = 7;
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
_context.next = 4;
|
|
394
|
+
return this.runnable.invoke(input);
|
|
395
|
+
case 4:
|
|
396
|
+
response = _context.sent;
|
|
397
|
+
aiMessage = new AIMessage(response);
|
|
398
|
+
return _context.abrupt("return", aiMessage.content);
|
|
399
|
+
case 7:
|
|
400
|
+
_context.next = 9;
|
|
401
|
+
return this.runnable.invoke(input);
|
|
402
|
+
case 9:
|
|
403
|
+
return _context.abrupt("return", _context.sent);
|
|
404
|
+
case 10:
|
|
405
|
+
case "end":
|
|
406
|
+
return _context.stop();
|
|
407
|
+
}
|
|
408
|
+
}, _callee, this);
|
|
409
|
+
}));
|
|
410
|
+
function chain(_x) {
|
|
411
|
+
return _chain.apply(this, arguments);
|
|
412
|
+
}
|
|
413
|
+
return chain;
|
|
414
|
+
}();
|
|
415
|
+
return BaseChain;
|
|
416
|
+
}();
|
|
417
|
+
|
|
387
418
|
var BaseAgent = /*#__PURE__*/function () {
|
|
388
419
|
function BaseAgent(container) {
|
|
389
420
|
this.container = void 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"medpromptjs.esm.js","sources":["../src/mydi.ts","../src/chain.ts","../src/agent.ts","../src/tool.ts"],"sourcesContent":["/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nconst mydi = (c: any, m: string, b: string) => {\n try {\n return c.resolve(m + '_' + b);\n } catch (e) {\n return c.resolve(b);\n }\n};\n\nexport default mydi;\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\nimport {\n RunnablePassthrough,\n RunnableSequence,\n} from '@langchain/core/runnables';\n\nexport class BaseChain {\n container: any;\n tools: ToolInterface[];\n name: string;\n description: string;\n prompt: PromptTemplate;\n llm: LLM;\n template: string = `\n Summarize the following text:\n {input}\n `;\n\n constructor(\n container: any,\n name: string,\n description: string,\n template: string = ''\n ) {\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.tools = this.resolve('tools');\n this.prompt =\n this.resolve('prompt') !== ''\n ? this.resolve('prompt')\n : PromptTemplate.fromTemplate(this.template);\n this.llm = this.resolve('main-llm');\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n // https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/\n //! Override this method\n chain(input: any) {\n const _chain = RunnableSequence.from([\n new RunnablePassthrough(),\n this.prompt,\n this.llm,\n ]);\n return _chain.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport {\n AgentExecutor,\n createReactAgent,\n createStructuredChatAgent,\n} from 'langchain/agents';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\n\nexport class BaseAgent {\n container: any;\n tools: ToolInterface[];\n name: string;\n prompt: PromptTemplate;\n llm: LLM;\n template: string = `\n ## Question: {question}\n ## Answer: {answer}\n Given the above question and answer, generate a chain of thought explanation for the answer.\n First, start with the model generated chain of thought explanation.\n End the chain of though explanation with:\n Therefore, the answer is {answer}.\n `;\n\n constructor(container: any) {\n this.container = container;\n this.name = this.camelize(this.constructor.name);\n this.tools = this.resolve('tools');\n this.prompt =\n this.resolve('prompt') !== ''\n ? this.resolve('prompt')\n : PromptTemplate.fromTemplate(this.template);\n this.llm = this.resolve('main-llm');\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n async run(input: any) {\n const agent = await createStructuredChatAgent({\n llm: this.llm,\n tools: this.tools,\n prompt: this.prompt,\n });\n const agentExecutor = new AgentExecutor({\n agent: agent,\n tools: this.tools,\n // verbose: true,\n handleParsingErrors:\n 'Please try again, paying close attention to the final answer',\n });\n return await agentExecutor.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DynamicStructuredTool } from 'langchain/tools';\nimport mydi from './mydi';\n\nexport class BaseTool extends DynamicStructuredTool {\n container: any;\n name: string;\n description: string;\n schema: any;\n constructor(\n container: any,\n name: string,\n description: string,\n schema: any,\n func: any = null\n ) {\n super({\n name: name,\n description: description,\n schema: schema,\n func: func,\n });\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.schema = schema;\n }\n\n func = async (args: any) => {\n return 'override this';\n };\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n}\n"],"names":["mydi","c","m","b","resolve","e","BaseChain","container","name","description","template","tools","prompt","llm","camelize","constructor","snake_case","PromptTemplate","fromTemplate","_proto","prototype","str","replace","word","index","toLowerCase","toUpperCase","chain","input","_chain","RunnableSequence","from","RunnablePassthrough","invoke","BaseAgent","run","_run","_asyncToGenerator","_regeneratorRuntime","mark","_callee","agent","agentExecutor","wrap","_callee$","_context","prev","next","createStructuredChatAgent","sent","AgentExecutor","handleParsingErrors","abrupt","stop","_x","apply","arguments","BaseTool","_DynamicStructuredToo","schema","func","_this","call","_ref","args","_inheritsLoose","DynamicStructuredTool"],"mappings":";;;;;AAAA;;;;;;;;;;;;;;AAcG;AAEH,IAAMA,IAAI,GAAG,SAAPA,IAAIA,CAAIC,CAAM,EAAEC,CAAS,EAAEC,CAAS,EAAI;EAC5C,IAAI;IACF,OAAOF,CAAC,CAACG,OAAO,CAACF,CAAC,GAAG,GAAG,GAAGC,CAAC,CAAC,CAAA;GAC9B,CAAC,OAAOE,CAAC,EAAE;AACV,IAAA,OAAOJ,CAAC,CAACG,OAAO,CAACD,CAAC,CAAC,CAAA;AACrB,GAAA;AACF,CAAC;;ACtBD;;;;;;;;;;;;;;AAcG;AAWH,IAAaG,SAAS,gBAAA,YAAA;EAYpB,SAAAA,SAAAA,CACEC,SAAc,EACdC,IAAY,EACZC,WAAmB,EACnBC,UAAqB;AAAA,IAAA,IAAA,CAfvBH,SAAS,GAAA,KAAA,CAAA,CAAA;AAAA,IAAA,IAAA,CACTI,KAAK,GAAA,KAAA,CAAA,CAAA;AAAA,IAAA,IAAA,CACLH,IAAI,GAAA,KAAA,CAAA,CAAA;AAAA,IAAA,IAAA,CACJC,WAAW,GAAA,KAAA,CAAA,CAAA;AAAA,IAAA,IAAA,CACXG,MAAM,GAAA,KAAA,CAAA,CAAA;AAAA,IAAA,IAAA,CACNC,GAAG,GAAA,KAAA,CAAA,CAAA;AAAA,IAAA,IAAA,CACHH,QAAQ,GAAA,wEAAA,CAAA;IAWN,IAAI,CAACH,SAAS,GAAGA,SAAS,CAAA;AAC1B,IAAA,IAAI,CAACC,IAAI,GAAGA,IAAI,KAAK,EAAE,GAAG,IAAI,CAACM,QAAQ,CAAC,IAAI,CAACC,WAAW,CA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AE,KAAA,CAAA;AACvB,GAAA;EAACI,cAAA,CAAAR,QAAA,EAAAC,qBAAA,CAAA,CAAA;AAAA,EAAA,IAAAvC,MAAA,GAAAsC,QAAA,CAAArC,SAAA,CAAA;AAAAD,EAAAA,MAAA,CAMDf,OAAO,GAAP,SAAAA,OAAOA,CAACI,IAAY,EAAA;IAClB,OAAOR,IAAI,CAAC,IAAI,CAACO,SAAS,EAAE,IAAI,CAACC,IAAI,EAAEA,IAAI,CAAC,CAAA;GAC7C,CAAA;AAAAW,EAAAA,MAAA,CAEDL,QAAQ,GAAR,SAAAA,QAAQA,CAACO,GAAW,EAAA;IAClB,OAAOA,GAAG,CACPC,OAAO,CAAC,qBAAqB,EAAE,UAAUC,IAAI,EAAEC,KAAK,EAAA;AACnD,MAAA,OAAOA,KAAK,KAAK,CAAC,GAAGD,IAAI,CAACE,WAAW,EAAE,GAAGF,IAAI,CAACG,WAAW,EAAE,CAAA;AAC9D,KAAC,CAAC,CACDJ,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;GACvB,CAAA;AAAAH,EAAAA,MAAA,CAEDH,UAAU,GAAV,SAAAA,UAAUA,CAACK,GAAW,EAAA;IACpB,OAAOA,GAAG,CACPC,OAAO,CAAC,qBAAqB,EAAE,UAAUC,IAAI,EAAEC,KAAK,EAAA;AACnD,MAAA,OAAOA,KAAK,KAAK,CAAC,GAAGD,IAAI,CAACE,WAAW,EAAE,GAAG,GAAG,GAAGF,IAAI,CAACE,WAAW,EAAE,CAAA;AACpE,KAAC,CAAC,CACDH,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;GACvB,CAAA;AAAA,EAAA,OAAAmC,QAAA,CAAA;AAAA,CAAA,CA/C2BS,qBAAqB;;;;"}
|
|
1
|
+
{"version":3,"file":"medpromptjs.esm.js","sources":["../src/mydi.ts","../src/chain.ts","../src/agent.ts","../src/tool.ts"],"sourcesContent":["/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nconst mydi = (c: any, m: string, b: string) => {\n try {\n return c.resolve(m + '_' + b);\n } catch (e) {\n return c.resolve(b);\n }\n};\n\nexport default mydi;\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\nimport {\n RunnablePassthrough,\n RunnableSequence,\n} from '@langchain/core/runnables';\nimport { AIMessage } from '@langchain/core/messages';\n\nexport class BaseChain {\n container: any;\n tools: ToolInterface[];\n name: string;\n description: string;\n prompt: any;\n llm: LLM;\n template: string;\n chat_model: boolean;\n runnable: any;\n\n constructor(\n container: any,\n name: string,\n description: string,\n template: string = `\n Summarize the following text:\n {input}\n `\n ) {\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.template = template;\n this.tools = this.resolve('tools');\n this.llm = this.resolve('main-llm');\n this.chat_model = false;\n try {\n this.chat_model = this.resolve('chat_model');\n } catch (e) {}\n if (this.chat_model) {\n this.prompt = ChatPromptTemplate.fromTemplate(this.template);\n } else {\n this.prompt = PromptTemplate.fromTemplate(this.template);\n }\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n // https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/\n //! Override this method\n async chain(input: any) {\n this.runnable = RunnableSequence.from([\n new RunnablePassthrough(),\n this.prompt,\n this.llm,\n ]);\n if (this.chat_model) {\n const response: string = await this.runnable.invoke(input);\n const aiMessage = new AIMessage(response);\n return aiMessage.content;\n }\n return await this.runnable.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { PromptTemplate, ChatPromptTemplate } from '@langchain/core/prompts';\nimport { LLM } from '@langchain/core/language_models/llms';\nimport {\n AgentExecutor,\n createReactAgent,\n createStructuredChatAgent,\n} from 'langchain/agents';\nimport type { ToolInterface } from '@langchain/core/tools';\nimport mydi from './mydi';\n\nexport class BaseAgent {\n container: any;\n tools: ToolInterface[];\n name: string;\n prompt: PromptTemplate;\n llm: LLM;\n template: string = `\n ## Question: {question}\n ## Answer: {answer}\n Given the above question and answer, generate a chain of thought explanation for the answer.\n First, start with the model generated chain of thought explanation.\n End the chain of though explanation with:\n Therefore, the answer is {answer}.\n `;\n\n constructor(container: any) {\n this.container = container;\n this.name = this.camelize(this.constructor.name);\n this.tools = this.resolve('tools');\n this.prompt =\n this.resolve('prompt') !== ''\n ? this.resolve('prompt')\n : PromptTemplate.fromTemplate(this.template);\n this.llm = this.resolve('main-llm');\n }\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n\n async run(input: any) {\n const agent = await createStructuredChatAgent({\n llm: this.llm,\n tools: this.tools,\n prompt: this.prompt,\n });\n const agentExecutor = new AgentExecutor({\n agent: agent,\n tools: this.tools,\n // verbose: true,\n handleParsingErrors:\n 'Please try again, paying close attention to the final answer',\n });\n return await agentExecutor.invoke(input);\n }\n}\n","/**\n * Copyright 2024 Bell Eapen\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { DynamicStructuredTool } from 'langchain/tools';\nimport mydi from './mydi';\n\nexport class BaseTool extends DynamicStructuredTool {\n container: any;\n name: string;\n description: string;\n schema: any;\n constructor(\n container: any,\n name: string,\n description: string,\n schema: any,\n func: any = null\n ) {\n super({\n name: name,\n description: description,\n schema: schema,\n func: func,\n });\n this.container = container;\n this.name = name === '' ? this.camelize(this.constructor.name) : name;\n this.description =\n description === '' ? this.snake_case(this.constructor.name) : description;\n this.schema = schema;\n }\n\n func = async (args: any) => {\n return 'override this';\n };\n\n resolve(name: string) {\n return mydi(this.container, this.name, name);\n }\n\n camelize(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : word.toUpperCase();\n })\n .replace(/\\s+/g, '');\n }\n\n snake_case(str: string) {\n return str\n .replace(/(?:^\\w|[A-Z]|\\b\\w)/g, function (word, index) {\n return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();\n })\n .replace(/\\s+/g, '');\n }\n}\n"],"names":["mydi","c","m","b","resolve","e","BaseChain","container","name","description","template","tools","prompt","llm","chat_model","runnable","camelize","constructor","snake_case","ChatPromptTemplate","fromTemplate","PromptTemplate","_proto","prototype","str","replace","word","index","toLowerCase","toUpperCase","chain","_chain","_asyncToGenerator","_regeneratorRuntime","mark","_callee","input","response","aiMessage","wrap","_callee$","_context","prev","next","RunnableSequence","from","RunnablePassthrough","invoke","sent","AIMessage","abrupt","content","stop","_x","apply","arguments","BaseAgent","run","_run","agent","agentExecutor","createStructuredChatAgent","AgentExecutor","handleParsingErrors","BaseTool","_DynamicStructuredToo","schema","func","_this","call","_ref","args","_inheritsLoose","DynamicStructuredTool"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;AAcG;AAEH,IAAMA,IAAI,GAAG,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AA,EAAAC,qBAAA,CAAA,CAAA;AAAA,EAAA,IAAA3C,MAAA,GAAA0C,QAAA,CAAAzC,SAAA,CAAA;AAAAD,EAAAA,MAAA,CAMDlB,OAAO,GAAP,SAAAA,OAAOA,CAACI,IAAY,EAAA;IAClB,OAAOR,IAAI,CAAC,IAAI,CAACO,SAAS,EAAE,IAAI,CAACC,IAAI,EAAEA,IAAI,CAAC,CAAA;GAC7C,CAAA;AAAAc,EAAAA,MAAA,CAEDN,QAAQ,GAAR,SAAAA,QAAQA,CAACQ,GAAW,EAAA;IAClB,OAAOA,GAAG,CACPC,OAAO,CAAC,qBAAqB,EAAE,UAAUC,IAAI,EAAEC,KAAK,EAAA;AACnD,MAAA,OAAOA,KAAK,KAAK,CAAC,GAAGD,IAAI,CAACE,WAAW,EAAE,GAAGF,IAAI,CAACG,WAAW,EAAE,CAAA;AAC9D,KAAC,CAAC,CACDJ,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;GACvB,CAAA;AAAAH,EAAAA,MAAA,CAEDJ,UAAU,GAAV,SAAAA,UAAUA,CAACM,GAAW,EAAA;IACpB,OAAOA,GAAG,CACPC,OAAO,CAAC,qBAAqB,EAAE,UAAUC,IAAI,EAAEC,KAAK,EAAA;AACnD,MAAA,OAAOA,KAAK,KAAK,CAAC,GAAGD,IAAI,CAACE,WAAW,EAAE,GAAG,GAAG,GAAGF,IAAI,CAACE,WAAW,EAAE,CAAA;AACpE,KAAC,CAAC,CACDH,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;GACvB,CAAA;AAAA,EAAA,OAAAuC,QAAA,CAAA;AAAA,CAAA,CA/C2BS,qBAAqB;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "medpromptjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A library of langchain base classes for building medical applications.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Bell Eapen",
|
|
@@ -30,7 +30,8 @@
|
|
|
30
30
|
"prepare": "dts build",
|
|
31
31
|
"size": "size-limit",
|
|
32
32
|
"start": "dts watch",
|
|
33
|
-
"test": "dts test",
|
|
33
|
+
"test": "dts test --verbose --testPathIgnorePatterns=./test/e2e/*",
|
|
34
|
+
"e2e": "dts test --verbose ./test/e2e --detectOpenHandles",
|
|
34
35
|
"docs": "typedoc --out docs src"
|
|
35
36
|
},
|
|
36
37
|
"husky": {
|
|
@@ -61,29 +62,30 @@
|
|
|
61
62
|
}
|
|
62
63
|
],
|
|
63
64
|
"devDependencies": {
|
|
65
|
+
"@langchain/google-genai": "^0.2.9",
|
|
64
66
|
"@size-limit/preset-small-lib": "^11.0.1",
|
|
65
67
|
"@tsconfig/recommended": "^1.0.3",
|
|
66
68
|
"@types/cors": "^2.8.17",
|
|
67
|
-
"@types/express": "^
|
|
69
|
+
"@types/express": "^5.0.2",
|
|
68
70
|
"@types/supertest": "^6.0.2",
|
|
69
71
|
"dts-cli": "^2.0.3",
|
|
70
|
-
"google-auth-library": "^
|
|
71
|
-
"husky": "^
|
|
72
|
-
"size-limit": "^11.0
|
|
73
|
-
"supertest": "^
|
|
72
|
+
"google-auth-library": "^9.15.1",
|
|
73
|
+
"husky": "^9.1.7",
|
|
74
|
+
"size-limit": "^11.2.0",
|
|
75
|
+
"supertest": "^7.1.1",
|
|
74
76
|
"ts-mockito": "^2.6.1",
|
|
75
77
|
"tslib": "^2.6.2",
|
|
76
|
-
"typedoc": "^0.
|
|
78
|
+
"typedoc": "^0.28.4",
|
|
77
79
|
"typescript": "^5.3.3"
|
|
78
80
|
},
|
|
79
81
|
"dependencies": {
|
|
82
|
+
"@langchain/community": "0.3.44",
|
|
80
83
|
"cors": "^2.8.5",
|
|
81
84
|
"dotenv": "^16.3.1",
|
|
82
85
|
"express": "^5.1.0",
|
|
83
86
|
"langchain": "^0.3.27",
|
|
84
|
-
"@langchain/community": "0.3.44",
|
|
85
87
|
"reflect-metadata": "^0.2.1",
|
|
86
88
|
"tslog": "^4.9.2",
|
|
87
89
|
"tsyringe": "^4.8.0"
|
|
88
90
|
}
|
|
89
|
-
}
|
|
91
|
+
}
|
package/src/chain.ts
CHANGED
|
@@ -22,35 +22,44 @@ import {
|
|
|
22
22
|
RunnablePassthrough,
|
|
23
23
|
RunnableSequence,
|
|
24
24
|
} from '@langchain/core/runnables';
|
|
25
|
+
import { AIMessage } from '@langchain/core/messages';
|
|
25
26
|
|
|
26
27
|
export class BaseChain {
|
|
27
28
|
container: any;
|
|
28
29
|
tools: ToolInterface[];
|
|
29
30
|
name: string;
|
|
30
31
|
description: string;
|
|
31
|
-
prompt:
|
|
32
|
+
prompt: any;
|
|
32
33
|
llm: LLM;
|
|
33
|
-
template: string
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
`;
|
|
34
|
+
template: string;
|
|
35
|
+
chat_model: boolean;
|
|
36
|
+
runnable: any;
|
|
37
37
|
|
|
38
38
|
constructor(
|
|
39
39
|
container: any,
|
|
40
40
|
name: string,
|
|
41
41
|
description: string,
|
|
42
|
-
template: string =
|
|
42
|
+
template: string = `
|
|
43
|
+
Summarize the following text:
|
|
44
|
+
{input}
|
|
45
|
+
`
|
|
43
46
|
) {
|
|
44
47
|
this.container = container;
|
|
45
48
|
this.name = name === '' ? this.camelize(this.constructor.name) : name;
|
|
46
49
|
this.description =
|
|
47
50
|
description === '' ? this.snake_case(this.constructor.name) : description;
|
|
51
|
+
this.template = template;
|
|
48
52
|
this.tools = this.resolve('tools');
|
|
49
|
-
this.prompt =
|
|
50
|
-
this.resolve('prompt') !== ''
|
|
51
|
-
? this.resolve('prompt')
|
|
52
|
-
: PromptTemplate.fromTemplate(this.template);
|
|
53
53
|
this.llm = this.resolve('main-llm');
|
|
54
|
+
this.chat_model = false;
|
|
55
|
+
try {
|
|
56
|
+
this.chat_model = this.resolve('chat_model');
|
|
57
|
+
} catch (e) {}
|
|
58
|
+
if (this.chat_model) {
|
|
59
|
+
this.prompt = ChatPromptTemplate.fromTemplate(this.template);
|
|
60
|
+
} else {
|
|
61
|
+
this.prompt = PromptTemplate.fromTemplate(this.template);
|
|
62
|
+
}
|
|
54
63
|
}
|
|
55
64
|
|
|
56
65
|
resolve(name: string) {
|
|
@@ -75,12 +84,17 @@ export class BaseChain {
|
|
|
75
84
|
|
|
76
85
|
// https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/
|
|
77
86
|
//! Override this method
|
|
78
|
-
chain(input: any) {
|
|
79
|
-
|
|
87
|
+
async chain(input: any) {
|
|
88
|
+
this.runnable = RunnableSequence.from([
|
|
80
89
|
new RunnablePassthrough(),
|
|
81
90
|
this.prompt,
|
|
82
91
|
this.llm,
|
|
83
92
|
]);
|
|
84
|
-
|
|
93
|
+
if (this.chat_model) {
|
|
94
|
+
const response: string = await this.runnable.invoke(input);
|
|
95
|
+
const aiMessage = new AIMessage(response);
|
|
96
|
+
return aiMessage.content;
|
|
97
|
+
}
|
|
98
|
+
return await this.runnable.invoke(input);
|
|
85
99
|
}
|
|
86
100
|
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { BaseChain } from '../chain';
|
|
2
|
+
import { MapQuery } from './map_query';
|
|
3
|
+
import { CharacterTextSplitter } from '@langchain/textsplitters';
|
|
4
|
+
import { ReduceChain } from './reduce';
|
|
5
|
+
import { MapDoc } from './map_doc';
|
|
6
|
+
import { Logger, ILogObj } from 'tslog';
|
|
7
|
+
|
|
8
|
+
const log: Logger<ILogObj> = new Logger();
|
|
9
|
+
export class LLMLoop extends BaseChain {
|
|
10
|
+
// 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, '', '');
|
|
17
|
+
|
|
18
|
+
async checkAssertion(expression: string, context: string): Promise<boolean> {
|
|
19
|
+
const _input = {
|
|
20
|
+
input: {
|
|
21
|
+
expression: expression,
|
|
22
|
+
context: this.findDatesAndConvertToTimeElapsed(
|
|
23
|
+
this.camelToString(context)
|
|
24
|
+
),
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return this.chain(_input);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async checkMention(expression: string, context: string): Promise<boolean> {
|
|
32
|
+
if (expression && context) return true; // Placeholder for assertion check logic
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async checkNegation(expression: string, context: string): Promise<boolean> {
|
|
37
|
+
if (expression && context) return true; // Placeholder for assertion check logic
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
findDatesAndConvertToTimeElapsed(text: string): string {
|
|
42
|
+
// Regular expression to match dates in format of 'mm/dd/yyyy' or 'mm-dd-yyyy'
|
|
43
|
+
const dateRegex = /(\d{1,2}[-/]\d{1,2}[-/]\d{4})/g;
|
|
44
|
+
let matches;
|
|
45
|
+
let currentDate = new Date();
|
|
46
|
+
|
|
47
|
+
while ((matches = dateRegex.exec(text)) !== null) {
|
|
48
|
+
let date = new Date(matches[0]);
|
|
49
|
+
let timeElapsed =
|
|
50
|
+
(currentDate.getTime() - date.getTime()) / (1000 * 60 * 60 * 24);
|
|
51
|
+
text = text.replace(
|
|
52
|
+
matches[0],
|
|
53
|
+
Math.floor(timeElapsed).toString() + ' days ago.'
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return text;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
camelToString(camelCase: string): string {
|
|
60
|
+
return camelCase.replace(/([A-Z])/g, ' $1').replace(/^./, function (str) {
|
|
61
|
+
return str.toUpperCase();
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
stringToBoolean(str: string): boolean {
|
|
66
|
+
log.info(`Converting string to boolean: ${str}`);
|
|
67
|
+
const positiveKeywords = [
|
|
68
|
+
'true',
|
|
69
|
+
'contains',
|
|
70
|
+
'confirms',
|
|
71
|
+
'yes',
|
|
72
|
+
'affirmative',
|
|
73
|
+
'matches',
|
|
74
|
+
'exists',
|
|
75
|
+
'present',
|
|
76
|
+
'absolutely',
|
|
77
|
+
'certainly',
|
|
78
|
+
'definitely',
|
|
79
|
+
'indeed',
|
|
80
|
+
];
|
|
81
|
+
const lowerStr = str.toLowerCase();
|
|
82
|
+
return positiveKeywords.some((keyword) => lowerStr.includes(keyword));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async textSplitter(
|
|
86
|
+
text: string,
|
|
87
|
+
chunkSize: number = 3900,
|
|
88
|
+
chunkOverlap: number = 0
|
|
89
|
+
): Promise<string[]> {
|
|
90
|
+
const textSplitter = new CharacterTextSplitter({
|
|
91
|
+
chunkSize: chunkSize,
|
|
92
|
+
chunkOverlap: chunkOverlap,
|
|
93
|
+
});
|
|
94
|
+
const texts = await textSplitter.splitText(text);
|
|
95
|
+
// Log the number of chunks created
|
|
96
|
+
if (texts.length === 0) {
|
|
97
|
+
log.warn('No text chunks created. Please check the input text.');
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
if (texts.length > 100) {
|
|
101
|
+
log.warn(
|
|
102
|
+
`Warning: More than 100 text chunks created (${texts.length}). This may affect performance.`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
if (chunkSize < 1000) {
|
|
106
|
+
log.warn(
|
|
107
|
+
`Warning: Chunk size is less than 1000 characters (${chunkSize}). This may lead to very small chunks.`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
log.info(
|
|
111
|
+
`Text split into ${texts.length} chunks with size ${chunkSize} and overlap ${chunkOverlap}`
|
|
112
|
+
);
|
|
113
|
+
return texts;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
printValues(obj: any) {
|
|
117
|
+
for (var key in obj) {
|
|
118
|
+
if (typeof obj[key] === 'object') {
|
|
119
|
+
this.printValues(obj[key]);
|
|
120
|
+
} else {
|
|
121
|
+
this.string_expression += obj[key] + ' ';
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const _eliminate = [
|
|
125
|
+
'true',
|
|
126
|
+
'false',
|
|
127
|
+
'null',
|
|
128
|
+
'undefined',
|
|
129
|
+
'String',
|
|
130
|
+
'Number',
|
|
131
|
+
'Object',
|
|
132
|
+
'Array',
|
|
133
|
+
'Boolean',
|
|
134
|
+
'value',
|
|
135
|
+
'Type',
|
|
136
|
+
'Specifier',
|
|
137
|
+
'Named',
|
|
138
|
+
];
|
|
139
|
+
_eliminate.forEach((element) => {
|
|
140
|
+
const regex = new RegExp(element, 'g');
|
|
141
|
+
this.string_expression = this.string_expression.replace(regex, '');
|
|
142
|
+
});
|
|
143
|
+
this.string_expression = this.string_expression.replace(
|
|
144
|
+
/(?:https?|ftp):\/\/[\n\S]+/g,
|
|
145
|
+
''
|
|
146
|
+
);
|
|
147
|
+
return this.camelToString(this.string_expression);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async chain(input: any) {
|
|
151
|
+
// Create a chain using RunnableSequence, RunnablePassthrough and RunnableParallel
|
|
152
|
+
// input has two properties: expression and context
|
|
153
|
+
// expression goes through mapQuery and parallely
|
|
154
|
+
// context goes through textSplitter and mapDoc
|
|
155
|
+
// Finally reduceChain is called with the results of mapQuery and mapDoc
|
|
156
|
+
// LCEL-based implementation using RunnableSequence, RunnableParallel, and RunnablePassthrough
|
|
157
|
+
const { RunnableSequence, RunnableParallel, RunnablePassthrough } =
|
|
158
|
+
await import('@langchain/core/runnables');
|
|
159
|
+
|
|
160
|
+
const mapQueryChain = RunnablePassthrough.assign({
|
|
161
|
+
query: async (input: any) =>
|
|
162
|
+
this.mapQuery.chain({
|
|
163
|
+
input: { expression: input.input.expression },
|
|
164
|
+
}),
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const mapDocChain = RunnablePassthrough.assign({
|
|
168
|
+
documents: async (input: any) => {
|
|
169
|
+
const textChunks = await this.textSplitter(input.input.context);
|
|
170
|
+
return Promise.all(
|
|
171
|
+
textChunks.map((chunk) =>
|
|
172
|
+
this.mapDoc.chain({
|
|
173
|
+
input: { document: chunk, question: input.input.expression },
|
|
174
|
+
})
|
|
175
|
+
)
|
|
176
|
+
);
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const parallelChain = RunnableParallel.from([mapQueryChain, mapDocChain]);
|
|
181
|
+
|
|
182
|
+
const sequence = RunnableSequence.from([
|
|
183
|
+
parallelChain,
|
|
184
|
+
async (results: any) => {
|
|
185
|
+
// results is an array: [{ query: ... }, { documents: [...] }]
|
|
186
|
+
const query = results[0].query;
|
|
187
|
+
const documents = results[1].documents;
|
|
188
|
+
return this.reduceChain.chain({
|
|
189
|
+
input: {
|
|
190
|
+
query: query,
|
|
191
|
+
facts: documents,
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
},
|
|
195
|
+
(result: any) => this.stringToBoolean(result),
|
|
196
|
+
]);
|
|
197
|
+
|
|
198
|
+
return sequence.invoke(input);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MapQuery } from './map_query';
|
|
@@ -0,0 +1,12 @@
|
|
|
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 a question.\n
|
|
8
|
+
Summarize the document chunk commenting on: {query} \n
|
|
9
|
+
Do not include absent or negative mentions.\n
|
|
10
|
+
document: {document} \n
|
|
11
|
+
Summary:: `;
|
|
12
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
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 a CQL query to a natural language.
|
|
8
|
+
You should give a single line answer>> as in the example below.
|
|
9
|
+
Example:
|
|
10
|
+
|
|
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
|
+
|
|
19
|
+
answer>> Penicillin allergy, Headache, Weakness, Numbness, and Intact sensation to light touch as findings?
|
|
20
|
+
|
|
21
|
+
CQL: exists (
|
|
22
|
+
[DocumentReference] D
|
|
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
|
+
`;
|
|
36
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
You will be given facts and a question to answer with ONLY a yes or no.\n
|
|
8
|
+
Do not include the facts in the answer.\n
|
|
9
|
+
Say yes if the facts mentions all aspects of the question, else say no.\n
|
|
10
|
+
facts: {facts} \n
|
|
11
|
+
question: Does the facts mention {question} Say YES OR NO Only::`;
|
|
12
|
+
}
|