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