@promptbook/core 0.65.0-1 → 0.65.0-3

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 (26) hide show
  1. package/esm/index.es.js +1540 -3
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/src/_packages/anthropic-claude.index.d.ts +6 -0
  4. package/esm/typings/src/_packages/core.index.d.ts +4 -0
  5. package/esm/typings/src/_packages/node.index.d.ts +0 -4
  6. package/esm/typings/src/_packages/remote-client.index.d.ts +2 -2
  7. package/esm/typings/src/_packages/types.index.d.ts +16 -2
  8. package/esm/typings/src/llm-providers/_common/config.d.ts +3 -3
  9. package/esm/typings/src/llm-providers/_common/createLlmToolsFromConfiguration.d.ts +2 -2
  10. package/esm/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionTools.d.ts +5 -3
  11. package/esm/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionToolsOptions.d.ts +23 -2
  12. package/esm/typings/src/llm-providers/anthropic-claude/createAnthropicClaudeExecutionTools.d.ts +13 -0
  13. package/esm/typings/src/llm-providers/anthropic-claude/playground/playground.d.ts +1 -1
  14. package/esm/typings/src/llm-providers/remote/RemoteLlmExecutionTools.d.ts +3 -2
  15. package/esm/typings/src/llm-providers/remote/interfaces/Promptbook_Server_Error.d.ts +2 -2
  16. package/esm/typings/src/llm-providers/remote/interfaces/Promptbook_Server_Progress.d.ts +2 -2
  17. package/esm/typings/src/llm-providers/remote/interfaces/Promptbook_Server_Request.d.ts +14 -2
  18. package/esm/typings/src/llm-providers/remote/interfaces/RemoteLlmExecutionToolsOptions.d.ts +49 -0
  19. package/esm/typings/src/llm-providers/remote/interfaces/RemoteServerOptions.d.ts +23 -2
  20. package/esm/typings/src/llm-providers/remote/playground/playground.d.ts +2 -0
  21. package/esm/typings/src/llm-providers/remote/startRemoteServer.d.ts +2 -1
  22. package/esm/typings/src/types/typeAliases.d.ts +6 -0
  23. package/package.json +6 -1
  24. package/umd/index.umd.js +1561 -6
  25. package/umd/index.umd.js.map +1 -1
  26. package/esm/typings/src/llm-providers/remote/RemoteLlmExecutionToolsOptions.d.ts +0 -26
