@promptbook/openai 0.102.0-6 → 0.102.0-8

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/umd/index.umd.js CHANGED
@@ -25,7 +25,7 @@
25
25
  * @generated
26
26
  * @see https://github.com/webgptorg/promptbook
27
27
  */
28
- const PROMPTBOOK_ENGINE_VERSION = '0.102.0-6';
28
+ const PROMPTBOOK_ENGINE_VERSION = '0.102.0-8';
29
29
  /**
30
30
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
31
31
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -2010,6 +2010,7 @@
2010
2010
  * @public exported from `@promptbook/openai`
2011
2011
  */
2012
2012
  class OpenAiCompatibleExecutionTools {
2013
+ // Removed retriedUnsupportedParameters and attemptHistory instance fields
2013
2014
  /**
2014
2015
  * Creates OpenAI compatible Execution Tools.
2015
2016
  *
@@ -2021,14 +2022,6 @@
2021
2022
  * OpenAI API client.
2022
2023
  */
2023
2024
  this.client = null;
2024
- /**
2025
- * Tracks models and parameters that have already been retried to prevent infinite loops
2026
- */
2027
- this.retriedUnsupportedParameters = new Set();
2028
- /**
2029
- * Tracks the history of attempts for error reporting
2030
- */
2031
- this.attemptHistory = [];
2032
2025
  // TODO: Allow configuring rate limits via options
2033
2026
  this.limiter = new Bottleneck__default["default"]({
2034
2027
  minTime: 60000 / (this.options.maxRequestsPerMinute || DEFAULT_MAX_REQUESTS_PER_MINUTE),
@@ -2090,12 +2083,16 @@
2090
2083
  * Calls OpenAI compatible API to use a chat model.
2091
2084
  */
2092
2085
  async callChatModel(prompt) {
2093
- return this.callChatModelWithRetry(prompt, prompt.modelRequirements);
2086
+ // Deep clone prompt and modelRequirements to avoid mutation across calls
2087
+ const clonedPrompt = JSON.parse(JSON.stringify(prompt));
2088
+ // Use local Set for retried parameters to ensure independence and thread safety
2089
+ const retriedUnsupportedParameters = new Set();
2090
+ return this.callChatModelWithRetry(clonedPrompt, clonedPrompt.modelRequirements, [], retriedUnsupportedParameters);
2094
2091
  }
2095
2092
  /**
2096
2093
  * Internal method that handles parameter retry for chat model calls
2097
2094
  */
2098
- async callChatModelWithRetry(prompt, currentModelRequirements, attemptStack = []) {
2095
+ async callChatModelWithRetry(prompt, currentModelRequirements, attemptStack = [], retriedUnsupportedParameters = new Set()) {
2099
2096
  var _a;
2100
2097
  if (this.options.isVerbose) {
2101
2098
  console.info(`💬 ${this.title} callChatModel call`, { prompt, currentModelRequirements });
@@ -2205,10 +2202,12 @@
2205
2202
  // If we have attemptStack, include it in the error message
2206
2203
  if (attemptStack.length > 0) {
2207
2204
  throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2208
- attemptStack.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2205
+ attemptStack
2206
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2209
2207
  (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2210
2208
  `, Error: ${a.errorMessage}` +
2211
- (a.stripped ? ' (stripped and retried)' : '')).join('\n') +
2209
+ (a.stripped ? ' (stripped and retried)' : ''))
2210
+ .join('\n') +
2212
2211
  `\nFinal error: ${error.message}`);
2213
2212
  }
2214
2213
  throw error;
@@ -2223,7 +2222,7 @@
2223
2222
  }
2224
2223
  // Create a unique key for this model + parameter combination to prevent infinite loops
2225
2224
  const retryKey = `${modelName}-${unsupportedParameter}`;
2226
- if (this.retriedUnsupportedParameters.has(retryKey)) {
2225
+ if (retriedUnsupportedParameters.has(retryKey)) {
2227
2226
  // Already retried this parameter, throw the error with attemptStack
2228
2227
  attemptStack.push({
2229
2228
  modelName,
@@ -2232,14 +2231,16 @@
2232
2231
  stripped: true,
2233
2232
  });
2234
2233
  throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2235
- attemptStack.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2234
+ attemptStack
2235
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2236
2236
  (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2237
2237
  `, Error: ${a.errorMessage}` +
2238
- (a.stripped ? ' (stripped and retried)' : '')).join('\n') +
2238
+ (a.stripped ? ' (stripped and retried)' : ''))
2239
+ .join('\n') +
2239
2240
  `\nFinal error: ${error.message}`);
2240
2241
  }
2241
2242
  // Mark this parameter as retried
2242
- this.retriedUnsupportedParameters.add(retryKey);
2243
+ retriedUnsupportedParameters.add(retryKey);
2243
2244
  // Log warning in verbose mode
2244
2245
  if (this.options.isVerbose) {
2245
2246
  console.warn(colors__default["default"].bgYellow('Warning'), `Removing unsupported parameter '${unsupportedParameter}' for model '${modelName}' and retrying request`);
@@ -2253,19 +2254,22 @@
2253
2254
  });
2254
2255
  // Remove the unsupported parameter and retry
2255
2256
  const modifiedModelRequirements = removeUnsupportedModelRequirement(currentModelRequirements, unsupportedParameter);
2256
- return this.callChatModelWithRetry(prompt, modifiedModelRequirements, attemptStack);
2257
+ return this.callChatModelWithRetry(prompt, modifiedModelRequirements, attemptStack, retriedUnsupportedParameters);
2257
2258
  }
