@promptbook/core 0.66.0-7 → 0.66.0-9

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 (50) hide show
  1. package/esm/index.es.js +247 -1736
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/src/_packages/anthropic-claude.index.d.ts +0 -2
  4. package/esm/typings/src/_packages/azure-openai.index.d.ts +4 -0
  5. package/esm/typings/src/_packages/cli.index.d.ts +8 -0
  6. package/esm/typings/src/_packages/core.index.d.ts +2 -0
  7. package/esm/typings/src/_packages/types.index.d.ts +22 -14
  8. package/esm/typings/src/_packages/utils.index.d.ts +7 -7
  9. package/esm/typings/src/execution/LlmExecutionToolsConstructor.d.ts +1 -1
  10. package/esm/typings/src/llm-providers/_common/$llmToolsMetadataRegister.d.ts +2 -2
  11. package/esm/typings/src/llm-providers/_common/$llmToolsRegister.d.ts +2 -2
  12. package/esm/typings/src/llm-providers/_common/$registeredLlmToolsMessage.d.ts +9 -0
  13. package/esm/typings/src/llm-providers/_common/LlmToolsConfiguration.d.ts +1 -1
  14. package/esm/typings/src/llm-providers/_common/LlmToolsMetadata.d.ts +1 -1
  15. package/esm/typings/src/llm-providers/_common/createLlmToolsFromConfigurationFromEnv.d.ts +1 -0
  16. package/esm/typings/src/llm-providers/_common/getLlmToolsForTestingAndScriptsAndPlayground.d.ts +1 -1
  17. package/esm/typings/src/llm-providers/anthropic-claude/register-constructor.d.ts +1 -0
  18. package/esm/typings/src/llm-providers/azure-openai/createAzureOpenAiExecutionTools.d.ts +15 -0
  19. package/esm/typings/src/llm-providers/azure-openai/register-configuration.d.ts +9 -0
  20. package/esm/typings/src/llm-providers/azure-openai/register-constructor.d.ts +12 -0
  21. package/esm/typings/src/llm-providers/openai/register-constructor.d.ts +1 -0
  22. package/esm/typings/src/llm-providers/remote/RemoteLlmExecutionTools.d.ts +1 -0
  23. package/esm/typings/src/llm-providers/remote/interfaces/{Promptbook_Server_Error.d.ts → PromptbookServer_Error.d.ts} +1 -1
  24. package/esm/typings/src/llm-providers/remote/interfaces/PromptbookServer_ListModels_Request.d.ts +34 -0
  25. package/esm/typings/src/llm-providers/remote/interfaces/PromptbookServer_ListModels_Response.d.ts +15 -0
  26. package/esm/typings/src/llm-providers/remote/interfaces/{Promptbook_Server_Progress.d.ts → PromptbookServer_Prompt_Progress.d.ts} +1 -1
  27. package/esm/typings/src/llm-providers/remote/interfaces/{Promptbook_Server_Request.d.ts → PromptbookServer_Prompt_Request.d.ts} +15 -3
  28. package/esm/typings/src/llm-providers/remote/interfaces/{Promptbook_Server_Response.d.ts → PromptbookServer_Prompt_Response.d.ts} +1 -1
  29. package/esm/typings/src/llm-providers/remote/interfaces/RemoteLlmExecutionToolsOptions.d.ts +1 -7
  30. package/esm/typings/src/llm-providers/remote/startRemoteServer.d.ts +1 -0
  31. package/esm/typings/src/utils/{Register.d.ts → $Register.d.ts} +6 -2
  32. package/esm/typings/src/utils/environment/{getGlobalScope.d.ts → $getGlobalScope.d.ts} +1 -1
  33. package/esm/typings/src/utils/organization/f.d.ts +6 -0
  34. package/package.json +1 -6
  35. package/umd/index.umd.js +251 -1737
  36. package/umd/index.umd.js.map +1 -1
  37. package/esm/typings/src/llm-providers/_common/config.d.ts +0 -14
  38. package/esm/typings/src/llm-providers/anthropic-claude/register1.d.ts +0 -4
  39. /package/esm/typings/src/llm-providers/mocked/{fakeTextToExpectations.d.ts → $fakeTextToExpectations.d.ts} +0 -0
  40. /package/esm/typings/src/utils/{currentDate.d.ts → $currentDate.d.ts} +0 -0
  41. /package/esm/typings/src/utils/environment/{isRunningInBrowser.d.ts → $isRunningInBrowser.d.ts} +0 -0
  42. /package/esm/typings/src/utils/environment/{isRunningInNode.d.ts → $isRunningInNode.d.ts} +0 -0
  43. /package/esm/typings/src/utils/environment/{isRunningInWebWorker.d.ts → $isRunningInWebWorker.d.ts} +0 -0
  44. /package/esm/typings/src/utils/files/{isDirectoryExisting.d.ts → $isDirectoryExisting.d.ts} +0 -0
  45. /package/esm/typings/src/utils/files/{isDirectoryExisting.test.d.ts → $isDirectoryExisting.test.d.ts} +0 -0
  46. /package/esm/typings/src/utils/files/{isFileExisting.d.ts → $isFileExisting.d.ts} +0 -0
  47. /package/esm/typings/src/utils/files/{isFileExisting.test.d.ts → $isFileExisting.test.d.ts} +0 -0
  48. /package/esm/typings/src/utils/files/{listAllFiles.d.ts → $listAllFiles.d.ts} +0 -0
  49. /package/esm/typings/src/utils/files/{listAllFiles.test.d.ts → $listAllFiles.test.d.ts} +0 -0
  50. /package/esm/typings/src/utils/random/{randomSeed.d.ts → $randomSeed.d.ts} +0 -0
package/esm/index.es.js CHANGED
@@ -3,18 +3,13 @@ import { format } from 'prettier';
3
3
  import parserHtml from 'prettier/parser-html';
4
4
  import hexEncoder from 'crypto-js/enc-hex';
5
5
  import sha256 from 'crypto-js/sha256';
6
- import { io } from 'socket.io-client';
7
- import Anthropic from '@anthropic-ai/sdk';
8
- import colors from 'colors';
9
- import { OpenAIClient, AzureKeyCredential } from '@azure/openai';
10
- import OpenAI from 'openai';
11
6
  import moment from 'moment';
12
7
 
13
8
  // ⚠️ WARNING: This code has been generated so that any manual changes will be overwritten
14
9
  /**
15
10
  * The version of the Promptbook library
16
11
  */
17
- var PROMPTBOOK_VERSION = '0.66.0-6';
12
+ var PROMPTBOOK_VERSION = '0.66.0-8';
18
13
  // TODO: !!!! List here all the versions and annotate + put into script
19
14
 
