medpromptjs 0.5.3 → 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.
@@ -1,764 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var prompts = require('@langchain/core/prompts');
6
- var runnables = require('@langchain/core/runnables');
7
- var messages = require('@langchain/core/messages');
8
- var output_parsers = require('@langchain/core/output_parsers');
9
- var testing = require('@langchain/core/utils/testing');
10
- var agents = require('langchain/agents');
11
- var tools = require('langchain/tools');
12
- var textsplitters = require('@langchain/textsplitters');
13
- var tslog = require('tslog');
14
- var llms = require('@langchain/core/language_models/llms');
15
- var embeddings = require('@langchain/core/embeddings');
16
-
17
- /**
18
- * Copyright 2024 Bell Eapen
19
- *
20
- * Licensed under the Apache License, Version 2.0 (the "License");
21
- * you may not use this file except in compliance with the License.
22
- * You may obtain a copy of the License at
23
- *
24
- * https://www.apache.org/licenses/LICENSE-2.0
25
- *
26
- * Unless required by applicable law or agreed to in writing, software
27
- * distributed under the License is distributed on an "AS IS" BASIS,
28
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29
- * See the License for the specific language governing permissions and
30
- * limitations under the License.
31
- */
32
- const mydi = (c, m, b) => {
33
- try {
34
- return c.resolve(m + '_' + b);
35
- } catch (e) {}
36
- try {
37
- return c.resolve(m + '-' + b);
38
- } catch (e) {}
39
- try {
40
- return c.resolve(b);
41
- } catch (e) {}
42
- return null;
43
- };
44
-
45
- /**
46
- * Copyright 2024 Bell Eapen
47
- *
48
- * Licensed under the Apache License, Version 2.0 (the "License");
49
- * you may not use this file except in compliance with the License.
50
- * You may obtain a copy of the License at
51
- *
52
- * https://www.apache.org/licenses/LICENSE-2.0
53
- *
54
- * Unless required by applicable law or agreed to in writing, software
55
- * distributed under the License is distributed on an "AS IS" BASIS,
56
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
57
- * See the License for the specific language governing permissions and
58
- * limitations under the License.
59
- */
60
- class BaseChain {
61
- constructor(container) {
62
- this.container = void 0;
63
- this._name = '';
64
- this._description = '';
65
- this._template = '';
66
- this.prompt = void 0;
67
- this.llm = void 0;
68
- this.chat_model = void 0;
69
- this.runnable = void 0;
70
- this.fakeLLM = new testing.FakeListChatModel({
71
- responses: ['Hello, this is a fake response (There is no real LLM available)!', 'bootstrap main-llm with a real LLM instance', 'This is another fake response (There is no real LLM available)!']
72
- });
73
- this.container = container;
74
- this._name = this.camelize(this.constructor.name);
75
- this._description = this.snake_case(this.constructor.name);
76
- this.llm = this.resolve('llm', this.fakeLLM);
77
- this._template = this.resolve('template', '{input}');
78
- this.chat_model = this.resolve('chat-model', false);
79
- this.initializePrompt();
80
- }
81
- initializePrompt() {
82
- if (this.chat_model) {
83
- this.prompt = prompts.ChatPromptTemplate.fromTemplate(this._template);
84
- } else {
85
- this.prompt = prompts.PromptTemplate.fromTemplate(this._template);
86
- }
87
- }
88
- // Getters and setters
89
- get name() {
90
- return this._name;
91
- }
92
- set name(value) {
93
- this._name = value === '' ? this.camelize(this.constructor.name) : value;
94
- }
95
- get description() {
96
- return this._description;
97
- }
98
- set description(value) {
99
- this._description = value === '' ? this.snake_case(this.constructor.name) : value;
100
- }
101
- get template() {
102
- return this._template;
103
- }
104
- set template(value) {
105
- this._template = value;
106
- this.initializePrompt();
107
- }
108
- resolve(name, defaultValue = null) {
109
- return mydi(this.container, this.camelize(this.constructor.name), name) || defaultValue;
110
- }
111
- camelize(str) {
112
- return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
113
- return index === 0 ? word.toLowerCase() : word.toUpperCase();
114
- }).replace(/\s+/g, '');
115
- }
116
- snake_case(str) {
117
- return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
118
- return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();
119
- }).replace(/\s+/g, '');
120
- }
121
- // https://js.langchain.com/v0.1/docs/expression_language/how_to/routing/
122
- //! Override this method
123
- async chain(input) {
124
- const outputParser = new output_parsers.StringOutputParser();
125
- this.runnable = runnables.RunnableSequence.from([{
126
- input: new runnables.RunnablePassthrough()
127
- // context: async () => loadContextFromStore(),
128
- }, this.prompt, this.llm, outputParser]);
129
- if (this.chat_model) {
130
- const response = await this.runnable.invoke(input);
131
- const aiMessage = new messages.AIMessage(response);
132
- return aiMessage.content;
133
- }
134
- return await this.runnable.invoke(input);
135
- }
136
- async invoke(input) {
137
- return this.chain(input);
138
- }
139
- }
140
-
141
- /**
142
- * Copyright 2024 Bell Eapen
143
- *
144
- * Licensed under the Apache License, Version 2.0 (the "License");
145
- * you may not use this file except in compliance with the License.
146
- * You may obtain a copy of the License at
147
- *
148
- * https://www.apache.org/licenses/LICENSE-2.0
149
- *
150
- * Unless required by applicable law or agreed to in writing, software
151
- * distributed under the License is distributed on an "AS IS" BASIS,
152
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
153
- * See the License for the specific language governing permissions and
154
- * limitations under the License.
155
- */
156
- class BaseAgent extends BaseChain {
157
- constructor(...args) {
158
- super(...args);
159
- this._tools = [];
160
- }
161
- get tools() {
162
- return this._tools;
163
- }
164
- set tools(value) {
165
- this._tools = value;
166
- }
167
- /**
168
- * Run the agent using the provided input.
169
- * This uses createStructuredChatAgent and AgentExecutor from langchain/agents.
170
- */
171
- async run(input) {
172
- const agent = await agents.createStructuredChatAgent({
173
- llm: this.llm,
174
- tools: this._tools,
175
- prompt: this.prompt
176
- });
177
- const agentExecutor = new agents.AgentExecutor({
178
- agent: agent,
179
- tools: this.tools,
180
- // verbose: true,
181
- handleParsingErrors: 'Please try again, paying close attention to the final answer'
182
- });
183
- return await agentExecutor.invoke(input);
184
- }
185
- }
186
-
187
- /**
188
- * Copyright 2024 Bell Eapen
189
- *
190
- * Licensed under the Apache License, Version 2.0 (the "License");
191
- * you may not use this file except in compliance with the License.
192
- * You may obtain a copy of the License at
193
- *
194
- * https://www.apache.org/licenses/LICENSE-2.0
195
- *
196
- * Unless required by applicable law or agreed to in writing, software
197
- * distributed under the License is distributed on an "AS IS" BASIS,
198
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
199
- * See the License for the specific language governing permissions and
200
- * limitations under the License.
201
- */
202
- class BaseTool extends tools.DynamicStructuredTool {
203
- constructor(container, name, description, schema, func = null) {
204
- super({
205
- name: name,
206
- description: description,
207
- schema: schema,
208
- func: func
209
- });
210
- /** @hidden */
211
- this.container = void 0;
212
- this.name = void 0;
213
- this.description = void 0;
214
- this.schema = void 0;
215
- this.func = async args => {
216
- return 'override this';
217
- };
218
- this.container = container;
219
- this.name = name === '' ? this.camelize(this.constructor.name) : name;
220
- this.description = description === '' ? this.snake_case(this.constructor.name) : description;
221
- this.schema = schema;
222
- }
223
- resolve(name) {
224
- return mydi(this.container, this.name, name);
225
- }
226
- camelize(str) {
227
- return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
228
- return index === 0 ? word.toLowerCase() : word.toUpperCase();
229
- }).replace(/\s+/g, '');
230
- }
231
- snake_case(str) {
232
- return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
233
- return index === 0 ? word.toLowerCase() : '_' + word.toLowerCase();
234
- }).replace(/\s+/g, '');
235
- }
236
- /**
237
- * @param input - The input to process in batch.
238
- * @param config - The configuration for the batch.
239
- * @returns A promise resolving to an array of results.
240
- */
241
- async _batchWithConfig(input, config) {
242
- // Call the parent implementation or provide your own
243
- return super._batchWithConfig(input, config);
244
- }
245
- }
246
-
247
- const log = /*#__PURE__*/new tslog.Logger();
248
- class LLMLoop extends BaseChain {
249
- constructor(...args) {
250
- super(...args);
251
- // Implement the LLM loop logic here
252
- this.stringExpression = '';
253
- this._mapQueryTemplate = `
254
- You are an assistant that can convert statements to a natural language query as in the example below.\n
255
-
256
-
257
- Statements: Visual foot exam in the last month. Visual foot exam showed active infection.
258
- query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?
259
- Statements: {expression}
260
- query: `;
261
- this._mapDocTemplate = `
262
- You will be given a document and few statements.\n
263
- Extract facts from the document that are relevant to the statements.\n
264
- Do not include any irrelevant information or context.\n
265
-
266
-
267
- Example:\n
268
- document: The patient is a 45-year-old male with a history of hypertension. He presented with chest pain and was diagnosed with myocardial infarction. He was treated with aspirin and beta-blockers.\n
269
- statements: diagnosis of myocardial infarction. currently on beta-blockers.\n
270
- facts: The patient was diagnosed with myocardial infarction and is currently on beta-blockers.\n
271
- document: {document} \n
272
- statements: {statements}\n
273
- facts: `;
274
- this._reduceChainTemplate = `
275
- Say yes if the facts mentions all aspects of the query, else say no.\n
276
-
277
- Example:
278
- facts: Patient had a laproscopy 27 days back. The findings were normal. \n
279
- query: Did the patient have a laproscopy this month?\n
280
- answer: YES. The patient had a laproscopy 27 days back.\n
281
- facts: The patient is a diabetic and hypertensive. He is on metformin and amlodipine. \n
282
- query: Is the patient on heparin and metformin?\n
283
- answer: NO. The patient is on heparin but not on metformin.\n
284
- facts: A visual foot examination was performed 26 days ago to assess skin integrity, circulation, and structural abnormalities. The exam revealed normal skin condition, nail health, circulation, and absence of edema and deformities. However, there were signs of an active infection with erythema and swelling. \n
285
- query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?\n
286
- answer: YES. The patient had a visual foot exam in the last month and there is evidence of active infection.\n
287
- facts: A visual foot examination was performed 126 days ago to assess skin integrity, circulation, and structural abnormalities. The exam revealed normal skin condition, nail health, circulation, and absence of edema and deformities. However, there were signs of an active infection with erythema and swelling. \n
288
- query: Did the patient have a visual foot exam in the last month? Is there any evidence of active infection?\n
289
- answer: NO. The patient did not have a visual foot exam in the last month but there is evidence of active infection.\n
290
- facts: {facts} \n
291
- query: {query}\n
292
- answer: `;
293
- }
294
- // Add getters and setters for the templates if needed
295
- get mapQueryTemplate() {
296
- return this._mapQueryTemplate;
297
- }
298
- set mapQueryTemplate(template) {
299
- this._mapQueryTemplate = template;
300
- }
301
- get mapDocTemplate() {
302
- return this._mapDocTemplate;
303
- }
304
- set mapDocTemplate(template) {
305
- this._mapDocTemplate = template;
306
- }
307
- get reduceChainTemplate() {
308
- return this._reduceChainTemplate;
309
- }
310
- set reduceChainTemplate(template) {
311
- this._reduceChainTemplate = template;
312
- }
313
- async checkAssertion(expression, context) {
314
- let _expression = '';
315
- try {
316
- // Check if expression is a valid JSON string
317
- _expression = this.printValues(JSON.parse(expression));
318
- } catch (error) {
319
- _expression = expression; // If not, use the expression as is
320
- }
321
- log.info(`Checking assertion with expression: ${_expression}`);
322
- // Initialize context and content
323
- let _context = undefined;
324
- let _content = '';
325
- try {
326
- _context = JSON.parse(context);
327
- } catch (error) {
328
- _context = context; // If not, use the context as is
329
- log.warn(`Context is not a valid JSON string: ${context}`);
330
- log.warn(`Using context as a string: ${_context}`);
331
- }
332
- try {
333
- // Check if context is a valid JSON string
334
- _context = JSON.parse(context);
335
- _context.forEach(element => {
336
- element.content.forEach(contentItem => {
337
- _content += atob(contentItem.attachment.data.value);
338
- });
339
- });
340
- } catch (error) {
341
- _content = context; // If not, use the context as is
342
- log.warn(`Context is not a valid JSON : ${context}`);
343
- log.warn(`Using context as a string: ${_content}`);
344
- }
345
- _content = this.findDatesAndConvertToTimeElapsed(_content.replace(/(\r\n|\n|\r)/gm, ' '));
346
- log.info(`Checking assertion with context: ${_content}`);
347
- const _input = {
348
- input: {
349
- expression: _expression,
350
- context: _content
351
- }
352
- };
353
- const result = await this.chain(_input);
354
- log.info(`Assertion check result: ${result}`);
355
- return result;
356
- }
357
- async checkMention(expression, context) {
358
- if (expression && context) return true; // Placeholder for assertion check logic
359
- return false;
360
- }
361
- async checkNegation(expression, context) {
362
- if (expression && context) return true; // Placeholder for assertion check logic
363
- return false;
364
- }
365
- findDatesAndConvertToTimeElapsed(text) {
366
- // Regular expression to match dates in format of 'mm/dd/yyyy' or 'mm-dd-yyyy'
367
- const dateRegex = /(\d{1,2}[-/]\d{1,2}[-/]\d{4})/g;
368
- let matches;
369
- let currentDate = new Date();
370
- while ((matches = dateRegex.exec(text)) !== null) {
371
- let date = new Date(matches[0]);
372
- let timeElapsed = (currentDate.getTime() - date.getTime()) / (1000 * 60 * 60 * 24);
373
- text = text.replace(matches[0], Math.floor(timeElapsed).toString() + ' days ago.');
374
- }
375
- return text;
376
- }
377
- camelToString(camelCase) {
378
- return camelCase.replace(/([A-Z])/g, ' $1').replace(/^./, function (str) {
379
- return str.toUpperCase();
380
- });
381
- }
382
- stringToBoolean(str) {
383
- log.info(`Converting string to boolean: ${str}`);
384
- const positiveKeywords = ['true', 'contains', 'confirms', 'yes', 'affirmative', 'matches', 'exists', 'present', 'absolutely', 'certainly', 'definitely', 'indeed', 'did have', 'showed', 'revealed'];
385
- const lowerStr = str.toLowerCase();
386
- return positiveKeywords.some(keyword => lowerStr.includes(keyword));
387
- }
388
- async textSplitter(text, chunkSize = 3900, chunkOverlap = 0) {
389
- const textSplitter = new textsplitters.CharacterTextSplitter({
390
- chunkSize: chunkSize,
391
- chunkOverlap: chunkOverlap
392
- });
393
- const texts = await textSplitter.splitText(text);
394
- // Log the number of chunks created
395
- if (texts.length === 0) {
396
- log.warn('No text chunks created. Please check the input text.');
397
- return [];
398
- }
399
- if (texts.length > 100) {
400
- log.warn(`Warning: More than 100 text chunks created (${texts.length}). This may affect performance.`);
401
- }
402
- if (chunkSize < 1000) {
403
- log.warn(`Warning: Chunk size is less than 1000 characters (${chunkSize}). This may lead to very small chunks.`);
404
- }
405
- log.info(`Text split into ${texts.length} chunks with size ${chunkSize} and overlap ${chunkOverlap}`);
406
- return texts;
407
- }
408
- printValues(obj) {
409
- log.info(`Processing Rule: ${obj.name} for ${obj.context}`);
410
- // For each obj.expression:arg:where
411
- if (obj.expression && obj.expression.arg && obj.expression.arg.where) {
412
- // for each args in obj.expression.arg.where, extract the nested key called "value"
413
- const statements = this.extractObjectsByKey(obj.expression.arg.where, 'value');
414
- return statements.join(' ').replace(/,/g, ' ');
415
- }
416
- // return statements array as a string seperated by space
417
- return '';
418
- }
419
- extractObjectsByKey(obj, key) {
420
- const results = [];
421
- function traverse(obj) {
422
- for (const currentKey in obj) {
423
- if (obj.hasOwnProperty(currentKey)) {
424
- if (currentKey === key) {
425
- results.push(obj[currentKey]);
426
- }
427
- if (typeof obj[currentKey] === 'object' && obj[currentKey] !== null) {
428
- traverse(obj[currentKey]);
429
- }
430
- }
431
- }
432
- }
433
- traverse(obj);
434
- return results;
435
- }
436
- _printValues(obj) {
437
- for (var key in obj) {
438
- if (typeof obj[key] === 'object') {
439
- this._printValues(obj[key]);
440
- } else {
441
- this.stringExpression += obj[key] + ' ';
442
- }
443
- }
444
- const _eliminate = ['true', 'false', 'null', 'undefined', 'String', 'Number', 'Object', 'Array', 'Boolean', 'value', 'Type', 'Specifier', 'Named'];
445
- _eliminate.forEach(element => {
446
- const regex = new RegExp(element, 'g');
447
- this.stringExpression = this.stringExpression.replace(regex, '');
448
- });
449
- this.stringExpression = this.stringExpression.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');
450
- return this.camelToString(this.stringExpression);
451
- }
452
- async chain(input) {
453
- // Create a chain using RunnableSequence, RunnablePassthrough and RunnableParallel
454
- // input has two properties: expression and context
455
- // expression goes through mapQuery and parallely
456
- // context goes through textSplitter and mapDoc
457
- // Finally reduceChain is called with the results of mapQuery and mapDoc
458
- // LCEL-based implementation using RunnableSequence, RunnableParallel, and RunnablePassthrough
459
- const {
460
- RunnableSequence,
461
- RunnableParallel,
462
- RunnablePassthrough
463
- } = await import('@langchain/core/runnables');
464
- const mapQuery = new BaseChain(this.container);
465
- const mapDoc = new BaseChain(this.container);
466
- const reduceChain = new BaseChain(this.container);
467
- mapQuery.name = 'MapQuery';
468
- mapQuery.description = 'Map the expression to a natural language query.';
469
- mapQuery.template = this.resolve('litl_mapquery_template', this._mapQueryTemplate);
470
- mapDoc.name = 'MapDoc';
471
- mapDoc.description = 'Map the document to a set of facts.';
472
- mapDoc.template = this.resolve('litl_mapdoc_template', this._mapDocTemplate);
473
- reduceChain.name = 'ReduceChain';
474
- reduceChain.description = 'Reduce a set of documents to binary answer.';
475
- reduceChain.template = this.resolve('litl_reduce_chain_template', this._reduceChainTemplate);
476
- const mapQueryChain = RunnablePassthrough.assign({
477
- query: async input => mapQuery.chain({
478
- expression: input.input.expression
479
- })
480
- });
481
- const mapDocChain = RunnablePassthrough.assign({
482
- documents: async input => {
483
- const textChunks = await this.textSplitter(input.input.context);
484
- return Promise.all(textChunks.map(chunk => mapDoc.chain({
485
- document: chunk,
486
- statements: input.input.expression
487
- })));
488
- }
489
- });
490
- const parallelChain = RunnableParallel.from([mapQueryChain, mapDocChain]);
491
- const sequence = RunnableSequence.from([parallelChain, async results => {
492
- /* results is an array: [{ query: ... }, { documents: [...] }]
493
- {
494
- "0": {
495
- "input": {
496
- "expression": "0 Visual foot exam showed active infection?",
497
- "context": "Procedure: The patient underwent a visual foot examination on 26 days ago. to assess skin integrity, circulation, and structural abnormalities. Findings: - Skin Condition: No signs of dryness, cracking, or ulceration were observed. No abnormal lesions or discoloration present. - Nail Health: Toenails appeared intact with no evidence of fungal infection or ingrown nails. - Circulation: Normal coloration, with no signs of cyanosis or pallor. Capillary refill time within normal limits. - Edema: No visible swelling in the feet or ankles. - Deformities: No structural abnormalities such as bunions, hammertoes, or Charcot foot deformities noted. - Infections: Showed signs of active infection with erythema and swelling. - Sensation: No visible signs of neuropathy, though further testing may be required to assess sensory deficits. "
498
- },
499
- "query": "The text indicates a question about whether a visual foot exam revealed an active infection.\n"
500
- },
501
- "1": {
502
- "input": {
503
- "expression": "0 Visual foot exam showed active infection?",
504
- "context": "Procedure: The patient underwent a visual foot examination on 26 days ago. to assess skin integrity, circulation, and structural abnormalities. Findings: - Skin Condition: No signs of dryness, cracking, or ulceration were observed. No abnormal lesions or discoloration present. - Nail Health: Toenails appeared intact with no evidence of fungal infection or ingrown nails. - Circulation: Normal coloration, with no signs of cyanosis or pallor. Capillary refill time within normal limits. - Edema: No visible swelling in the feet or ankles. - Deformities: No structural abnormalities such as bunions, hammertoes, or Charcot foot deformities noted. - Infections: Showed signs of active infection with erythema and swelling. - Sensation: No visible signs of neuropathy, though further testing may be required to assess sensory deficits. "
505
- },
506
- "documents": [
507
- "26 days ago, a visual foot examination revealed no dryness, cracking, ulceration, lesions, discoloration, fungal infection, ingrown nails, cyanosis, pallor, edema, bunions, hammertoes, or Charcot foot deformities. Circulation and capillary refill were normal. However, the exam showed signs"
508
- ]
509
- }
510
- }
511
- */
512
- log.info(`Results from parallel chain: ${JSON.stringify(results)}`);
513
- const expression = results[0].input.expression;
514
- const query = results[0].query;
515
- const documents = results[1].documents;
516
- return reduceChain.chain({
517
- facts: documents,
518
- query: query + ' ' + expression
519
- });
520
- }, result => this.stringToBoolean(result)]);
521
- return sequence.invoke(input);
522
- }
523
- }
524
-
525
- /**
526
- * Copyright 2024 Bell Eapen
527
- *
528
- * Licensed under the Apache License, Version 2.0 (the "License");
529
- * you may not use this file except in compliance with the License.
530
- * You may obtain a copy of the License at
531
- *
532
- * https://www.apache.org/licenses/LICENSE-2.0
533
- *
534
- * Unless required by applicable law or agreed to in writing, software
535
- * distributed under the License is distributed on an "AS IS" BASIS,
536
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
537
- * See the License for the specific language governing permissions and
538
- * limitations under the License.
539
- */
540
- /**
541
- * BaseLLM is a base class for LLMs that communicate with a remote API.
542
- * It extends the LangChain LLM class and provides configurable parameters
543
- * for model inference.
544
- */
545
- class BaseLLM extends llms.LLM {
546
- constructor(fields) {
547
- var _fields$apiKey, _fields$timeout, _fields$backend, _fields$temperature, _fields$topP, _fields$topK, _fields$nBatch, _fields$nThreads, _fields$nPredict, _fields$maxOutputToke, _fields$repeatLastN, _fields$repeatPenalty;
548
- super(fields);
549
- this.baseUrl = void 0;
550
- this.model = void 0;
551
- this.apiKey = void 0;
552
- this.timeout = void 0;
553
- this.backend = void 0;
554
- this.temperature = void 0;
555
- this.topP = void 0;
556
- this.topK = void 0;
557
- this.nBatch = void 0;
558
- this.nThreads = void 0;
559
- this.nPredict = void 0;
560
- this.maxOutputTokens = void 0;
561
- this.repeatLastN = void 0;
562
- this.repeatPenalty = void 0;
563
- this.baseUrl = fields.baseUrl;
564
- this.model = fields.model;
565
- this.apiKey = (_fields$apiKey = fields.apiKey) != null ? _fields$apiKey : '';
566
- this.timeout = (_fields$timeout = fields.timeout) != null ? _fields$timeout : 60000;
567
- this.backend = (_fields$backend = fields.backend) != null ? _fields$backend : 'dhti';
568
- this.temperature = (_fields$temperature = fields.temperature) != null ? _fields$temperature : 0.1;
569
- this.topP = (_fields$topP = fields.topP) != null ? _fields$topP : 0.8;
570
- this.topK = (_fields$topK = fields.topK) != null ? _fields$topK : 40;
571
- this.nBatch = (_fields$nBatch = fields.nBatch) != null ? _fields$nBatch : 8;
572
- this.nThreads = (_fields$nThreads = fields.nThreads) != null ? _fields$nThreads : 4;
573
- this.nPredict = (_fields$nPredict = fields.nPredict) != null ? _fields$nPredict : 256;
574
- this.maxOutputTokens = (_fields$maxOutputToke = fields.maxOutputTokens) != null ? _fields$maxOutputToke : 512;
575
- this.repeatLastN = (_fields$repeatLastN = fields.repeatLastN) != null ? _fields$repeatLastN : 64;
576
- this.repeatPenalty = (_fields$repeatPenalty = fields.repeatPenalty) != null ? _fields$repeatPenalty : 1.18;
577
- }
578
- /**
579
- * Get the model default parameters
580
- */
581
- get modelDefaultParameters() {
582
- return {
583
- maxOutputTokens: this.maxOutputTokens,
584
- nPredict: this.nPredict,
585
- topK: this.topK,
586
- topP: this.topP,
587
- temperature: this.temperature,
588
- nBatch: this.nBatch,
589
- repeatPenalty: this.repeatPenalty,
590
- repeatLastN: this.repeatLastN
591
- };
592
- }
593
- /**
594
- * Get the identifying parameters
595
- */
596
- get identifyingParams() {
597
- return {
598
- model: this.model,
599
- baseUrl: this.baseUrl,
600
- modelParameters: this.modelDefaultParameters
601
- };
602
- }
603
- /**
604
- * Get the LLM type identifier
605
- */
606
- _llmType() {
607
- return 'dhti';
608
- }
609
- /**
610
- * Prepare the request payload for the API
611
- */
612
- preparePayload(prompt) {
613
- return {
614
- model: this.model,
615
- options: this.modelDefaultParameters,
616
- messages: [{
617
- role: 'user',
618
- content: prompt
619
- }]
620
- };
621
- }
622
- /**
623
- * Make the API call to generate text
624
- */
625
- async _call(prompt, _options, _runManager) {
626
- const payload = this.preparePayload(prompt);
627
- const headers = {
628
- 'Content-Type': 'application/json'
629
- };
630
- if (this.apiKey) {
631
- headers['Authorization'] = `Bearer ${this.apiKey}`;
632
- }
633
- const controller = new AbortController();
634
- const timeoutId = setTimeout(() => controller.abort(), this.timeout);
635
- try {
636
- const response = await fetch(this.baseUrl, {
637
- method: 'POST',
638
- headers,
639
- body: JSON.stringify(payload),
640
- signal: controller.signal
641
- });
642
- clearTimeout(timeoutId);
643
- if (!response.ok) {
644
- const body = await response.text();
645
- throw new Error(`API request failed: status=${response.status}; body=${body}`);
646
- }
647
- const data = await response.json();
648
- // Expecting structure like: { "choices": [ { "message": { "role":"assistant","content":"..." } } ] }
649
- // Adapt this path if the API differs
650
- if (data.choices && Array.isArray(data.choices) && data.choices.length > 0) {
651
- const choice = data.choices[0];
652
- // support both "message" and direct "text"
653
- if (typeof choice === 'object' && choice !== null && 'message' in choice && typeof choice.message === 'object' && choice.message !== null) {
654
- const content = choice.message.content;
655
- if (typeof content === 'string') {
656
- return content;
657
- }
658
- } else if ('text' in choice) {
659
- const text = choice.text;
660
- if (typeof text === 'string') {
661
- return text;
662
- }
663
- }
664
- }
665
- // Fallback: return raw JSON string for debugging
666
- return JSON.stringify(data);
667
- } catch (error) {
668
- clearTimeout(timeoutId);
669
- if (error instanceof Error && error.name === 'AbortError') {
670
- throw new Error(`API request timed out after ${this.timeout}ms`);
671
- }
672
- throw error;
673
- }
674
- }
675
- }
676
-
677
- /**
678
- * Copyright 2024 Bell Eapen
679
- *
680
- * Licensed under the Apache License, Version 2.0 (the "License");
681
- * you may not use this file except in compliance with the License.
682
- * You may obtain a copy of the License at
683
- *
684
- * https://www.apache.org/licenses/LICENSE-2.0
685
- *
686
- * Unless required by applicable law or agreed to in writing, software
687
- * distributed under the License is distributed on an "AS IS" BASIS,
688
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
689
- * See the License for the specific language governing permissions and
690
- * limitations under the License.
691
- */
692
- /**
693
- * BaseEmbedding is a base class for embeddings that communicate with a remote API.
694
- * It extends the LangChain Embeddings class and provides methods for embedding
695
- * documents and queries.
696
- */
697
- class BaseEmbedding extends embeddings.Embeddings {
698
- constructor(fields) {
699
- var _fields$apiKey;
700
- super(fields);
701
- this.baseUrl = void 0;
702
- this.model = void 0;
703
- this.apiKey = void 0;
704
- this.baseUrl = fields.baseUrl;
705
- this.model = fields.model;
706
- this.apiKey = (_fields$apiKey = fields.apiKey) != null ? _fields$apiKey : '';
707
- }
708
- /**
709
- * Embed a list of documents
710
- * @param texts - Array of strings to embed
711
- * @returns Promise of array of embedding vectors
712
- */
713
- async embedDocuments(texts) {
714
- return this.getEmbeddings(texts);
715
- }
716
- /**
717
- * Embed a single query
718
- * @param text - String to embed
719
- * @returns Promise of embedding vector
720
- */
721
- async embedQuery(text) {
722
- const embeddings = await this.getEmbeddings([text]);
723
- return embeddings[0];
724
- }
725
- /**
726
- * Helper method to get embeddings for a list of texts
727
- * @param texts - Array of strings to embed
728
- * @returns Promise of array of embedding vectors
729
- */
730
- async getEmbeddings(texts) {
731
- const headers = {
732
- 'Content-Type': 'application/json'
733
- };
734
- if (this.apiKey) {
735
- headers['Authorization'] = `Bearer ${this.apiKey}`;
736
- }
737
- const payload = {
738
- model: this.model,
739
- input: texts
740
- };
741
- const response = await fetch(this.baseUrl, {
742
- method: 'POST',
743
- headers,
744
- body: JSON.stringify(payload)
745
- });
746
- if (!response.ok) {
747
- const body = await response.text();
748
- throw new Error(`API request failed: status=${response.status}; body=${body}`);
749
- }
750
- const respJson = await response.json();
751
- if (!('embeddings' in respJson)) {
752
- throw new Error(`API response missing 'embeddings' key. Received keys: ${Object.keys(respJson).join(', ')}`);
753
- }
754
- return respJson.embeddings;
755
- }
756
- }
757
-
758
- exports.BaseAgent = BaseAgent;
759
- exports.BaseChain = BaseChain;
760
- exports.BaseEmbedding = BaseEmbedding;
761
- exports.BaseLLM = BaseLLM;
762
- exports.BaseTool = BaseTool;
763
- exports.LLMLoop = LLMLoop;
764
- //# sourceMappingURL=medpromptjs.cjs.development.js.map