@cellaware/utils 6.2.0 → 7.0.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.
@@ -5,7 +5,7 @@ import { ModelName, ModelOptions } from './model.js';
5
5
  * output key is allowed. Output key is defaulted to `answer`.
6
6
  */
7
7
  export declare class SingleActionChain extends BaseChain {
8
- private name;
8
+ private _name;
9
9
  private llm;
10
10
  private _inputKeys;
11
11
  private _outputKey;
@@ -43,7 +43,7 @@ export declare class ChainStore {
43
43
  */
44
44
  addChain(name: string, template: string | null, options: ModelOptions, verbose?: boolean): void;
45
45
  addExistingChain(chain: SingleActionChain): void;
46
- callChain(name: string, args: any, tokenUsages: any[]): Promise<import("langchain/dist/schema/index.js").ChainValues>;
46
+ callChain(name: string, args: any, tokenUsages: any[]): Promise<import("@langchain/core/utils/types.js").ChainValues>;
47
47
  translate(statement: string, language: string, tokenUsages?: any[]): Promise<string>;
48
48
  getPrompts(): string;
49
49
  }
@@ -1,7 +1,8 @@
1
1
  import fs from 'fs';
2
- import { ChatOpenAI } from 'langchain/chat_models/openai';
3
- import { PromptTemplate } from 'langchain/prompts';
4
- import { LLMChain, BaseChain } from 'langchain/chains';
2
+ import { BaseChain } from 'langchain/chains';
3
+ import { ChatPromptTemplate } from "@langchain/core/prompts";
4
+ import { ChatOpenAI } from "@langchain/openai";
5
+ import { StringOutputParser } from "@langchain/core/output_parsers";
5
6
  import { getLLMTransactionCost } from './cost.js';
6
7
  const CHAIN_TIMEOUT_MS = 150_000;
7
8
  const TRANSLATION_CHAIN_NAME = 'translation_chain';
@@ -12,7 +13,7 @@ const TRANSLATION_CHAIN_NAME = 'translation_chain';
12
13
  export class SingleActionChain extends BaseChain {
13
14
  constructor(fields, name, inputKeys) {
14
15
  super(fields);
15
- Object.defineProperty(this, "name", {
16
+ Object.defineProperty(this, "_name", {
16
17
  enumerable: true,
17
18
  configurable: true,
18
19
  writable: true,
@@ -51,36 +52,32 @@ export class SingleActionChain extends BaseChain {
51
52
  if (inputKeys.length == 0) {
52
53
  throw new Error(`SINGLE_ACTION_CHAIN: Chain '${name}' needs at least one input key!`);
53
54
  }
54
- this.name = name;
55
+ this._name = name;
55
56
  this.llm = fields.llm;
56
57
  this._inputKeys = inputKeys;
57
58
  this._outputKey = 'answer';
58
59
  this.prompt = fields.prompt;
59
- this.promptTemplate = new PromptTemplate({
60
- template: fields.prompt,
61
- inputVariables: this.inputKeys
62
- });
60
+ this.promptTemplate = ChatPromptTemplate.fromTemplate(this.prompt);
63
61
  }
64
62
  async _call(values, runManager) {
65
63
  let output = {
66
64
  [this._outputKey]: ''
67
65
  };
68
- const llmChain = new LLMChain({
69
- prompt: this.promptTemplate,
70
- llm: this.llm,
71
- outputKey: this._outputKey,
72
- memory: this.memory,
73
- });
66
+ const llmChain = this.promptTemplate
67
+ .pipe(this.llm)
68
+ .pipe(new StringOutputParser());
74
69
  let llmInputs = {};
75
70
  for (const inputKey of this._inputKeys) {
76
71
  llmInputs[inputKey] = values[inputKey];
77
72
  }
78
- const llmAnswer = await llmChain.predict(llmInputs, runManager?.getChild());
73
+ const llmAnswer = await llmChain.invoke(llmInputs, {
74
+ callbacks: runManager ? [runManager.getChild()] : undefined,
75
+ });
79
76
  output[this._outputKey] = llmAnswer;
80
77
  return output;
81
78
  }
82
79
  _chainType() {
83
- return this.name;
80
+ return this._name;
84
81
  }
85
82
  get inputKeys() {
86
83
  return this._inputKeys;
@@ -233,13 +230,17 @@ Your translation here:
233
230
  throw new Error(`CHAIN_STORE: Input key '${inputKey}' not present for chain '${chain._chainType()}'`);
234
231
  }
235
232
  }
236
- return chain.call(args, [
237
- {
238
- handleLLMEnd: async (cbOutput) => {
239
- tokenUsages.push(ChainStore.getTokenUsage(chain._chainType(), chain.getModelName(), cbOutput.llmOutput?.tokenUsage));
240
- },
241
- }
242
- ]);
233
+ return chain.invoke(args, {
234
+ callbacks: [
235
+ {
236
+ handleLLMEnd: async (output) => {
237
+ console.log(output);
238
+ const usage = output?.llmOutput?.tokenUsage;
239
+ tokenUsages.push(ChainStore.getTokenUsage(chain._chainType(), chain.getModelName(), usage));
240
+ },
241
+ }
242
+ ]
243
+ });
243
244
  }
244
245
  async translate(statement, language, tokenUsages) {
245
246
  // No need to translate if requested language is in default language.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cellaware/utils",
3
- "version": "6.2.0",
3
+ "version": "7.0.1",
4
4
  "description": "Cellaware Utilities for Node.js",
5
5
  "author": "Cellaware Technologies",
6
6
  "type": "module",
@@ -21,7 +21,7 @@
21
21
  "@azure/functions": "^4.5.1",
22
22
  "@azure/storage-blob": "^12.16.0",
23
23
  "dotenv": "^16.3.1",
24
- "langchain": "^0.0.170"
24
+ "langchain": "^0.2.19"
25
25
  },
26
26
  "devDependencies": {
27
27
  "typescript": "^5.3.2"