20
15
  /*! *****************************************************************************
@@ -1645,7 +1640,7 @@ function forEachAsync(array, options, callbackfunction) {
1645
1640
  });
1646
1641
  }
1647
1642
 
1648
- var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.66.0-6",parameters:[{name:"knowledgeContent",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledgePieces",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {knowledgeContent}",dependentParameterNames:["knowledgeContent"],resultingParameterName:"knowledgePieces"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.66.0-6",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {knowledgePieceContent}",dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"keywords"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.66.0-6",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"title",description:"The title of the document",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced content creator, write best title for the document.\n\n# Rules\n\n- Write just title, nothing else\n- Title should be concise and clear\n- Write maximum 5 words for the title\n\n# The document\n\n> {knowledgePieceContent}",expectations:{words:{min:1,max:8}},dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"title"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.66.0-6",parameters:[{name:"availableModelNames",description:"List of available model names separated by comma (,)",isInput:true,isOutput:false},{name:"personaDescription",description:"Description of the persona",isInput:true,isOutput:false},{name:"modelRequirements",description:"Specific requirements for the model",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"make-model-requirements",title:"Make modelRequirements",modelRequirements:{modelVariant:"CHAT",modelName:"gpt-4-turbo"},content:"You are experienced AI engineer, you need to create virtual assistant.\nWrite\n\n## Sample\n\n```json\n{\n\"modelName\": \"gpt-4o\",\n\"systemMessage\": \"You are experienced AI engineer and helpfull assistant.\",\n\"temperature\": 0.7\n}\n```\n\n## Instructions\n\n### Option `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Option `systemMessage`\n\nThe system message is used to communicate instructions or provide context to the model at the beginning of a conversation. It is displayed in a different format compared to user messages, helping the model understand its role in the conversation. The system message typically guides the model's behavior, sets the tone, or specifies desired output from the model. By utilizing the system message effectively, users can steer the model towards generating more accurate and relevant responses.\n\nFor example:\n\n> You are an experienced AI engineer and helpful assistant.\n\n> You are a friendly and knowledgeable chatbot.\n\n### Option `temperature`\n\nThe sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.\n\nYou can pick a value between 0 and 2. For example:\n\n- `0.1`: Low temperature, extremely conservative and deterministic\n- `0.5`: Medium temperature, balanced between conservative and creative\n- `1.0`: High temperature, creative and bit random\n- `1.5`: Very high temperature, extremely creative and often chaotic and unpredictable\n- `2.0`: Maximum temperature, completely random and unpredictable, for some extreme creative use cases\n\n# The assistant\n\nTake this description of the persona:\n\n> {personaDescription}",expectFormat:"JSON",dependentParameterNames:["availableModelNames","personaDescription"],resultingParameterName:"modelRequirements"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
1643
+ var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.66.0-8",parameters:[{name:"knowledgeContent",description:"Markdown document content",isInput:true,isOutput:false},{name:"knowledgePieces",description:"The knowledge JSON object",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, extract the important knowledge from the document.\n\n# Rules\n\n- Make pieces of information concise, clear, and easy to understand\n- One piece of information should be approximately 1 paragraph\n- Divide the paragraphs by markdown horizontal lines ---\n- Omit irrelevant information\n- Group redundant information\n- Write just extracted information, nothing else\n\n# The document\n\nTake information from this document:\n\n> {knowledgeContent}",dependentParameterNames:["knowledgeContent"],resultingParameterName:"knowledgePieces"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-from-markdown.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-keywords.ptbk.md",promptbookVersion:"0.66.0-8",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"keywords",description:"Keywords separated by comma",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced data researcher, detect the important keywords in the document.\n\n# Rules\n\n- Write just keywords separated by comma\n\n# The document\n\nTake information from this document:\n\n> {knowledgePieceContent}",dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"keywords"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-keywords.ptbk.md"},{title:"Prepare Title",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-title.ptbk.md",promptbookVersion:"0.66.0-8",parameters:[{name:"knowledgePieceContent",description:"The content",isInput:true,isOutput:false},{name:"title",description:"The title of the document",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"knowledge",title:"Knowledge",modelRequirements:{modelVariant:"CHAT",modelName:"claude-3-opus-20240229"},content:"You are experienced content creator, write best title for the document.\n\n# Rules\n\n- Write just title, nothing else\n- Title should be concise and clear\n- Write maximum 5 words for the title\n\n# The document\n\n> {knowledgePieceContent}",expectations:{words:{min:1,max:8}},dependentParameterNames:["knowledgePieceContent"],resultingParameterName:"title"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-knowledge-title.ptbk.md"},{title:"Prepare Keywords",pipelineUrl:"https://promptbook.studio/promptbook/prepare-persona.ptbk.md",promptbookVersion:"0.66.0-8",parameters:[{name:"availableModelNames",description:"List of available model names separated by comma (,)",isInput:true,isOutput:false},{name:"personaDescription",description:"Description of the persona",isInput:true,isOutput:false},{name:"modelRequirements",description:"Specific requirements for the model",isInput:false,isOutput:true}],promptTemplates:[{blockType:"PROMPT_TEMPLATE",name:"make-model-requirements",title:"Make modelRequirements",modelRequirements:{modelVariant:"CHAT",modelName:"gpt-4-turbo"},content:"You are experienced AI engineer, you need to create virtual assistant.\nWrite\n\n## Sample\n\n```json\n{\n\"modelName\": \"gpt-4o\",\n\"systemMessage\": \"You are experienced AI engineer and helpfull assistant.\",\n\"temperature\": 0.7\n}\n```\n\n## Instructions\n\n### Option `modelName`\n\nPick from the following models:\n\n- {availableModelNames}\n\n### Option `systemMessage`\n\nThe system message is used to communicate instructions or provide context to the model at the beginning of a conversation. It is displayed in a different format compared to user messages, helping the model understand its role in the conversation. The system message typically guides the model's behavior, sets the tone, or specifies desired output from the model. By utilizing the system message effectively, users can steer the model towards generating more accurate and relevant responses.\n\nFor example:\n\n> You are an experienced AI engineer and helpful assistant.\n\n> You are a friendly and knowledgeable chatbot.\n\n### Option `temperature`\n\nThe sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.\n\nYou can pick a value between 0 and 2. For example:\n\n- `0.1`: Low temperature, extremely conservative and deterministic\n- `0.5`: Medium temperature, balanced between conservative and creative\n- `1.0`: High temperature, creative and bit random\n- `1.5`: Very high temperature, extremely creative and often chaotic and unpredictable\n- `2.0`: Maximum temperature, completely random and unpredictable, for some extreme creative use cases\n\n# The assistant\n\nTake this description of the persona:\n\n> {personaDescription}",expectFormat:"JSON",dependentParameterNames:["availableModelNames","personaDescription"],resultingParameterName:"modelRequirements"}],knowledgeSources:[],knowledgePieces:[],personas:[],preparations:[],sourceFile:"./promptbook-collection/prepare-persona.ptbk.md"}];
1649
1644
 
1650
1645
  var defaultDiacriticsRemovalMap = [
1651
1646
  {
@@ -2243,8 +2238,37 @@ var MultipleLlmExecutionTools = /** @class */ (function () {
2243
2238
  */
2244
2239
  MultipleLlmExecutionTools.prototype.checkConfiguration = function () {
2245
2240
  return __awaiter(this, void 0, void 0, function () {
2246
- return __generator(this, function (_a) {
2247
- return [2 /*return*/];
2241
+ var _a, _b, llmExecutionTools, e_1_1;
2242
+ var e_1, _c;
2243
+ return __generator(this, function (_d) {
2244
+ switch (_d.label) {
2245
+ case 0:
2246
+ _d.trys.push([0, 5, 6, 7]);
2247
+ _a = __values(this.llmExecutionTools), _b = _a.next();
2248
+ _d.label = 1;
2249
+ case 1:
2250
+ if (!!_b.done) return [3 /*break*/, 4];
2251
+ llmExecutionTools = _b.value;
2252
+ return [4 /*yield*/, llmExecutionTools.checkConfiguration()];
2253
+ case 2:
2254
+ _d.sent();
2255
+ _d.label = 3;
2256
+ case 3:
2257
+ _b = _a.next();
2258
+ return [3 /*break*/, 1];
2259
+ case 4: return [3 /*break*/, 7];
2260
+ case 5:
2261
+ e_1_1 = _d.sent();
2262
+ e_1 = { error: e_1_1 };
2263
+ return [3 /*break*/, 7];
2264
+ case 6:
2265
+ try {
2266
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2267
+ }
2268
+ finally { if (e_1) throw e_1.error; }
2269
+ return [7 /*endfinally*/];
2270
+ case 7: return [2 /*return*/];
2271
+ }
2248
2272
  });
2249
2273
  });
2250
2274
  };
@@ -2254,8 +2278,8 @@ var MultipleLlmExecutionTools = /** @class */ (function () {
2254
2278
  */
2255
2279
  MultipleLlmExecutionTools.prototype.listModels = function () {
2256
2280
  return __awaiter(this, void 0, void 0, function () {
2257
- var availableModels, _a, _b, llmExecutionTools, models, e_1_1;
2258
- var e_1, _c;
2281
+ var availableModels, _a, _b, llmExecutionTools, models, e_2_1;
2282
+ var e_2, _c;
2259
2283
  return __generator(this, function (_d) {
2260
2284
  switch (_d.label) {
2261
2285
  case 0:
@@ -2278,14 +2302,14 @@ var MultipleLlmExecutionTools = /** @class */ (function () {
2278
2302
  return [3 /*break*/, 2];
2279
2303
  case 5: return [3 /*break*/, 8];
2280
2304
  case 6:
2281
- e_1_1 = _d.sent();
2282
- e_1 = { error: e_1_1 };
2305
+ e_2_1 = _d.sent();
2306
+ e_2 = { error: e_2_1 };
2283
2307
  return [3 /*break*/, 8];
2284
2308
  case 7:
2285
2309
  try {
2286
2310
  if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
2287
2311
  }
2288
- finally { if (e_1) throw e_1.error; }
2312
+ finally { if (e_2) throw e_2.error; }
2289
2313
  return [7 /*endfinally*/];
2290
2314
  case 8: return [2 /*return*/, availableModels];
2291
2315
  }
@@ -2318,8 +2342,8 @@ var MultipleLlmExecutionTools = /** @class */ (function () {
2318
2342
  */
2319
2343
  MultipleLlmExecutionTools.prototype.callCommonModel = function (prompt) {
2320
2344
  return __awaiter(this, void 0, void 0, function () {
2321
- var errors, _a, _b, llmExecutionTools, _c, error_1, e_2_1;
2322
- var e_2, _d;
2345
+ var errors, _a, _b, llmExecutionTools, _c, error_1, e_3_1;
2346
+ var e_3, _d;
2323
2347
  var _this = this;
2324
2348
  return __generator(this, function (_e) {
2325
2349
  switch (_e.label) {
@@ -2375,14 +2399,14 @@ var MultipleLlmExecutionTools = /** @class */ (function () {
2375
2399
  return [3 /*break*/, 2];
2376
2400
  case 14: return [3 /*break*/, 17];
2377
2401
  case 15:
2378
- e_2_1 = _e.sent();
2379
- e_2 = { error: e_2_1 };
2402
+ e_3_1 = _e.sent();
2403
+ e_3 = { error: e_3_1 };
2380
2404
  return [3 /*break*/, 17];
2381
2405
  case 16:
2382
2406
  try {
2383
2407
  if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
2384
2408
  }
2385
- finally { if (e_2) throw e_2.error; }
2409
+ finally { if (e_3) throw e_3.error; }
2386
2410
  return [7 /*endfinally*/];
2387
2411
  case 17:
2388
2412
  if (errors.length === 1) {
@@ -6517,7 +6541,7 @@ var CallbackInterfaceTools = /** @class */ (function () {
6517
6541
  /**
6518
6542
  * @@@
6519
6543
  *
6520
- * Note: `$` is used to indicate that this function is not a pure function - it access global
6544
+ * Note: `$` is used to indicate that this function is not a pure function - it access global scope
6521
6545
  *
6522
6546
  * @public exported from `@promptbook/utils`
6523
6547
  */
@@ -6531,41 +6555,45 @@ function $getGlobalScope() {
6531
6555
  /**
6532
6556
  * Register is @@@
6533
6557
  *
6558
+ * Note: `$` is used to indicate that this function is not a pure function - it accesses and adds variables in global scope.
6559
+ *
6534
6560
  * @private internal utility, exported are only signleton instances of this class
6535
6561
  */
6536
- var Register = /** @class */ (function () {
6537
- function Register(storage) {
6538
- this.storage = storage;
6562
+ var $Register = /** @class */ (function () {
6563
+ function $Register(storageName) {
6564
+ this.storageName = storageName;
6565
+ storageName = "_promptbook_".concat(storageName);
6566
+ var globalScope = $getGlobalScope();
6567
+ if (globalScope[storageName] === undefined) {
6568
+ globalScope[storageName] = [];
6569
+ }
6570
+ else if (!Array.isArray(globalScope[storageName])) {
6571
+ throw new UnexpectedError("Expected (global) ".concat(storageName, " to be an array, but got ").concat(typeof globalScope[storageName]));
6572
+ }
6573
+ this.storage = globalScope[storageName];
6539
6574
  }
6540
- Register.prototype.list = function () {
6575
+ $Register.prototype.list = function () {
6541
6576
  // <- TODO: ReadonlyDeep<Array<TRegistered>>
6542
6577
  return this.storage;
6543
6578
  };
6544
- Register.prototype.register = function (registered) {
6579
+ $Register.prototype.register = function (registered) {
6545
6580
  // <- TODO: What to return here
6546
6581
  var packageName = registered.packageName, className = registered.className;
6547
6582
  var existingRegistrationIndex = this.storage.findIndex(function (item) { return item.packageName === packageName && item.className === className; });
6548
6583
  var existingRegistration = this.storage[existingRegistrationIndex];
6549
- if (existingRegistration) {
6550
- console.warn("!!!!!! Re-registering ".concat(packageName, ".").concat(className, " again"));
6551
- this.storage[existingRegistrationIndex] = registered;
6584
+ // TODO: !!!!!! Global IS_VERBOSE mode
6585
+ if (!existingRegistration) {
6586
+ console.warn("[\uD83D\uDCE6] Registering `".concat(packageName, ".").concat(className, "` to `").concat(this.storageName, "`"));
6587
+ this.storage.push(registered);
6552
6588
  }
6553
6589
  else {
6554
- this.storage.push(registered);
6590
+ console.warn("[\uD83D\uDCE6] Re-registering `".concat(packageName, ".").concat(className, "` to `").concat(this.storageName, "`"));
6591
+ this.storage[existingRegistrationIndex] = registered;
6555
6592
  }
6556
6593
  };
6557
- return Register;
6594
+ return $Register;
6558
6595
  }());
6559
6596
 
6560
- // TODO: !!!!!! Move this logic to Register and rename to $Register
6561
- var globalScope = $getGlobalScope();
6562
- if (globalScope.$llmToolsMetadataRegister === undefined) {
6563
- globalScope.$llmToolsMetadataRegister = [];
6564
- }
6565
- else if (!Array.isArray(globalScope.$llmToolsMetadataRegister)) {
6566
- throw new UnexpectedError("Expected $llmToolsMetadataRegister to be an array, but got ".concat(typeof globalScope.$llmToolsMetadataRegister));
6567
- }
6568
- var _ = globalScope.$llmToolsMetadataRegister;
6569
6597
  /**
6570
6598
  * @@@
6571
6599
  *
@@ -6573,8 +6601,7 @@ var _ = globalScope.$llmToolsMetadataRegister;
6573
6601
  * @singleton Only one instance of each register is created per build, but thare can be more @@@
6574
6602
  * @public exported from `@promptbook/core`
6575
6603
  */
6576
- var $llmToolsMetadataRegister = new Register(_);
6577
- $getGlobalScope().$llmToolsMetadataRegister;
6604
+ var $llmToolsMetadataRegister = new $Register('llm_tools_metadata');
6578
6605
 
6579
6606
  /**
6580
6607
  * @@@
@@ -6583,1738 +6610,176 @@ $getGlobalScope().$llmToolsMetadataRegister;
6583
6610
  * @singleton Only one instance of each register is created per build, but thare can be more @@@
6584
6611
  * @public exported from `@promptbook/core`
6585
6612
  */
6586
- var $llmToolsRegister = new Register([
6587
- // TODO: !!!!!! Take from global scope
6588
- ]);
6613
+ var $llmToolsRegister = new $Register('llm_execution_tools_constructors');
6589
6614
 
6590
6615
  /**
6591
- * Remote server is a proxy server that uses its execution tools internally and exposes the executor interface externally.
6616
+ * Creates a message with all registered LLM tools
6592
6617
  *
6593
- * You can simply use `RemoteExecutionTools` on client-side javascript and connect to your remote server.
6594
- * This is useful to make all logic on browser side but not expose your API keys or no need to use customer's GPU.
6618
+ * Note: This function is used to create a (error) message when there is no constructor for some LLM provider
6595
6619
  *
6596
- * @see https://github.com/webgptorg/promptbook#remote-server
6597
- * @public exported from `@promptbook/remote-client`
6620
+ * @private internal function of `createLlmToolsFromConfiguration` and `createLlmToolsFromEnv`
6598
6621
  */
6599
- var RemoteLlmExecutionTools = /** @class */ (function () {
6600
- function RemoteLlmExecutionTools(options) {
6601
- this.options = options;
6602
- }
6603
- Object.defineProperty(RemoteLlmExecutionTools.prototype, "title", {
6604
- get: function () {
6605
- // TODO: [🧠] Maybe fetch title+description from the remote server (as well as if model methods are defined)
6606
- return 'Remote server';
6607
- },
6608
- enumerable: false,
6609
- configurable: true
6610
- });
6611
- Object.defineProperty(RemoteLlmExecutionTools.prototype, "description", {
6612
- get: function () {
6613
- return 'Use all models by your remote server';
6614
- },
6615
- enumerable: false,
6616
- configurable: true
6617
- });
6618
- /**
6619
- * Check the configuration of all execution tools
6620
- */
6621
- RemoteLlmExecutionTools.prototype.checkConfiguration = function () {
6622
- return __awaiter(this, void 0, void 0, function () {
6623
- return __generator(this, function (_a) {
6624
- return [2 /*return*/];
6625
- });
6626
- });
6627
- };
6628
- /**
6629
- * List all available models that can be used
6630
- */
6631
- RemoteLlmExecutionTools.prototype.listModels = function () {
6632
- return __awaiter(this, void 0, void 0, function () {
6633
- return __generator(this, function (_a) {
6634
- return [2 /*return*/, (this.options.models ||
6635
- [
6636
- /* !!!!!! */
6637
- ])];
6638
- });
6639
- });
6640
- };
6641
- /**
6642
- * Creates a connection to the remote proxy server.
6643
- */
6644
- RemoteLlmExecutionTools.prototype.makeConnection = function () {
6645
- var _this = this;
6646
- return new Promise(
6647
- // <- TODO: [🧱] Implement in a functional (not new Class) way
6648
- function (resolve, reject) {
6649
- var socket = io(_this.options.remoteUrl, {
6650
- path: _this.options.path,
6651
- // path: `${this.remoteUrl.pathname}/socket.io`,
6652
- transports: [/*'websocket', <- TODO: [🌬] Make websocket transport work */ 'polling'],
6653
- });
6654
- // console.log('Connecting to', this.options.remoteUrl.href, { socket });
6655
- socket.on('connect', function () {
6656
- resolve(socket);
6657
- });
6658
- // TODO: !!!! Better timeout handling
6659
- setTimeout(function () {
6660
- reject(new Error("Timeout while connecting to ".concat(_this.options.remoteUrl)));
6661
- }, 1000 /* <- TODO: Timeout to config */);
6662
- });
6663
- };
6622
+ function $registeredLlmToolsMessage() {
6623
+ var e_1, _a, e_2, _b;
6664
6624
  /**
6665
- * Calls remote proxy server to use a chat model
6625
+ * Mixes registered LLM tools from $llmToolsMetadataRegister and $llmToolsRegister
6666
6626
  */
6667
- RemoteLlmExecutionTools.prototype.callChatModel = function (prompt) {
6668
- if (this.options.isVerbose) {
6669
- console.info("\uD83D\uDD8B Remote callChatModel call");
6627
+ var all = [];
6628
+ var _loop_1 = function (packageName, className) {
6629
+ if (all.some(function (item) { return item.packageName === packageName && item.className === className; })) {
6630
+ return "continue";
6670
6631
  }
6671
- return /* not await */ this.callCommonModel(prompt);
6632
+ all.push({ packageName: packageName, className: className });
6672
6633
  };
6673
- /**
6674
- * Calls remote proxy server to use a completion model
6675
- */
6676
- RemoteLlmExecutionTools.prototype.callCompletionModel = function (prompt) {
6677
- if (this.options.isVerbose) {
6678
- console.info("\uD83D\uDCAC Remote callCompletionModel call");
6634
+ try {
6635
+ for (var _c = __values($llmToolsMetadataRegister.list()), _d = _c.next(); !_d.done; _d = _c.next()) {
6636
+ var _e = _d.value, packageName = _e.packageName, className = _e.className;
6637
+ _loop_1(packageName, className);
6679
6638
  }
6680
- return /* not await */ this.callCommonModel(prompt);
6681
- };
6682
- /**
6683
- * Calls remote proxy server to use a embedding model
6684
- */
6685
- RemoteLlmExecutionTools.prototype.callEmbeddingModel = function (prompt) {
6686
- if (this.options.isVerbose) {
6687
- console.info("\uD83D\uDCAC Remote callEmbeddingModel call");
6639
+ }
6640
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
6641
+ finally {
6642
+ try {
6643
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
6644
+ }
6645
+ finally { if (e_1) throw e_1.error; }
6646
+ }
6647
+ var _loop_2 = function (packageName, className) {
6648
+ if (all.some(function (item) { return item.packageName === packageName && item.className === className; })) {
6649
+ return "continue";
6688
6650
  }
6689
- return /* not await */ this.callCommonModel(prompt);
6651
+ all.push({ packageName: packageName, className: className });
6690
6652
  };
6691
- // <- Note: [🤖] callXxxModel
6692
- /**
6693
- * Calls remote proxy server to use both completion or chat model
6694
- */
6695
- RemoteLlmExecutionTools.prototype.callCommonModel = function (prompt) {
6696
- return __awaiter(this, void 0, void 0, function () {
6697
- var socket, promptResult;
6698
- return __generator(this, function (_a) {
6699
- switch (_a.label) {
6700
- case 0: return [4 /*yield*/, this.makeConnection()];
6701
- case 1:
6702
- socket = _a.sent();
6703
- if (this.options.isAnonymous) {
6704
- socket.emit('request', {
6705
- llmToolsConfiguration: this.options.llmToolsConfiguration,
6706
- prompt: prompt,
6707
- // <- TODO: [🛫] `prompt` is NOT fully serializable as JSON, it contains functions which are not serializable
6708
- });
6709
- }
6710
- else {
6711
- socket.emit('request', {
6712
- clientId: this.options.clientId,
6713
- prompt: prompt,
6714
- // <- TODO: [🛫] `prompt` is NOT fully serializable as JSON, it contains functions which are not serializable
6715
- });
6716
- }
6717
- return [4 /*yield*/, new Promise(function (resolve, reject) {
6718
- socket.on('response', function (response) {
6719
- resolve(response.promptResult);
6720
- socket.disconnect();
6721
- });
6722
- socket.on('error', function (error) {
6723
- reject(new PipelineExecutionError(error.errorMessage));
6724
- socket.disconnect();
6725
- });
6726
- })];
6727
- case 2:
6728
- promptResult = _a.sent();
6729
- socket.disconnect();
6730
- return [2 /*return*/, promptResult];
6731
- }
6732
- });
6653
+ try {
6654
+ for (var _f = __values($llmToolsRegister.list()), _g = _f.next(); !_g.done; _g = _f.next()) {
6655
+ var _h = _g.value, packageName = _h.packageName, className = _h.className;
6656
+ _loop_2(packageName, className);
6657
+ }
6658
+ }
6659
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
6660
+ finally {
6661
+ try {
6662
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
6663
+ }
6664
+ finally { if (e_2) throw e_2.error; }
6665
+ }
6666
+ var metadata = all.map(function (metadata) {
6667
+ var isMetadataAviailable = $llmToolsMetadataRegister
6668
+ .list()
6669
+ .find(function (_a) {
6670
+ var packageName = _a.packageName, className = _a.className;
6671
+ return metadata.packageName === packageName && metadata.className === className;
6733
6672
  });
6734
- };
6735
- return RemoteLlmExecutionTools;
6736
- }());
6737
- /**
6738
- * TODO: [🍓] Allow to list compatible models with each variant
6739
- * TODO: [🗯] RemoteLlmExecutionTools should extend Destroyable and implement IDestroyable
6740
- * TODO: [🧠][🌰] Allow to pass `title` for tracking purposes
6741
- * TODO: [🧠] Maybe remove `@promptbook/remote-client` and just use `@promptbook/core`
6742
- */
6743
-
6744
- /**
6745
- * Function computeUsage will create price per one token based on the string value found on openai page
6746
- *
6747
- * @private within the repository, used only as internal helper for `OPENAI_MODELS`
6748
- */
6749
- function computeUsage(value) {
6750
- var _a = __read(value.split(' / '), 2), price = _a[0], tokens = _a[1];
6751
- return parseFloat(price.replace('$', '')) / parseFloat(tokens.replace('M tokens', '')) / 1000000;
6752
- }
6753
-
6754
- /**
6755
- * List of available Anthropic Claude models with pricing
6756
- *
6757
- * Note: Done at 2024-08-16
6758
- *
6759
- * @see https://docs.anthropic.com/en/docs/models-overview
6760
- * @public exported from `@promptbook/anthropic-claude`
6761
- */
6762
- var ANTHROPIC_CLAUDE_MODELS = [
6763
- {
6764
- modelVariant: 'CHAT',
6765
- modelTitle: 'Claude 3.5 Sonnet',
6766
- modelName: 'claude-3-5-sonnet-20240620',
6767
- pricing: {
6768
- prompt: computeUsage("$3.00 / 1M tokens"),
6769
- output: computeUsage("$15.00 / 1M tokens"),
6770
- },
6771
- },
6772
- {
6773
- modelVariant: 'CHAT',
6774
- modelTitle: 'Claude 3 Opus',
6775
- modelName: 'claude-3-opus-20240229',
6776
- pricing: {
6777
- prompt: computeUsage("$15.00 / 1M tokens"),
6778
- output: computeUsage("$75.00 / 1M tokens"),
6779
- },
6780
- },
6781
- {
6782
- modelVariant: 'CHAT',
6783
- modelTitle: 'Claude 3 Sonnet',
6784
- modelName: 'claude-3-sonnet-20240229',
6785
- pricing: {
6786
- prompt: computeUsage("$3.00 / 1M tokens"),
6787
- output: computeUsage("$15.00 / 1M tokens"),
6788
- },
6789
- },
6790
- {
6791
- modelVariant: 'CHAT',
6792
- modelTitle: 'Claude 3 Haiku',
6793
- modelName: ' claude-3-haiku-20240307',
6794
- pricing: {
6795
- prompt: computeUsage("$0.25 / 1M tokens"),
6796
- output: computeUsage("$1.25 / 1M tokens"),
6797
- },
6798
- },
6799
- {
6800
- modelVariant: 'CHAT',
6801
- modelTitle: 'Claude 2.1',
6802
- modelName: 'claude-2.1',
6803
- pricing: {
6804
- prompt: computeUsage("$8.00 / 1M tokens"),
6805
- output: computeUsage("$24.00 / 1M tokens"),
6806
- },
6807
- },
6808
- {
6809
- modelVariant: 'CHAT',
6810
- modelTitle: 'Claude 2',
6811
- modelName: 'claude-2.0',
6812
- pricing: {
6813
- prompt: computeUsage("$8.00 / 1M tokens"),
6814
- output: computeUsage("$24.00 / 1M tokens"),
6815
- },
6816
- },
6817
- {
6818
- modelVariant: 'CHAT',
6819
- modelTitle: ' Claude Instant 1.2',
6820
- modelName: 'claude-instant-1.2',
6821
- pricing: {
6822
- prompt: computeUsage("$0.80 / 1M tokens"),
6823
- output: computeUsage("$2.40 / 1M tokens"),
6824
- },
6825
- },
6826
- // TODO: !!! Claude 1 and 2 has also completion versions - ask Hoagy
6827
- ];
6828
- /**
6829
- * Note: [🤖] Add models of new variant
6830
- * TODO: [🧠] !!! Add embedding models OR Anthropic has only chat+completion models?
6831
- * TODO: [🧠] Some mechanism to propagate unsureness
6832
- * TODO: [🧠][👮‍♀️] Put here more info like description, isVision, trainingDateCutoff, languages, strengths ( Top-level performance, intelligence, fluency, and understanding), contextWindow,...
6833
- * TODO: [🎰] Some mechanism to auto-update available models
6834
- */
6835
-
6836
- /**
6837
- * Get current date in ISO 8601 format
6838
- *
6839
- * @private internal utility
6840
- */
6841
- function getCurrentIsoDate() {
6842
- return new Date().toISOString();
6673
+ var isInstalled = $llmToolsRegister
6674
+ .list()
6675
+ .find(function (_a) {
6676
+ var packageName = _a.packageName, className = _a.className;
6677
+ return metadata.packageName === packageName && metadata.className === className;
6678
+ });
6679
+ return __assign(__assign({}, metadata), { isMetadataAviailable: isMetadataAviailable, isInstalled: isInstalled });
6680
+ });
6681
+ return spaceTrim(function (block) { return "\n Available LLM providers are:\n ".concat(block(metadata
6682
+ .map(function (_a, i) {
6683
+ var packageName = _a.packageName, className = _a.className, isMetadataAviailable = _a.isMetadataAviailable, isInstalled = _a.isInstalled;
6684
+ var more;
6685
+ if (just(false)) {
6686
+ more = '';
6687
+ }
6688
+ else if (!isMetadataAviailable && !isInstalled) {
6689
+ // TODO: [�][�] Maybe do allow to do auto-install if package not registered and not found
6690
+ more = "(not installed and no metadata, looks like a unexpected behavior)";
6691
+ }
6692
+ else if (isMetadataAviailable && !isInstalled) {
6693
+ // TODO: [�][�]
6694
+ more = "(not installed)";
6695
+ }
6696
+ else if (!isMetadataAviailable && isInstalled) {
6697
+ more = "(no metadata, looks like a unexpected behavior)";
6698
+ }
6699
+ else if (isMetadataAviailable && isInstalled) {
6700
+ more = "(installed)";
6701
+ }
6702
+ else {
6703
+ more = "(unknown state, looks like a unexpected behavior)";
6704
+ }
6705
+ return "".concat(i + 1, ") `").concat(className, "` from `").concat(packageName, "` ").concat(more);
6706
+ })
6707
+ .join('\n')), "\n "); });
6843
6708
  }
6844
6709
 
6845
6710
  /**
6846
- * Helper of usage compute
6711
+ * @@@
6847
6712
  *
6848
- * @param content the content of prompt or response
6849
- * @returns part of PromptResultUsageCounts
6713
+ * Note: This function is not cached, every call creates new instance of `MultipleLlmExecutionTools`
6850
6714
  *
6851
- * @private internal utility of LlmExecutionTools
6715
+ * @returns @@@
6716
+ * @public exported from `@promptbook/core`
6852
6717
  */
6853
- function computeUsageCounts(content) {
6854
- return {
6855
- charactersCount: { value: countCharacters(content) },
6856
- wordsCount: { value: countWords(content) },
6857
- sentencesCount: { value: countSentences(content) },
6858
- linesCount: { value: countLines(content) },
6859
- paragraphsCount: { value: countParagraphs(content) },
6860
- pagesCount: { value: countPages(content) },
6861
- };
6718
+ function createLlmToolsFromConfiguration(configuration, options) {
6719
+ if (options === void 0) { options = {}; }
6720
+ var _a = options.isVerbose, isVerbose = _a === void 0 ? false : _a;
6721
+ var llmTools = configuration.map(function (llmConfiguration) {
6722
+ var registeredItem = $llmToolsRegister
6723
+ .list()
6724
+ .find(function (_a) {
6725
+ var packageName = _a.packageName, className = _a.className;
6726
+ return llmConfiguration.packageName === packageName && llmConfiguration.className === className;
6727
+ });
6728
+ if (registeredItem === undefined) {
6729
+ throw new Error(spaceTrim(function (block) { return "\n There is no constructor for LLM provider `".concat(llmConfiguration.className, "` from `").concat(llmConfiguration.packageName, "`\n\n You have probably forgotten install and import the provider package.\n To fix this issue, you can:\n\n Install:\n\n > npm install ").concat(llmConfiguration.packageName, "\n\n And import:\n\n > import '").concat(llmConfiguration.packageName, "';\n\n\n ").concat(block($registeredLlmToolsMessage()), "\n "); }));
6730
+ }
6731
+ return registeredItem(__assign({ isVerbose: isVerbose }, llmConfiguration.options));
6732
+ });
6733
+ return joinLlmExecutionTools.apply(void 0, __spreadArray([], __read(llmTools), false));
6862
6734
  }
6863
-
6864
6735
  /**
6865
- * Make UncertainNumber
6866
- *
6867
- * @param value
6868
- *
6869
- * @private utility for initializating UncertainNumber
6736
+ * TODO: [🎌] Togethere with `createLlmToolsFromConfiguration` + 'EXECUTION_TOOLS_CLASSES' gets to `@promptbook/core` ALL model providers, make this more efficient
6737
+ * TODO: [🧠][🎌] Dynamically install required providers
6738
+ * TODO: @@@ write discussion about this - wizzard
6739
+ * TODO: [🧠][🍛] Which name is better `createLlmToolsFromConfig` or `createLlmToolsFromConfiguration`?
6740
+ * TODO: [🧠] Is there some meaningfull way how to test this util
6741
+ * TODO: This should be maybe not under `_common` but under `utils`
6870
6742
  */
6871
- function uncertainNumber(value) {
6872
- if (value === null || value === undefined || Number.isNaN(value)) {
6873
- return { value: 0, isUncertain: true };
6874
- }
6875
- return { value: value };
6876
- }
6877
6743
 
6878
6744
  /**
6879
- * Computes the usage of the Anthropic Claude API based on the response from Anthropic Claude
6745
+ * Stores
6880
6746
  *
6881
- * @param promptContent The content of the prompt
6882
- * @param resultContent The content of the result (for embedding prompts or failed prompts pass empty string)
6883
- * @param rawResponse The raw response from Anthropic Claude API
6884
- * @throws {PipelineExecutionError} If the usage is not defined in the response from Anthropic Claude
6885
- * @private internal utility of `AnthropicClaudeExecutionTools`
6747
+ * @public exported from `@promptbook/core`
6886
6748
  */
6887
- function computeAnthropicClaudeUsage(promptContent, // <- Note: Intentionally using [] to access type properties to bring jsdoc from Prompt/PromptResult to consumer
6888
- resultContent, rawResponse) {
6889
- var _a, _b;
6890
- if (rawResponse.usage === undefined) {
6891
- throw new PipelineExecutionError('The usage is not defined in the response from Anthropic Claude');
6892
- }
6893
- if (((_a = rawResponse.usage) === null || _a === void 0 ? void 0 : _a.input_tokens) === undefined) {
6894
- throw new PipelineExecutionError('In Anthropic Claude response `usage.prompt_tokens` not defined');
6895
- }
6896
- var inputTokens = rawResponse.usage.input_tokens;
6897
- var outputTokens = ((_b = rawResponse.usage) === null || _b === void 0 ? void 0 : _b.output_tokens) || 0;
6898
- var modelInfo = ANTHROPIC_CLAUDE_MODELS.find(function (model) { return model.modelName === rawResponse.model; });
6899
- var price;
6900
- if (modelInfo === undefined || modelInfo.pricing === undefined) {
6901
- price = uncertainNumber();
6902
- }
6903
- else {
6904
- price = uncertainNumber(inputTokens * modelInfo.pricing.prompt + outputTokens * modelInfo.pricing.output);
6749
+ var MemoryStorage = /** @class */ (function () {
6750
+ function MemoryStorage() {
6751
+ this.storage = {};
6905
6752
  }
6906
- return {
6907
- price: price,
6908
- input: __assign({ tokensCount: uncertainNumber(rawResponse.usage.input_tokens) }, computeUsageCounts(promptContent)),
6909
- output: __assign({ tokensCount: uncertainNumber(outputTokens) }, computeUsageCounts(resultContent)),
6910
- };
6911
- }
6912
- /**
6913
- * TODO: [🤝] DRY Maybe some common abstraction between `computeOpenAiUsage` and `computeAnthropicClaudeUsage`
6914
- */
6915
-
6916
- /**
6917
- * Execution Tools for calling Anthropic Claude API.
6918
- *
6919
- * @public exported from `@promptbook/anthropic-claude`
6920
- * @deprecated use `createAnthropicClaudeExecutionTools` instead
6921
- */
6922
- var AnthropicClaudeExecutionTools = /** @class */ (function () {
6923
- /**
6924
- * Creates Anthropic Claude Execution Tools.
6925
- *
6926
- * @param options which are relevant are directly passed to the Anthropic Claude client
6927
- */
6928
- function AnthropicClaudeExecutionTools(options) {
6929
- if (options === void 0) { options = { isProxied: false }; }
6930
- this.options = options;
6753
+ Object.defineProperty(MemoryStorage.prototype, "length", {
6931
6754
  /**
6932
- * Anthropic Claude API client.
6755
+ * Returns the number of key/value pairs currently present in the list associated with the object.
6933
6756
  */
6934
- this.client = null;
6935
- }
6936
- Object.defineProperty(AnthropicClaudeExecutionTools.prototype, "title", {
6937
- get: function () {
6938
- return 'Anthropic Claude';
6939
- },
6940
- enumerable: false,
6941
- configurable: true
6942
- });
6943
- Object.defineProperty(AnthropicClaudeExecutionTools.prototype, "description", {
6944
6757
  get: function () {
6945
- return 'Use all models provided by Anthropic Claude';
6758
+ return Object.keys(this.storage).length;
6946
6759
  },
6947
6760
  enumerable: false,
6948
6761
  configurable: true
6949
6762
  });
6950
- AnthropicClaudeExecutionTools.prototype.getClient = function () {
6951
- return __awaiter(this, void 0, void 0, function () {
6952
- var anthropicOptions;
6953
- return __generator(this, function (_a) {
6954
- if (this.client === null) {
6955
- anthropicOptions = __assign({}, this.options);
6956
- delete anthropicOptions.isVerbose;
6957
- delete anthropicOptions.isProxied;
6958
- this.client = new Anthropic(anthropicOptions);
6959
- }
6960
- return [2 /*return*/, this.client];
6961
- });
6962
- });
6763
+ /**
6764
+ * Empties the list associated with the object of all key/value pairs, if there are any.
6765
+ */
6766
+ MemoryStorage.prototype.clear = function () {
6767
+ this.storage = {};
6963
6768
  };
6964
6769
  /**
6965
- * Check the `options` passed to `constructor`
6770
+ * Returns the current value associated with the given key, or null if the given key does not exist in the list associated with the object.
6966
6771
  */
6967
- AnthropicClaudeExecutionTools.prototype.checkConfiguration = function () {
6968
- return __awaiter(this, void 0, void 0, function () {
6969
- return __generator(this, function (_a) {
6970
- switch (_a.label) {
6971
- case 0: return [4 /*yield*/, this.getClient()];
6972
- case 1:
6973
- _a.sent();
6974
- return [2 /*return*/];
6975
- }
6976
- });
6977
- });
6772
+ MemoryStorage.prototype.getItem = function (key) {
6773
+ return this.storage[key] || null;
6978
6774
  };
6979
6775
  /**
6980
- * List all available Anthropic Claude models that can be used
6776
+ * Returns the name of the nth key in the list, or null if n is greater than or equal to the number of key/value pairs in the object.
6981
6777
  */
6982
- AnthropicClaudeExecutionTools.prototype.listModels = function () {
6983
- return ANTHROPIC_CLAUDE_MODELS;
6778
+ MemoryStorage.prototype.key = function (index) {
6779
+ return Object.keys(this.storage)[index] || null;
6984
6780
  };
6985
6781
  /**
6986
- * Calls Anthropic Claude API to use a chat model.
6987
- */
6988
- AnthropicClaudeExecutionTools.prototype.callChatModel = function (prompt) {
6989
- return __awaiter(this, void 0, void 0, function () {
6990
- var content, parameters, modelRequirements, client, modelName, rawPromptContent, rawRequest, start, complete, rawResponse, contentBlock, resultContent, usage;
6991
- return __generator(this, function (_a) {
6992
- switch (_a.label) {
6993
- case 0:
6994
- if (this.options.isVerbose) {
6995
- console.info('💬 Anthropic Claude callChatModel call');
6996
- }
6997
- content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements;
6998
- return [4 /*yield*/, this.getClient()];
6999
- case 1:
7000
- client = _a.sent();
7001
- // TODO: [☂] Use here more modelRequirements
7002
- if (modelRequirements.modelVariant !== 'CHAT') {
7003
- throw new PipelineExecutionError('Use callChatModel only for CHAT variant');
7004
- }
7005
- modelName = modelRequirements.modelName || this.getDefaultChatModel().modelName;
7006
- rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
7007
- rawRequest = {
7008
- model: modelRequirements.modelName || this.getDefaultChatModel().modelName,
7009
- max_tokens: modelRequirements.maxTokens || 4096,
7010
- // <- TODO: [🌾] Make some global max cap for maxTokens
7011
- temperature: modelRequirements.temperature,
7012
- system: modelRequirements.systemMessage,
7013
- // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
7014
- // <- Note: [🧆]
7015
- messages: [
7016
- {
7017
- role: 'user',
7018
- content: rawPromptContent,
7019
- },
7020
- ],
7021
- // TODO: Is here some equivalent of user identification?> user: this.options.user,
7022
- };
7023
- start = getCurrentIsoDate();
7024
- if (this.options.isVerbose) {
7025
- console.info(colors.bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
7026
- }
7027
- return [4 /*yield*/, client.messages.create(rawRequest)];
7028
- case 2:
7029
- rawResponse = _a.sent();
7030
- if (this.options.isVerbose) {
7031
- console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
7032
- }
7033
- if (!rawResponse.content[0]) {
7034
- throw new PipelineExecutionError('No content from Anthropic Claude');
7035
- }
7036
- if (rawResponse.content.length > 1) {
7037
- throw new PipelineExecutionError('More than one content blocks from Anthropic Claude');
7038
- }
7039
- contentBlock = rawResponse.content[0];
7040
- if (contentBlock.type !== 'text') {
7041
- throw new PipelineExecutionError("Returned content is not \"text\" type but \"".concat(contentBlock.type, "\""));
7042
- }
7043
- resultContent = contentBlock.text;
7044
- // eslint-disable-next-line prefer-const
7045
- complete = getCurrentIsoDate();
7046
- usage = computeAnthropicClaudeUsage(content, '', rawResponse);
7047
- return [2 /*return*/, {
7048
- content: resultContent,
7049
- modelName: rawResponse.model,
7050
- timing: {
7051
- start: start,
7052
- complete: complete,
7053
- },
7054
- usage: usage,
7055
- rawPromptContent: rawPromptContent,
7056
- rawRequest: rawRequest,
7057
- rawResponse: rawResponse,
7058
- // <- [🗯]
7059
- }];
7060
- }
7061
- });
7062
- });
7063
- };
7064
- /*
7065
- TODO: [👏]
7066
- public async callCompletionModel(
7067
- prompt: Pick<Prompt, 'content' | 'parameters' | 'modelRequirements'>,
7068
- ): Promise<PromptCompletionResult> {
7069
-
7070
- if (this.options.isVerbose) {
7071
- console.info('🖋 Anthropic Claude callCompletionModel call');
7072
- }
7073
-
7074
- const { content, parameters, modelRequirements } = prompt;
7075
-
7076
- // TODO: [☂] Use here more modelRequirements
7077
- if (modelRequirements.modelVariant !== 'COMPLETION') {
7078
- throw new PipelineExecutionError('Use callCompletionModel only for COMPLETION variant');
7079
- }
7080
-
7081
- const modelName = modelRequirements.modelName || this.getDefaultChatModel().modelName;
7082
- const modelSettings = {
7083
- model: modelName,
7084
- max_tokens: modelRequirements.maxTokens || 2000, // <- Note: 2000 is for lagacy reasons
7085
- // <- TODO: [🌾] Make some global max cap for maxTokens
7086
- // <- TODO: Use here `systemMessage`, `temperature` and `seed`
7087
- };
7088
-
7089
- const rawRequest: xxxx.Completions.CompletionCreateParamsNonStreaming = {
7090
- ...modelSettings,
7091
- prompt: rawPromptContent,
7092
- user: this.options.user,
7093
- };
7094
- const start: string_date_iso8601 = getCurrentIsoDate();
7095
- let complete: string_date_iso8601;
7096
-
7097
- if (this.options.isVerbose) {
7098
- console.info(colors.bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
7099
- }
7100
- const rawResponse = await this.client.completions.create(rawRequest);
7101
- if (this.options.isVerbose) {
7102
- console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
7103
- }
7104
-
7105
- if (!rawResponse.choices[0]) {
7106
- throw new PipelineExecutionError('No choises from Anthropic Claude');
7107
- }
7108
-
7109
- if (rawResponse.choices.length > 1) {
7110
- // TODO: This should be maybe only warning
7111
- throw new PipelineExecutionError('More than one choise from Anthropic Claude');
7112
- }
7113
-
7114
- const resultContent = rawResponse.choices[0].text;
7115
- // eslint-disable-next-line prefer-const
7116
- complete = getCurrentIsoDate();
7117
- const usage = { price: 'UNKNOWN', inputTokens: 0, outputTokens: 0 /* <- TODO: [🐞] Compute usage * / } satisfies PromptResultUsage;
7118
-
7119
-
7120
-
7121
- return {
7122
- content: resultContent,
7123
- modelName: rawResponse.model || model,
7124
- timing: {
7125
- start,
7126
- complete,
7127
- },
7128
- usage,
7129
- rawResponse,
7130
- // <- [🗯]
7131
- };
7132
- }
7133
- */
7134
- // <- Note: [🤖] callXxxModel
7135
- /**
7136
- * Get the model that should be used as default
7137
- */
7138
- AnthropicClaudeExecutionTools.prototype.getDefaultModel = function (defaultModelName) {
7139
- var model = ANTHROPIC_CLAUDE_MODELS.find(function (_a) {
7140
- var modelName = _a.modelName;
7141
- return modelName.startsWith(defaultModelName);
7142
- });
7143
- if (model === undefined) {
7144
- throw new UnexpectedError(spaceTrim(function (block) {
7145
- return "\n Cannot find model in OpenAI models with name \"".concat(defaultModelName, "\" which should be used as default.\n\n Available models:\n ").concat(block(ANTHROPIC_CLAUDE_MODELS.map(function (_a) {
7146
- var modelName = _a.modelName;
7147
- return "- \"".concat(modelName, "\"");
7148
- }).join('\n')), "\n\n ");
7149
- }));
7150
- }
7151
- return model;
7152
- };
7153
- /**
7154
- * Default model for chat variant.
7155
- */
7156
- AnthropicClaudeExecutionTools.prototype.getDefaultChatModel = function () {
7157
- return this.getDefaultModel('claude-3-opus');
7158
- };
7159
- return AnthropicClaudeExecutionTools;
7160
- }());
7161
- /**
7162
- * TODO: [🍆] JSON mode
7163
- * TODO: [🧠] Maybe handle errors via transformAnthropicError (like transformAzureError)
7164
- * TODO: Maybe Create some common util for callChatModel and callCompletionModel
7165
- * TODO: Maybe make custom OpenAiError
7166
- * TODO: [🧠][🈁] Maybe use `isDeterministic` from options
7167
- * TODO: [🧠][🌰] Allow to pass `title` for tracking purposes
7168
- * TODO: [📅] Maybe instead of `RemoteLlmExecutionToolsOptions` use `proxyWithAnonymousRemoteServer` (if implemented)
7169
- */
7170
-
7171
- /**
7172
- * Execution Tools for calling Anthropic Claude API.
7173
- *
7174
- * @public exported from `@promptbook/anthropic-claude`
7175
- */
7176
- var createAnthropicClaudeExecutionTools = Object.assign(function (options) {
7177
- if (options.isProxied) {
7178
- return new RemoteLlmExecutionTools(__assign(__assign({}, options), { isAnonymous: true, llmToolsConfiguration: [
7179
- {
7180
- title: 'Anthropic Claude (proxied)',
7181
- packageName: '@promptbook/anthropic-claude',
7182
- className: 'AnthropicClaudeExecutionTools',
7183
- options: __assign(__assign({}, options), { isProxied: false }),
7184
- },
7185
- ], models: ANTHROPIC_CLAUDE_MODELS }));
7186
- }
7187
- return new AnthropicClaudeExecutionTools(options);
7188
- }, {
7189
- packageName: '@promptbook/anthropic-claude',
7190
- className: 'AnthropicClaudeExecutionTools',
7191
- });
7192
- /**
7193
- * TODO: [🧠] !!!! Make anonymous this with all LLM providers
7194
- * TODO: [🧠][🧱] !!!! Maybe change all `new AnthropicClaudeExecutionTools` -> `createAnthropicClaudeExecutionTools` in manual
7195
- * TODO: [🧠] Maybe auto-detect usage in browser and determine default value of `isProxied`
7196
- * TODO: [🦺] Is there some way how to put `packageName` and `className` on top and function definition on bottom?
7197
- * TODO: [🎶] Naming "constructor" vs "creator" vs "factory"
7198
- */
7199
-
7200
- /**
7201
- * List of available OpenAI models with pricing
7202
- *
7203
- * Note: Done at 2024-05-20
7204
- *
7205
- * @see https://platform.openai.com/docs/models/
7206
- * @see https://openai.com/api/pricing/
7207
- * @public exported from `@promptbook/openai`
7208
- */
7209
- var OPENAI_MODELS = [
7210
- /*/
7211
- {
7212
- modelTitle: 'dall-e-3',
7213
- modelName: 'dall-e-3',
7214
- },
7215
- /**/
7216
- /*/
7217
- {
7218
- modelTitle: 'whisper-1',
7219
- modelName: 'whisper-1',
7220
- },
7221
- /**/
7222
- /**/
7223
- {
7224
- modelVariant: 'COMPLETION',
7225
- modelTitle: 'davinci-002',
7226
- modelName: 'davinci-002',
7227
- pricing: {
7228
- prompt: computeUsage("$2.00 / 1M tokens"),
7229
- output: computeUsage("$2.00 / 1M tokens"), // <- not sure
7230
- },
7231
- },
7232
- /**/
7233
- /*/
7234
- {
7235
- modelTitle: 'dall-e-2',
7236
- modelName: 'dall-e-2',
7237
- },
7238
- /**/
7239
- /**/
7240
- {
7241
- modelVariant: 'CHAT',
7242
- modelTitle: 'gpt-3.5-turbo-16k',
7243
- modelName: 'gpt-3.5-turbo-16k',
7244
- pricing: {
7245
- prompt: computeUsage("$3.00 / 1M tokens"),
7246
- output: computeUsage("$4.00 / 1M tokens"),
7247
- },
7248
- },
7249
- /**/
7250
- /*/
7251
- {
7252
- modelTitle: 'tts-1-hd-1106',
7253
- modelName: 'tts-1-hd-1106',
7254
- },
7255
- /**/
7256
- /*/
7257
- {
7258
- modelTitle: 'tts-1-hd',
7259
- modelName: 'tts-1-hd',
7260
- },
7261
- /**/
7262
- /**/
7263
- {
7264
- modelVariant: 'CHAT',
7265
- modelTitle: 'gpt-4',
7266
- modelName: 'gpt-4',
7267
- pricing: {
7268
- prompt: computeUsage("$30.00 / 1M tokens"),
7269
- output: computeUsage("$60.00 / 1M tokens"),
7270
- },
7271
- },
7272
- /**/
7273
- /**/
7274
- {
7275
- modelVariant: 'CHAT',
7276
- modelTitle: 'gpt-4-32k',
7277
- modelName: 'gpt-4-32k',
7278
- pricing: {
7279
- prompt: computeUsage("$60.00 / 1M tokens"),
7280
- output: computeUsage("$120.00 / 1M tokens"),
7281
- },
7282
- },
7283
- /**/
7284
- /*/
7285
- {
7286
- modelVariant: 'CHAT',
7287
- modelTitle: 'gpt-4-0613',
7288
- modelName: 'gpt-4-0613',
7289
- pricing: {
7290
- prompt: computeUsage(` / 1M tokens`),
7291
- output: computeUsage(` / 1M tokens`),
7292
- },
7293
- },
7294
- /**/
7295
- /**/
7296
- {
7297
- modelVariant: 'CHAT',
7298
- modelTitle: 'gpt-4-turbo-2024-04-09',
7299
- modelName: 'gpt-4-turbo-2024-04-09',
7300
- pricing: {
7301
- prompt: computeUsage("$10.00 / 1M tokens"),
7302
- output: computeUsage("$30.00 / 1M tokens"),
7303
- },
7304
- },
7305
- /**/
7306
- /**/
7307
- {
7308
- modelVariant: 'CHAT',
7309
- modelTitle: 'gpt-3.5-turbo-1106',
7310
- modelName: 'gpt-3.5-turbo-1106',
7311
- pricing: {
7312
- prompt: computeUsage("$1.00 / 1M tokens"),
7313
- output: computeUsage("$2.00 / 1M tokens"),
7314
- },
7315
- },
7316
- /**/
7317
- /**/
7318
- {
7319
- modelVariant: 'CHAT',
7320
- modelTitle: 'gpt-4-turbo',
7321
- modelName: 'gpt-4-turbo',
7322
- pricing: {
7323
- prompt: computeUsage("$10.00 / 1M tokens"),
7324
- output: computeUsage("$30.00 / 1M tokens"),
7325
- },
7326
- },
7327
- /**/
7328
- /**/
7329
- {
7330
- modelVariant: 'COMPLETION',
7331
- modelTitle: 'gpt-3.5-turbo-instruct-0914',
7332
- modelName: 'gpt-3.5-turbo-instruct-0914',
7333
- pricing: {
7334
- prompt: computeUsage("$1.50 / 1M tokens"),
7335
- output: computeUsage("$2.00 / 1M tokens"), // <- For gpt-3.5-turbo-instruct
7336
- },
7337
- },
7338
- /**/
7339
- /**/
7340
- {
7341
- modelVariant: 'COMPLETION',
7342
- modelTitle: 'gpt-3.5-turbo-instruct',
7343
- modelName: 'gpt-3.5-turbo-instruct',
7344
- pricing: {
7345
- prompt: computeUsage("$1.50 / 1M tokens"),
7346
- output: computeUsage("$2.00 / 1M tokens"),
7347
- },
7348
- },
7349
- /**/
7350
- /*/
7351
- {
7352
- modelTitle: 'tts-1',
7353
- modelName: 'tts-1',
7354
- },
7355
- /**/
7356
- /**/
7357
- {
7358
- modelVariant: 'CHAT',
7359
- modelTitle: 'gpt-3.5-turbo',
7360
- modelName: 'gpt-3.5-turbo',
7361
- pricing: {
7362
- prompt: computeUsage("$3.00 / 1M tokens"),
7363
- output: computeUsage("$6.00 / 1M tokens"), // <- Not sure, refer to gpt-3.5-turbo in Fine-tuning models
7364
- },
7365
- },
7366
- /**/
7367
- /**/
7368
- {
7369
- modelVariant: 'CHAT',
7370
- modelTitle: 'gpt-3.5-turbo-0301',
7371
- modelName: 'gpt-3.5-turbo-0301',
7372
- pricing: {
7373
- prompt: computeUsage("$1.50 / 1M tokens"),
7374
- output: computeUsage("$2.00 / 1M tokens"),
7375
- },
7376
- },
7377
- /**/
7378
- /**/
7379
- {
7380
- modelVariant: 'COMPLETION',
7381
- modelTitle: 'babbage-002',
7382
- modelName: 'babbage-002',
7383
- pricing: {
7384
- prompt: computeUsage("$0.40 / 1M tokens"),
7385
- output: computeUsage("$0.40 / 1M tokens"), // <- Not sure
7386
- },
7387
- },
7388
- /**/
7389
- /**/
7390
- {
7391
- modelVariant: 'CHAT',
7392
- modelTitle: 'gpt-4-1106-preview',
7393
- modelName: 'gpt-4-1106-preview',
7394
- pricing: {
7395
- prompt: computeUsage("$10.00 / 1M tokens"),
7396
- output: computeUsage("$30.00 / 1M tokens"),
7397
- },
7398
- },
7399
- /**/
7400
- /**/
7401
- {
7402
- modelVariant: 'CHAT',
7403
- modelTitle: 'gpt-4-0125-preview',
7404
- modelName: 'gpt-4-0125-preview',
7405
- pricing: {
7406
- prompt: computeUsage("$10.00 / 1M tokens"),
7407
- output: computeUsage("$30.00 / 1M tokens"),
7408
- },
7409
- },
7410
- /**/
7411
- /*/
7412
- {
7413
- modelTitle: 'tts-1-1106',
7414
- modelName: 'tts-1-1106',
7415
- },
7416
- /**/
7417
- /**/
7418
- {
7419
- modelVariant: 'CHAT',
7420
- modelTitle: 'gpt-3.5-turbo-0125',
7421
- modelName: 'gpt-3.5-turbo-0125',
7422
- pricing: {
7423
- prompt: computeUsage("$0.50 / 1M tokens"),
7424
- output: computeUsage("$1.50 / 1M tokens"),
7425
- },
7426
- },
7427
- /**/
7428
- /**/
7429
- {
7430
- modelVariant: 'CHAT',
7431
- modelTitle: 'gpt-4-turbo-preview',
7432
- modelName: 'gpt-4-turbo-preview',
7433
- pricing: {
7434
- prompt: computeUsage("$10.00 / 1M tokens"),
7435
- output: computeUsage("$30.00 / 1M tokens"), // <- Not sure, just for gpt-4-turbo
7436
- },
7437
- },
7438
- /**/
7439
- /**/
7440
- {
7441
- modelVariant: 'EMBEDDING',
7442
- modelTitle: 'text-embedding-3-large',
7443
- modelName: 'text-embedding-3-large',
7444
- pricing: {
7445
- prompt: computeUsage("$0.13 / 1M tokens"),
7446
- // TODO: [🏏] Leverage the batch API @see https://platform.openai.com/docs/guides/batch
7447
- output: 0, // <- Note: [🆖] In Embedding models you dont pay for output
7448
- },
7449
- },
7450
- /**/
7451
- /**/
7452
- {
7453
- modelVariant: 'EMBEDDING',
7454
- modelTitle: 'text-embedding-3-small',
7455
- modelName: 'text-embedding-3-small',
7456
- pricing: {
7457
- prompt: computeUsage("$0.02 / 1M tokens"),
7458
- // TODO: [🏏] Leverage the batch API @see https://platform.openai.com/docs/guides/batch
7459
- output: 0, // <- Note: [🆖] In Embedding models you dont pay for output
7460
- },
7461
- },
7462
- /**/
7463
- /**/
7464
- {
7465
- modelVariant: 'CHAT',
7466
- modelTitle: 'gpt-3.5-turbo-0613',
7467
- modelName: 'gpt-3.5-turbo-0613',
7468
- pricing: {
7469
- prompt: computeUsage("$1.50 / 1M tokens"),
7470
- output: computeUsage("$2.00 / 1M tokens"),
7471
- },
7472
- },
7473
- /**/
7474
- /**/
7475
- {
7476
- modelVariant: 'EMBEDDING',
7477
- modelTitle: 'text-embedding-ada-002',
7478
- modelName: 'text-embedding-ada-002',
7479
- pricing: {
7480
- prompt: computeUsage("$0.1 / 1M tokens"),
7481
- // TODO: [🏏] Leverage the batch API @see https://platform.openai.com/docs/guides/batch
7482
- output: 0, // <- Note: [🆖] In Embedding models you dont pay for output
7483
- },
7484
- },
7485
- /**/
7486
- /*/
7487
- {
7488
- modelVariant: 'CHAT',
7489
- modelTitle: 'gpt-4-1106-vision-preview',
7490
- modelName: 'gpt-4-1106-vision-preview',
7491
- },
7492
- /**/
7493
- /*/
7494
- {
7495
- modelVariant: 'CHAT',
7496
- modelTitle: 'gpt-4-vision-preview',
7497
- modelName: 'gpt-4-vision-preview',
7498
- pricing: {
7499
- prompt: computeUsage(`$10.00 / 1M tokens`),
7500
- output: computeUsage(`$30.00 / 1M tokens`),
7501
- },
7502
- },
7503
- /**/
7504
- /**/
7505
- {
7506
- modelVariant: 'CHAT',
7507
- modelTitle: 'gpt-4o-2024-05-13',
7508
- modelName: 'gpt-4o-2024-05-13',
7509
- pricing: {
7510
- prompt: computeUsage("$5.00 / 1M tokens"),
7511
- output: computeUsage("$15.00 / 1M tokens"),
7512
- },
7513
- },
7514
- /**/
7515
- /**/
7516
- {
7517
- modelVariant: 'CHAT',
7518
- modelTitle: 'gpt-4o',
7519
- modelName: 'gpt-4o',
7520
- pricing: {
7521
- prompt: computeUsage("$5.00 / 1M tokens"),
7522
- output: computeUsage("$15.00 / 1M tokens"),
7523
- },
7524
- },
7525
- /**/
7526
- /**/
7527
- {
7528
- modelVariant: 'CHAT',
7529
- modelTitle: 'gpt-3.5-turbo-16k-0613',
7530
- modelName: 'gpt-3.5-turbo-16k-0613',
7531
- pricing: {
7532
- prompt: computeUsage("$3.00 / 1M tokens"),
7533
- output: computeUsage("$4.00 / 1M tokens"),
7534
- },
7535
- },
7536
- /**/
7537
- ];
7538
- /**
7539
- * Note: [🤖] Add models of new variant
7540
- * TODO: [🧠] Some mechanism to propagate unsureness
7541
- * TODO: [🎰] Some mechanism to auto-update available models
7542
- * TODO: [🎰][👮‍♀️] Make this list dynamic - dynamically can be listed modelNames but not modelVariant, legacy status, context length and pricing
7543
- * TODO: [🧠][👮‍♀️] Put here more info like description, isVision, trainingDateCutoff, languages, strengths ( Top-level performance, intelligence, fluency, and understanding), contextWindow,...
7544
- * @see https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4
7545
- * @see https://openai.com/api/pricing/
7546
- * @see /other/playground/playground.ts
7547
- * TODO: [🍓] Make better
7548
- * TODO: Change model titles to human eg: "gpt-4-turbo-2024-04-09" -> "GPT-4 Turbo (2024-04-09)"
7549
- * TODO: [🚸] Not all models are compatible with JSON mode, add this information here and use it
7550
- */
7551
-
7552
- /**
7553
- * Execution Tools for calling Azure OpenAI API.
7554
- *
7555
- * @public exported from `@promptbook/azure-openai`
7556
- */
7557
- var AzureOpenAiExecutionTools = /** @class */ (function () {
7558
- /**
7559
- * Creates OpenAI Execution Tools.
7560
- *
7561
- * @param options which are relevant are directly passed to the OpenAI client
7562
- */
7563
- function AzureOpenAiExecutionTools(options) {
7564
- this.options = options;
7565
- /**
7566
- * OpenAI Azure API client.
7567
- */
7568
- this.client = null;
7569
- }
7570
- Object.defineProperty(AzureOpenAiExecutionTools.prototype, "title", {
7571
- get: function () {
7572
- return 'Azure OpenAI';
7573
- },
7574
- enumerable: false,
7575
- configurable: true
7576
- });
7577
- Object.defineProperty(AzureOpenAiExecutionTools.prototype, "description", {
7578
- get: function () {
7579
- return 'Use all models trained by OpenAI provided by Azure';
7580
- },
7581
- enumerable: false,
7582
- configurable: true
7583
- });
7584
- AzureOpenAiExecutionTools.prototype.getClient = function () {
7585
- return __awaiter(this, void 0, void 0, function () {
7586
- return __generator(this, function (_a) {
7587
- if (this.client === null) {
7588
- this.client = new OpenAIClient("https://".concat(this.options.resourceName, ".openai.azure.com/"), new AzureKeyCredential(this.options.apiKey));
7589
- }
7590
- return [2 /*return*/, this.client];
7591
- });
7592
- });
7593
- };
7594
- /**
7595
- * Check the `options` passed to `constructor`
7596
- */
7597
- AzureOpenAiExecutionTools.prototype.checkConfiguration = function () {
7598
- return __awaiter(this, void 0, void 0, function () {
7599
- return __generator(this, function (_a) {
7600
- switch (_a.label) {
7601
- case 0: return [4 /*yield*/, this.getClient()];
7602
- case 1:
7603
- _a.sent();
7604
- return [2 /*return*/];
7605
- }
7606
- });
7607
- });
7608
- };
7609
- /**
7610
- * List all available Azure OpenAI models that can be used
7611
- */
7612
- AzureOpenAiExecutionTools.prototype.listModels = function () {
7613
- return __awaiter(this, void 0, void 0, function () {
7614
- return __generator(this, function (_a) {
7615
- // TODO: !!! Do here some filtering which models are really available as deployment
7616
- // @see https://management.azure.com/subscriptions/subscriptionId/resourceGroups/resourceGroupName/providers/Microsoft.CognitiveServices/accounts/accountName/deployments?api-version=2023-05-01
7617
- return [2 /*return*/, OPENAI_MODELS.map(function (_a) {
7618
- var modelTitle = _a.modelTitle, modelName = _a.modelName, modelVariant = _a.modelVariant;
7619
- return ({
7620
- modelTitle: "Azure ".concat(modelTitle),
7621
- modelName: modelName,
7622
- modelVariant: modelVariant,
7623
- });
7624
- })];
7625
- });
7626
- });
7627
- };
7628
- /**
7629
- * Calls OpenAI API to use a chat model.
7630
- */
7631
- AzureOpenAiExecutionTools.prototype.callChatModel = function (prompt) {
7632
- var _a, _b;
7633
- return __awaiter(this, void 0, void 0, function () {
7634
- var content, parameters, modelRequirements, client, modelName, modelSettings, rawPromptContent, messages, start, complete, rawRequest, rawResponse, resultContent, usage, error_1;
7635
- return __generator(this, function (_c) {
7636
- switch (_c.label) {
7637
- case 0:
7638
- if (this.options.isVerbose) {
7639
- console.info('💬 OpenAI callChatModel call');
7640
- }
7641
- content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements;
7642
- return [4 /*yield*/, this.getClient()];
7643
- case 1:
7644
- client = _c.sent();
7645
- // TODO: [☂] Use here more modelRequirements
7646
- if (modelRequirements.modelVariant !== 'CHAT') {
7647
- throw new PipelineExecutionError('Use callChatModel only for CHAT variant');
7648
- }
7649
- _c.label = 2;
7650
- case 2:
7651
- _c.trys.push([2, 4, , 5]);
7652
- modelName = prompt.modelRequirements.modelName || this.options.deploymentName;
7653
- modelSettings = {
7654
- maxTokens: modelRequirements.maxTokens,
7655
- // <- TODO: [🌾] Make some global max cap for maxTokens
7656
- temperature: modelRequirements.temperature,
7657
- user: this.options.user,
7658
- // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
7659
- // <- Note: [🧆]
7660
- };
7661
- rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
7662
- messages = __spreadArray(__spreadArray([], __read((modelRequirements.systemMessage === undefined
7663
- ? []
7664
- : [
7665
- {
7666
- role: 'system',
7667
- content: modelRequirements.systemMessage,
7668
- },
7669
- ])), false), [
7670
- {
7671
- role: 'user',
7672
- content: rawPromptContent,
7673
- },
7674
- ], false);
7675
- start = getCurrentIsoDate();
7676
- complete = void 0;
7677
- if (this.options.isVerbose) {
7678
- console.info(colors.bgWhite('messages'), JSON.stringify(messages, null, 4));
7679
- }
7680
- rawRequest = [modelName, messages, modelSettings];
7681
- return [4 /*yield*/, client.getChatCompletions.apply(client, __spreadArray([], __read(rawRequest), false))];
7682
- case 3:
7683
- rawResponse = _c.sent();
7684
- if (this.options.isVerbose) {
7685
- console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
7686
- }
7687
- if (!rawResponse.choices[0]) {
7688
- throw new PipelineExecutionError('No choises from Azure OpenAI');
7689
- }
7690
- if (rawResponse.choices.length > 1) {
7691
- // TODO: This should be maybe only warning
7692
- throw new PipelineExecutionError('More than one choise from Azure OpenAI');
7693
- }
7694
- if (!rawResponse.choices[0].message || !rawResponse.choices[0].message.content) {
7695
- throw new PipelineExecutionError('Empty response from Azure OpenAI');
7696
- }
7697
- resultContent = rawResponse.choices[0].message.content;
7698
- // eslint-disable-next-line prefer-const
7699
- complete = getCurrentIsoDate();
7700
- usage = {
7701
- price: uncertainNumber() /* <- TODO: [🐞] Compute usage */,
7702
- input: __assign({ tokensCount: uncertainNumber((_a = rawResponse.usage) === null || _a === void 0 ? void 0 : _a.promptTokens) }, computeUsageCounts(prompt.content)),
7703
- output: __assign({ tokensCount: uncertainNumber((_b = rawResponse.usage) === null || _b === void 0 ? void 0 : _b.completionTokens) }, computeUsageCounts(prompt.content)),
7704
- };
7705
- return [2 /*return*/, {
7706
- content: resultContent,
7707
- modelName: modelName,
7708
- timing: {
7709
- start: start,
7710
- complete: complete,
7711
- },
7712
- usage: usage,
7713
- rawPromptContent: rawPromptContent,
7714
- rawRequest: rawRequest,
7715
- rawResponse: rawResponse,
7716
- // <- [🗯]
7717
- }];
7718
- case 4:
7719
- error_1 = _c.sent();
7720
- throw this.transformAzureError(error_1);
7721
- case 5: return [2 /*return*/];
7722
- }
7723
- });
7724
- });
7725
- };
7726
- /**
7727
- * Calls Azure OpenAI API to use a complete model.
7728
- */
7729
- AzureOpenAiExecutionTools.prototype.callCompletionModel = function (prompt) {
7730
- var _a, _b;
7731
- return __awaiter(this, void 0, void 0, function () {
7732
- var content, parameters, modelRequirements, client, modelName, modelSettings, start, complete, rawPromptContent, rawRequest, rawResponse, resultContent, usage, error_2;
7733
- return __generator(this, function (_c) {
7734
- switch (_c.label) {
7735
- case 0:
7736
- if (this.options.isVerbose) {
7737
- console.info('🖋 OpenAI callCompletionModel call');
7738
- }
7739
- content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements;
7740
- return [4 /*yield*/, this.getClient()];
7741
- case 1:
7742
- client = _c.sent();
7743
- // TODO: [☂] Use here more modelRequirements
7744
- if (modelRequirements.modelVariant !== 'COMPLETION') {
7745
- throw new PipelineExecutionError('Use callCompletionModel only for COMPLETION variant');
7746
- }
7747
- _c.label = 2;
7748
- case 2:
7749
- _c.trys.push([2, 4, , 5]);
7750
- modelName = prompt.modelRequirements.modelName || this.options.deploymentName;
7751
- modelSettings = {
7752
- maxTokens: modelRequirements.maxTokens || 2000,
7753
- // <- TODO: [🌾] Make some global max cap for maxTokens
7754
- temperature: modelRequirements.temperature,
7755
- user: this.options.user,
7756
- // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
7757
- // <- Note: [🧆]
7758
- };
7759
- start = getCurrentIsoDate();
7760
- complete = void 0;
7761
- if (this.options.isVerbose) {
7762
- console.info(colors.bgWhite('content'), JSON.stringify(content, null, 4));
7763
- console.info(colors.bgWhite('parameters'), JSON.stringify(parameters, null, 4));
7764
- }
7765
- rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
7766
- rawRequest = [
7767
- modelName,
7768
- [rawPromptContent],
7769
- modelSettings,
7770
- ];
7771
- return [4 /*yield*/, client.getCompletions.apply(client, __spreadArray([], __read(rawRequest), false))];
7772
- case 3:
7773
- rawResponse = _c.sent();
7774
- if (this.options.isVerbose) {
7775
- console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
7776
- }
7777
- if (!rawResponse.choices[0]) {
7778
- throw new PipelineExecutionError('No choises from OpenAI');
7779
- }
7780
- if (rawResponse.choices.length > 1) {
7781
- // TODO: This should be maybe only warning
7782
- throw new PipelineExecutionError('More than one choise from OpenAI');
7783
- }
7784
- resultContent = rawResponse.choices[0].text;
7785
- // eslint-disable-next-line prefer-const
7786
- complete = getCurrentIsoDate();
7787
- usage = {
7788
- price: uncertainNumber() /* <- TODO: [🐞] Compute usage */,
7789
- input: __assign({ tokensCount: uncertainNumber((_a = rawResponse.usage) === null || _a === void 0 ? void 0 : _a.promptTokens) }, computeUsageCounts(prompt.content)),
7790
- output: __assign({ tokensCount: uncertainNumber((_b = rawResponse.usage) === null || _b === void 0 ? void 0 : _b.completionTokens) }, computeUsageCounts(prompt.content)),
7791
- };
7792
- return [2 /*return*/, {
7793
- content: resultContent,
7794
- modelName: modelName,
7795
- timing: {
7796
- start: start,
7797
- complete: complete,
7798
- },
7799
- usage: usage,
7800
- rawPromptContent: rawPromptContent,
7801
- rawRequest: rawRequest,
7802
- rawResponse: rawResponse,
7803
- // <- [🗯]
7804
- }];
7805
- case 4:
7806
- error_2 = _c.sent();
7807
- throw this.transformAzureError(error_2);
7808
- case 5: return [2 /*return*/];
7809
- }
7810
- });
7811
- });
7812
- };
7813
- // <- Note: [🤖] callXxxModel
7814
- /**
7815
- * Changes Azure error (which is not propper Error but object) to propper Error
7816
- */
7817
- AzureOpenAiExecutionTools.prototype.transformAzureError = function (azureError) {
7818
- if (typeof azureError !== 'object' || azureError === null) {
7819
- return new PipelineExecutionError("Unknown Azure OpenAI error");
7820
- }
7821
- var code = azureError.code, message = azureError.message;
7822
- return new PipelineExecutionError("".concat(code, ": ").concat(message));
7823
- };
7824
- return AzureOpenAiExecutionTools;
7825
- }());
7826
- /**
7827
- * TODO: Maybe Create some common util for callChatModel and callCompletionModel
7828
- * TODO: Maybe make custom AzureOpenAiError
7829
- * TODO: [🧠][🈁] Maybe use `isDeterministic` from options
7830
- * TODO: [🧠][🌰] Allow to pass `title` for tracking purposes
7831
- */
7832
-
7833
- /**
7834
- * Computes the usage of the OpenAI API based on the response from OpenAI
7835
- *
7836
- * @param promptContent The content of the prompt
7837
- * @param resultContent The content of the result (for embedding prompts or failed prompts pass empty string)
7838
- * @param rawResponse The raw response from OpenAI API
7839
- * @throws {PipelineExecutionError} If the usage is not defined in the response from OpenAI
7840
- * @private internal utility of `OpenAiExecutionTools`
7841
- */
7842
- function computeOpenAiUsage(promptContent, // <- Note: Intentionally using [] to access type properties to bring jsdoc from Prompt/PromptResult to consumer
7843
- resultContent, rawResponse) {
7844
- var _a, _b;
7845
- if (rawResponse.usage === undefined) {
7846
- throw new PipelineExecutionError('The usage is not defined in the response from OpenAI');
7847
- }
7848
- if (((_a = rawResponse.usage) === null || _a === void 0 ? void 0 : _a.prompt_tokens) === undefined) {
7849
- throw new PipelineExecutionError('In OpenAI response `usage.prompt_tokens` not defined');
7850
- }
7851
- var inputTokens = rawResponse.usage.prompt_tokens;
7852
- var outputTokens = ((_b = rawResponse.usage) === null || _b === void 0 ? void 0 : _b.completion_tokens) || 0;
7853
- var modelInfo = OPENAI_MODELS.find(function (model) { return model.modelName === rawResponse.model; });
7854
- var price;
7855
- if (modelInfo === undefined || modelInfo.pricing === undefined) {
7856
- price = uncertainNumber();
7857
- }
7858
- else {
7859
- price = uncertainNumber(inputTokens * modelInfo.pricing.prompt + outputTokens * modelInfo.pricing.output);
7860
- }
7861
- return {
7862
- price: price,
7863
- input: __assign({ tokensCount: uncertainNumber(rawResponse.usage.prompt_tokens) }, computeUsageCounts(promptContent)),
7864
- output: __assign({ tokensCount: uncertainNumber(outputTokens) }, computeUsageCounts(resultContent)),
7865
- };
7866
- }
7867
- /**
7868
- * TODO: [🤝] DRY Maybe some common abstraction between `computeOpenAiUsage` and `computeAnthropicClaudeUsage`
7869
- */
7870
-
7871
- /**
7872
- * Execution Tools for calling OpenAI API
7873
- *
7874
- * @public exported from `@promptbook/openai`
7875
- */
7876
- var OpenAiExecutionTools = /** @class */ (function () {
7877
- /**
7878
- * Creates OpenAI Execution Tools.
7879
- *
7880
- * @param options which are relevant are directly passed to the OpenAI client
7881
- */
7882
- function OpenAiExecutionTools(options) {
7883
- if (options === void 0) { options = {}; }
7884
- this.options = options;
7885
- /**
7886
- * OpenAI API client.
7887
- */
7888
- this.client = null;
7889
- }
7890
- Object.defineProperty(OpenAiExecutionTools.prototype, "title", {
7891
- get: function () {
7892
- return 'OpenAI';
7893
- },
7894
- enumerable: false,
7895
- configurable: true
7896
- });
7897
- Object.defineProperty(OpenAiExecutionTools.prototype, "description", {
7898
- get: function () {
7899
- return 'Use all models provided by OpenAI';
7900
- },
7901
- enumerable: false,
7902
- configurable: true
7903
- });
7904
- OpenAiExecutionTools.prototype.getClient = function () {
7905
- return __awaiter(this, void 0, void 0, function () {
7906
- var openAiOptions;
7907
- return __generator(this, function (_a) {
7908
- if (this.client === null) {
7909
- openAiOptions = __assign({}, this.options);
7910
- delete openAiOptions.isVerbose;
7911
- delete openAiOptions.user;
7912
- this.client = new OpenAI(__assign({}, openAiOptions));
7913
- }
7914
- return [2 /*return*/, this.client];
7915
- });
7916
- });
7917
- };
7918
- /**
7919
- * Check the `options` passed to `constructor`
7920
- */
7921
- OpenAiExecutionTools.prototype.checkConfiguration = function () {
7922
- return __awaiter(this, void 0, void 0, function () {
7923
- return __generator(this, function (_a) {
7924
- switch (_a.label) {
7925
- case 0: return [4 /*yield*/, this.getClient()];
7926
- case 1:
7927
- _a.sent();
7928
- return [2 /*return*/];
7929
- }
7930
- });
7931
- });
7932
- };
7933
- /**
7934
- * List all available OpenAI models that can be used
7935
- */
7936
- OpenAiExecutionTools.prototype.listModels = function () {
7937
- /*
7938
- Note: Dynamic lising of the models
7939
- const models = await this.openai.models.list({});
7940
-
7941
- console.log({ models });
7942
- console.log(models.data);
7943
- */
7944
- return OPENAI_MODELS;
7945
- };
7946
- /**
7947
- * Calls OpenAI API to use a chat model.
7948
- */
7949
- OpenAiExecutionTools.prototype.callChatModel = function (prompt) {
7950
- return __awaiter(this, void 0, void 0, function () {
7951
- var content, parameters, modelRequirements, expectFormat, client, modelName, modelSettings, rawPromptContent, rawRequest, start, complete, rawResponse, resultContent, usage;
7952
- return __generator(this, function (_a) {
7953
- switch (_a.label) {
7954
- case 0:
7955
- if (this.options.isVerbose) {
7956
- console.info('💬 OpenAI callChatModel call', { prompt: prompt });
7957
- }
7958
- content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements, expectFormat = prompt.expectFormat;
7959
- return [4 /*yield*/, this.getClient()];
7960
- case 1:
7961
- client = _a.sent();
7962
- // TODO: [☂] Use here more modelRequirements
7963
- if (modelRequirements.modelVariant !== 'CHAT') {
7964
- throw new PipelineExecutionError('Use callChatModel only for CHAT variant');
7965
- }
7966
- modelName = modelRequirements.modelName || this.getDefaultChatModel().modelName;
7967
- modelSettings = {
7968
- model: modelName,
7969
- max_tokens: modelRequirements.maxTokens,
7970
- // <- TODO: [🌾] Make some global max cap for maxTokens
7971
- temperature: modelRequirements.temperature,
7972
- // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
7973
- // <- Note: [🧆]
7974
- };
7975
- if (expectFormat === 'JSON') {
7976
- modelSettings.response_format = {
7977
- type: 'json_object',
7978
- };
7979
- }
7980
- rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
7981
- rawRequest = __assign(__assign({}, modelSettings), { messages: __spreadArray(__spreadArray([], __read((modelRequirements.systemMessage === undefined
7982
- ? []
7983
- : [
7984
- {
7985
- role: 'system',
7986
- content: modelRequirements.systemMessage,
7987
- },
7988
- ])), false), [
7989
- {
7990
- role: 'user',
7991
- content: rawPromptContent,
7992
- },
7993
- ], false), user: this.options.user });
7994
- start = getCurrentIsoDate();
7995
- if (this.options.isVerbose) {
7996
- console.info(colors.bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
7997
- }
7998
- return [4 /*yield*/, client.chat.completions.create(rawRequest)];
7999
- case 2:
8000
- rawResponse = _a.sent();
8001
- if (this.options.isVerbose) {
8002
- console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
8003
- }
8004
- if (!rawResponse.choices[0]) {
8005
- throw new PipelineExecutionError('No choises from OpenAI');
8006
- }
8007
- if (rawResponse.choices.length > 1) {
8008
- // TODO: This should be maybe only warning
8009
- throw new PipelineExecutionError('More than one choise from OpenAI');
8010
- }
8011
- resultContent = rawResponse.choices[0].message.content;
8012
- // eslint-disable-next-line prefer-const
8013
- complete = getCurrentIsoDate();
8014
- usage = computeOpenAiUsage(content, resultContent || '', rawResponse);
8015
- if (resultContent === null) {
8016
- throw new PipelineExecutionError('No response message from OpenAI');
8017
- }
8018
- return [2 /*return*/, {
8019
- content: resultContent,
8020
- modelName: rawResponse.model || modelName,
8021
- timing: {
8022
- start: start,
8023
- complete: complete,
8024
- },
8025
- usage: usage,
8026
- rawPromptContent: rawPromptContent,
8027
- rawRequest: rawRequest,
8028
- rawResponse: rawResponse,
8029
- // <- [🗯]
8030
- }];
8031
- }
8032
- });
8033
- });
8034
- };
8035
- /**
8036
- * Calls OpenAI API to use a complete model.
8037
- */
8038
- OpenAiExecutionTools.prototype.callCompletionModel = function (prompt) {
8039
- return __awaiter(this, void 0, void 0, function () {
8040
- var content, parameters, modelRequirements, client, modelName, modelSettings, rawPromptContent, rawRequest, start, complete, rawResponse, resultContent, usage;
8041
- return __generator(this, function (_a) {
8042
- switch (_a.label) {
8043
- case 0:
8044
- if (this.options.isVerbose) {
8045
- console.info('🖋 OpenAI callCompletionModel call', { prompt: prompt });
8046
- }
8047
- content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements;
8048
- return [4 /*yield*/, this.getClient()];
8049
- case 1:
8050
- client = _a.sent();
8051
- // TODO: [☂] Use here more modelRequirements
8052
- if (modelRequirements.modelVariant !== 'COMPLETION') {
8053
- throw new PipelineExecutionError('Use callCompletionModel only for COMPLETION variant');
8054
- }
8055
- modelName = modelRequirements.modelName || this.getDefaultCompletionModel().modelName;
8056
- modelSettings = {
8057
- model: modelName,
8058
- max_tokens: modelRequirements.maxTokens || 2000,
8059
- // <- TODO: [🌾] Make some global max cap for maxTokens
8060
- temperature: modelRequirements.temperature,
8061
- // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
8062
- // <- Note: [🧆]
8063
- };
8064
- rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
8065
- rawRequest = __assign(__assign({}, modelSettings), { prompt: rawPromptContent, user: this.options.user });
8066
- start = getCurrentIsoDate();
8067
- if (this.options.isVerbose) {
8068
- console.info(colors.bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
8069
- }
8070
- return [4 /*yield*/, client.completions.create(rawRequest)];
8071
- case 2:
8072
- rawResponse = _a.sent();
8073
- if (this.options.isVerbose) {
8074
- console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
8075
- }
8076
- if (!rawResponse.choices[0]) {
8077
- throw new PipelineExecutionError('No choises from OpenAI');
8078
- }
8079
- if (rawResponse.choices.length > 1) {
8080
- // TODO: This should be maybe only warning
8081
- throw new PipelineExecutionError('More than one choise from OpenAI');
8082
- }
8083
- resultContent = rawResponse.choices[0].text;
8084
- // eslint-disable-next-line prefer-const
8085
- complete = getCurrentIsoDate();
8086
- usage = computeOpenAiUsage(content, resultContent || '', rawResponse);
8087
- return [2 /*return*/, {
8088
- content: resultContent,
8089
- modelName: rawResponse.model || modelName,
8090
- timing: {
8091
- start: start,
8092
- complete: complete,
8093
- },
8094
- usage: usage,
8095
- rawPromptContent: rawPromptContent,
8096
- rawRequest: rawRequest,
8097
- rawResponse: rawResponse,
8098
- // <- [🗯]
8099
- }];
8100
- }
8101
- });
8102
- });
8103
- };
8104
- /**
8105
- * Calls OpenAI API to use a embedding model
8106
- */
8107
- OpenAiExecutionTools.prototype.callEmbeddingModel = function (prompt) {
8108
- return __awaiter(this, void 0, void 0, function () {
8109
- var content, parameters, modelRequirements, client, modelName, rawPromptContent, rawRequest, start, complete, rawResponse, resultContent, usage;
8110
- return __generator(this, function (_a) {
8111
- switch (_a.label) {
8112
- case 0:
8113
- if (this.options.isVerbose) {
8114
- console.info('🖋 OpenAI embedding call', { prompt: prompt });
8115
- }
8116
- content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements;
8117
- return [4 /*yield*/, this.getClient()];
8118
- case 1:
8119
- client = _a.sent();
8120
- // TODO: [☂] Use here more modelRequirements
8121
- if (modelRequirements.modelVariant !== 'EMBEDDING') {
8122
- throw new PipelineExecutionError('Use embed only for EMBEDDING variant');
8123
- }
8124
- modelName = modelRequirements.modelName || this.getDefaultEmbeddingModel().modelName;
8125
- rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
8126
- rawRequest = {
8127
- input: rawPromptContent,
8128
- model: modelName,
8129
- };
8130
- start = getCurrentIsoDate();
8131
- if (this.options.isVerbose) {
8132
- console.info(colors.bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
8133
- }
8134
- return [4 /*yield*/, client.embeddings.create(rawRequest)];
8135
- case 2:
8136
- rawResponse = _a.sent();
8137
- if (this.options.isVerbose) {
8138
- console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
8139
- }
8140
- if (rawResponse.data.length !== 1) {
8141
- throw new PipelineExecutionError("Expected exactly 1 data item in response, got ".concat(rawResponse.data.length));
8142
- }
8143
- resultContent = rawResponse.data[0].embedding;
8144
- // eslint-disable-next-line prefer-const
8145
- complete = getCurrentIsoDate();
8146
- usage = computeOpenAiUsage(content, '', rawResponse);
8147
- return [2 /*return*/, {
8148
- content: resultContent,
8149
- modelName: rawResponse.model || modelName,
8150
- timing: {
8151
- start: start,
8152
- complete: complete,
8153
- },
8154
- usage: usage,
8155
- rawPromptContent: rawPromptContent,
8156
- rawRequest: rawRequest,
8157
- rawResponse: rawResponse,
8158
- // <- [🗯]
8159
- }];
8160
- }
8161
- });
8162
- });
8163
- };
8164
- // <- Note: [🤖] callXxxModel
8165
- /**
8166
- * Get the model that should be used as default
8167
- */
8168
- OpenAiExecutionTools.prototype.getDefaultModel = function (defaultModelName) {
8169
- var model = OPENAI_MODELS.find(function (_a) {
8170
- var modelName = _a.modelName;
8171
- return modelName === defaultModelName;
8172
- });
8173
- if (model === undefined) {
8174
- throw new UnexpectedError(spaceTrim(function (block) {
8175
- return "\n Cannot find model in OpenAI models with name \"".concat(defaultModelName, "\" which should be used as default.\n\n Available models:\n ").concat(block(OPENAI_MODELS.map(function (_a) {
8176
- var modelName = _a.modelName;
8177
- return "- \"".concat(modelName, "\"");
8178
- }).join('\n')), "\n\n ");
8179
- }));
8180
- }
8181
- return model;
8182
- };
8183
- /**
8184
- * Default model for chat variant.
8185
- */
8186
- OpenAiExecutionTools.prototype.getDefaultChatModel = function () {
8187
- return this.getDefaultModel('gpt-4o');
8188
- };
8189
- /**
8190
- * Default model for completion variant.
8191
- */
8192
- OpenAiExecutionTools.prototype.getDefaultCompletionModel = function () {
8193
- return this.getDefaultModel('gpt-3.5-turbo-instruct');
8194
- };
8195
- /**
8196
- * Default model for completion variant.
8197
- */
8198
- OpenAiExecutionTools.prototype.getDefaultEmbeddingModel = function () {
8199
- return this.getDefaultModel('text-embedding-3-large');
8200
- };
8201
- return OpenAiExecutionTools;
8202
- }());
8203
- /**
8204
- * TODO: [🧠][🧙‍♂️] Maybe there can be some wizzard for thoose who want to use just OpenAI
8205
- * TODO: Maybe Create some common util for callChatModel and callCompletionModel
8206
- * TODO: Maybe make custom OpenAiError
8207
- * TODO: [🧠][🈁] Maybe use `isDeterministic` from options
8208
- * TODO: [🧠][🌰] Allow to pass `title` for tracking purposes
8209
- */
8210
-
8211
- /**
8212
- * Execution Tools for calling OpenAI API
8213
- *
8214
- * @public exported from `@promptbook/openai`
8215
- */
8216
- var createOpenAiExecutionTools = Object.assign(function (options) {
8217
- // TODO: !!!!!! If browser, auto add `dangerouslyAllowBrowser`
8218
- return new OpenAiExecutionTools(options);
8219
- }, {
8220
- packageName: '@promptbook/openai',
8221
- className: 'OpenAiExecutionTools',
8222
- });
8223
- /**
8224
- * TODO: [🦺] Is there some way how to put `packageName` and `className` on top and function definition on bottom?
8225
- * TODO: [🎶] Naming "constructor" vs "creator" vs "factory"
8226
- */
8227
-
8228
- /**
8229
- * @@@
8230
- *
8231
- * TODO: !!!!!! Remove EXECUTION_TOOLS_CLASSES and use $llmToolsRegister instead
8232
- *
8233
- * @private internal type for `createLlmToolsFromConfiguration`
8234
- */
8235
- var EXECUTION_TOOLS_CLASSES = {
8236
- createOpenAiExecutionTools: createOpenAiExecutionTools,
8237
- createAnthropicClaudeExecutionTools: createAnthropicClaudeExecutionTools,
8238
- createAzureOpenAiExecutionTools: function (options) {
8239
- return new AzureOpenAiExecutionTools(
8240
- // <- TODO: [🧱] Implement in a functional (not new Class) way
8241
- options);
8242
- },
8243
- // <- Note: [🦑] Add here new LLM provider
8244
- };
8245
- /**
8246
- * TODO: !!!!!!! Make global register for this
8247
- * TODO: [🧠][🎌] Adding this should be responsibility of each provider package NOT this one central place
8248
- */
8249
-
8250
- /**
8251
- * @@@
8252
- *
8253
- * Note: This function is not cached, every call creates new instance of `MultipleLlmExecutionTools`
8254
- *
8255
- * @returns @@@
8256
- * @public exported from `@promptbook/core`
8257
- */
8258
- function createLlmToolsFromConfiguration(configuration, options) {
8259
- if (options === void 0) { options = {}; }
8260
- var _a = options.isVerbose, isVerbose = _a === void 0 ? false : _a;
8261
- var llmTools = configuration.map(function (llmConfiguration) {
8262
- var constructor = EXECUTION_TOOLS_CLASSES["create".concat(llmConfiguration.className)];
8263
- if (!constructor) {
8264
- throw new Error(spaceTrim(function (block) { return "\n There is no constructor for LLM provider `".concat(llmConfiguration.className, "`\n\n\n @@@\n\n Available constructors are:\n ").concat(block('@@@'), "\n\n\n "); }));
8265
- }
8266
- return constructor(__assign({ isVerbose: isVerbose }, llmConfiguration.options));
8267
- });
8268
- return joinLlmExecutionTools.apply(void 0, __spreadArray([], __read(llmTools), false));
8269
- }
8270
- /**
8271
- * TODO: [🎌] Togethere with `createLlmToolsFromConfiguration` + 'EXECUTION_TOOLS_CLASSES' gets to `@promptbook/core` ALL model providers, make this more efficient
8272
- * TODO: [🧠][🎌] Dynamically install required providers
8273
- * TODO: @@@ write discussion about this - wizzard
8274
- * TODO: [🧠][🍛] Which name is better `createLlmToolsFromConfig` or `createLlmToolsFromConfiguration`?
8275
- * TODO: [🧠] Is there some meaningfull way how to test this util
8276
- * TODO: This should be maybe not under `_common` but under `utils`
8277
- */
8278
-
8279
- /**
8280
- * Stores
8281
- *
8282
- * @public exported from `@promptbook/core`
8283
- */
8284
- var MemoryStorage = /** @class */ (function () {
8285
- function MemoryStorage() {
8286
- this.storage = {};
8287
- }
8288
- Object.defineProperty(MemoryStorage.prototype, "length", {
8289
- /**
8290
- * Returns the number of key/value pairs currently present in the list associated with the object.
8291
- */
8292
- get: function () {
8293
- return Object.keys(this.storage).length;
8294
- },
8295
- enumerable: false,
8296
- configurable: true
8297
- });
8298
- /**
8299
- * Empties the list associated with the object of all key/value pairs, if there are any.
8300
- */
8301
- MemoryStorage.prototype.clear = function () {
8302
- this.storage = {};
8303
- };
8304
- /**
8305
- * Returns the current value associated with the given key, or null if the given key does not exist in the list associated with the object.
8306
- */
8307
- MemoryStorage.prototype.getItem = function (key) {
8308
- return this.storage[key] || null;
8309
- };
8310
- /**
8311
- * Returns the name of the nth key in the list, or null if n is greater than or equal to the number of key/value pairs in the object.
8312
- */
8313
- MemoryStorage.prototype.key = function (index) {
8314
- return Object.keys(this.storage)[index] || null;
8315
- };
8316
- /**
8317
- * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
6782
+ * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
8318
6783
  */
8319
6784
  MemoryStorage.prototype.setItem = function (key, value) {
8320
6785
  this.storage[key] = value;
@@ -8534,7 +6999,7 @@ var _AnthropicClaudeMetadataRegistration = $llmToolsMetadataRegister.register({
8534
6999
  if (typeof env.ANTHROPIC_CLAUDE_API_KEY === 'string') {
8535
7000
  return {
8536
7001
  title: 'Claude (from env)',
8537
- packageName: '@promptbook/antrhopic-claude',
7002
+ packageName: '@promptbook/anthropic-claude',
8538
7003
  className: 'AnthropicClaudeExecutionTools',
8539
7004
  options: {
8540
7005
  apiKey: process.env.ANTHROPIC_CLAUDE_API_KEY,
@@ -8545,6 +7010,52 @@ var _AnthropicClaudeMetadataRegistration = $llmToolsMetadataRegister.register({
8545
7010
  },
8546
7011
  });
8547
7012
 
7013
+ /**
7014
+ * @@@ registration1 of default configuration for Azure Open AI
7015
+ *
7016
+ * Note: [🏐] Configurations registrations are done in @@@ BUT constructor @@@
7017
+ *
7018
+ * @public exported from `@promptbook/core`
7019
+ * @public exported from `@promptbook/cli`
7020
+ */
7021
+ var _AzureOpenAiMetadataRegistration = $llmToolsMetadataRegister.register({
7022
+ title: 'Azure Open AI',
7023
+ packageName: '@promptbook/azure-openai',
7024
+ className: 'AzureOpenAiExecutionTools',
7025
+ getBoilerplateConfiguration: function () {
7026
+ return {
7027
+ title: 'Azure Open AI (boilerplate)',
7028
+ packageName: '@promptbook/azure-openai',
7029
+ className: 'AzureOpenAiExecutionTools',
7030
+ options: {
7031
+ apiKey: 'sk-',
7032
+ },
7033
+ };
7034
+ },
7035
+ createConfigurationFromEnv: function (env) {
7036
+ if (typeof env.AZUREOPENAI_RESOURCE_NAME === 'string' &&
7037
+ typeof env.AZUREOPENAI_DEPLOYMENT_NAME === 'string' &&
7038
+ typeof env.AZUREOPENAI_API_KEY === 'string') {
7039
+ return {
7040
+ title: 'Azure Open AI (from env)',
7041
+ packageName: '@promptbook/azure-openai',
7042
+ className: 'AzureOpenAiExecutionTools',
7043
+ options: {
7044
+ resourceName: env.AZUREOPENAI_RESOURCE_NAME,
7045
+ deploymentName: env.AZUREOPENAI_DEPLOYMENT_NAME,
7046
+ apiKey: env.AZUREOPENAI_API_KEY,
7047
+ },
7048
+ };
7049
+ }
7050
+ else if (typeof env.AZUREOPENAI_RESOURCE_NAME === 'string' ||
7051
+ typeof env.AZUREOPENAI_DEPLOYMENT_NAME === 'string' ||
7052
+ typeof env.AZUREOPENAI_API_KEY === 'string') {
7053
+ throw new Error(spaceTrim("\n You must provide all of the following environment variables:\n \n - AZUREOPENAI_RESOURCE_NAME (".concat(typeof env.AZUREOPENAI_RESOURCE_NAME === 'string' ? 'defined' : 'not defined', ")\n - AZUREOPENAI_DEPLOYMENT_NAME (").concat(typeof env.AZUREOPENAI_DEPLOYMENT_NAME === 'string' ? 'defined' : 'not defined', ")\n - AZUREOPENAI_API_KEY (").concat(typeof env.AZUREOPENAI_API_KEY === 'string' ? 'defined' : 'not defined', ") \n ")));
7054
+ }
7055
+ return null;
7056
+ },
7057
+ });
7058
+
8548
7059
  /**
8549
7060
  * @@@ registration1 of default configuration for Open AI
8550
7061
  *
@@ -8554,9 +7065,9 @@ var _AnthropicClaudeMetadataRegistration = $llmToolsMetadataRegister.register({
8554
7065
  * @public exported from `@promptbook/cli`
8555
7066
  */
8556
7067
  var _OpenAiMetadataRegistration = $llmToolsMetadataRegister.register({
8557
- title: 'Anthropic Claude',
8558
- packageName: '@promptbook/anthropic-claude',
8559
- className: 'AnthropicClaudeExecutionTools',
7068
+ title: 'Open AI',
7069
+ packageName: '@promptbook/openai',
7070
+ className: 'OpenAiExecutionTools',
8560
7071
  getBoilerplateConfiguration: function () {
8561
7072
  return {
8562
7073
  title: 'Open AI (boilerplate)',
@@ -8932,5 +7443,5 @@ function executionReportJsonToString(executionReportJson, options) {
8932
7443
  * TODO: [🧠] Should be in generated file GENERATOR_WARNING
8933
7444
  */
8934
7445
 
8935
- export { $llmToolsMetadataRegister, $llmToolsRegister, BlockTypes, CLAIM, CallbackInterfaceTools, CollectionError, DEFAULT_REMOTE_URL, DEFAULT_REMOTE_URL_PATH, EXECUTIONS_CACHE_DIRNAME, EXPECTATION_UNITS, EnvironmentMismatchError, ExecutionReportStringOptionsDefaults, LimitReachedError, MAX_EXECUTION_ATTEMPTS, MAX_FILENAME_LENGTH, MAX_KNOWLEDGE_SOURCES_SCRAPING_DEPTH, MAX_KNOWLEDGE_SOURCES_SCRAPING_TOTAL, MAX_PARALLEL_COUNT, MODEL_VARIANTS, MemoryStorage, NotFoundError, NotYetImplementedError, PIPELINE_COLLECTION_BASE_FILENAME, PROMPTBOOK_VERSION, ParsingError, PipelineExecutionError, PipelineLogicError, PrefixStorage, RESERVED_PARAMETER_NAMES, ReferenceError$1 as ReferenceError, UnexpectedError, VersionMismatchError, ZERO_USAGE, _AnthropicClaudeMetadataRegistration, _OpenAiMetadataRegistration, addUsage, assertsExecutionSuccessful, cacheLlmTools, collectionToJson, countTotalUsage, createCollectionFromJson, createCollectionFromPromise, createCollectionFromUrl, createLlmToolsFromConfiguration, createPipelineExecutor, createSubcollection, embeddingVectorToString, executionReportJsonToString, isPassingExpectations, isPipelinePrepared, joinLlmExecutionTools, limitTotalUsage, pipelineJsonToString, pipelineStringToJson, pipelineStringToJsonSync, prepareKnowledgeFromMarkdown, prepareKnowledgePieces, preparePersona, preparePipeline, prepareTemplates, prettifyPipelineString, stringifyPipelineJson, unpreparePipeline, usageToHuman, usageToWorktime, validatePipeline };
7446
+ export { $llmToolsMetadataRegister, $llmToolsRegister, BlockTypes, CLAIM, CallbackInterfaceTools, CollectionError, DEFAULT_REMOTE_URL, DEFAULT_REMOTE_URL_PATH, EXECUTIONS_CACHE_DIRNAME, EXPECTATION_UNITS, EnvironmentMismatchError, ExecutionReportStringOptionsDefaults, LimitReachedError, MAX_EXECUTION_ATTEMPTS, MAX_FILENAME_LENGTH, MAX_KNOWLEDGE_SOURCES_SCRAPING_DEPTH, MAX_KNOWLEDGE_SOURCES_SCRAPING_TOTAL, MAX_PARALLEL_COUNT, MODEL_VARIANTS, MemoryStorage, NotFoundError, NotYetImplementedError, PIPELINE_COLLECTION_BASE_FILENAME, PROMPTBOOK_VERSION, ParsingError, PipelineExecutionError, PipelineLogicError, PrefixStorage, RESERVED_PARAMETER_NAMES, ReferenceError$1 as ReferenceError, UnexpectedError, VersionMismatchError, ZERO_USAGE, _AnthropicClaudeMetadataRegistration, _AzureOpenAiMetadataRegistration, _OpenAiMetadataRegistration, addUsage, assertsExecutionSuccessful, cacheLlmTools, collectionToJson, countTotalUsage, createCollectionFromJson, createCollectionFromPromise, createCollectionFromUrl, createLlmToolsFromConfiguration, createPipelineExecutor, createSubcollection, embeddingVectorToString, executionReportJsonToString, isPassingExpectations, isPipelinePrepared, joinLlmExecutionTools, limitTotalUsage, pipelineJsonToString, pipelineStringToJson, pipelineStringToJsonSync, prepareKnowledgeFromMarkdown, prepareKnowledgePieces, preparePersona, preparePipeline, prepareTemplates, prettifyPipelineString, stringifyPipelineJson, unpreparePipeline, usageToHuman, usageToWorktime, validatePipeline };
8936
7447
  //# sourceMappingURL=index.es.js.map