package/umd/index.umd.js CHANGED
@@ -1,22 +1,44 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('crypto-js/enc-hex'), require('crypto-js/sha256'), require('moment')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'crypto-js/enc-hex', 'crypto-js/sha256', 'moment'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim, global.prettier, global.parserHtml, global.hexEncoder, global.sha256, global.moment));
5
- })(this, (function (exports, spaceTrim, prettier, parserHtml, hexEncoder, sha256, moment) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('crypto-js/enc-hex'), require('crypto-js/sha256'), require('socket.io-client'), require('@anthropic-ai/sdk'), require('colors'), require('@azure/openai'), require('openai'), require('dotenv'), require('moment')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'spacetrim', 'prettier', 'prettier/parser-html', 'crypto-js/enc-hex', 'crypto-js/sha256', 'socket.io-client', '@anthropic-ai/sdk', 'colors', '@azure/openai', 'openai', 'dotenv', 'moment'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-core"] = {}, global.spaceTrim, global.prettier, global.parserHtml, global.hexEncoder, global.sha256, global.socket_ioClient, global.Anthropic, global.colors, global.openai, global.OpenAI, global.dotenv, global.moment));
5
+ })(this, (function (exports, spaceTrim, prettier, parserHtml, hexEncoder, sha256, socket_ioClient, Anthropic, colors, openai, OpenAI, dotenv, moment) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
9
+ function _interopNamespace(e) {
10
+ if (e && e.__esModule) return e;
11
+ var n = Object.create(null);
12
+ if (e) {
13
+ Object.keys(e).forEach(function (k) {
14
+ if (k !== 'default') {
15
+ var d = Object.getOwnPropertyDescriptor(e, k);
16
+ Object.defineProperty(n, k, d.get ? d : {
17
+ enumerable: true,
18
+ get: function () { return e[k]; }
19
+ });
20
+ }
21
+ });
22
+ }
23
+ n["default"] = e;
24
+ return Object.freeze(n);
25
+ }
26
+
9
27
  var spaceTrim__default = /*#__PURE__*/_interopDefaultLegacy(spaceTrim);
10
28
  var parserHtml__default = /*#__PURE__*/_interopDefaultLegacy(parserHtml);
11
29
  var hexEncoder__default = /*#__PURE__*/_interopDefaultLegacy(hexEncoder);
12
30
  var sha256__default = /*#__PURE__*/_interopDefaultLegacy(sha256);
31
+ var Anthropic__default = /*#__PURE__*/_interopDefaultLegacy(Anthropic);
32
+ var colors__default = /*#__PURE__*/_interopDefaultLegacy(colors);
33
+ var OpenAI__default = /*#__PURE__*/_interopDefaultLegacy(OpenAI);
34
+ var dotenv__namespace = /*#__PURE__*/_interopNamespace(dotenv);
13
35
  var moment__default = /*#__PURE__*/_interopDefaultLegacy(moment);
14
36
 
15
37
  // ⚠️ WARNING: This code has been generated so that any manual changes will be overwritten
16
38
  /**
17
39
  * The version of the Promptbook library
18
40
  */
19
- var PROMPTBOOK_VERSION = '0.65.0-0';
41
+ var PROMPTBOOK_VERSION = '0.65.0-2';
20
42
  // TODO: !!!! List here all the versions and annotate + put into script
21
43
 
22
44
  /*! *****************************************************************************
@@ -1631,7 +1653,7 @@
1631
1653
  });
1632
1654
  }
1633
1655
 
1634
- var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.65.0-0",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.65.0-0",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.65.0-0",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.65.0-0",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"}];
1656
+ var PipelineCollection = [{title:"Prepare Knowledge from Markdown",pipelineUrl:"https://promptbook.studio/promptbook/prepare-knowledge-from-markdown.ptbk.md",promptbookVersion:"0.65.0-2",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.65.0-2",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.65.0-2",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.65.0-2",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"}];
1635
1657
 
1636
1658
  var defaultDiacriticsRemovalMap = [
1637
1659
  {
@@ -6479,6 +6501,1537 @@
6479
6501
  return CallbackInterfaceTools;
6480
6502
  }());
6481
6503
 
6504
+ /**
6505
+ * Remote server is a proxy server that uses its execution tools internally and exposes the executor interface externally.
6506
+ *
6507
+ * You can simply use `RemoteExecutionTools` on client-side javascript and connect to your remote server.
6508
+ * This is useful to make all logic on browser side but not expose your API keys or no need to use customer's GPU.
6509
+ *
6510
+ * @see https://github.com/webgptorg/promptbook#remote-server
6511
+ * @public exported from `@promptbook/remote-client`
6512
+ */
6513
+ var RemoteLlmExecutionTools = /** @class */ (function () {
6514
+ function RemoteLlmExecutionTools(options) {
6515
+ this.options = options;
6516
+ }
6517
+ Object.defineProperty(RemoteLlmExecutionTools.prototype, "title", {
6518
+ get: function () {
6519
+ // TODO: [🧠] Maybe fetch title+description from the remote server (as well as if model methods are defined)
6520
+ return 'Remote server';
6521
+ },
6522
+ enumerable: false,
6523
+ configurable: true
6524
+ });
6525
+ Object.defineProperty(RemoteLlmExecutionTools.prototype, "description", {
6526
+ get: function () {
6527
+ return 'Use all models by your remote server';
6528
+ },
6529
+ enumerable: false,
6530
+ configurable: true
6531
+ });
6532
+ /**
6533
+ * Creates a connection to the remote proxy server.
6534
+ */
6535
+ RemoteLlmExecutionTools.prototype.makeConnection = function () {
6536
+ var _this = this;
6537
+ return new Promise(function (resolve, reject) {
6538
+ var socket = socket_ioClient.io(_this.options.remoteUrl, {
6539
+ path: _this.options.path,
6540
+ // path: `${this.remoteUrl.pathname}/socket.io`,
6541
+ transports: [/*'websocket', <- TODO: [🌬] Make websocket transport work */ 'polling'],
6542
+ });
6543
+ // console.log('Connecting to', this.options.remoteUrl.href, { socket });
6544
+ socket.on('connect', function () {
6545
+ resolve(socket);
6546
+ });
6547
+ setTimeout(function () {
6548
+ reject(new Error("Timeout while connecting to ".concat(_this.options.remoteUrl)));
6549
+ }, 60000 /* <- TODO: Timeout to config */);
6550
+ });
6551
+ };
6552
+ /**
6553
+ * Calls remote proxy server to use a chat model
6554
+ */
6555
+ RemoteLlmExecutionTools.prototype.callChatModel = function (prompt) {
6556
+ if (this.options.isVerbose) {
6557
+ console.info("\uD83D\uDD8B Remote callChatModel call");
6558
+ }
6559
+ return /* not await */ this.callCommonModel(prompt);
6560
+ };
6561
+ /**
6562
+ * Calls remote proxy server to use a completion model
6563
+ */
6564
+ RemoteLlmExecutionTools.prototype.callCompletionModel = function (prompt) {
6565
+ if (this.options.isVerbose) {
6566
+ console.info("\uD83D\uDCAC Remote callCompletionModel call");
6567
+ }
6568
+ return /* not await */ this.callCommonModel(prompt);
6569
+ };
6570
+ /**
6571
+ * Calls remote proxy server to use a embedding model
6572
+ */
6573
+ RemoteLlmExecutionTools.prototype.callEmbeddingModel = function (prompt) {
6574
+ if (this.options.isVerbose) {
6575
+ console.info("\uD83D\uDCAC Remote callEmbeddingModel call");
6576
+ }
6577
+ return /* not await */ this.callCommonModel(prompt);
6578
+ };
6579
+ // <- Note: [🤖] callXxxModel
6580
+ /**
6581
+ * Calls remote proxy server to use both completion or chat model
6582
+ */
6583
+ RemoteLlmExecutionTools.prototype.callCommonModel = function (prompt) {
6584
+ return __awaiter(this, void 0, void 0, function () {
6585
+ var socket, promptResult;
6586
+ return __generator(this, function (_a) {
6587
+ switch (_a.label) {
6588
+ case 0: return [4 /*yield*/, this.makeConnection()];
6589
+ case 1:
6590
+ socket = _a.sent();
6591
+ if (this.options.isAnonymous) {
6592
+ socket.emit('request', {
6593
+ llmToolsConfiguration: this.options.llmToolsConfiguration,
6594
+ prompt: prompt,
6595
+ // <- TODO: [🛫] `prompt` is NOT fully serializable as JSON, it contains functions which are not serializable
6596
+ });
6597
+ }
6598
+ else {
6599
+ socket.emit('request', {
6600
+ clientId: this.options.clientId,
6601
+ prompt: prompt,
6602
+ // <- TODO: [🛫] `prompt` is NOT fully serializable as JSON, it contains functions which are not serializable
6603
+ });
6604
+ }
6605
+ return [4 /*yield*/, new Promise(function (resolve, reject) {
6606
+ socket.on('response', function (response) {
6607
+ resolve(response.promptResult);
6608
+ socket.disconnect();
6609
+ });
6610
+ socket.on('error', function (error) {
6611
+ reject(new PipelineExecutionError(error.errorMessage));
6612
+ socket.disconnect();
6613
+ });
6614
+ })];
6615
+ case 2:
6616
+ promptResult = _a.sent();
6617
+ socket.disconnect();
6618
+ return [2 /*return*/, promptResult];
6619
+ }
6620
+ });
6621
+ });
6622
+ };
6623
+ /**
6624
+ * List all available models that can be used
6625
+ */
6626
+ RemoteLlmExecutionTools.prototype.listModels = function () {
6627
+ return __awaiter(this, void 0, void 0, function () {
6628
+ return __generator(this, function (_a) {
6629
+ return [2 /*return*/, [
6630
+ /* !!! */
6631
+ ]];
6632
+ });
6633
+ });
6634
+ };
6635
+ return RemoteLlmExecutionTools;
6636
+ }());
6637
+ /**
6638
+ * TODO: [🍜] !!!!!! Default remote remoteUrl and path for anonymous server
6639
+ * TODO: [🍓] Allow to list compatible models with each variant
6640
+ * TODO: [🗯] RemoteLlmExecutionTools should extend Destroyable and implement IDestroyable
6641
+ * TODO: [🧠][🌰] Allow to pass `title` for tracking purposes
6642
+ * TODO: [🧠] Maybe remove `@promptbook/remote-client` and just use `@promptbook/core`
6643
+ */
6644
+
6645
+ /**
6646
+ * Helper of usage compute
6647
+ *
6648
+ * @param content the content of prompt or response
6649
+ * @returns part of PromptResultUsageCounts
6650
+ *
6651
+ * @private internal utility of LlmExecutionTools
6652
+ */
6653
+ function computeUsageCounts(content) {
6654
+ return {
6655
+ charactersCount: { value: countCharacters(content) },
6656
+ wordsCount: { value: countWords(content) },
6657
+ sentencesCount: { value: countSentences(content) },
6658
+ linesCount: { value: countLines(content) },
6659
+ paragraphsCount: { value: countParagraphs(content) },
6660
+ pagesCount: { value: countPages(content) },
6661
+ };
6662
+ }
6663
+
6664
+ /**
6665
+ * Make UncertainNumber
6666
+ *
6667
+ * @param value
6668
+ *
6669
+ * @private utility for initializating UncertainNumber
6670
+ */
6671
+ function uncertainNumber(value) {
6672
+ if (value === null || value === undefined || Number.isNaN(value)) {
6673
+ return { value: 0, isUncertain: true };
6674
+ }
6675
+ return { value: value };
6676
+ }
6677
+
6678
+ /**
6679
+ * Get current date in ISO 8601 format
6680
+ *
6681
+ * @private internal utility
6682
+ */
6683
+ function getCurrentIsoDate() {
6684
+ return new Date().toISOString();
6685
+ }
6686
+
6687
+ /**
6688
+ * Function computeUsage will create price per one token based on the string value found on openai page
6689
+ *
6690
+ * @private within the repository, used only as internal helper for `OPENAI_MODELS`
6691
+ */
6692
+ function computeUsage(value) {
6693
+ var _a = __read(value.split(' / '), 2), price = _a[0], tokens = _a[1];
6694
+ return parseFloat(price.replace('$', '')) / parseFloat(tokens.replace('M tokens', '')) / 1000000;
6695
+ }
6696
+
6697
+ /**
6698
+ * List of available Anthropic Claude models with pricing
6699
+ *
6700
+ * Note: Done at 2024-05-25
6701
+ *
6702
+ * @see https://docs.anthropic.com/en/docs/models-overview
6703
+ * @public exported from `@promptbook/anthropic-claude`
6704
+ */
6705
+ var ANTHROPIC_CLAUDE_MODELS = [
6706
+ {
6707
+ modelVariant: 'CHAT',
6708
+ modelTitle: 'Claude 3 Opus',
6709
+ modelName: 'claude-3-opus-20240229',
6710
+ pricing: {
6711
+ prompt: computeUsage("$15.00 / 1M tokens"),
6712
+ output: computeUsage("$75.00 / 1M tokens"),
6713
+ },
6714
+ },
6715
+ {
6716
+ modelVariant: 'CHAT',
6717
+ modelTitle: 'Claude 3 Sonnet',
6718
+ modelName: 'claude-3-sonnet-20240229',
6719
+ pricing: {
6720
+ prompt: computeUsage("$3.00 / 1M tokens"),
6721
+ output: computeUsage("$15.00 / 1M tokens"),
6722
+ },
6723
+ },
6724
+ {
6725
+ modelVariant: 'CHAT',
6726
+ modelTitle: 'Claude 3 Haiku',
6727
+ modelName: ' claude-3-haiku-20240307',
6728
+ pricing: {
6729
+ prompt: computeUsage("$0.25 / 1M tokens"),
6730
+ output: computeUsage("$1.25 / 1M tokens"),
6731
+ },
6732
+ },
6733
+ {
6734
+ modelVariant: 'CHAT',
6735
+ modelTitle: 'Claude 2.1',
6736
+ modelName: 'claude-2.1',
6737
+ pricing: {
6738
+ prompt: computeUsage("$8.00 / 1M tokens"),
6739
+ output: computeUsage("$24.00 / 1M tokens"),
6740
+ },
6741
+ },
6742
+ {
6743
+ modelVariant: 'CHAT',
6744
+ modelTitle: 'Claude 2',
6745
+ modelName: 'claude-2.0',
6746
+ pricing: {
6747
+ prompt: computeUsage("$8.00 / 1M tokens"),
6748
+ output: computeUsage("$24.00 / 1M tokens"),
6749
+ },
6750
+ },
6751
+ {
6752
+ modelVariant: 'CHAT',
6753
+ modelTitle: ' Claude Instant 1.2',
6754
+ modelName: 'claude-instant-1.2',
6755
+ pricing: {
6756
+ prompt: computeUsage("$0.80 / 1M tokens"),
6757
+ output: computeUsage("$2.40 / 1M tokens"),
6758
+ },
6759
+ },
6760
+ // TODO: !!! Claude 1 and 2 has also completion versions - ask Hoagy
6761
+ ];
6762
+ /**
6763
+ * Note: [🤖] Add models of new variant
6764
+ * TODO: [🧠] !!! Add embedding models OR Anthropic has only chat+completion models?
6765
+ * TODO: [🧠] Some mechanism to propagate unsureness
6766
+ * TODO: [🧠][👮‍♀️] Put here more info like description, isVision, trainingDateCutoff, languages, strengths ( Top-level performance, intelligence, fluency, and understanding), contextWindow,...
6767
+ * TODO: [🕚] Make this list dynamic - dynamically can be listed modelNames but not modelVariant, legacy status, context length and pricing
6768
+ */
6769
+
6770
+ /**
6771
+ * Execution Tools for calling Anthropic Claude API.
6772
+ *
6773
+ * @public exported from `@promptbook/anthropic-claude`
6774
+ * @deprecated use `createAnthropicClaudeExecutionTools` instead
6775
+ */
6776
+ var AnthropicClaudeExecutionTools = /** @class */ (function () {
6777
+ /**
6778
+ * Creates Anthropic Claude Execution Tools.
6779
+ *
6780
+ * @param options which are relevant are directly passed to the Anthropic Claude client
6781
+ */
6782
+ function AnthropicClaudeExecutionTools(options) {
6783
+ if (options === void 0) { options = { isProxied: false }; }
6784
+ this.options = options;
6785
+ // Note: Passing only Anthropic Claude relevant options to Anthropic constructor
6786
+ var anthropicOptions = __assign({}, options);
6787
+ delete anthropicOptions.isVerbose;
6788
+ delete anthropicOptions.isProxied;
6789
+ this.client = new Anthropic__default["default"](anthropicOptions);
6790
+ }
6791
+ Object.defineProperty(AnthropicClaudeExecutionTools.prototype, "title", {
6792
+ get: function () {
6793
+ return 'Anthropic Claude';
6794
+ },
6795
+ enumerable: false,
6796
+ configurable: true
6797
+ });
6798
+ Object.defineProperty(AnthropicClaudeExecutionTools.prototype, "description", {
6799
+ get: function () {
6800
+ return 'Use all models provided by Anthropic Claude';
6801
+ },
6802
+ enumerable: false,
6803
+ configurable: true
6804
+ });
6805
+ /**
6806
+ * Calls Anthropic Claude API to use a chat model.
6807
+ */
6808
+ AnthropicClaudeExecutionTools.prototype.callChatModel = function (prompt) {
6809
+ return __awaiter(this, void 0, void 0, function () {
6810
+ var content, parameters, modelRequirements, modelName, rawPromptContent, rawRequest, start, complete, rawResponse, resultContent, usage;
6811
+ return __generator(this, function (_a) {
6812
+ switch (_a.label) {
6813
+ case 0:
6814
+ if (this.options.isVerbose) {
6815
+ console.info('💬 Anthropic Claude callChatModel call');
6816
+ }
6817
+ content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements;
6818
+ // TODO: [☂] Use here more modelRequirements
6819
+ if (modelRequirements.modelVariant !== 'CHAT') {
6820
+ throw new PipelineExecutionError('Use callChatModel only for CHAT variant');
6821
+ }
6822
+ modelName = modelRequirements.modelName || this.getDefaultChatModel().modelName;
6823
+ rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
6824
+ rawRequest = {
6825
+ model: modelRequirements.modelName || this.getDefaultChatModel().modelName,
6826
+ max_tokens: modelRequirements.maxTokens || 4096,
6827
+ // <- TODO: [🌾] Make some global max cap for maxTokens
6828
+ temperature: modelRequirements.temperature,
6829
+ system: modelRequirements.systemMessage,
6830
+ // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
6831
+ // <- Note: [🧆]
6832
+ messages: [
6833
+ {
6834
+ role: 'user',
6835
+ content: rawPromptContent,
6836
+ },
6837
+ ],
6838
+ // TODO: Is here some equivalent of user identification?> user: this.options.user,
6839
+ };
6840
+ start = getCurrentIsoDate();
6841
+ if (this.options.isVerbose) {
6842
+ console.info(colors__default["default"].bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
6843
+ }
6844
+ return [4 /*yield*/, this.client.messages.create(rawRequest)];
6845
+ case 1:
6846
+ rawResponse = _a.sent();
6847
+ if (this.options.isVerbose) {
6848
+ console.info(colors__default["default"].bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
6849
+ }
6850
+ if (!rawResponse.content[0]) {
6851
+ throw new PipelineExecutionError('No content from Anthropic Claude');
6852
+ }
6853
+ if (rawResponse.content.length > 1) {
6854
+ throw new PipelineExecutionError('More than one content blocks from Anthropic Claude');
6855
+ }
6856
+ resultContent = rawResponse.content[0].text;
6857
+ // eslint-disable-next-line prefer-const
6858
+ complete = getCurrentIsoDate();
6859
+ usage = {
6860
+ price: { value: 0, isUncertain: true } /* <- TODO: [🐞] Compute usage */,
6861
+ input: __assign({ tokensCount: uncertainNumber(rawResponse.usage.input_tokens) }, computeUsageCounts(prompt.content)),
6862
+ output: __assign({ tokensCount: uncertainNumber(rawResponse.usage.output_tokens) }, computeUsageCounts(prompt.content)),
6863
+ };
6864
+ return [2 /*return*/, {
6865
+ content: resultContent,
6866
+ modelName: rawResponse.model,
6867
+ timing: {
6868
+ start: start,
6869
+ complete: complete,
6870
+ },
6871
+ usage: usage,
6872
+ rawPromptContent: rawPromptContent,
6873
+ rawRequest: rawRequest,
6874
+ rawResponse: rawResponse,
6875
+ // <- [🗯]
6876
+ }];
6877
+ }
6878
+ });
6879
+ });
6880
+ };
6881
+ /*
6882
+ TODO: [👏]
6883
+ public async callCompletionModel(
6884
+ prompt: Pick<Prompt, 'content' | 'parameters' | 'modelRequirements'>,
6885
+ ): Promise<PromptCompletionResult> {
6886
+
6887
+ if (this.options.isVerbose) {
6888
+ console.info('🖋 Anthropic Claude callCompletionModel call');
6889
+ }
6890
+
6891
+ const { content, parameters, modelRequirements } = prompt;
6892
+
6893
+ // TODO: [☂] Use here more modelRequirements
6894
+ if (modelRequirements.modelVariant !== 'COMPLETION') {
6895
+ throw new PipelineExecutionError('Use callCompletionModel only for COMPLETION variant');
6896
+ }
6897
+
6898
+ const modelName = modelRequirements.modelName || this.getDefaultChatModel().modelName;
6899
+ const modelSettings = {
6900
+ model: modelName,
6901
+ max_tokens: modelRequirements.maxTokens || 2000, // <- Note: 2000 is for lagacy reasons
6902
+ // <- TODO: [🌾] Make some global max cap for maxTokens
6903
+ // <- TODO: Use here `systemMessage`, `temperature` and `seed`
6904
+ };
6905
+
6906
+ const rawRequest: xxxx.Completions.CompletionCreateParamsNonStreaming = {
6907
+ ...modelSettings,
6908
+ prompt: rawPromptContent,
6909
+ user: this.options.user,
6910
+ };
6911
+ const start: string_date_iso8601 = getCurrentIsoDate();
6912
+ let complete: string_date_iso8601;
6913
+
6914
+ if (this.options.isVerbose) {
6915
+ console.info(colors.bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
6916
+ }
6917
+ const rawResponse = await this.client.completions.create(rawRequest);
6918
+ if (this.options.isVerbose) {
6919
+ console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
6920
+ }
6921
+
6922
+ if (!rawResponse.choices[0]) {
6923
+ throw new PipelineExecutionError('No choises from Anthropic Claude');
6924
+ }
6925
+
6926
+ if (rawResponse.choices.length > 1) {
6927
+ // TODO: This should be maybe only warning
6928
+ throw new PipelineExecutionError('More than one choise from Anthropic Claude');
6929
+ }
6930
+
6931
+ const resultContent = rawResponse.choices[0].text;
6932
+ // eslint-disable-next-line prefer-const
6933
+ complete = getCurrentIsoDate();
6934
+ const usage = { price: 'UNKNOWN', inputTokens: 0, outputTokens: 0 /* <- TODO: [🐞] Compute usage * / } satisfies PromptResultUsage;
6935
+
6936
+
6937
+
6938
+ return {
6939
+ content: resultContent,
6940
+ modelName: rawResponse.model || model,
6941
+ timing: {
6942
+ start,
6943
+ complete,
6944
+ },
6945
+ usage,
6946
+ rawResponse,
6947
+ // <- [🗯]
6948
+ };
6949
+ }
6950
+ */
6951
+ // <- Note: [🤖] callXxxModel
6952
+ /**
6953
+ * Get the model that should be used as default
6954
+ */
6955
+ AnthropicClaudeExecutionTools.prototype.getDefaultModel = function (defaultModelName) {
6956
+ var model = ANTHROPIC_CLAUDE_MODELS.find(function (_a) {
6957
+ var modelName = _a.modelName;
6958
+ return modelName.startsWith(defaultModelName);
6959
+ });
6960
+ if (model === undefined) {
6961
+ throw new UnexpectedError(spaceTrim__default["default"](function (block) {
6962
+ 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) {
6963
+ var modelName = _a.modelName;
6964
+ return "- \"".concat(modelName, "\"");
6965
+ }).join('\n')), "\n\n ");
6966
+ }));
6967
+ }
6968
+ return model;
6969
+ };
6970
+ /**
6971
+ * Default model for chat variant.
6972
+ */
6973
+ AnthropicClaudeExecutionTools.prototype.getDefaultChatModel = function () {
6974
+ return this.getDefaultModel('claude-3-opus');
6975
+ };
6976
+ // <- Note: [🤖] getDefaultXxxModel
6977
+ /**
6978
+ * List all available Anthropic Claude models that can be used
6979
+ */
6980
+ AnthropicClaudeExecutionTools.prototype.listModels = function () {
6981
+ return ANTHROPIC_CLAUDE_MODELS;
6982
+ };
6983
+ return AnthropicClaudeExecutionTools;
6984
+ }());
6985
+ /**
6986
+ * TODO: [🍆] JSON mode
6987
+ * TODO: [🧠] Maybe handle errors via transformAnthropicError (like transformAzureError)
6988
+ * TODO: Maybe Create some common util for callChatModel and callCompletionModel
6989
+ * TODO: Maybe make custom OpenaiError
6990
+ * TODO: [🧠][🈁] Maybe use `isDeterministic` from options
6991
+ * TODO: [🍜] !!!!!! Auto use anonymous server in browser
6992
+ * TODO: [🧠][🌰] Allow to pass `title` for tracking purposes
6993
+ * TODO: [📅] Maybe instead of `RemoteLlmExecutionToolsOptions` use `proxyWithAnonymousRemoteServer` (if implemented)
6994
+ */
6995
+
6996
+ /**
6997
+ * Execution Tools for calling Anthropic Claude API.
6998
+ *
6999
+ * @public exported from `@promptbook/anthropic-claude`
7000
+ */
7001
+ function createAnthropicClaudeExecutionTools(options) {
7002
+ if (options.isProxied) {
7003
+ return new RemoteLlmExecutionTools(__assign(__assign({}, options), { isAnonymous: true, llmToolsConfiguration: [
7004
+ {
7005
+ title: 'Anthropic Claude (proxied)',
7006
+ packageName: '@promptbook/anthropic-claude',
7007
+ className: 'AnthropicClaudeExecutionTools',
7008
+ options: __assign(__assign({}, options), { isProxied: false }),
7009
+ },
7010
+ ] }));
7011
+ }
7012
+ return new AnthropicClaudeExecutionTools(options);
7013
+ }
7014
+ /**
7015
+ * TODO: !!!!!! Make this with all LLM providers
7016
+ * TODO: !!!!!! Maybe change all `new AnthropicClaudeExecutionTools` -> `createAnthropicClaudeExecutionTools` in manual
7017
+ */
7018
+
7019
+ /**
7020
+ * List of available OpenAI models with pricing
7021
+ *
7022
+ * Note: Done at 2024-05-20
7023
+ *
7024
+ * @see https://platform.openai.com/docs/models/
7025
+ * @see https://openai.com/api/pricing/
7026
+ * @public exported from `@promptbook/openai`
7027
+ */
7028
+ var OPENAI_MODELS = [
7029
+ /*/
7030
+ {
7031
+ modelTitle: 'dall-e-3',
7032
+ modelName: 'dall-e-3',
7033
+ },
7034
+ /**/
7035
+ /*/
7036
+ {
7037
+ modelTitle: 'whisper-1',
7038
+ modelName: 'whisper-1',
7039
+ },
7040
+ /**/
7041
+ /**/
7042
+ {
7043
+ modelVariant: 'COMPLETION',
7044
+ modelTitle: 'davinci-002',
7045
+ modelName: 'davinci-002',
7046
+ pricing: {
7047
+ prompt: computeUsage("$2.00 / 1M tokens"),
7048
+ output: computeUsage("$2.00 / 1M tokens"), // <- not sure
7049
+ },
7050
+ },
7051
+ /**/
7052
+ /*/
7053
+ {
7054
+ modelTitle: 'dall-e-2',
7055
+ modelName: 'dall-e-2',
7056
+ },
7057
+ /**/
7058
+ /**/
7059
+ {
7060
+ modelVariant: 'CHAT',
7061
+ modelTitle: 'gpt-3.5-turbo-16k',
7062
+ modelName: 'gpt-3.5-turbo-16k',
7063
+ pricing: {
7064
+ prompt: computeUsage("$3.00 / 1M tokens"),
7065
+ output: computeUsage("$4.00 / 1M tokens"),
7066
+ },
7067
+ },
7068
+ /**/
7069
+ /*/
7070
+ {
7071
+ modelTitle: 'tts-1-hd-1106',
7072
+ modelName: 'tts-1-hd-1106',
7073
+ },
7074
+ /**/
7075
+ /*/
7076
+ {
7077
+ modelTitle: 'tts-1-hd',
7078
+ modelName: 'tts-1-hd',
7079
+ },
7080
+ /**/
7081
+ /**/
7082
+ {
7083
+ modelVariant: 'CHAT',
7084
+ modelTitle: 'gpt-4',
7085
+ modelName: 'gpt-4',
7086
+ pricing: {
7087
+ prompt: computeUsage("$30.00 / 1M tokens"),
7088
+ output: computeUsage("$60.00 / 1M tokens"),
7089
+ },
7090
+ },
7091
+ /**/
7092
+ /**/
7093
+ {
7094
+ modelVariant: 'CHAT',
7095
+ modelTitle: 'gpt-4-32k',
7096
+ modelName: 'gpt-4-32k',
7097
+ pricing: {
7098
+ prompt: computeUsage("$60.00 / 1M tokens"),
7099
+ output: computeUsage("$120.00 / 1M tokens"),
7100
+ },
7101
+ },
7102
+ /**/
7103
+ /*/
7104
+ {
7105
+ modelVariant: 'CHAT',
7106
+ modelTitle: 'gpt-4-0613',
7107
+ modelName: 'gpt-4-0613',
7108
+ pricing: {
7109
+ prompt: computeUsage(` / 1M tokens`),
7110
+ output: computeUsage(` / 1M tokens`),
7111
+ },
7112
+ },
7113
+ /**/
7114
+ /**/
7115
+ {
7116
+ modelVariant: 'CHAT',
7117
+ modelTitle: 'gpt-4-turbo-2024-04-09',
7118
+ modelName: 'gpt-4-turbo-2024-04-09',
7119
+ pricing: {
7120
+ prompt: computeUsage("$10.00 / 1M tokens"),
7121
+ output: computeUsage("$30.00 / 1M tokens"),
7122
+ },
7123
+ },
7124
+ /**/
7125
+ /**/
7126
+ {
7127
+ modelVariant: 'CHAT',
7128
+ modelTitle: 'gpt-3.5-turbo-1106',
7129
+ modelName: 'gpt-3.5-turbo-1106',
7130
+ pricing: {
7131
+ prompt: computeUsage("$1.00 / 1M tokens"),
7132
+ output: computeUsage("$2.00 / 1M tokens"),
7133
+ },
7134
+ },
7135
+ /**/
7136
+ /**/
7137
+ {
7138
+ modelVariant: 'CHAT',
7139
+ modelTitle: 'gpt-4-turbo',
7140
+ modelName: 'gpt-4-turbo',
7141
+ pricing: {
7142
+ prompt: computeUsage("$10.00 / 1M tokens"),
7143
+ output: computeUsage("$30.00 / 1M tokens"),
7144
+ },
7145
+ },
7146
+ /**/
7147
+ /**/
7148
+ {
7149
+ modelVariant: 'COMPLETION',
7150
+ modelTitle: 'gpt-3.5-turbo-instruct-0914',
7151
+ modelName: 'gpt-3.5-turbo-instruct-0914',
7152
+ pricing: {
7153
+ prompt: computeUsage("$1.50 / 1M tokens"),
7154
+ output: computeUsage("$2.00 / 1M tokens"), // <- For gpt-3.5-turbo-instruct
7155
+ },
7156
+ },
7157
+ /**/
7158
+ /**/
7159
+ {
7160
+ modelVariant: 'COMPLETION',
7161
+ modelTitle: 'gpt-3.5-turbo-instruct',
7162
+ modelName: 'gpt-3.5-turbo-instruct',
7163
+ pricing: {
7164
+ prompt: computeUsage("$1.50 / 1M tokens"),
7165
+ output: computeUsage("$2.00 / 1M tokens"),
7166
+ },
7167
+ },
7168
+ /**/
7169
+ /*/
7170
+ {
7171
+ modelTitle: 'tts-1',
7172
+ modelName: 'tts-1',
7173
+ },
7174
+ /**/
7175
+ /**/
7176
+ {
7177
+ modelVariant: 'CHAT',
7178
+ modelTitle: 'gpt-3.5-turbo',
7179
+ modelName: 'gpt-3.5-turbo',
7180
+ pricing: {
7181
+ prompt: computeUsage("$3.00 / 1M tokens"),
7182
+ output: computeUsage("$6.00 / 1M tokens"), // <- Not sure, refer to gpt-3.5-turbo in Fine-tuning models
7183
+ },
7184
+ },
7185
+ /**/
7186
+ /**/
7187
+ {
7188
+ modelVariant: 'CHAT',
7189
+ modelTitle: 'gpt-3.5-turbo-0301',
7190
+ modelName: 'gpt-3.5-turbo-0301',
7191
+ pricing: {
7192
+ prompt: computeUsage("$1.50 / 1M tokens"),
7193
+ output: computeUsage("$2.00 / 1M tokens"),
7194
+ },
7195
+ },
7196
+ /**/
7197
+ /**/
7198
+ {
7199
+ modelVariant: 'COMPLETION',
7200
+ modelTitle: 'babbage-002',
7201
+ modelName: 'babbage-002',
7202
+ pricing: {
7203
+ prompt: computeUsage("$0.40 / 1M tokens"),
7204
+ output: computeUsage("$0.40 / 1M tokens"), // <- Not sure
7205
+ },
7206
+ },
7207
+ /**/
7208
+ /**/
7209
+ {
7210
+ modelVariant: 'CHAT',
7211
+ modelTitle: 'gpt-4-1106-preview',
7212
+ modelName: 'gpt-4-1106-preview',
7213
+ pricing: {
7214
+ prompt: computeUsage("$10.00 / 1M tokens"),
7215
+ output: computeUsage("$30.00 / 1M tokens"),
7216
+ },
7217
+ },
7218
+ /**/
7219
+ /**/
7220
+ {
7221
+ modelVariant: 'CHAT',
7222
+ modelTitle: 'gpt-4-0125-preview',
7223
+ modelName: 'gpt-4-0125-preview',
7224
+ pricing: {
7225
+ prompt: computeUsage("$10.00 / 1M tokens"),
7226
+ output: computeUsage("$30.00 / 1M tokens"),
7227
+ },
7228
+ },
7229
+ /**/
7230
+ /*/
7231
+ {
7232
+ modelTitle: 'tts-1-1106',
7233
+ modelName: 'tts-1-1106',
7234
+ },
7235
+ /**/
7236
+ /**/
7237
+ {
7238
+ modelVariant: 'CHAT',
7239
+ modelTitle: 'gpt-3.5-turbo-0125',
7240
+ modelName: 'gpt-3.5-turbo-0125',
7241
+ pricing: {
7242
+ prompt: computeUsage("$0.50 / 1M tokens"),
7243
+ output: computeUsage("$1.50 / 1M tokens"),
7244
+ },
7245
+ },
7246
+ /**/
7247
+ /**/
7248
+ {
7249
+ modelVariant: 'CHAT',
7250
+ modelTitle: 'gpt-4-turbo-preview',
7251
+ modelName: 'gpt-4-turbo-preview',
7252
+ pricing: {
7253
+ prompt: computeUsage("$10.00 / 1M tokens"),
7254
+ output: computeUsage("$30.00 / 1M tokens"), // <- Not sure, just for gpt-4-turbo
7255
+ },
7256
+ },
7257
+ /**/
7258
+ /**/
7259
+ {
7260
+ modelVariant: 'EMBEDDING',
7261
+ modelTitle: 'text-embedding-3-large',
7262
+ modelName: 'text-embedding-3-large',
7263
+ pricing: {
7264
+ prompt: computeUsage("$0.13 / 1M tokens"),
7265
+ // TODO: [🏏] Leverage the batch API @see https://platform.openai.com/docs/guides/batch
7266
+ output: 0, // <- Note: [🆖] In Embedding models you dont pay for output
7267
+ },
7268
+ },
7269
+ /**/
7270
+ /**/
7271
+ {
7272
+ modelVariant: 'EMBEDDING',
7273
+ modelTitle: 'text-embedding-3-small',
7274
+ modelName: 'text-embedding-3-small',
7275
+ pricing: {
7276
+ prompt: computeUsage("$0.02 / 1M tokens"),
7277
+ // TODO: [🏏] Leverage the batch API @see https://platform.openai.com/docs/guides/batch
7278
+ output: 0, // <- Note: [🆖] In Embedding models you dont pay for output
7279
+ },
7280
+ },
7281
+ /**/
7282
+ /**/
7283
+ {
7284
+ modelVariant: 'CHAT',
7285
+ modelTitle: 'gpt-3.5-turbo-0613',
7286
+ modelName: 'gpt-3.5-turbo-0613',
7287
+ pricing: {
7288
+ prompt: computeUsage("$1.50 / 1M tokens"),
7289
+ output: computeUsage("$2.00 / 1M tokens"),
7290
+ },
7291
+ },
7292
+ /**/
7293
+ /**/
7294
+ {
7295
+ modelVariant: 'EMBEDDING',
7296
+ modelTitle: 'text-embedding-ada-002',
7297
+ modelName: 'text-embedding-ada-002',
7298
+ pricing: {
7299
+ prompt: computeUsage("$0.1 / 1M tokens"),
7300
+ // TODO: [🏏] Leverage the batch API @see https://platform.openai.com/docs/guides/batch
7301
+ output: 0, // <- Note: [🆖] In Embedding models you dont pay for output
7302
+ },
7303
+ },
7304
+ /**/
7305
+ /*/
7306
+ {
7307
+ modelVariant: 'CHAT',
7308
+ modelTitle: 'gpt-4-1106-vision-preview',
7309
+ modelName: 'gpt-4-1106-vision-preview',
7310
+ },
7311
+ /**/
7312
+ /*/
7313
+ {
7314
+ modelVariant: 'CHAT',
7315
+ modelTitle: 'gpt-4-vision-preview',
7316
+ modelName: 'gpt-4-vision-preview',
7317
+ pricing: {
7318
+ prompt: computeUsage(`$10.00 / 1M tokens`),
7319
+ output: computeUsage(`$30.00 / 1M tokens`),
7320
+ },
7321
+ },
7322
+ /**/
7323
+ /**/
7324
+ {
7325
+ modelVariant: 'CHAT',
7326
+ modelTitle: 'gpt-4o-2024-05-13',
7327
+ modelName: 'gpt-4o-2024-05-13',
7328
+ pricing: {
7329
+ prompt: computeUsage("$5.00 / 1M tokens"),
7330
+ output: computeUsage("$15.00 / 1M tokens"),
7331
+ },
7332
+ },
7333
+ /**/
7334
+ /**/
7335
+ {
7336
+ modelVariant: 'CHAT',
7337
+ modelTitle: 'gpt-4o',
7338
+ modelName: 'gpt-4o',
7339
+ pricing: {
7340
+ prompt: computeUsage("$5.00 / 1M tokens"),
7341
+ output: computeUsage("$15.00 / 1M tokens"),
7342
+ },
7343
+ },
7344
+ /**/
7345
+ /**/
7346
+ {
7347
+ modelVariant: 'CHAT',
7348
+ modelTitle: 'gpt-3.5-turbo-16k-0613',
7349
+ modelName: 'gpt-3.5-turbo-16k-0613',
7350
+ pricing: {
7351
+ prompt: computeUsage("$3.00 / 1M tokens"),
7352
+ output: computeUsage("$4.00 / 1M tokens"),
7353
+ },
7354
+ },
7355
+ /**/
7356
+ ];
7357
+ /**
7358
+ * Note: [🤖] Add models of new variant
7359
+ * TODO: [🧠] Some mechanism to propagate unsureness
7360
+ * TODO: [🕚][👮‍♀️] Make this list dynamic - dynamically can be listed modelNames but not modelVariant, legacy status, context length and pricing
7361
+ * TODO: [🧠][👮‍♀️] Put here more info like description, isVision, trainingDateCutoff, languages, strengths ( Top-level performance, intelligence, fluency, and understanding), contextWindow,...
7362
+ * @see https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4
7363
+ * @see https://openai.com/api/pricing/
7364
+ * @see /other/playground/playground.ts
7365
+ * TODO: [🍓] Make better
7366
+ * TODO: Change model titles to human eg: "gpt-4-turbo-2024-04-09" -> "GPT-4 Turbo (2024-04-09)"
7367
+ * TODO: [🚸] Not all models are compatible with JSON mode, add this information here and use it
7368
+ */
7369
+
7370
+ /**
7371
+ * Execution Tools for calling Azure OpenAI API.
7372
+ *
7373
+ * @public exported from `@promptbook/azure-openai`
7374
+ */
7375
+ var AzureOpenAiExecutionTools = /** @class */ (function () {
7376
+ /**
7377
+ * Creates OpenAI Execution Tools.
7378
+ *
7379
+ * @param options which are relevant are directly passed to the OpenAI client
7380
+ */
7381
+ function AzureOpenAiExecutionTools(options) {
7382
+ this.options = options;
7383
+ this.client = new openai.OpenAIClient("https://".concat(options.resourceName, ".openai.azure.com/"), new openai.AzureKeyCredential(options.apiKey));
7384
+ }
7385
+ Object.defineProperty(AzureOpenAiExecutionTools.prototype, "title", {
7386
+ get: function () {
7387
+ return 'Azure OpenAI';
7388
+ },
7389
+ enumerable: false,
7390
+ configurable: true
7391
+ });
7392
+ Object.defineProperty(AzureOpenAiExecutionTools.prototype, "description", {
7393
+ get: function () {
7394
+ return 'Use all models trained by OpenAI provided by Azure';
7395
+ },
7396
+ enumerable: false,
7397
+ configurable: true
7398
+ });
7399
+ /**
7400
+ * Calls OpenAI API to use a chat model.
7401
+ */
7402
+ AzureOpenAiExecutionTools.prototype.callChatModel = function (prompt) {
7403
+ var _a, _b;
7404
+ return __awaiter(this, void 0, void 0, function () {
7405
+ var content, parameters, modelRequirements, modelName, modelSettings, rawPromptContent, messages, start, complete, rawRequest, rawResponse, resultContent, usage, error_1;
7406
+ var _c;
7407
+ return __generator(this, function (_d) {
7408
+ switch (_d.label) {
7409
+ case 0:
7410
+ if (this.options.isVerbose) {
7411
+ console.info('💬 OpenAI callChatModel call');
7412
+ }
7413
+ content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements;
7414
+ // TODO: [☂] Use here more modelRequirements
7415
+ if (modelRequirements.modelVariant !== 'CHAT') {
7416
+ throw new PipelineExecutionError('Use callChatModel only for CHAT variant');
7417
+ }
7418
+ _d.label = 1;
7419
+ case 1:
7420
+ _d.trys.push([1, 3, , 4]);
7421
+ modelName = prompt.modelRequirements.modelName || this.options.deploymentName;
7422
+ modelSettings = {
7423
+ maxTokens: modelRequirements.maxTokens,
7424
+ // <- TODO: [🌾] Make some global max cap for maxTokens
7425
+ temperature: modelRequirements.temperature,
7426
+ user: this.options.user,
7427
+ // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
7428
+ // <- Note: [🧆]
7429
+ };
7430
+ rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
7431
+ messages = __spreadArray(__spreadArray([], __read((modelRequirements.systemMessage === undefined
7432
+ ? []
7433
+ : [
7434
+ {
7435
+ role: 'system',
7436
+ content: modelRequirements.systemMessage,
7437
+ },
7438
+ ])), false), [
7439
+ {
7440
+ role: 'user',
7441
+ content: rawPromptContent,
7442
+ },
7443
+ ], false);
7444
+ start = getCurrentIsoDate();
7445
+ complete = void 0;
7446
+ if (this.options.isVerbose) {
7447
+ console.info(colors__default["default"].bgWhite('messages'), JSON.stringify(messages, null, 4));
7448
+ }
7449
+ rawRequest = [modelName, messages, modelSettings];
7450
+ return [4 /*yield*/, (_c = this.client).getChatCompletions.apply(_c, __spreadArray([], __read(rawRequest), false))];
7451
+ case 2:
7452
+ rawResponse = _d.sent();
7453
+ if (this.options.isVerbose) {
7454
+ console.info(colors__default["default"].bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
7455
+ }
7456
+ if (!rawResponse.choices[0]) {
7457
+ throw new PipelineExecutionError('No choises from Azure OpenAI');
7458
+ }
7459
+ if (rawResponse.choices.length > 1) {
7460
+ // TODO: This should be maybe only warning
7461
+ throw new PipelineExecutionError('More than one choise from Azure OpenAI');
7462
+ }
7463
+ if (!rawResponse.choices[0].message || !rawResponse.choices[0].message.content) {
7464
+ throw new PipelineExecutionError('Empty response from Azure OpenAI');
7465
+ }
7466
+ resultContent = rawResponse.choices[0].message.content;
7467
+ // eslint-disable-next-line prefer-const
7468
+ complete = getCurrentIsoDate();
7469
+ usage = {
7470
+ price: uncertainNumber() /* <- TODO: [🐞] Compute usage */,
7471
+ input: __assign({ tokensCount: uncertainNumber((_a = rawResponse.usage) === null || _a === void 0 ? void 0 : _a.promptTokens) }, computeUsageCounts(prompt.content)),
7472
+ output: __assign({ tokensCount: uncertainNumber((_b = rawResponse.usage) === null || _b === void 0 ? void 0 : _b.completionTokens) }, computeUsageCounts(prompt.content)),
7473
+ };
7474
+ return [2 /*return*/, {
7475
+ content: resultContent,
7476
+ modelName: modelName,
7477
+ timing: {
7478
+ start: start,
7479
+ complete: complete,
7480
+ },
7481
+ usage: usage,
7482
+ rawPromptContent: rawPromptContent,
7483
+ rawRequest: rawRequest,
7484
+ rawResponse: rawResponse,
7485
+ // <- [🗯]
7486
+ }];
7487
+ case 3:
7488
+ error_1 = _d.sent();
7489
+ throw this.transformAzureError(error_1);
7490
+ case 4: return [2 /*return*/];
7491
+ }
7492
+ });
7493
+ });
7494
+ };
7495
+ /**
7496
+ * Calls Azure OpenAI API to use a complete model.
7497
+ */
7498
+ AzureOpenAiExecutionTools.prototype.callCompletionModel = function (prompt) {
7499
+ var _a, _b;
7500
+ return __awaiter(this, void 0, void 0, function () {
7501
+ var content, parameters, modelRequirements, modelName, modelSettings, start, complete, rawPromptContent, rawRequest, rawResponse, resultContent, usage, error_2;
7502
+ var _c;
7503
+ return __generator(this, function (_d) {
7504
+ switch (_d.label) {
7505
+ case 0:
7506
+ if (this.options.isVerbose) {
7507
+ console.info('🖋 OpenAI callCompletionModel call');
7508
+ }
7509
+ content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements;
7510
+ // TODO: [☂] Use here more modelRequirements
7511
+ if (modelRequirements.modelVariant !== 'COMPLETION') {
7512
+ throw new PipelineExecutionError('Use callCompletionModel only for COMPLETION variant');
7513
+ }
7514
+ _d.label = 1;
7515
+ case 1:
7516
+ _d.trys.push([1, 3, , 4]);
7517
+ modelName = prompt.modelRequirements.modelName || this.options.deploymentName;
7518
+ modelSettings = {
7519
+ maxTokens: modelRequirements.maxTokens || 2000,
7520
+ // <- TODO: [🌾] Make some global max cap for maxTokens
7521
+ temperature: modelRequirements.temperature,
7522
+ user: this.options.user,
7523
+ // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
7524
+ // <- Note: [🧆]
7525
+ };
7526
+ start = getCurrentIsoDate();
7527
+ complete = void 0;
7528
+ if (this.options.isVerbose) {
7529
+ console.info(colors__default["default"].bgWhite('content'), JSON.stringify(content, null, 4));
7530
+ console.info(colors__default["default"].bgWhite('parameters'), JSON.stringify(parameters, null, 4));
7531
+ }
7532
+ rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
7533
+ rawRequest = [
7534
+ modelName,
7535
+ [rawPromptContent],
7536
+ modelSettings,
7537
+ ];
7538
+ return [4 /*yield*/, (_c = this.client).getCompletions.apply(_c, __spreadArray([], __read(rawRequest), false))];
7539
+ case 2:
7540
+ rawResponse = _d.sent();
7541
+ if (this.options.isVerbose) {
7542
+ console.info(colors__default["default"].bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
7543
+ }
7544
+ if (!rawResponse.choices[0]) {
7545
+ throw new PipelineExecutionError('No choises from OpenAI');
7546
+ }
7547
+ if (rawResponse.choices.length > 1) {
7548
+ // TODO: This should be maybe only warning
7549
+ throw new PipelineExecutionError('More than one choise from OpenAI');
7550
+ }
7551
+ resultContent = rawResponse.choices[0].text;
7552
+ // eslint-disable-next-line prefer-const
7553
+ complete = getCurrentIsoDate();
7554
+ usage = {
7555
+ price: uncertainNumber() /* <- TODO: [🐞] Compute usage */,
7556
+ input: __assign({ tokensCount: uncertainNumber((_a = rawResponse.usage) === null || _a === void 0 ? void 0 : _a.promptTokens) }, computeUsageCounts(prompt.content)),
7557
+ output: __assign({ tokensCount: uncertainNumber((_b = rawResponse.usage) === null || _b === void 0 ? void 0 : _b.completionTokens) }, computeUsageCounts(prompt.content)),
7558
+ };
7559
+ return [2 /*return*/, {
7560
+ content: resultContent,
7561
+ modelName: modelName,
7562
+ timing: {
7563
+ start: start,
7564
+ complete: complete,
7565
+ },
7566
+ usage: usage,
7567
+ rawPromptContent: rawPromptContent,
7568
+ rawRequest: rawRequest,
7569
+ rawResponse: rawResponse,
7570
+ // <- [🗯]
7571
+ }];
7572
+ case 3:
7573
+ error_2 = _d.sent();
7574
+ throw this.transformAzureError(error_2);
7575
+ case 4: return [2 /*return*/];
7576
+ }
7577
+ });
7578
+ });
7579
+ };
7580
+ // <- Note: [🤖] callXxxModel
7581
+ /**
7582
+ * Changes Azure error (which is not propper Error but object) to propper Error
7583
+ */
7584
+ AzureOpenAiExecutionTools.prototype.transformAzureError = function (azureError) {
7585
+ if (typeof azureError !== 'object' || azureError === null) {
7586
+ return new PipelineExecutionError("Unknown Azure OpenAI error");
7587
+ }
7588
+ var code = azureError.code, message = azureError.message;
7589
+ return new PipelineExecutionError("".concat(code, ": ").concat(message));
7590
+ };
7591
+ /**
7592
+ * List all available Azure OpenAI models that can be used
7593
+ */
7594
+ AzureOpenAiExecutionTools.prototype.listModels = function () {
7595
+ return __awaiter(this, void 0, void 0, function () {
7596
+ return __generator(this, function (_a) {
7597
+ // TODO: !!! Do here some filtering which models are really available as deployment
7598
+ // @see https://management.azure.com/subscriptions/subscriptionId/resourceGroups/resourceGroupName/providers/Microsoft.CognitiveServices/accounts/accountName/deployments?api-version=2023-05-01
7599
+ return [2 /*return*/, OPENAI_MODELS.map(function (_a) {
7600
+ var modelTitle = _a.modelTitle, modelName = _a.modelName, modelVariant = _a.modelVariant;
7601
+ return ({
7602
+ modelTitle: "Azure ".concat(modelTitle),
7603
+ modelName: modelName,
7604
+ modelVariant: modelVariant,
7605
+ });
7606
+ })];
7607
+ });
7608
+ });
7609
+ };
7610
+ return AzureOpenAiExecutionTools;
7611
+ }());
7612
+ /**
7613
+ * TODO: Maybe Create some common util for callChatModel and callCompletionModel
7614
+ * TODO: Maybe make custom AzureOpenaiError
7615
+ * TODO: [🧠][🈁] Maybe use `isDeterministic` from options
7616
+ * TODO: [🧠][🌰] Allow to pass `title` for tracking purposes
7617
+ */
7618
+
7619
+ /**
7620
+ * Computes the usage of the OpenAI API based on the response from OpenAI
7621
+ *
7622
+ * @param promptContent The content of the prompt
7623
+ * @param resultContent The content of the result (for embedding prompts or failed prompts pass empty string)
7624
+ * @param rawResponse The raw response from OpenAI API
7625
+ * @throws {PipelineExecutionError} If the usage is not defined in the response from OpenAI
7626
+ * @private internal utility of `OpenAiExecutionTools`
7627
+ */
7628
+ function computeOpenaiUsage(promptContent, // <- Note: Intentionally using [] to access type properties to bring jsdoc from Prompt/PromptResult to consumer
7629
+ resultContent, rawResponse) {
7630
+ var _a, _b;
7631
+ if (rawResponse.usage === undefined) {
7632
+ throw new PipelineExecutionError('The usage is not defined in the response from OpenAI');
7633
+ }
7634
+ if (((_a = rawResponse.usage) === null || _a === void 0 ? void 0 : _a.prompt_tokens) === undefined) {
7635
+ throw new PipelineExecutionError('In OpenAI response `usage.prompt_tokens` not defined');
7636
+ }
7637
+ var inputTokens = rawResponse.usage.prompt_tokens;
7638
+ var outputTokens = ((_b = rawResponse.usage) === null || _b === void 0 ? void 0 : _b.completion_tokens) || 0;
7639
+ var modelInfo = OPENAI_MODELS.find(function (model) { return model.modelName === rawResponse.model; });
7640
+ var price;
7641
+ if (modelInfo === undefined || modelInfo.pricing === undefined) {
7642
+ price = uncertainNumber();
7643
+ }
7644
+ else {
7645
+ price = uncertainNumber(inputTokens * modelInfo.pricing.prompt + outputTokens * modelInfo.pricing.output);
7646
+ }
7647
+ return {
7648
+ price: price,
7649
+ input: __assign({ tokensCount: uncertainNumber(rawResponse.usage.prompt_tokens) }, computeUsageCounts(promptContent)),
7650
+ output: __assign({ tokensCount: uncertainNumber(outputTokens) }, computeUsageCounts(resultContent)),
7651
+ };
7652
+ }
7653
+
7654
+ /**
7655
+ * Execution Tools for calling OpenAI API.
7656
+ *
7657
+ * @public exported from `@promptbook/openai`
7658
+ */
7659
+ var OpenAiExecutionTools = /** @class */ (function () {
7660
+ /**
7661
+ * Creates OpenAI Execution Tools.
7662
+ *
7663
+ * @param options which are relevant are directly passed to the OpenAI client
7664
+ */
7665
+ function OpenAiExecutionTools(options) {
7666
+ if (options === void 0) { options = {}; }
7667
+ this.options = options;
7668
+ // Note: Passing only OpenAI relevant options to OpenAI constructor
7669
+ var openAiOptions = __assign({}, options);
7670
+ delete openAiOptions.isVerbose;
7671
+ delete openAiOptions.user;
7672
+ this.client = new OpenAI__default["default"](__assign({}, openAiOptions));
7673
+ }
7674
+ Object.defineProperty(OpenAiExecutionTools.prototype, "title", {
7675
+ get: function () {
7676
+ return 'OpenAI';
7677
+ },
7678
+ enumerable: false,
7679
+ configurable: true
7680
+ });
7681
+ Object.defineProperty(OpenAiExecutionTools.prototype, "description", {
7682
+ get: function () {
7683
+ return 'Use all models provided by OpenAI';
7684
+ },
7685
+ enumerable: false,
7686
+ configurable: true
7687
+ });
7688
+ /**
7689
+ * Calls OpenAI API to use a chat model.
7690
+ */
7691
+ OpenAiExecutionTools.prototype.callChatModel = function (prompt) {
7692
+ return __awaiter(this, void 0, void 0, function () {
7693
+ var content, parameters, modelRequirements, expectFormat, modelName, modelSettings, rawPromptContent, rawRequest, start, complete, rawResponse, resultContent, usage;
7694
+ return __generator(this, function (_a) {
7695
+ switch (_a.label) {
7696
+ case 0:
7697
+ if (this.options.isVerbose) {
7698
+ console.info('💬 OpenAI callChatModel call', { prompt: prompt });
7699
+ }
7700
+ content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements, expectFormat = prompt.expectFormat;
7701
+ // TODO: [☂] Use here more modelRequirements
7702
+ if (modelRequirements.modelVariant !== 'CHAT') {
7703
+ throw new PipelineExecutionError('Use callChatModel only for CHAT variant');
7704
+ }
7705
+ modelName = modelRequirements.modelName || this.getDefaultChatModel().modelName;
7706
+ modelSettings = {
7707
+ model: modelName,
7708
+ max_tokens: modelRequirements.maxTokens,
7709
+ // <- TODO: [🌾] Make some global max cap for maxTokens
7710
+ temperature: modelRequirements.temperature,
7711
+ // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
7712
+ // <- Note: [🧆]
7713
+ };
7714
+ if (expectFormat === 'JSON') {
7715
+ modelSettings.response_format = {
7716
+ type: 'json_object',
7717
+ };
7718
+ }
7719
+ rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
7720
+ rawRequest = __assign(__assign({}, modelSettings), { messages: __spreadArray(__spreadArray([], __read((modelRequirements.systemMessage === undefined
7721
+ ? []
7722
+ : [
7723
+ {
7724
+ role: 'system',
7725
+ content: modelRequirements.systemMessage,
7726
+ },
7727
+ ])), false), [
7728
+ {
7729
+ role: 'user',
7730
+ content: rawPromptContent,
7731
+ },
7732
+ ], false), user: this.options.user });
7733
+ start = getCurrentIsoDate();
7734
+ if (this.options.isVerbose) {
7735
+ console.info(colors__default["default"].bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
7736
+ }
7737
+ return [4 /*yield*/, this.client.chat.completions.create(rawRequest)];
7738
+ case 1:
7739
+ rawResponse = _a.sent();
7740
+ if (this.options.isVerbose) {
7741
+ console.info(colors__default["default"].bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
7742
+ }
7743
+ if (!rawResponse.choices[0]) {
7744
+ throw new PipelineExecutionError('No choises from OpenAI');
7745
+ }
7746
+ if (rawResponse.choices.length > 1) {
7747
+ // TODO: This should be maybe only warning
7748
+ throw new PipelineExecutionError('More than one choise from OpenAI');
7749
+ }
7750
+ resultContent = rawResponse.choices[0].message.content;
7751
+ // eslint-disable-next-line prefer-const
7752
+ complete = getCurrentIsoDate();
7753
+ usage = computeOpenaiUsage(content, resultContent || '', rawResponse);
7754
+ if (resultContent === null) {
7755
+ throw new PipelineExecutionError('No response message from OpenAI');
7756
+ }
7757
+ return [2 /*return*/, {
7758
+ content: resultContent,
7759
+ modelName: rawResponse.model || modelName,
7760
+ timing: {
7761
+ start: start,
7762
+ complete: complete,
7763
+ },
7764
+ usage: usage,
7765
+ rawPromptContent: rawPromptContent,
7766
+ rawRequest: rawRequest,
7767
+ rawResponse: rawResponse,
7768
+ // <- [🗯]
7769
+ }];
7770
+ }
7771
+ });
7772
+ });
7773
+ };
7774
+ /**
7775
+ * Calls OpenAI API to use a complete model.
7776
+ */
7777
+ OpenAiExecutionTools.prototype.callCompletionModel = function (prompt) {
7778
+ return __awaiter(this, void 0, void 0, function () {
7779
+ var content, parameters, modelRequirements, modelName, modelSettings, rawPromptContent, rawRequest, start, complete, rawResponse, resultContent, usage;
7780
+ return __generator(this, function (_a) {
7781
+ switch (_a.label) {
7782
+ case 0:
7783
+ if (this.options.isVerbose) {
7784
+ console.info('🖋 OpenAI callCompletionModel call', { prompt: prompt });
7785
+ }
7786
+ content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements;
7787
+ // TODO: [☂] Use here more modelRequirements
7788
+ if (modelRequirements.modelVariant !== 'COMPLETION') {
7789
+ throw new PipelineExecutionError('Use callCompletionModel only for COMPLETION variant');
7790
+ }
7791
+ modelName = modelRequirements.modelName || this.getDefaultCompletionModel().modelName;
7792
+ modelSettings = {
7793
+ model: modelName,
7794
+ max_tokens: modelRequirements.maxTokens || 2000,
7795
+ // <- TODO: [🌾] Make some global max cap for maxTokens
7796
+ temperature: modelRequirements.temperature,
7797
+ // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
7798
+ // <- Note: [🧆]
7799
+ };
7800
+ rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
7801
+ rawRequest = __assign(__assign({}, modelSettings), { prompt: rawPromptContent, user: this.options.user });
7802
+ start = getCurrentIsoDate();
7803
+ if (this.options.isVerbose) {
7804
+ console.info(colors__default["default"].bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
7805
+ }
7806
+ return [4 /*yield*/, this.client.completions.create(rawRequest)];
7807
+ case 1:
7808
+ rawResponse = _a.sent();
7809
+ if (this.options.isVerbose) {
7810
+ console.info(colors__default["default"].bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
7811
+ }
7812
+ if (!rawResponse.choices[0]) {
7813
+ throw new PipelineExecutionError('No choises from OpenAI');
7814
+ }
7815
+ if (rawResponse.choices.length > 1) {
7816
+ // TODO: This should be maybe only warning
7817
+ throw new PipelineExecutionError('More than one choise from OpenAI');
7818
+ }
7819
+ resultContent = rawResponse.choices[0].text;
7820
+ // eslint-disable-next-line prefer-const
7821
+ complete = getCurrentIsoDate();
7822
+ usage = computeOpenaiUsage(content, resultContent || '', rawResponse);
7823
+ return [2 /*return*/, {
7824
+ content: resultContent,
7825
+ modelName: rawResponse.model || modelName,
7826
+ timing: {
7827
+ start: start,
7828
+ complete: complete,
7829
+ },
7830
+ usage: usage,
7831
+ rawPromptContent: rawPromptContent,
7832
+ rawRequest: rawRequest,
7833
+ rawResponse: rawResponse,
7834
+ // <- [🗯]
7835
+ }];
7836
+ }
7837
+ });
7838
+ });
7839
+ };
7840
+ /**
7841
+ * Calls OpenAI API to use a embedding model
7842
+ */
7843
+ OpenAiExecutionTools.prototype.callEmbeddingModel = function (prompt) {
7844
+ return __awaiter(this, void 0, void 0, function () {
7845
+ var content, parameters, modelRequirements, modelName, rawPromptContent, rawRequest, start, complete, rawResponse, resultContent, usage;
7846
+ return __generator(this, function (_a) {
7847
+ switch (_a.label) {
7848
+ case 0:
7849
+ if (this.options.isVerbose) {
7850
+ console.info('🖋 OpenAI embedding call', { prompt: prompt });
7851
+ }
7852
+ content = prompt.content, parameters = prompt.parameters, modelRequirements = prompt.modelRequirements;
7853
+ // TODO: [☂] Use here more modelRequirements
7854
+ if (modelRequirements.modelVariant !== 'EMBEDDING') {
7855
+ throw new PipelineExecutionError('Use embed only for EMBEDDING variant');
7856
+ }
7857
+ modelName = modelRequirements.modelName || this.getDefaultEmbeddingModel().modelName;
7858
+ rawPromptContent = replaceParameters(content, __assign(__assign({}, parameters), { modelName: modelName }));
7859
+ rawRequest = {
7860
+ input: rawPromptContent,
7861
+ model: modelName,
7862
+ };
7863
+ start = getCurrentIsoDate();
7864
+ if (this.options.isVerbose) {
7865
+ console.info(colors__default["default"].bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
7866
+ }
7867
+ return [4 /*yield*/, this.client.embeddings.create(rawRequest)];
7868
+ case 1:
7869
+ rawResponse = _a.sent();
7870
+ if (this.options.isVerbose) {
7871
+ console.info(colors__default["default"].bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
7872
+ }
7873
+ if (rawResponse.data.length !== 1) {
7874
+ throw new PipelineExecutionError("Expected exactly 1 data item in response, got ".concat(rawResponse.data.length));
7875
+ }
7876
+ resultContent = rawResponse.data[0].embedding;
7877
+ // eslint-disable-next-line prefer-const
7878
+ complete = getCurrentIsoDate();
7879
+ usage = computeOpenaiUsage(content, '', rawResponse);
7880
+ return [2 /*return*/, {
7881
+ content: resultContent,
7882
+ modelName: rawResponse.model || modelName,
7883
+ timing: {
7884
+ start: start,
7885
+ complete: complete,
7886
+ },
7887
+ usage: usage,
7888
+ rawPromptContent: rawPromptContent,
7889
+ rawRequest: rawRequest,
7890
+ rawResponse: rawResponse,
7891
+ // <- [🗯]
7892
+ }];
7893
+ }
7894
+ });
7895
+ });
7896
+ };
7897
+ // <- Note: [🤖] callXxxModel
7898
+ /**
7899
+ * Get the model that should be used as default
7900
+ */
7901
+ OpenAiExecutionTools.prototype.getDefaultModel = function (defaultModelName) {
7902
+ var model = OPENAI_MODELS.find(function (_a) {
7903
+ var modelName = _a.modelName;
7904
+ return modelName === defaultModelName;
7905
+ });
7906
+ if (model === undefined) {
7907
+ throw new UnexpectedError(spaceTrim__default["default"](function (block) {
7908
+ 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) {
7909
+ var modelName = _a.modelName;
7910
+ return "- \"".concat(modelName, "\"");
7911
+ }).join('\n')), "\n\n ");
7912
+ }));
7913
+ }
7914
+ return model;
7915
+ };
7916
+ /**
7917
+ * Default model for chat variant.
7918
+ */
7919
+ OpenAiExecutionTools.prototype.getDefaultChatModel = function () {
7920
+ return this.getDefaultModel('gpt-4o');
7921
+ };
7922
+ /**
7923
+ * Default model for completion variant.
7924
+ */
7925
+ OpenAiExecutionTools.prototype.getDefaultCompletionModel = function () {
7926
+ return this.getDefaultModel('gpt-3.5-turbo-instruct');
7927
+ };
7928
+ /**
7929
+ * Default model for completion variant.
7930
+ */
7931
+ OpenAiExecutionTools.prototype.getDefaultEmbeddingModel = function () {
7932
+ return this.getDefaultModel('text-embedding-3-large');
7933
+ };
7934
+ // <- Note: [🤖] getDefaultXxxModel
7935
+ /**
7936
+ * List all available OpenAI models that can be used
7937
+ */
7938
+ OpenAiExecutionTools.prototype.listModels = function () {
7939
+ /*
7940
+ Note: Dynamic lising of the models
7941
+ const models = await this.openai.models.list({});
7942
+
7943
+ console.log({ models });
7944
+ console.log(models.data);
7945
+ */
7946
+ return OPENAI_MODELS;
7947
+ };
7948
+ return OpenAiExecutionTools;
7949
+ }());
7950
+ /**
7951
+ * TODO: [🧠][🧙‍♂️] Maybe there can be some wizzard for thoose who want to use just OpenAI
7952
+ * TODO: Maybe Create some common util for callChatModel and callCompletionModel
7953
+ * TODO: Maybe make custom OpenaiError
7954
+ * TODO: [🧠][🈁] Maybe use `isDeterministic` from options
7955
+ * TODO: [🧠][🌰] Allow to pass `title` for tracking purposes
7956
+ */
7957
+
7958
+ /**
7959
+ * @public exported from `@promptbook/core`
7960
+ */
7961
+ var LLM_CONFIGURATION_BOILERPLATES = [
7962
+ {
7963
+ title: 'Open AI',
7964
+ packageName: '@promptbook/openai',
7965
+ className: 'OpenAiExecutionTools',
7966
+ options: {
7967
+ apiKey: 'sk-',
7968
+ },
7969
+ },
7970
+ {
7971
+ title: 'Anthropic Claude',
7972
+ packageName: '@promptbook/anthropic-claude',
7973
+ className: 'AnthropicClaudeExecutionTools',
7974
+ options: {
7975
+ apiKey: 'sk-ant-api03-',
7976
+ isProxied: true,
7977
+ remoteUrl: 'https://!!!!!!',
7978
+ path: '/socket.io!!!!!!',
7979
+ },
7980
+ },
7981
+ {
7982
+ title: 'Azure Open AI',
7983
+ packageName: '@promptbook/azure-openai',
7984
+ className: 'AzureOpenAiExecutionTools',
7985
+ options: {
7986
+ // TODO: !!!> resourceName
7987
+ // TODO: !!!> deploymentName
7988
+ apiKey: 'sk-',
7989
+ },
7990
+ },
7991
+ // <- Note: [🦑] Add here new LLM provider
7992
+ ];
7993
+ /**
7994
+ * @private internal type for `createLlmToolsFromConfiguration`
7995
+ */
7996
+ var EXECUTION_TOOLS_CLASSES = {
7997
+ createOpenAiExecutionTools: function (options) {
7998
+ return new OpenAiExecutionTools(__assign(__assign({}, options), { dangerouslyAllowBrowser: true /* <- TODO: [🧠] !!! Some mechanism for auto-detection of browser, maybe hide in `OpenAiExecutionTools` */ }));
7999
+ },
8000
+ createAnthropicClaudeExecutionTools: createAnthropicClaudeExecutionTools,
8001
+ createAzureOpenAiExecutionTools: function (options) { return new AzureOpenAiExecutionTools(options); },
8002
+ // <- Note: [🦑] Add here new LLM provider
8003
+ };
8004
+ /**
8005
+ * TODO: [🧠] Better file name than `config.ts` + maybe move to two separate files
8006
+ * TODO: [🧠][🎌] Adding this should be responsibility of each provider package NOT this one central place
8007
+ */
8008
+
8009
+ /**
8010
+ * @@@
8011
+ *
8012
+ * Note: This function is not cached, every call creates new instance of `MultipleLlmExecutionTools`
8013
+ *
8014
+ * @returns @@@
8015
+ * @public exported from `@promptbook/core`
8016
+ */
8017
+ function createLlmToolsFromConfiguration(configuration, options) {
8018
+ if (options === void 0) { options = {}; }
8019
+ var _a = options.isVerbose, isVerbose = _a === void 0 ? false : _a;
8020
+ dotenv__namespace.config();
8021
+ var llmTools = configuration.map(function (llmConfiguration) {
8022
+ return EXECUTION_TOOLS_CLASSES["create".concat(llmConfiguration.className)](__assign({ isVerbose: isVerbose }, llmConfiguration.options));
8023
+ });
8024
+ return joinLlmExecutionTools.apply(void 0, __spreadArray([], __read(llmTools), false));
8025
+ }
8026
+ /**
8027
+ * TODO: [🎌] Togethere with `createLlmToolsFromConfiguration` + 'EXECUTION_TOOLS_CLASSES' gets to `@promptbook/core` ALL model providers, make this more efficient
8028
+ * TODO: [🧠][🎌] Dynamically install required providers
8029
+ * TODO: @@@ write discussion about this - wizzard
8030
+ * TODO: [🧠][🍛] Which name is better `createLlmToolsFromConfig` or `createLlmToolsFromConfiguration`?
8031
+ * TODO: [🧠] Is there some meaningfull way how to test this util
8032
+ * TODO: This should be maybe not under `_common` but under `utils`
8033
+ */
8034
+
6482
8035
  /**
6483
8036
  * Stores
6484
8037
  *
@@ -7060,6 +8613,7 @@
7060
8613
  exports.EXPECTATION_UNITS = EXPECTATION_UNITS;
7061
8614
  exports.EnvironmentMismatchError = EnvironmentMismatchError;
7062
8615
  exports.ExecutionReportStringOptionsDefaults = ExecutionReportStringOptionsDefaults;
8616
+ exports.LLM_CONFIGURATION_BOILERPLATES = LLM_CONFIGURATION_BOILERPLATES;
7063
8617
  exports.LimitReachedError = LimitReachedError;
7064
8618
  exports.MAX_EXECUTION_ATTEMPTS = MAX_EXECUTION_ATTEMPTS;
7065
8619
  exports.MAX_FILENAME_LENGTH = MAX_FILENAME_LENGTH;
@@ -7089,6 +8643,7 @@
7089
8643
  exports.createCollectionFromJson = createCollectionFromJson;
7090
8644
  exports.createCollectionFromPromise = createCollectionFromPromise;
7091
8645
  exports.createCollectionFromUrl = createCollectionFromUrl;
8646
+ exports.createLlmToolsFromConfiguration = createLlmToolsFromConfiguration;
7092
8647
  exports.createPipelineExecutor = createPipelineExecutor;
7093
8648
  exports.createSubcollection = createSubcollection;
7094
8649
  exports.embeddingVectorToString = embeddingVectorToString;