2258
2259
  }
2259
2260
  /**
2260
2261
  * Calls OpenAI API to use a complete model.
2261
2262
  */
2262
2263
  async callCompletionModel(prompt) {
2263
- return this.callCompletionModelWithRetry(prompt, prompt.modelRequirements);
2264
+ // Deep clone prompt and modelRequirements to avoid mutation across calls
2265
+ const clonedPrompt = JSON.parse(JSON.stringify(prompt));
2266
+ const retriedUnsupportedParameters = new Set();
2267
+ return this.callCompletionModelWithRetry(clonedPrompt, clonedPrompt.modelRequirements, [], retriedUnsupportedParameters);
2264
2268
  }
2265
2269
  /**
2266
2270
  * Internal method that handles parameter retry for completion model calls
2267
2271
  */
2268
- async callCompletionModelWithRetry(prompt, currentModelRequirements, attemptStack = []) {
2272
+ async callCompletionModelWithRetry(prompt, currentModelRequirements, attemptStack = [], retriedUnsupportedParameters = new Set()) {
2269
2273
  var _a;
2270
2274
  if (this.options.isVerbose) {
2271
2275
  console.info(`🖋 ${this.title} callCompletionModel call`, { prompt, currentModelRequirements });
@@ -2281,8 +2285,6 @@
2281
2285
  model: modelName,
2282
2286
  max_tokens: currentModelRequirements.maxTokens,
2283
2287
  temperature: currentModelRequirements.temperature,
2284
- // <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
2285
- // <- Note: [🧆]
2286
2288
  };
2287
2289
  const rawPromptContent = templateParameters(content, { ...parameters, modelName });
2288
2290
  const rawRequest = {
@@ -2312,7 +2314,6 @@
2312
2314
  throw new PipelineExecutionError(`No choises from ${this.title}`);
2313
2315
  }
2314
2316
  if (rawResponse.choices.length > 1) {
2315
- // TODO: This should be maybe only warning
2316
2317
  throw new PipelineExecutionError(`More than one choise from ${this.title}`);
2317
2318
  }
2318
2319
  const resultContent = rawResponse.choices[0].text;
@@ -2332,25 +2333,24 @@
2332
2333
  rawPromptContent,
2333
2334
  rawRequest,
2334
2335
  rawResponse,
2335
- // <- [🗯]
2336
2336
  },
2337
2337
  });
2338
2338
  }
2339
2339
  catch (error) {
2340
2340
  assertsError(error);
2341
- // Check if this is an unsupported parameter error
2342
2341
  if (!isUnsupportedParameterError(error)) {
2343
2342
  if (attemptStack.length > 0) {
2344
2343
  throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2345
- attemptStack.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2344
+ attemptStack
2345
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2346
2346
  (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2347
2347
  `, Error: ${a.errorMessage}` +
2348
- (a.stripped ? ' (stripped and retried)' : '')).join('\n') +
2348
+ (a.stripped ? ' (stripped and retried)' : ''))
2349
+ .join('\n') +
2349
2350
  `\nFinal error: ${error.message}`);
2350
2351
  }
