@cellaware/utils 8.0.3 → 8.1.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.
Files changed (53) hide show
  1. package/dist/azure/cosmos.d.ts +98 -0
  2. package/dist/azure/cosmos.js +276 -0
  3. package/dist/azure/email.d.ts +3 -0
  4. package/dist/azure/email.js +20 -0
  5. package/dist/azure/function.d.ts +14 -0
  6. package/dist/azure/function.js +124 -0
  7. package/dist/azure/slot.d.ts +1 -0
  8. package/dist/azure/slot.js +4 -0
  9. package/dist/azure/storage.d.ts +4 -0
  10. package/dist/azure/storage.js +67 -0
  11. package/dist/chatwms/alert.d.ts +86 -0
  12. package/dist/chatwms/alert.js +62 -0
  13. package/dist/chatwms/azure/cosmos.d.ts +21 -0
  14. package/dist/chatwms/azure/cosmos.js +34 -0
  15. package/dist/chatwms/azure/function.d.ts +21 -0
  16. package/dist/chatwms/azure/function.js +29 -0
  17. package/dist/chatwms/azure/storage.d.ts +5 -0
  18. package/dist/chatwms/azure/storage.js +17 -0
  19. package/dist/chatwms/client.d.ts +18 -0
  20. package/dist/chatwms/client.js +48 -0
  21. package/dist/chatwms/dashboard.d.ts +82 -0
  22. package/dist/chatwms/dashboard.js +17 -0
  23. package/dist/chatwms/datagrid.d.ts +120 -0
  24. package/dist/chatwms/datagrid.js +781 -0
  25. package/dist/chatwms/developer.d.ts +27 -0
  26. package/dist/chatwms/developer.js +12 -0
  27. package/dist/chatwms/github/issue.d.ts +1 -0
  28. package/dist/chatwms/github/issue.js +4 -0
  29. package/dist/chatwms/instance.d.ts +16 -0
  30. package/dist/chatwms/instance.js +18 -0
  31. package/dist/chatwms/report.d.ts +69 -0
  32. package/dist/chatwms/report.js +24 -0
  33. package/dist/chatwms/response.d.ts +12 -0
  34. package/dist/chatwms/response.js +13 -0
  35. package/dist/chatwms/search.d.ts +12 -0
  36. package/dist/chatwms/search.js +9 -0
  37. package/dist/chatwms/user.d.ts +27 -0
  38. package/dist/chatwms/user.js +40 -0
  39. package/dist/github/issue.d.ts +1 -0
  40. package/dist/github/issue.js +23 -0
  41. package/dist/llm/chain-store.d.ts +49 -0
  42. package/dist/llm/chain-store.js +284 -0
  43. package/dist/llm/cost.d.ts +3 -0
  44. package/dist/llm/cost.js +78 -0
  45. package/dist/llm/model.d.ts +12 -0
  46. package/dist/llm/model.js +1 -0
  47. package/dist/stopwatch.d.ts +8 -0
  48. package/dist/stopwatch.js +36 -0
  49. package/dist/util.d.ts +36 -0
  50. package/dist/util.js +257 -0
  51. package/dist/version.d.ts +4 -0
  52. package/dist/version.js +12 -0
  53. package/package.json +1 -1
