@promptbook/wizard 0.102.0-3 → 0.102.0-5

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.
package/esm/index.es.js CHANGED
@@ -36,7 +36,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
36
36
  * @generated
37
37
  * @see https://github.com/webgptorg/promptbook
38
38
  */
39
- const PROMPTBOOK_ENGINE_VERSION = '0.102.0-3';
39
+ const PROMPTBOOK_ENGINE_VERSION = '0.102.0-5';
40
40
  /**
41
41
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
42
42
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -4782,6 +4782,14 @@ class OpenAiCompatibleExecutionTools {
4782
4782
  // <- TODO: [🚸] Not all models are compatible with JSON mode
4783
4783
  // > 'response_format' of type 'json_object' is not supported with this model.
4784
4784
  const rawPromptContent = templateParameters(content, { ...parameters, modelName });
4785
+ // Convert thread to OpenAI format if present
4786
+ let threadMessages = [];
4787
+ if ('thread' in prompt && Array.isArray(prompt.thread)) {
4788
+ threadMessages = prompt.thread.map((msg) => ({
4789
+ role: msg.role === 'assistant' ? 'assistant' : 'user',
4790
+ content: msg.content,
4791
+ }));
4792
+ }
4785
4793
  const rawRequest = {
4786
4794
  ...modelSettings,
4787
4795
  messages: [
@@ -4793,6 +4801,7 @@ class OpenAiCompatibleExecutionTools {
4793
4801
  content: currentModelRequirements.systemMessage,
4794
4802
  },
4795
4803
  ]),
4804
+ ...threadMessages,
4796
4805
  {
4797
4806
  role: 'user',
4798
4807
  content: rawPromptContent,
@@ -5001,16 +5010,22 @@ class OpenAiCompatibleExecutionTools {
5001
5010
  * Calls OpenAI compatible API to use a embedding model
5002
5011
  */
