medpromptjs 0.5.5 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -5
- package/dist/agent.d.ts +2 -1
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +51 -0
- package/dist/agent.js.map +1 -0
- package/dist/chain.d.ts +1 -0
- package/dist/chain.d.ts.map +1 -0
- package/dist/chain.js +120 -0
- package/dist/chain.js.map +1 -0
- package/dist/embedding.d.ts +1 -0
- package/dist/embedding.d.ts.map +1 -0
- package/dist/embedding.js +82 -0
- package/dist/embedding.js.map +1 -0
- package/dist/index.d.ts +3 -4
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -8
- package/dist/index.js.map +1 -0
- package/dist/llm.d.ts +1 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +147 -0
- package/dist/llm.js.map +1 -0
- package/dist/llm_loop.d.ts +1 -0
- package/dist/llm_loop.d.ts.map +1 -0
- package/dist/llm_loop.js +352 -0
- package/dist/llm_loop.js.map +1 -0
- package/dist/mydi.d.ts +1 -0
- package/dist/mydi.d.ts.map +1 -0
- package/dist/mydi.js +34 -0
- package/dist/mydi.js.map +1 -0
- package/dist/tool.d.ts +2 -1
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +69 -0
- package/dist/tool.js.map +1 -0
- package/package.json +43 -55
- package/dist/medpromptjs.cjs.development.js +0 -764
- package/dist/medpromptjs.cjs.development.js.map +0 -1
- package/dist/medpromptjs.cjs.production.min.js +0 -2
- package/dist/medpromptjs.cjs.production.min.js.map +0 -1
- package/dist/medpromptjs.esm.js +0 -755
- package/dist/medpromptjs.esm.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# medpromptjs
|
|
2
2
|
|
|
3
|
-
A set of base classes for making GenAI application development easy. Implements the [LLM-in-the-Loop CQL execution on unstructured data](https://nuchange.ca/2025/06/v-llm-in-the-loop-cql-execution-with-unstructured-data-and-fhir-terminology-support.html).
|
|
3
|
+
A set of base classes for making GenAI application development easy. Implements the [LLM-in-the-Loop CQL execution on unstructured data](https://nuchange.ca/2025/06/v-llm-in-the-loop-cql-execution-with-unstructured-data-and-fhir-terminology-support.html). The implementation used by the [end-to-end application](https://github.com/dermatologist/cql-express-r4) is [in this file](src/llm_loop.ts).
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/medpromptjs)
|
|
6
6
|
[](https://www.npmjs.com/package/medpromptjs)
|
|
@@ -14,9 +14,11 @@ A set of base classes for making GenAI application development easy. Implements
|
|
|
14
14
|
npm i --save medpromptjs
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
##
|
|
17
|
+
## Base classes
|
|
18
18
|
|
|
19
|
-
###
|
|
19
|
+
### [BaseChain](src/chain) and [BaseAgent](src/agent) use dependency injection with the help of [tsyringe](https://github.com/microsoft/tsyringe). Other useful classes are:
|
|
20
|
+
|
|
21
|
+
#### BaseLLM
|
|
20
22
|
|
|
21
23
|
`BaseLLM` is a base class for LLMs that communicate with a remote API. It extends the LangChain `LLM` class and provides configurable parameters for model inference.
|
|
22
24
|
|
|
@@ -34,7 +36,7 @@ const llm = new BaseLLM({
|
|
|
34
36
|
const response = await llm.invoke('Hello, how are you?');
|
|
35
37
|
```
|
|
36
38
|
|
|
37
|
-
|
|
39
|
+
#### BaseEmbedding
|
|
38
40
|
|
|
39
41
|
`BaseEmbedding` is a base class for embeddings that communicate with a remote API. It extends the LangChain `Embeddings` class.
|
|
40
42
|
|
|
@@ -60,8 +62,8 @@ If you find this project useful, give us a star. It helps others discover the pr
|
|
|
60
62
|
## Related projects
|
|
61
63
|
|
|
62
64
|
* [FHIRy - FHIR to pandas dataframe](https://github.com/dermatologist/fhiry)
|
|
65
|
+
* [DHTI: a reference architecture for Gen AI in healthcare.](https://github.com/dermatologist/dhti)
|
|
63
66
|
* [kedro-multimodal - Template for multi-modal machine learning in healthcare using Kedro](https://github.com/dermatologist/kedro-multimodal)
|
|
64
|
-
* [ckblib - A library for clinical knowledge graphs](https://github.com/dermatologist/ckblib)
|
|
65
67
|
|
|
66
68
|
## Contributing
|
|
67
69
|
* PR welcome
|
package/dist/agent.d.ts
CHANGED
|
@@ -21,7 +21,8 @@ export declare class BaseAgent extends BaseChain {
|
|
|
21
21
|
set tools(value: ToolInterface[]);
|
|
22
22
|
/**
|
|
23
23
|
* Run the agent using the provided input.
|
|
24
|
-
* This uses
|
|
24
|
+
* This uses the classic agent API retained by LangChain 1.x.
|
|
25
25
|
*/
|
|
26
26
|
run(input: any): Promise<import("@langchain/core/utils/types").ChainValues>;
|
|
27
27
|
}
|
|
28
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAM3D,qBAAa,SAAU,SAAQ,SAAS;IACtC,OAAO,CAAC,MAAM,CAAuB;IAErC,IAAI,KAAK,IAAI,aAAa,EAAE,CAE3B;IACD,IAAI,KAAK,CAAC,KAAK,EAAE,aAAa,EAAE,EAE/B;IAED;;;OAGG;IACG,GAAG,CAAC,KAAK,EAAE,GAAG;CAcrB"}
|
package/dist/agent.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2024 Bell Eapen
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.BaseAgent = void 0;
|
|
19
|
+
const chain_1 = require("./chain");
|
|
20
|
+
const agents_1 = require("@langchain/classic/agents");
|
|
21
|
+
class BaseAgent extends chain_1.BaseChain {
|
|
22
|
+
constructor() {
|
|
23
|
+
super(...arguments);
|
|
24
|
+
this._tools = [];
|
|
25
|
+
}
|
|
26
|
+
get tools() {
|
|
27
|
+
return this._tools;
|
|
28
|
+
}
|
|
29
|
+
set tools(value) {
|
|
30
|
+
this._tools = value;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Run the agent using the provided input.
|
|
34
|
+
* This uses the classic agent API retained by LangChain 1.x.
|
|
35
|
+
*/
|
|
36
|
+
async run(input) {
|
|
37
|
+
const agent = await (0, agents_1.createStructuredChatAgent)({
|
|
38
|
+
llm: this.llm,
|
|
39
|
+
tools: this._tools,
|
|
40
|
+
prompt: this.prompt,
|
|
41
|
+
});
|
|
42
|
+
const agentExecutor = new agents_1.AgentExecutor({
|
|
43
|
+
agent,
|
|
44
|
+
tools: this.tools,
|
|
45
|
+
handleParsingErrors: 'Please try again, paying close attention to the final answer',
|
|
46
|
+
});
|
|
47
|
+
return await agentExecutor.invoke(input);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.BaseAgent = BaseAgent;
|
|
51
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,mCAAoC;AAEpC,sDAGmC;AAEnC,MAAa,SAAU,SAA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AAE,GAAG;IAU1B,gBAAgB;IAQhB,IAAI,IAAI,IAAI,MAAM,CAEjB;IACD,IAAI,IAAI,CAAC,KAAK,EAAE,MAAM,EAErB;IAED,IAAI,WAAW,IAAI,MAAM,CAExB;IACD,IAAI,WAAW,CAAC,KAAK,EAAE,MAAM,EAG5B;IAED,IAAI,QAAQ,IAAI,MAAM,CAErB;IACD,IAAI,QAAQ,CAAC,KAAK,EAAE,MAAM,EAGzB;IAED,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,YAAY,GAAE,GAAU,GAAG,GAAG;IAOpD,QAAQ,CAAC,GAAG,EAAE,MAAM;IAQpB,UAAU,CAAC,GAAG,EAAE,MAAM;IAUhB,KAAK,CAAC,KAAK,EAAE,GAAG;IAmBhB,MAAM,CAAC,KAAK,EAAE,GAAG;CAGxB"}
|
package/dist/chain.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2024 Bell Eapen
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.BaseChain = void 0;
|
|
22
|
+
const prompts_1 = require("@langchain/core/prompts");
|
|
23
|
+
const mydi_1 = __importDefault(require("./mydi"));
|
|
24
|
+
const runnables_1 = require("@langchain/core/runnables");
|
|
25
|
+
const messages_1 = require("@langchain/core/messages");
|
|
26
|
+
const output_parsers_1 = require("@langchain/core/output_parsers");
|
|
27
|
+
const testing_1 = require("@langchain/core/utils/testing");
|
|
28
|
+
class BaseChain {
|
|
29
|
+
constructor(container) {
|
|
30
|
+
this._name = '';
|
|
31
|
+
this._description = '';
|
|
32
|
+
this._template = '';
|
|
33
|
+
this.fakeLLM = new testing_1.FakeListChatModel({
|
|
34
|
+
responses: [
|
|
35
|
+
'Hello, this is a fake response (There is no real LLM available)!',
|
|
36
|
+
'bootstrap main-llm with a real LLM instance',
|
|
37
|
+
'This is another fake response (There is no real LLM available)!',
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
this.container = container;
|
|
41
|
+
this._name = this.camelize(this.constructor.name);
|
|
42
|
+
this._description = this.snake_case(this.constructor.name);
|
|
43
|
+
this.llm = this.resolve('llm', this.fakeLLM);
|
|
44
|
+
this._template = this.resolve('template', '{input}');
|
|
45
|
+
this.chat_model = this.resolve('chat-model', false);
|
|
46
|
+
this.initializePrompt();
|
|
47
|
+
}
|
|
48
|
+
initializePrompt() {
|
|
49
|
+
if (this.chat_model) {
|
|
50
|
+
this.prompt = prompts_1.ChatPromptTemplate.fromTemplate(this._template);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this.prompt = prompts_1.PromptTemplate.fromTemplate(this._template);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Getters and setters
|
|
57
|
+
get name() {
|
|
58
|
+
return this._name;
|
|
59
|
+
}
|
|
60
|
+
set name(value) {
|
|
61
|
+
this._name = value === '' ? this.camelize(this.constructor.name) : value;
|
|
62
|
+
}
|
|
63
|
+
get description() {
|
|
64
|
+
return this._description;
|
|
65
|
+
}
|
|
66
|
+
set description(value) {
|
|
67
|
+
this._description =
|
|
68
|
+
value === '' ? this.snake_case(this.constructor.name) : value;
|
|
69
|
+
}
|
|
70
|
+
get template() {
|
|
71
|
+
return this._template;
|
|
72
|
+
}
|
|
73
|
+
set template(value) {
|
|
74
|
+
this._template = value;
|
|
75
|
+
this.initializePrompt();
|
|
76
|
+
}
|
|
77
|
+
resolve(name, defaultValue = null) {
|
|
78
|
+
return ((0, mydi_1.default)(this.container, this.camelize(this.constructor.name), name) ||
|
|
79
|
+
defaultValue);
|
|
80
|
+
}
|
|
81
|
+
camelize(str) {
|
|
82
|
+
return str
|
|
83
|
+
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
84
|
+
return index === 0 ? word.toLowerCase() : word.toUpperCase();
|
|
85
|
+
})
|
|
86
|
+
.replace(/\s+/g, '');
|
|
87
|
+
}
|
|
88
|
+
snake_case(str) {
|
|
89
|
+
return str
|
|
90
|
+
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
91
|
+
return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();
|
|
92
|
+
})
|
|
93
|
+
.replace(/\s+/g, '');
|
|
94
|
+
}
|
|
95
|
+
// https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/
|
|
96
|
+
//! Override this method
|
|
97
|
+
async chain(input) {
|
|
98
|
+
const outputParser = new output_parsers_1.StringOutputParser();
|
|
99
|
+
this.runnable = runnables_1.RunnableSequence.from([
|
|
100
|
+
{
|
|
101
|
+
input: new runnables_1.RunnablePassthrough(),
|
|
102
|
+
// context: async () => loadContextFromStore(),
|
|
103
|
+
},
|
|
104
|
+
this.prompt,
|
|
105
|
+
this.llm,
|
|
106
|
+
outputParser,
|
|
107
|
+
]);
|
|
108
|
+
if (this.chat_model) {
|
|
109
|
+
const response = await this.runnable.invoke(input);
|
|
110
|
+
const aiMessage = new messages_1.AIMessage(response);
|
|
111
|
+
return aiMessage.content;
|
|
112
|
+
}
|
|
113
|
+
return await this.runnable.invoke(input);
|
|
114
|
+
}
|
|
115
|
+
async invoke(input) {
|
|
116
|
+
return this.chain(input);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.BaseChain = BaseChain;
|
|
120
|
+
//# sourceMappingURL=chain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chain.js","sourceRoot":"","sources":["../src/chain.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;;;;AAEH,qDAA6E;AAE7E,kDAA0B;AAC1B,yDAGmC;AACnC,uDAAqD;AACrD,mEAAoE;AACpE,2DAAkE;AAElE,MAAa,SAAS;IAiBpB,YAAY,SAAc;QAflB,UAAK,GAAG,EAAE,CAAC;QACX,iBAAY,GAAG,EAAE,CAAC;QAClB,cAAS,GAAG,EAAE,CAAC;QAKvB,YAAO,GAAuB,IAAI,2BAAiB,CAAC;YAClD,SAAS,EAAE;gBACT,kEAAkE;gBAClE,6CAA6C;gBAC7C,iEAAiE;aAClE;SACF,CAAC,CAAC;QAGD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QAClD,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;QAC3D,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC7C,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,SAAS,CAAC,CAAC;QACrD,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;QACpD,IAAI,CAAC,gBAAgB,EAAE,CAAC;IAC1B,CAAC;IAED,gBAAgB;QACd,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACpB,IAAI,CAAC,MAAM,GAAG,4BAAkB,C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AAC;CACF;AAzGD,8BAyGC"}
|
package/dist/embedding.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedding.d.ts","sourceRoot":"","sources":["../src/embedding.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAE/E;;GAEG;AACH,MAAM,WAAW,kBAAmB,SAAQ,gBAAgB;IAC1D,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;;GAIG;AACH,qBAAa,aAAc,SAAQ,UAAU;IAC3C,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;gBAEH,MAAM,EAAE,kBAAkB;IAOtC;;;;OAIG;IACG,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;IAI1D;;;;OAIG;IACG,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAKjD;;;;OAIG;cACa,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;CAuCpE"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2024 Bell Eapen
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.BaseEmbedding = void 0;
|
|
19
|
+
const embeddings_1 = require("@langchain/core/embeddings");
|
|
20
|
+
/**
|
|
21
|
+
* BaseEmbedding is a base class for embeddings that communicate with a remote API.
|
|
22
|
+
* It extends the LangChain Embeddings class and provides methods for embedding
|
|
23
|
+
* documents and queries.
|
|
24
|
+
*/
|
|
25
|
+
class BaseEmbedding extends embeddings_1.Embeddings {
|
|
26
|
+
constructor(fields) {
|
|
27
|
+
super(fields);
|
|
28
|
+
this.baseUrl = fields.baseUrl;
|
|
29
|
+
this.model = fields.model;
|
|
30
|
+
this.apiKey = fields.apiKey ?? '';
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Embed a list of documents
|
|
34
|
+
* @param texts - Array of strings to embed
|
|
35
|
+
* @returns Promise of array of embedding vectors
|
|
36
|
+
*/
|
|
37
|
+
async embedDocuments(texts) {
|
|
38
|
+
return this.getEmbeddings(texts);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Embed a single query
|
|
42
|
+
* @param text - String to embed
|
|
43
|
+
* @returns Promise of embedding vector
|
|
44
|
+
*/
|
|
45
|
+
async embedQuery(text) {
|
|
46
|
+
const embeddings = await this.getEmbeddings([text]);
|
|
47
|
+
return embeddings[0];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Helper method to get embeddings for a list of texts
|
|
51
|
+
* @param texts - Array of strings to embed
|
|
52
|
+
* @returns Promise of array of embedding vectors
|
|
53
|
+
*/
|
|
54
|
+
async getEmbeddings(texts) {
|
|
55
|
+
const headers = {
|
|
56
|
+
'Content-Type': 'application/json',
|
|
57
|
+
};
|
|
58
|
+
if (this.apiKey) {
|
|
59
|
+
headers['Authorization'] = `Bearer ${this.apiKey}`;
|
|
60
|
+
}
|
|
61
|
+
const payload = {
|
|
62
|
+
model: this.model,
|
|
63
|
+
input: texts,
|
|
64
|
+
};
|
|
65
|
+
const response = await fetch(this.baseUrl, {
|
|
66
|
+
method: 'POST',
|
|
67
|
+
headers,
|
|
68
|
+
body: JSON.stringify(payload),
|
|
69
|
+
});
|
|
70
|
+
if (!response.ok) {
|
|
71
|
+
const body = await response.text();
|
|
72
|
+
throw new Error(`API request failed: status=${response.status}; body=${body}`);
|
|
73
|
+
}
|
|
74
|
+
const respJson = await response.json();
|
|
75
|
+
if (!('embeddings' in respJson)) {
|
|
76
|
+
throw new Error(`API response missing 'embeddings' key. Received keys: ${Object.keys(respJson).join(', ')}`);
|
|
77
|
+
}
|
|
78
|
+
return respJson.embeddings;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.BaseEmbedding = BaseEmbedding;
|
|
82
|
+
//# sourceMappingURL=embedding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedding.js","sourceRoot":"","sources":["../src/embedding.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,2DAA+E;AAW/E;;;;GAIG;AACH,MAAa,aAAc,SAAQ,uBAAU;IAK3C,YAAY,MAA0B;QACpC,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC9B,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC;IACpC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,cAAc,CAAC,KAAe;QAClC,OAAO,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,UAAU,CAAC,IAAY;QAC3B,MAAM,UAAU,GAAG,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2
2
|
export { BaseAgent } from './agent';
|
|
3
3
|
export { BaseTool } from './tool';
|
|
4
4
|
export { LLMLoop } from './llm_loop';
|
|
5
|
-
export { BaseLLM } from './llm';
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
export type { BaseEmbeddingInput } from './embedding';
|
|
5
|
+
export { BaseLLM, BaseLLMInput, ModelParams } from './llm';
|
|
6
|
+
export { BaseEmbedding, BaseEmbeddingInput } from './embedding';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseEmbedding = exports.BaseLLM = exports.LLMLoop = exports.BaseTool = exports.BaseAgent = exports.BaseChain = void 0;
|
|
4
|
+
var chain_1 = require("./chain");
|
|
5
|
+
Object.defineProperty(exports, "BaseChain", { enumerable: true, get: function () { return chain_1.BaseChain; } });
|
|
6
|
+
var agent_1 = require("./agent");
|
|
7
|
+
Object.defineProperty(exports, "BaseAgent", { enumerable: true, get: function () { return agent_1.BaseAgent; } });
|
|
8
|
+
var tool_1 = require("./tool");
|
|
9
|
+
Object.defineProperty(exports, "BaseTool", { enumerable: true, get: function () { return tool_1.BaseTool; } });
|
|
10
|
+
var llm_loop_1 = require("./llm_loop");
|
|
11
|
+
Object.defineProperty(exports, "LLMLoop", { enumerable: true, get: function () { return llm_loop_1.LLMLoop; } });
|
|
12
|
+
var llm_1 = require("./llm");
|
|
13
|
+
Object.defineProperty(exports, "BaseLLM", { enumerable: true, get: function () { return llm_1.BaseLLM; } });
|
|
14
|
+
var embedding_1 = require("./embedding");
|
|
15
|
+
Object.defineProperty(exports, "BaseEmbedding", { enumerable: true, get: function () { return embedding_1.BaseEmbedding; } });
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iCAAoC;AAA3B,kGAAA,SAAS,OAAA;AAClB,iCAAoC;AAA3B,kGAAA,SAAS,OAAA;AAClB,+BAAkC;AAAzB,gGAAA,QAAQ,OAAA;AACjB,uCAAqC;AAA5B,mGAAA,OAAO,OAAA;AAChB,6BAA2D;AAAlD,8FAAA,OAAO,OAAA;AAChB,yCAAgE;AAAvD,0GAAA,aAAa,OAAA"}
|
package/dist/llm.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm.d.ts","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,GAAG,EAAE,KAAK,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAC/E,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,mCAAmC,CAAC;AAElF;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CA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I,sBAAsB,IAAI,WAAW,CAWxC;IAED;;OAEG;IACH,IAAI,iBAAiB,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAM/C;IAED;;OAEG;IACH,QAAQ,IAAI,MAAM;IAIlB;;OAEG;IACH,SAAS,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAQjE;;OAEG;IACG,KAAK,CACT,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,IAAI,CAAC,mBAAmB,CAAC,EACnC,WAAW,CAAC,EAAE,wBAAwB,GACrC,OAAO,CAAC,MAAM,CAAC;CAsEnB"}
|
package/dist/llm.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2024 Bell Eapen
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.BaseLLM = void 0;
|
|
19
|
+
const llms_1 = require("@langchain/core/language_models/llms");
|
|
20
|
+
/**
|
|
21
|
+
* BaseLLM is a base class for LLMs that communicate with a remote API.
|
|
22
|
+
* It extends the LangChain LLM class and provides configurable parameters
|
|
23
|
+
* for model inference.
|
|
24
|
+
*/
|
|
25
|
+
class BaseLLM extends llms_1.LLM {
|
|
26
|
+
constructor(fields) {
|
|
27
|
+
super(fields);
|
|
28
|
+
this.baseUrl = fields.baseUrl;
|
|
29
|
+
this.model = fields.model;
|
|
30
|
+
this.apiKey = fields.apiKey ?? '';
|
|
31
|
+
this.timeout = fields.timeout ?? 60000;
|
|
32
|
+
this.backend = fields.backend ?? 'dhti';
|
|
33
|
+
this.temperature = fields.temperature ?? 0.1;
|
|
34
|
+
this.topP = fields.topP ?? 0.8;
|
|
35
|
+
this.topK = fields.topK ?? 40;
|
|
36
|
+
this.nBatch = fields.nBatch ?? 8;
|
|
37
|
+
this.nThreads = fields.nThreads ?? 4;
|
|
38
|
+
this.nPredict = fields.nPredict ?? 256;
|
|
39
|
+
this.maxOutputTokens = fields.maxOutputTokens ?? 512;
|
|
40
|
+
this.repeatLastN = fields.repeatLastN ?? 64;
|
|
41
|
+
this.repeatPenalty = fields.repeatPenalty ?? 1.18;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get the model default parameters
|
|
45
|
+
*/
|
|
46
|
+
get modelDefaultParameters() {
|
|
47
|
+
return {
|
|
48
|
+
maxOutputTokens: this.maxOutputTokens,
|
|
49
|
+
nPredict: this.nPredict,
|
|
50
|
+
topK: this.topK,
|
|
51
|
+
topP: this.topP,
|
|
52
|
+
temperature: this.temperature,
|
|
53
|
+
nBatch: this.nBatch,
|
|
54
|
+
repeatPenalty: this.repeatPenalty,
|
|
55
|
+
repeatLastN: this.repeatLastN,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Get the identifying parameters
|
|
60
|
+
*/
|
|
61
|
+
get identifyingParams() {
|
|
62
|
+
return {
|
|
63
|
+
model: this.model,
|
|
64
|
+
baseUrl: this.baseUrl,
|
|
65
|
+
modelParameters: this.modelDefaultParameters,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get the LLM type identifier
|
|
70
|
+
*/
|
|
71
|
+
_llmType() {
|
|
72
|
+
return 'dhti';
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Prepare the request payload for the API
|
|
76
|
+
*/
|
|
77
|
+
preparePayload(prompt) {
|
|
78
|
+
return {
|
|
79
|
+
model: this.model,
|
|
80
|
+
options: this.modelDefaultParameters,
|
|
81
|
+
messages: [{ role: 'user', content: prompt }],
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Make the API call to generate text
|
|
86
|
+
*/
|
|
87
|
+
async _call(prompt, _options, _runManager) {
|
|
88
|
+
const payload = this.preparePayload(prompt);
|
|
89
|
+
const headers = {
|
|
90
|
+
'Content-Type': 'application/json',
|
|
91
|
+
};
|
|
92
|
+
if (this.apiKey) {
|
|
93
|
+
headers['Authorization'] = `Bearer ${this.apiKey}`;
|
|
94
|
+
}
|
|
95
|
+
const controller = new AbortController();
|
|
96
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
97
|
+
try {
|
|
98
|
+
const response = await fetch(this.baseUrl, {
|
|
99
|
+
method: 'POST',
|
|
100
|
+
headers,
|
|
101
|
+
body: JSON.stringify(payload),
|
|
102
|
+
signal: controller.signal,
|
|
103
|
+
});
|
|
104
|
+
clearTimeout(timeoutId);
|
|
105
|
+
if (!response.ok) {
|
|
106
|
+
const body = await response.text();
|
|
107
|
+
throw new Error(`API request failed: status=${response.status}; body=${body}`);
|
|
108
|
+
}
|
|
109
|
+
const data = await response.json();
|
|
110
|
+
// Expecting structure like: { "choices": [ { "message": { "role":"assistant","content":"..." } } ] }
|
|
111
|
+
// Adapt this path if the API differs
|
|
112
|
+
if (data.choices &&
|
|
113
|
+
Array.isArray(data.choices) &&
|
|
114
|
+
data.choices.length > 0) {
|
|
115
|
+
const choice = data.choices[0];
|
|
116
|
+
// support both "message" and direct "text"
|
|
117
|
+
if (typeof choice === 'object' &&
|
|
118
|
+
choice !== null &&
|
|
119
|
+
'message' in choice &&
|
|
120
|
+
typeof choice.message === 'object' &&
|
|
121
|
+
choice.message !== null) {
|
|
122
|
+
const content = choice.message.content;
|
|
123
|
+
if (typeof content === 'string') {
|
|
124
|
+
return content;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if ('text' in choice) {
|
|
128
|
+
const text = choice.text;
|
|
129
|
+
if (typeof text === 'string') {
|
|
130
|
+
return text;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// Fallback: return raw JSON string for debugging
|
|
135
|
+
return JSON.stringify(data);
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
clearTimeout(timeoutId);
|
|
139
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
140
|
+
throw new Error(`API request timed out after ${this.timeout}ms`);
|
|
141
|
+
}
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
exports.BaseLLM = BaseLLM;
|
|
147
|
+
//# sourceMappingURL=llm.js.map
|
package/dist/llm.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm.js","sourceRoot":"","sources":["../src/llm.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,+DAA+E;AAqC/E;;;;GAIG;AACH,MAAa,OAAQ,SAAQ,UAAG;IAgB9B,YAAY,MAAoB;QAC9B,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC9B,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC;QAClC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,KAAK,CAAC;QACvC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC;QACxC,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,IAAI,GAAG,CAAC;QAC7C,IAAI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