@@ -0,0 +1,27 @@
1
+ import { KeyValuePair } from "../util.js";
2
+ import { DataContext } from "./client.js";
3
+ export interface SqlCacheContext {
4
+ contextName: DataContext;
5
+ variables: KeyValuePair[];
6
+ tables: {
7
+ table_name: string;
8
+ table_description: string;
9
+ }[];
10
+ columns: {
11
+ table_name: string;
12
+ column_id: number;
13
+ column_name: string;
14
+ data_type: string;
15
+ column_description: string;
16
+ column_comment: string;
17
+ }[];
18
+ functions: {
19
+ name: string;
20
+ type: string;
21
+ args: string[];
22
+ description: string;
23
+ }[];
24
+ tableMappings: KeyValuePair[];
25
+ columnMappings: KeyValuePair[];
26
+ }
27
+ export declare function initSqlCacheContext(): SqlCacheContext;
@@ -0,0 +1,12 @@
1
+ import { CHATWMS_CONTEXT_NAME } from "./client.js";
2
+ export function initSqlCacheContext() {
3
+ return {
4
+ contextName: CHATWMS_CONTEXT_NAME,
5
+ variables: [],
6
+ tables: [],
7
+ columns: [],
8
+ functions: [],
9
+ tableMappings: [],
10
+ columnMappings: []
11
+ };
12
+ }
@@ -0,0 +1 @@
1
+ export declare function chatwmsGithubCreateIssue(title: string, body: any, labels: string[]): Promise<boolean>;
@@ -0,0 +1,4 @@
1
+ import { githubCreateIssue } from "../../github/issue.js";
2
+ export async function chatwmsGithubCreateIssue(title, body, labels) {
3
+ return githubCreateIssue(`https://api.github.com/repos/cellaware/chatwms-feedback/issues`, title, body, labels);
4
+ }
@@ -0,0 +1,16 @@
1
+ export interface ChatWMSInstance {
2
+ id: string;
3
+ clientId: string;
4
+ customer: string;
5
+ description: string;
6
+ url: string;
7
+ key: string;
8
+ }
9
+ export declare function initChatWMSInstance(): ChatWMSInstance;
10
+ export interface ChatWMSInstanceWarehouse {
11
+ instanceId: string;
12
+ clientId: string;
13
+ customer: string;
14
+ warehouse: string;
15
+ }
16
+ export declare function initChatWMSInstanceWarehouse(): ChatWMSInstanceWarehouse;
@@ -0,0 +1,18 @@
1
+ export function initChatWMSInstance() {
2
+ return {
3
+ id: '',
4
+ clientId: '',
5
+ customer: '',
6
+ description: '',
7
+ url: '',
8
+ key: ''
9
+ };
10
+ }
11
+ export function initChatWMSInstanceWarehouse() {
12
+ return {
13
+ instanceId: '',
14
+ clientId: '',
15
+ customer: '',
16
+ warehouse: '',
17
+ };
18
+ }
@@ -0,0 +1,69 @@
1
+ import { DataContext } from "./client.js";
2
+ import { DatagridStateBase } from "./datagrid.js";
3
+ export declare const REPORT_VISIBILITY_PRIVATE = "private";
4
+ export declare const REPORT_VISIBILITY_PUBLIC = "public";
5
+ export declare const REPORT_VISIBILITY_GLOBAL = "global";
6
+ export interface ReportParameter {
7
+ sequence: number;
8
+ name: string;
9
+ required: boolean;
10
+ exampleValue: string;
11
+ }
12
+ export interface Report {
13
+ reportId: string;
14
+ reportTitle: string;
15
+ reportRoute: string;
16
+ visibility: string;
17
+ readonly: boolean;
18
+ parameters: ReportParameter[];
19
+ folder: string;
20
+ brief: string;
21
+ clientId: string;
22
+ userId: string;
23
+ customerLevel?: boolean;
24
+ }
25
+ export declare function initReport(): Report;
26
+ export interface LineReportParameter {
27
+ sequence: number;
28
+ parameterName: string;
29
+ columnName: string;
30
+ }
31
+ export interface ReportLineContentInfo extends DatagridStateBase {
32
+ visualizationOption: string;
33
+ reportParameters: LineReportParameter[];
34
+ contextName: DataContext;
35
+ specification: string;
36
+ query: string;
37
+ summary: string;
38
+ reviewOk?: boolean;
39
+ reviewConfidence?: string;
40
+ reviewFeedback?: string;
41
+ tableGroups?: string[];
42
+ developer?: boolean;
43
+ archive?: boolean;
44
+ referenceLineId?: string;
45
+ }
46
+ export interface ReportLine {
47
+ lineId: string;
48
+ lineTitle: string;
49
+ reportId: string;
50
+ contentInfo: ReportLineContentInfo;
51
+ sequence: number;
52
+ clientId: string;
53
+ userId: string;
54
+ }
55
+ export interface ReportLineResult {
56
+ rows: any[];
57
+ rowsStr: string;
58
+ chartRows: any[];
59
+ }
60
+ export declare function initReportLineResult(): ReportLineResult;
61
+ export interface ReportUsage {
62
+ reportId: string;
63
+ reportTitle: string;
64
+ visibility: string;
65
+ clientId: string;
66
+ userId: string;
67
+ datetime: Date;
68
+ customerLevel?: boolean;
69
+ }
@@ -0,0 +1,24 @@
1
+ export const REPORT_VISIBILITY_PRIVATE = 'private';
2
+ export const REPORT_VISIBILITY_PUBLIC = 'public';
3
+ export const REPORT_VISIBILITY_GLOBAL = 'global';
4
+ export function initReport() {
5
+ return {
6
+ reportId: '',
7
+ reportTitle: '',
8
+ reportRoute: '',
9
+ visibility: REPORT_VISIBILITY_PRIVATE,
10
+ readonly: false,
11
+ parameters: [],
12
+ folder: '',
13
+ brief: '',
14
+ clientId: '',
15
+ userId: ''
16
+ };
17
+ }
18
+ export function initReportLineResult() {
19
+ return {
20
+ rows: [],
21
+ rowsStr: '',
22
+ chartRows: []
23
+ };
24
+ }
@@ -0,0 +1,12 @@
1
+ export interface ChatWMSResponse {
2
+ ok: boolean;
3
+ error: string;
4
+ data: any;
5
+ }
6
+ /**
7
+ * Initialization:
8
+ * - `ok`: `true`
9
+ * - `error`: `''`
10
+ * - `data`: `{}`
11
+ */
12
+ export declare function initChatWMSResponse(data?: any): ChatWMSResponse;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Initialization:
3
+ * - `ok`: `true`
4
+ * - `error`: `''`
5
+ * - `data`: `{}`
6
+ */
7
+ export function initChatWMSResponse(data) {
8
+ return {
9
+ ok: true,
10
+ error: '',
11
+ data: data ?? {}
12
+ };
13
+ }
@@ -0,0 +1,12 @@
1
+ export declare enum SearchRecordType {
2
+ DASHBOARD = "dashboard",
3
+ DASHBOARD_WIDGET = "dashboard_widget",
4
+ DASHBOARD_FOLDER = "dashboard_folder",
5
+ REPORT = "report",
6
+ REPORT_LINE = "report_line",
7
+ REPORT_FOLDER = "report_folder"
8
+ }
9
+ export interface SearchRecord {
10
+ type: SearchRecordType;
11
+ data: any;
12
+ }
@@ -0,0 +1,9 @@
1
+ export var SearchRecordType;
2
+ (function (SearchRecordType) {
3
+ SearchRecordType["DASHBOARD"] = "dashboard";
4
+ SearchRecordType["DASHBOARD_WIDGET"] = "dashboard_widget";
5
+ SearchRecordType["DASHBOARD_FOLDER"] = "dashboard_folder";
6
+ SearchRecordType["REPORT"] = "report";
7
+ SearchRecordType["REPORT_LINE"] = "report_line";
8
+ SearchRecordType["REPORT_FOLDER"] = "report_folder";
9
+ })(SearchRecordType || (SearchRecordType = {}));
@@ -0,0 +1,27 @@
1
+ import { HttpRequest } from "@azure/functions";
2
+ export interface ChatWMSUserData {
3
+ userId: string;
4
+ userDetails: string;
5
+ identityProvider: string;
6
+ }
7
+ export declare function chatwmsInitUserData(): ChatWMSUserData;
8
+ /**
9
+ * NOTE: will throw error if empty principal or decode issue
10
+ */
11
+ export declare function chatwmsExtractUserDataFromHeaders(request: HttpRequest): ChatWMSUserData;
12
+ export declare const CHATWMS_DEFAULT_LANGUAGE: string;
13
+ export type ChatWMSTheme = 'light' | 'dark';
14
+ export declare const CHATWMS_THEME_LIGHT: ChatWMSTheme;
15
+ export declare const CHATWMS_THEME_DARK: ChatWMSTheme;
16
+ export type ChatWMSDateFormat = 'MMDDYYYY' | 'DDMMYYYY' | 'YYYYMMDD';
17
+ export declare const CHATWMS_DEFAULT_DATE_FORMAT: ChatWMSDateFormat;
18
+ export interface ChatWMSUserSettings {
19
+ userId: string;
20
+ userDetails: string;
21
+ language: string;
22
+ theme: ChatWMSTheme;
23
+ developer?: boolean;
24
+ dateFormat?: ChatWMSDateFormat;
25
+ clientId: string;
26
+ }
27
+ export declare function chatwmsInitUserSettings(): ChatWMSUserSettings;
@@ -0,0 +1,40 @@
1
+ import { AZURE_HEADER_PRINCIPAL } from "../azure/function.js";
2
+ export function chatwmsInitUserData() {
3
+ return {
4
+ userId: '',
5
+ userDetails: '',
6
+ identityProvider: ''
7
+ };
8
+ }
9
+ /**
10
+ * NOTE: will throw error if empty principal or decode issue
11
+ */
12
+ export function chatwmsExtractUserDataFromHeaders(request) {
13
+ // https://learn.microsoft.com/en-us/azure/static-web-apps/user-information
14
+ const principal = request.headers.get(AZURE_HEADER_PRINCIPAL) ?? '';
15
+ if (principal !== '') {
16
+ try {
17
+ const userData = JSON.parse(Buffer.from(principal, 'base64').toString('utf8'));
18
+ return userData;
19
+ }
20
+ catch (err) {
21
+ throw new Error(`Authorization error`);
22
+ }
23
+ }
24
+ else {
25
+ throw new Error(`Authorization header not found`);
26
+ }
27
+ }
28
+ export const CHATWMS_DEFAULT_LANGUAGE = 'English';
29
+ export const CHATWMS_THEME_LIGHT = 'light';
30
+ export const CHATWMS_THEME_DARK = 'dark';
31
+ export const CHATWMS_DEFAULT_DATE_FORMAT = 'MMDDYYYY';
32
+ export function chatwmsInitUserSettings() {
33
+ return {
34
+ userId: '',
35
+ userDetails: '',
36
+ language: CHATWMS_DEFAULT_LANGUAGE,
37
+ theme: CHATWMS_THEME_LIGHT,
38
+ clientId: ''
39
+ };
40
+ }
@@ -0,0 +1 @@
1
+ export declare function githubCreateIssue(issueUrl: string, issueTitle: string, issueBody: any, issueLabels: string[]): Promise<boolean>;
@@ -0,0 +1,23 @@
1
+ export async function githubCreateIssue(issueUrl, issueTitle, issueBody, issueLabels) {
2
+ const token = process.env.GITHUB_TOKEN ?? '';
3
+ if (token.length === 0) {
4
+ throw new Error('GITHUB: `GITHUB_TOKEN` environment variable is not set');
5
+ }
6
+ const headers = {
7
+ 'Authorization': `token ${token}`,
8
+ 'X-GitHub-Api-Version': '2022-11-28',
9
+ 'Accept': 'application/vnd.github+json'
10
+ };
11
+ const body = {
12
+ title: issueTitle,
13
+ body: issueBody,
14
+ labels: issueLabels
15
+ };
16
+ const req = {
17
+ method: 'POST',
18
+ headers,
19
+ body: JSON.stringify(body)
20
+ };
21
+ let res = await fetch(issueUrl, req);
22
+ return res.ok;
23
+ }
@@ -0,0 +1,49 @@
1
+ import { BaseChain } from 'langchain/chains';
2
+ import { ModelName, ModelOptions } from './model.js';
3
+ /**
4
+ * `SingleActionChain` only supports single interaction with LLM, and only 1
5
+ * output key is allowed. Output key is defaulted to `answer`.
6
+ */
7
+ export declare class SingleActionChain extends BaseChain {
8
+ private _name;
9
+ private llm;
10
+ private _inputKeys;
11
+ private _outputKey;
12
+ private prompt;
13
+ private promptTemplate;
14
+ constructor(fields: any, name: string, inputKeys: string[]);
15
+ _call(values: any, runManager: any): Promise<any>;
16
+ _chainType(): string;
17
+ get inputKeys(): string[];
18
+ get outputKeys(): string[];
19
+ getModelName(): ModelName;
20
+ getPrompt(): string;
21
+ }
22
+ export declare function createSingleActionChain(name: string, modelName: ModelName, inputKeys: string[], prompt: string, temperature?: number, verbose?: boolean): SingleActionChain;
23
+ /**
24
+ * A `ChainStore` is a chain registry and calling interface. The `ChainStore`
25
+ * keeps a map of all registered chains.
26
+ *
27
+ * The following chains are built-in:
28
+ * - `translation_chain`: `translate()`
29
+ *
30
+ */
31
+ export declare class ChainStore {
32
+ private promptsPath;
33
+ private chains;
34
+ constructor(promptsPath: string, defaultModelName?: ModelName);
35
+ private initBuiltinChains;
36
+ private static getTokenUsage;
37
+ private getPrompt;
38
+ /**
39
+ * - `name`: Chain name
40
+ * - `template`: Template chain name to copy prompt from
41
+ * - `options`: LLM options
42
+ * - `verbose`: OpenAI verbose parameter
43
+ */
44
+ addChain(name: string, template: string | null, options: ModelOptions, verbose?: boolean): void;
45
+ addExistingChain(chain: SingleActionChain): void;
46
+ callChain(name: string, args: any, tokenUsages: any[]): Promise<import("@langchain/core/utils/types.js").ChainValues>;
47
+ translate(statement: string, language: string, tokenUsages?: any[]): Promise<string>;
48
+ getPrompts(): string;
49
+ }
@@ -0,0 +1,284 @@
1
+ import fs from 'fs';
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";
6
+ import { getLLMTransactionCost } from './cost.js';
7
+ const CHAIN_TIMEOUT_MS = 150_000;
8
+ const TRANSLATION_CHAIN_NAME = 'translation_chain';
9
+ /**
10
+ * `SingleActionChain` only supports single interaction with LLM, and only 1
11
+ * output key is allowed. Output key is defaulted to `answer`.
12
+ */
13
+ export class SingleActionChain extends BaseChain {
14
+ constructor(fields, name, inputKeys) {
15
+ super(fields);
16
+ Object.defineProperty(this, "_name", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ Object.defineProperty(this, "llm", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ Object.defineProperty(this, "_inputKeys", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ Object.defineProperty(this, "_outputKey", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: void 0
39
+ });
40
+ Object.defineProperty(this, "prompt", {
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true,
44
+ value: void 0
45
+ });
46
+ Object.defineProperty(this, "promptTemplate", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: void 0
51
+ });
52
+ if (inputKeys.length == 0) {
53
+ throw new Error(`SINGLE_ACTION_CHAIN: Chain '${name}' needs at least one input key!`);
54
+ }
55
+ this._name = name;
56
+ this.llm = fields.llm;
57
+ this._inputKeys = inputKeys;
58
+ this._outputKey = 'answer';
59
+ this.prompt = fields.prompt;
60
+ this.promptTemplate = ChatPromptTemplate.fromTemplate(this.prompt);
61
+ }
62
+ async _call(values, runManager) {
63
+ let output = {
64
+ [this._outputKey]: ''
65
+ };
66
+ const llmChain = this.promptTemplate
67
+ .pipe(this.llm)
68
+ .pipe(new StringOutputParser());
69
+ let llmInputs = {};
70
+ for (const inputKey of this._inputKeys) {
71
+ llmInputs[inputKey] = values[inputKey];
72
+ }
73
+ let callbacks = undefined;
74
+ if (!!runManager && !!runManager.getChild()) {
75
+ callbacks = runManager.getChild().handlers;
76
+ }
77
+ const llmAnswer = await llmChain.invoke(llmInputs, {
78
+ callbacks
79
+ });
80
+ output[this._outputKey] = llmAnswer;
81
+ return output;
82
+ }
83
+ _chainType() {
84
+ return this._name;
85
+ }
86
+ get inputKeys() {
87
+ return this._inputKeys;
88
+ }
89
+ get outputKeys() {
90
+ return [this._outputKey];
91
+ }
92
+ getModelName() {
93
+ return this.llm.modelName;
94
+ }
95
+ getPrompt() {
96
+ return this.prompt;
97
+ }
98
+ }
99
+ export function createSingleActionChain(name, modelName, inputKeys, prompt, temperature, verbose) {
100
+ const llm = new ChatOpenAI({
101
+ modelName, temperature: temperature ?? 0, configuration: {
102
+ organization: process.env.OPENAI_ORGANIZATION,
103
+ timeout: CHAIN_TIMEOUT_MS
104
+ }
105
+ });
106
+ return new SingleActionChain({
107
+ llm,
108
+ prompt,
109
+ verbose: verbose ?? false
110
+ }, name, inputKeys);
111
+ }
112
+ /**
113
+ * A `ChainStore` is a chain registry and calling interface. The `ChainStore`
114
+ * keeps a map of all registered chains.
115
+ *
116
+ * The following chains are built-in:
117
+ * - `translation_chain`: `translate()`
118
+ *
119
+ */
120
+ export class ChainStore {
121
+ constructor(promptsPath, defaultModelName) {
122
+ Object.defineProperty(this, "promptsPath", {
123
+ enumerable: true,
124
+ configurable: true,
125
+ writable: true,
126
+ value: void 0
127
+ });
128
+ Object.defineProperty(this, "chains", {
129
+ enumerable: true,
130
+ configurable: true,
131
+ writable: true,
132
+ value: void 0
133
+ });
134
+ this.promptsPath = promptsPath;
135
+ this.chains = new Map();
136
+ this.initBuiltinChains(defaultModelName);
137
+ }
138
+ initBuiltinChains(defaultModelName) {
139
+ // Translation Chain:
140
+ {
141
+ this.addExistingChain(createSingleActionChain(TRANSLATION_CHAIN_NAME, defaultModelName ?? 'gpt-4o-mini', ['statement', 'language'], `You are a helpful AI translator who translates an English statement to {language}.
142
+
143
+ Given an English statement, translate the English statement into {language}.
144
+
145
+ Here is the English statement that you need to translate to {language}:
146
+ {statement}
147
+
148
+ Your translation here:
149
+ `));
150
+ }
151
+ }
152
+ static getTokenUsage(chainName, modelName, tokenUsage) {
153
+ return {
154
+ chain: chainName,
155
+ model: modelName,
156
+ tokenUsage,
157
+ cost: getLLMTransactionCost(tokenUsage, modelName)
158
+ };
159
+ }
160
+ getPrompt(chainName, templateChainName) {
161
+ let prompt = {
162
+ content: '',
163
+ variables: []
164
+ };
165
+ const path = !!templateChainName ? `${this.promptsPath}/${templateChainName}.md` : `${this.promptsPath}/${chainName}.md`;
166
+ if (!fs.existsSync(path)) {
167
+ throw new Error(`CHAIN_STORE: Prompt file '${path}' not found`);
168
+ }
169
+ const content = fs.readFileSync(path, 'utf8');
170
+ if (content.length === 0) {
171
+ throw new Error(`CHAIN_STORE: Prompt file '${path}' is empty`);
172
+ }
173
+ let matches = content.matchAll(/{.*?}/gm);
174
+ let distinctMatches = new Set();
175
+ for (const match of Array.from(matches)) {
176
+ let matchStr = match[0];
177
+ // Exclude escaped curly braces.
178
+ if (!matchStr.startsWith('{{')) {
179
+ distinctMatches.add(matchStr.substring(1, matchStr.length - 1));
180
+ }
181
+ }
182
+ prompt.content = content;
183
+ prompt.variables = Array.from(distinctMatches);
184
+ return prompt;
185
+ }
186
+ /**
187
+ * - `name`: Chain name
188
+ * - `template`: Template chain name to copy prompt from
189
+ * - `options`: LLM options
190
+ * - `verbose`: OpenAI verbose parameter
191
+ */
192
+ addChain(name, template, options, verbose) {
193
+ const prompt = this.getPrompt(name, template);
194
+ let llmArgs = {
195
+ modelName: options.modelName,
196
+ temperature: options.temperature ?? 0,
197
+ configuration: {
198
+ organization: process.env.OPENAI_ORGANIZATION,
199
+ timeout: CHAIN_TIMEOUT_MS
200
+ }
201
+ };
202
+ if (!!options.openai) {
203
+ if (!!options.openai.reasoning_effort) {
204
+ llmArgs.reasoning_effort = options.openai.reasoning_effort;
205
+ }
206
+ if (!!options.openai.verbosity) {
207
+ llmArgs.verbosity = options.openai.verbosity;
208
+ }
209
+ }
210
+ const llm = new ChatOpenAI(llmArgs);
211
+ const chain = new SingleActionChain({
212
+ llm,
213
+ prompt: prompt.content,
214
+ verbose: verbose ?? false
215
+ }, name, prompt.variables);
216
+ this.chains.set(name, chain);
217
+ }
218
+ addExistingChain(chain) {
219
+ this.chains.set(chain._chainType(), chain);
220
+ }
221
+ async callChain(name, args, tokenUsages) {
222
+ let chain = this.chains.get(name);
223
+ if (!chain) {
224
+ throw new Error(`CHAIN_STORE: Chain '${name}' does not exist`);
225
+ }
226
+ let presentInputKeySet = new Set();
227
+ for (const inputKey of Object.keys(args)) {
228
+ presentInputKeySet.add(inputKey);
229
+ }
230
+ let presentInputKeys = Array.from(presentInputKeySet);
231
+ // Make sure all input keys are present.
232
+ for (const inputKey of chain.inputKeys) {
233
+ if (!presentInputKeys.includes(inputKey)) {
234
+ throw new Error(`CHAIN_STORE: Input key '${inputKey}' not present for chain '${chain._chainType()}'`);
235
+ }
236
+ }
237
+ return chain.invoke(args, {
238
+ callbacks: [
239
+ {
240
+ handleLLMEnd: async (output) => {
241
+ const usage = output?.llmOutput?.tokenUsage;
242
+ tokenUsages.push(ChainStore.getTokenUsage(chain._chainType(), chain.getModelName(), usage));
243
+ },
244
+ }
245
+ ]
246
+ });
247
+ }
248
+ async translate(statement, language, tokenUsages) {
249
+ // No need to translate if requested language is in default language.
250
+ const DEFAULT_LANGUAGE = 'English';
251
+ if (language.toLowerCase() === DEFAULT_LANGUAGE.toLowerCase()) {
252
+ return statement;
253
+ }
254
+ const translationAnswer = await this.callChain(TRANSLATION_CHAIN_NAME, { statement, language }, tokenUsages ?? []);
255
+ let translationAnswerStr = translationAnswer.answer;
256
+ /*
257
+ Need to check if AI indicator exists in the answer. It is possible that the
258
+ translation chain will include this in an answer. Pretty sure this happens
259
+ because the translation chain can see conversation history, and messages from
260
+ AI will be prefixed as such.
261
+ */
262
+ const AI_PREFIX = 'AI: ';
263
+ if (translationAnswerStr.includes(AI_PREFIX)) {
264
+ console.log(`CHAIN_STORE: Removing AI indicator from translation answer`);
265
+ translationAnswerStr = translationAnswerStr.substring(translationAnswerStr.indexOf(AI_PREFIX) + AI_PREFIX.length);
266
+ }
267
+ // Remove 'Your translation here:' if it is there.
268
+ if (translationAnswerStr.includes('Your translation here:')) {
269
+ translationAnswerStr = translationAnswerStr.substring(translationAnswerStr.indexOf('Your translation here:') + 'Your translation here:'.length);
270
+ }
271
+ return translationAnswerStr.trim();
272
+ }
273
+ getPrompts() {
274
+ let buf = '';
275
+ for (const chain of Array.from(this.chains.values())) {
276
+ buf += `================================================= =================================================
277
+ ${chain._chainType()}
278
+ ================================================= =================================================
279
+ ${chain.getPrompt()}
280
+ `;
281
+ }
282
+ return buf;
283
+ }
284
+ }
@@ -0,0 +1,3 @@
1
+ import { ModelName } from "./model";
2
+ export declare function getLLMCostPerToken(modelName: ModelName): any;
3
+ export declare function getLLMTransactionCost(tokenUsage: any, modelName: ModelName): number;