5003
5012
  async callEmbeddingModel(prompt) {
5013
+ return this.callEmbeddingModelWithRetry(prompt, prompt.modelRequirements);
5014
+ }
5015
+ /**
5016
+ * Internal method that handles parameter retry for embedding model calls
5017
+ */
5018
+ async callEmbeddingModelWithRetry(prompt, currentModelRequirements) {
5004
5019
  if (this.options.isVerbose) {
5005
- console.info(`🖋 ${this.title} embedding call`, { prompt });
5020
+ console.info(`🖋 ${this.title} embedding call`, { prompt, currentModelRequirements });
5006
5021
  }
5007
- const { content, parameters, modelRequirements } = prompt;
5022
+ const { content, parameters } = prompt;
5008
5023
  const client = await this.getClient();
5009
5024
  // TODO: [☂] Use here more modelRequirements
5010
- if (modelRequirements.modelVariant !== 'EMBEDDING') {
5025
+ if (currentModelRequirements.modelVariant !== 'EMBEDDING') {
5011
5026
  throw new PipelineExecutionError('Use embed only for EMBEDDING variant');
5012
5027
  }
5013
- const modelName = modelRequirements.modelName || this.getDefaultEmbeddingModel().modelName;
5028
+ const modelName = currentModelRequirements.modelName || this.getDefaultEmbeddingModel().modelName;
5014
5029
  const rawPromptContent = templateParameters(content, { ...parameters, modelName });
5015
5030
  const rawRequest = {
5016
5031
  input: rawPromptContent,
@@ -5020,44 +5035,79 @@ class OpenAiCompatibleExecutionTools {
5020
5035
  if (this.options.isVerbose) {
5021
5036
  console.info(colors.bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
5022
5037
  }
5023
- const rawResponse = await this.limiter
5024
- .schedule(() => this.makeRequestWithNetworkRetry(() => client.embeddings.create(rawRequest)))
5025
- .catch((error) => {
5026
- assertsError(error);
5038
+ try {
5039
+ const rawResponse = await this.limiter
5040
+ .schedule(() => this.makeRequestWithNetworkRetry(() => client.embeddings.create(rawRequest)))
5041
+ .catch((error) => {
5042
+ assertsError(error);
5043
+ if (this.options.isVerbose) {
5044
+ console.info(colors.bgRed('error'), error);
5045
+ }
5046
+ throw error;
5047
+ });
5027
5048
  if (this.options.isVerbose) {
5028
- console.info(colors.bgRed('error'), error);
5049
+ console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
5029
5050
  }
5030
- throw error;
5031
- });
5032
- if (this.options.isVerbose) {
5033
- console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
5051
+ const complete = $getCurrentDate();
5052
+ if (rawResponse.data.length !== 1) {
5053
+ throw new PipelineExecutionError(`Expected exactly 1 data item in response, got ${rawResponse.data.length}`);
5054
+ }
5055
+ const resultContent = rawResponse.data[0].embedding;
5056
+ const usage = this.computeUsage(content || '', '',
5057
+ // <- Note: Embedding does not have result content
5058
+ rawResponse);
5059
+ return exportJson({
5060
+ name: 'promptResult',
5061
+ message: `Result of \`OpenAiCompatibleExecutionTools.callEmbeddingModel\``,
5062
+ order: [],
5063
+ value: {
5064
+ content: resultContent,
5065
+ modelName: rawResponse.model || modelName,
5066
+ timing: {
5067
+ start,
5068
+ complete,
5069
+ },
5070
+ usage,
5071
+ rawPromptContent,
5072
+ rawRequest,
5073
+ rawResponse,
5074
+ // <- [🗯]
5075
+ },
5076
+ });
5034
5077
  }
5035
- const complete = $getCurrentDate();
5036
- if (rawResponse.data.length !== 1) {
5037
- throw new PipelineExecutionError(`Expected exactly 1 data item in response, got ${rawResponse.data.length}`);
5078
+ catch (error) {
5079
+ assertsError(error);
5080
+ // Check if this is an unsupported parameter error
5081
+ if (!isUnsupportedParameterError(error)) {
5082
+ throw error;
5083
+ }
5084
+ // Parse which parameter is unsupported
5085
+ const unsupportedParameter = parseUnsupportedParameterError(error.message);
5086
+ if (!unsupportedParameter) {
5087
+ if (this.options.isVerbose) {
5088
+ console.warn(colors.bgYellow('Warning'), 'Could not parse unsupported parameter from error:', error.message);
5089
+ }
5090
+ throw error;
5091
+ }
5092
+ // Create a unique key for this model + parameter combination to prevent infinite loops
5093
+ const retryKey = `${modelName}-${unsupportedParameter}`;
5094
+ if (this.retriedUnsupportedParameters.has(retryKey)) {
5095
+ // Already retried this parameter, throw the error
5096
+ if (this.options.isVerbose) {
5097
+ console.warn(colors.bgRed('Error'), `Parameter '${unsupportedParameter}' for model '${modelName}' already retried once, throwing error:`, error.message);
5098
+ }
5099
+ throw error;
5100
+ }
5101
+ // Mark this parameter as retried
5102
+ this.retriedUnsupportedParameters.add(retryKey);
5103
+ // Log warning in verbose mode
5104
+ if (this.options.isVerbose) {
5105
+ console.warn(colors.bgYellow('Warning'), `Removing unsupported parameter '${unsupportedParameter}' for model '${modelName}' and retrying request`);
5106
+ }
5107
+ // Remove the unsupported parameter and retry
5108
+ const modifiedModelRequirements = removeUnsupportedModelRequirement(currentModelRequirements, unsupportedParameter);
5109
+ return this.callEmbeddingModelWithRetry(prompt, modifiedModelRequirements);
5038
5110
  }
5039
- const resultContent = rawResponse.data[0].embedding;
5040
- const usage = this.computeUsage(content || '', '',
5041
- // <- Note: Embedding does not have result content
5042
- rawResponse);
5043
- return exportJson({
5044
- name: 'promptResult',
5045
- message: `Result of \`OpenAiCompatibleExecutionTools.callEmbeddingModel\``,
5046
- order: [],
5047
- value: {
5048
- content: resultContent,
5049
- modelName: rawResponse.model || modelName,
5050
- timing: {
5051
- start,
5052
- complete,
5053
- },
5054
- usage,
5055
- rawPromptContent,
5056
- rawRequest,
5057
- rawResponse,
5058
- // <- [🗯]
5059
- },
5060
- });
5061
5111
  }
5062
5112
  // <- Note: [🤖] callXxxModel
5063
5113
  /**