2351
2352
  throw error;
2352
2353
  }
2353
- // Parse which parameter is unsupported
2354
2354
  const unsupportedParameter = parseUnsupportedParameterError(error.message);
2355
2355
  if (!unsupportedParameter) {
2356
2356
  if (this.options.isVerbose) {
@@ -2358,9 +2358,8 @@
2358
2358
  }
2359
2359
  throw error;
2360
2360
  }
2361
- // Create a unique key for this model + parameter combination to prevent infinite loops
2362
2361
  const retryKey = `${modelName}-${unsupportedParameter}`;
2363
- if (this.retriedUnsupportedParameters.has(retryKey)) {
2362
+ if (retriedUnsupportedParameters.has(retryKey)) {
2364
2363
  attemptStack.push({
2365
2364
  modelName,
2366
2365
  unsupportedParameter,
@@ -2368,15 +2367,15 @@
2368
2367
  stripped: true,
2369
2368
  });
2370
2369
  throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2371
- attemptStack.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2370
+ attemptStack
2371
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2372
2372
  (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2373
2373
  `, Error: ${a.errorMessage}` +
2374
- (a.stripped ? ' (stripped and retried)' : '')).join('\n') +
2374
+ (a.stripped ? ' (stripped and retried)' : ''))
2375
+ .join('\n') +
2375
2376
  `\nFinal error: ${error.message}`);
2376
2377
  }
2377
- // Mark this parameter as retried
2378
- this.retriedUnsupportedParameters.add(retryKey);
2379
- // Log warning in verbose mode
2378
+ retriedUnsupportedParameters.add(retryKey);
2380
2379
  if (this.options.isVerbose) {
2381
2380
  console.warn(colors__default["default"].bgYellow('Warning'), `Removing unsupported parameter '${unsupportedParameter}' for model '${modelName}' and retrying request`);
2382
2381
  }
@@ -2386,27 +2385,28 @@
2386
2385
  errorMessage: error.message,
2387
2386
  stripped: true,
2388
2387
  });
2389
- // Remove the unsupported parameter and retry
2390
2388
  const modifiedModelRequirements = removeUnsupportedModelRequirement(currentModelRequirements, unsupportedParameter);
2391
- return this.callCompletionModelWithRetry(prompt, modifiedModelRequirements, attemptStack);
2389
+ return this.callCompletionModelWithRetry(prompt, modifiedModelRequirements, attemptStack, retriedUnsupportedParameters);
2392
2390
  }
2393
2391
  }
2394
2392
  /**
2395
2393
  * Calls OpenAI compatible API to use a embedding model
2396
2394
  */
2397
2395
  async callEmbeddingModel(prompt) {
2398
- return this.callEmbeddingModelWithRetry(prompt, prompt.modelRequirements);
2396
+ // Deep clone prompt and modelRequirements to avoid mutation across calls
2397
+ const clonedPrompt = JSON.parse(JSON.stringify(prompt));
2398
+ const retriedUnsupportedParameters = new Set();
2399
+ return this.callEmbeddingModelWithRetry(clonedPrompt, clonedPrompt.modelRequirements, [], retriedUnsupportedParameters);
2399
2400
  }
2400
2401
  /**
2401
2402
  * Internal method that handles parameter retry for embedding model calls
2402
2403
  */
2403
- async callEmbeddingModelWithRetry(prompt, currentModelRequirements, attemptStack = []) {
2404
+ async callEmbeddingModelWithRetry(prompt, currentModelRequirements, attemptStack = [], retriedUnsupportedParameters = new Set()) {
2404
2405
  if (this.options.isVerbose) {
2405
2406
  console.info(`🖋 ${this.title} embedding call`, { prompt, currentModelRequirements });
2406
2407
  }
2407
2408
  const { content, parameters } = prompt;
2408
2409
  const client = await this.getClient();
2409
- // TODO: [☂] Use here more modelRequirements
2410
2410
  if (currentModelRequirements.modelVariant !== 'EMBEDDING') {
2411
2411
  throw new PipelineExecutionError('Use embed only for EMBEDDING variant');
2412
2412
  }
@@ -2438,9 +2438,7 @@
2438
2438
  throw new PipelineExecutionError(`Expected exactly 1 data item in response, got ${rawResponse.data.length}`);
2439
2439
  }
2440
2440
  const resultContent = rawResponse.data[0].embedding;
2441
- const usage = this.computeUsage(content || '', '',
2442
- // <- Note: Embedding does not have result content
2443
- rawResponse);
2441
+ const usage = this.computeUsage(content || '', '', rawResponse);
2444
2442
  return exportJson({
2445
2443
  name: 'promptResult',
2446
2444
  message: `Result of \`OpenAiCompatibleExecutionTools.callEmbeddingModel\``,
@@ -2456,25 +2454,24 @@
2456
2454
  rawPromptContent,
2457
2455
  rawRequest,
2458
2456
  rawResponse,
2459
- // <- [🗯]
2460
2457
  },
2461
2458
  });
2462
2459
  }
2463
2460
  catch (error) {
2464
2461
  assertsError(error);
2465
- // Check if this is an unsupported parameter error
2466
2462
  if (!isUnsupportedParameterError(error)) {
2467
2463
  if (attemptStack.length > 0) {
2468
2464
  throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2469
- attemptStack.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2465
+ attemptStack
2466
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2470
2467
  (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2471
2468
  `, Error: ${a.errorMessage}` +
2472
- (a.stripped ? ' (stripped and retried)' : '')).join('\n') +
2469
+ (a.stripped ? ' (stripped and retried)' : ''))
2470
+ .join('\n') +
2473
2471
  `\nFinal error: ${error.message}`);
2474
2472
  }
2475
2473
  throw error;
2476
2474
  }
2477
- // Parse which parameter is unsupported
2478
2475
  const unsupportedParameter = parseUnsupportedParameterError(error.message);
2479
2476
  if (!unsupportedParameter) {
2480
2477
  if (this.options.isVerbose) {
@@ -2482,9 +2479,8 @@
2482
2479
  }
2483
2480
  throw error;
2484
2481
  }
2485
- // Create a unique key for this model + parameter combination to prevent infinite loops
2486
2482
  const retryKey = `${modelName}-${unsupportedParameter}`;
2487
- if (this.retriedUnsupportedParameters.has(retryKey)) {
2483
+ if (retriedUnsupportedParameters.has(retryKey)) {
2488
2484
  attemptStack.push({
2489
2485
  modelName,
2490
2486
  unsupportedParameter,
@@ -2492,15 +2488,15 @@
2492
2488
  stripped: true,
2493
2489
  });
2494
2490
  throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
2495
- attemptStack.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2491
+ attemptStack
2492
+ .map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
2496
2493
  (a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
2497
2494
  `, Error: ${a.errorMessage}` +
2498
- (a.stripped ? ' (stripped and retried)' : '')).join('\n') +
2495
+ (a.stripped ? ' (stripped and retried)' : ''))
2496
+ .join('\n') +
2499
2497
  `\nFinal error: ${error.message}`);
2500
2498
  }
2501
- // Mark this parameter as retried
2502
- this.retriedUnsupportedParameters.add(retryKey);
2503
- // Log warning in verbose mode
2499
+ retriedUnsupportedParameters.add(retryKey);
2504
2500
  if (this.options.isVerbose) {
2505
2501
  console.warn(colors__default["default"].bgYellow('Warning'), `Removing unsupported parameter '${unsupportedParameter}' for model '${modelName}' and retrying request`);
2506
2502
  }
@@ -2510,9 +2506,8 @@
2510
2506
  errorMessage: error.message,
2511
2507
  stripped: true,
2512
2508
  });
2513
- // Remove the unsupported parameter and retry
2514
2509
  const modifiedModelRequirements = removeUnsupportedModelRequirement(currentModelRequirements, unsupportedParameter);
2515
- return this.callEmbeddingModelWithRetry(prompt, modifiedModelRequirements, attemptStack);
2510
+ return this.callEmbeddingModelWithRetry(prompt, modifiedModelRequirements, attemptStack, retriedUnsupportedParameters);
2516
2511
  }
2517
2512
  }
2518
2513
  // <- Note: [🤖] callXxxModel