@promptbook/openai 0.102.0-0 → 0.102.0-10
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 +189 -74
- package/esm/index.es.js.map +1 -1
- package/esm/typings/books/index.d.ts +81 -0
- package/esm/typings/src/_packages/components.index.d.ts +42 -22
- package/esm/typings/src/_packages/markdown-utils.index.d.ts +4 -0
- package/esm/typings/src/_packages/types.index.d.ts +18 -14
- package/esm/typings/src/_packages/utils.index.d.ts +12 -14
- package/esm/typings/src/book-2.0/agent-source/createAgentModelRequirements.d.ts +1 -1
- package/esm/typings/src/book-2.0/utils/generateGravatarUrl.d.ts +1 -1
- package/esm/typings/src/book-2.0/utils/generatePlaceholderAgentProfileImageUrl.d.ts +1 -1
- package/esm/typings/src/book-components/BookEditor/BookEditor.d.ts +1 -1
- package/esm/typings/src/book-components/BookEditor/BookEditorInner.d.ts +1 -1
- package/esm/typings/src/book-components/Chat/Chat/Chat.d.ts +1 -5
- package/esm/typings/src/book-components/Chat/Chat/ChatMessageItem.d.ts +35 -0
- package/esm/typings/src/book-components/Chat/Chat/ChatProps.d.ts +29 -7
- package/esm/typings/src/book-components/Chat/Chat/constants.d.ts +7 -0
- package/esm/typings/src/book-components/Chat/LlmChat/LlmChatProps.d.ts +19 -1
- package/esm/typings/src/book-components/{AvatarProfile/AvatarProfile → Chat/MockedChat}/MockedChat.d.ts +26 -9
- package/esm/typings/src/book-components/Chat/MockedChat/constants.d.ts +42 -0
- package/esm/typings/src/book-components/Chat/save/_common/ChatSaveFormatDefinition.d.ts +13 -0
- package/esm/typings/src/book-components/Chat/save/_common/getChatSaveFormatDefinitions.d.ts +8 -0
- package/esm/typings/src/book-components/Chat/save/_common/string_chat_format_name.d.ts +6 -0
- package/esm/typings/src/book-components/Chat/save/html/htmlSaveFormatDefinition.d.ts +12 -0
- package/esm/typings/src/book-components/Chat/save/index.d.ts +39 -0
- package/esm/typings/src/book-components/Chat/save/json/jsonSaveFormatDefinition.d.ts +12 -0
- package/esm/typings/src/book-components/Chat/save/markdown/mdSaveFormatDefinition.d.ts +12 -0
- package/esm/typings/src/book-components/Chat/save/pdf/pdfSaveFormatDefinition.d.ts +12 -0
- package/esm/typings/src/book-components/Chat/save/text/txtSaveFormatDefinition.d.ts +12 -0
- package/esm/typings/src/book-components/Chat/types/ChatMessage.d.ts +31 -5
- package/esm/typings/src/book-components/Chat/types/ChatParticipant.d.ts +3 -3
- package/esm/typings/src/book-components/Chat/utils/exportChatHistory.d.ts +3 -0
- package/esm/typings/src/book-components/icons/AttachmentIcon.d.ts +11 -0
- package/esm/typings/src/book-components/icons/CloseIcon.d.ts +11 -0
- package/esm/typings/src/execution/LlmExecutionToolsConstructor.d.ts +1 -1
- package/esm/typings/src/execution/execution-report/countWorkingDuration.d.ts +1 -1
- package/esm/typings/src/llm-providers/_common/register/$llmToolsMetadataRegister.d.ts +1 -1
- package/esm/typings/src/llm-providers/_common/register/$llmToolsRegister.d.ts +1 -1
- package/esm/typings/src/llm-providers/_common/register/$registeredLlmToolsMessage.d.ts +1 -2
- package/esm/typings/src/llm-providers/_common/register/LlmToolsConfiguration.d.ts +1 -1
- package/esm/typings/src/llm-providers/_common/register/LlmToolsMetadata.d.ts +1 -1
- package/esm/typings/src/llm-providers/_multiple/joinLlmExecutionTools.d.ts +1 -1
- package/esm/typings/src/llm-providers/agent/register-configuration.d.ts +1 -1
- package/esm/typings/src/llm-providers/agent/register-constructor.d.ts +1 -1
- package/esm/typings/src/llm-providers/anthropic-claude/register-configuration.d.ts +1 -1
- package/esm/typings/src/llm-providers/anthropic-claude/register-constructor.d.ts +1 -1
- package/esm/typings/src/llm-providers/azure-openai/register-configuration.d.ts +1 -1
- package/esm/typings/src/llm-providers/azure-openai/register-constructor.d.ts +1 -1
- package/esm/typings/src/llm-providers/deepseek/register-configuration.d.ts +1 -1
- package/esm/typings/src/llm-providers/deepseek/register-constructor.d.ts +1 -1
- package/esm/typings/src/llm-providers/google/register-configuration.d.ts +1 -1
- package/esm/typings/src/llm-providers/google/register-constructor.d.ts +1 -1
- package/esm/typings/src/llm-providers/ollama/register-configuration.d.ts +1 -1
- package/esm/typings/src/llm-providers/ollama/register-constructor.d.ts +1 -1
- package/esm/typings/src/llm-providers/openai/OpenAiAssistantExecutionTools.d.ts +1 -3
- package/esm/typings/src/llm-providers/openai/OpenAiCompatibleExecutionTools.d.ts +5 -5
- package/esm/typings/src/llm-providers/openai/register-configuration.d.ts +1 -1
- package/esm/typings/src/llm-providers/openai/register-constructor.d.ts +1 -1
- package/esm/typings/src/remote-server/types/RemoteServerOptions.d.ts +6 -0
- package/esm/typings/src/remote-server/ui/ServerApp.d.ts +13 -0
- package/esm/typings/src/remote-server/ui/renderServerIndexHtml.d.ts +7 -0
- package/esm/typings/src/remote-server/ui/types.d.ts +16 -0
- package/esm/typings/src/scrapers/_boilerplate/register-constructor.d.ts +1 -1
- package/esm/typings/src/scrapers/_boilerplate/register-metadata.d.ts +1 -1
- package/esm/typings/src/scrapers/_common/register/$scrapersMetadataRegister.d.ts +1 -1
- package/esm/typings/src/scrapers/_common/register/$scrapersRegister.d.ts +1 -1
- package/esm/typings/src/scrapers/_common/register/ScraperAndConverterMetadata.d.ts +2 -4
- package/esm/typings/src/scrapers/_common/register/ScraperConstructor.d.ts +1 -1
- package/esm/typings/src/scrapers/document/register-constructor.d.ts +1 -1
- package/esm/typings/src/scrapers/document/register-metadata.d.ts +1 -1
- package/esm/typings/src/scrapers/document-legacy/register-constructor.d.ts +1 -1
- package/esm/typings/src/scrapers/document-legacy/register-metadata.d.ts +1 -1
- package/esm/typings/src/scrapers/markdown/register-constructor.d.ts +1 -1
- package/esm/typings/src/scrapers/markdown/register-metadata.d.ts +1 -1
- package/esm/typings/src/scrapers/markitdown/register-constructor.d.ts +1 -1
- package/esm/typings/src/scrapers/markitdown/register-metadata.d.ts +1 -1
- package/esm/typings/src/scrapers/pdf/register-constructor.d.ts +1 -1
- package/esm/typings/src/scrapers/pdf/register-metadata.d.ts +1 -1
- package/esm/typings/src/scrapers/website/register-constructor.d.ts +1 -1
- package/esm/typings/src/scrapers/website/register-metadata.d.ts +1 -1
- package/esm/typings/src/scripting/javascript/postprocessing-functions.d.ts +6 -6
- package/esm/typings/src/types/Prompt.d.ts +5 -0
- package/esm/typings/src/utils/markdown/createMarkdownChart.d.ts +1 -1
- package/esm/typings/src/utils/markdown/humanizeAiText.d.ts +1 -1
- package/esm/typings/src/utils/markdown/humanizeAiTextEllipsis.d.ts +1 -1
- package/esm/typings/src/utils/markdown/humanizeAiTextEmdashed.d.ts +1 -1
- package/esm/typings/src/utils/markdown/humanizeAiTextQuotes.d.ts +1 -1
- package/esm/typings/src/utils/markdown/humanizeAiTextWhitespace.d.ts +1 -1
- package/esm/typings/src/utils/markdown/prettifyMarkdown.d.ts +2 -0
- package/esm/typings/src/utils/markdown/promptbookifyAiText.d.ts +1 -1
- package/esm/typings/src/utils/{trimCodeBlock.d.ts → markdown/trimCodeBlock.d.ts} +1 -1
- package/esm/typings/src/utils/{trimEndOfCodeBlock.d.ts → markdown/trimEndOfCodeBlock.d.ts} +1 -1
- package/esm/typings/src/utils/{$Register.d.ts → misc/$Register.d.ts} +2 -2
- package/esm/typings/src/utils/{$getCurrentDate.d.ts → misc/$getCurrentDate.d.ts} +1 -1
- package/esm/typings/src/utils/{arrayableToArray.d.ts → misc/arrayableToArray.d.ts} +1 -1
- package/esm/typings/src/utils/misc/debounce.d.ts +5 -0
- package/esm/typings/src/utils/{emojis.d.ts → misc/emojis.d.ts} +1 -1
- package/esm/typings/src/utils/misc/injectCssModuleIntoShadowRoot.d.ts +20 -0
- package/esm/typings/src/version.d.ts +1 -1
- package/package.json +2 -2
- package/umd/index.umd.js +189 -74
- package/umd/index.umd.js.map +1 -1
- package/esm/typings/src/book-components/AvatarProfile/AvatarProfile/index.d.ts +0 -3
- package/esm/typings/src/book-components/BookEditor/injectCssModuleIntoShadowRoot.d.ts +0 -11
- package/esm/typings/src/book-components/Chat/utils/savePlugins.d.ts +0 -55
- /package/esm/typings/src/{utils/arrayableToArray.test.d.ts → book-2.0/agent-source/createAgentModelRequirementsWithCommitments.delete.test.d.ts} +0 -0
- /package/esm/typings/src/utils/{trimCodeBlock.test.d.ts → markdown/trimCodeBlock.test.d.ts} +0 -0
- /package/esm/typings/src/utils/{trimEndOfCodeBlock.test.d.ts → markdown/trimEndOfCodeBlock.test.d.ts} +0 -0
- /package/esm/typings/src/utils/{FromtoItems.d.ts → misc/FromtoItems.d.ts} +0 -0
- /package/esm/typings/src/utils/{markdown/prettifyMarkdown.test.d.ts → misc/arrayableToArray.test.d.ts} +0 -0
- /package/esm/typings/src/utils/{parseNumber.d.ts → misc/parseNumber.d.ts} +0 -0
- /package/esm/typings/src/utils/{parseNumber.test.d.ts → misc/parseNumber.test.d.ts} +0 -0
- /package/esm/typings/src/utils/{removeEmojis.d.ts → normalization/removeEmojis.d.ts} +0 -0
- /package/esm/typings/src/utils/{removeEmojis.test.d.ts → normalization/removeEmojis.test.d.ts} +0 -0
- /package/esm/typings/src/utils/{removeQuotes.d.ts → normalization/removeQuotes.d.ts} +0 -0
- /package/esm/typings/src/utils/{removeQuotes.test.d.ts → normalization/removeQuotes.test.d.ts} +0 -0
- /package/esm/typings/src/utils/{unwrapResult.d.ts → normalization/unwrapResult.d.ts} +0 -0
- /package/esm/typings/src/utils/{unwrapResult.test.d.ts → normalization/unwrapResult.test.d.ts} +0 -0
package/esm/index.es.js
CHANGED
|
@@ -19,7 +19,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
|
19
19
|
* @generated
|
|
20
20
|
* @see https://github.com/webgptorg/promptbook
|
|
21
21
|
*/
|
|
22
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.102.0-
|
|
22
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.102.0-10';
|
|
23
23
|
/**
|
|
24
24
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
25
25
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -2004,6 +2004,7 @@ function isUnsupportedParameterError(error) {
|
|
|
2004
2004
|
* @public exported from `@promptbook/openai`
|
|
2005
2005
|
*/
|
|
2006
2006
|
class OpenAiCompatibleExecutionTools {
|
|
2007
|
+
// Removed retriedUnsupportedParameters and attemptHistory instance fields
|
|
2007
2008
|
/**
|
|
2008
2009
|
* Creates OpenAI compatible Execution Tools.
|
|
2009
2010
|
*
|
|
@@ -2015,10 +2016,6 @@ class OpenAiCompatibleExecutionTools {
|
|
|
2015
2016
|
* OpenAI API client.
|
|
2016
2017
|
*/
|
|
2017
2018
|
this.client = null;
|
|
2018
|
-
/**
|
|
2019
|
-
* Tracks models and parameters that have already been retried to prevent infinite loops
|
|
2020
|
-
*/
|
|
2021
|
-
this.retriedUnsupportedParameters = new Set();
|
|
2022
2019
|
// TODO: Allow configuring rate limits via options
|
|
2023
2020
|
this.limiter = new Bottleneck({
|
|
2024
2021
|
minTime: 60000 / (this.options.maxRequestsPerMinute || DEFAULT_MAX_REQUESTS_PER_MINUTE),
|
|
@@ -2080,12 +2077,16 @@ class OpenAiCompatibleExecutionTools {
|
|
|
2080
2077
|
* Calls OpenAI compatible API to use a chat model.
|
|
2081
2078
|
*/
|
|
2082
2079
|
async callChatModel(prompt) {
|
|
2083
|
-
|
|
2080
|
+
// Deep clone prompt and modelRequirements to avoid mutation across calls
|
|
2081
|
+
const clonedPrompt = JSON.parse(JSON.stringify(prompt));
|
|
2082
|
+
// Use local Set for retried parameters to ensure independence and thread safety
|
|
2083
|
+
const retriedUnsupportedParameters = new Set();
|
|
2084
|
+
return this.callChatModelWithRetry(clonedPrompt, clonedPrompt.modelRequirements, [], retriedUnsupportedParameters);
|
|
2084
2085
|
}
|
|
2085
2086
|
/**
|
|
2086
2087
|
* Internal method that handles parameter retry for chat model calls
|
|
2087
2088
|
*/
|
|
2088
|
-
async callChatModelWithRetry(prompt, currentModelRequirements) {
|
|
2089
|
+
async callChatModelWithRetry(prompt, currentModelRequirements, attemptStack = [], retriedUnsupportedParameters = new Set()) {
|
|
2089
2090
|
var _a;
|
|
2090
2091
|
if (this.options.isVerbose) {
|
|
2091
2092
|
console.info(`💬 ${this.title} callChatModel call`, { prompt, currentModelRequirements });
|
|
@@ -2112,6 +2113,14 @@ class OpenAiCompatibleExecutionTools {
|
|
|
2112
2113
|
// <- TODO: [🚸] Not all models are compatible with JSON mode
|
|
2113
2114
|
// > 'response_format' of type 'json_object' is not supported with this model.
|
|
2114
2115
|
const rawPromptContent = templateParameters(content, { ...parameters, modelName });
|
|
2116
|
+
// Convert thread to OpenAI format if present
|
|
2117
|
+
let threadMessages = [];
|
|
2118
|
+
if ('thread' in prompt && Array.isArray(prompt.thread)) {
|
|
2119
|
+
threadMessages = prompt.thread.map((msg) => ({
|
|
2120
|
+
role: msg.role === 'assistant' ? 'assistant' : 'user',
|
|
2121
|
+
content: msg.content,
|
|
2122
|
+
}));
|
|
2123
|
+
}
|
|
2115
2124
|
const rawRequest = {
|
|
2116
2125
|
...modelSettings,
|
|
2117
2126
|
messages: [
|
|
@@ -2123,6 +2132,7 @@ class OpenAiCompatibleExecutionTools {
|
|
|
2123
2132
|
content: currentModelRequirements.systemMessage,
|
|
2124
2133
|
},
|
|
2125
2134
|
]),
|
|
2135
|
+
...threadMessages,
|
|
2126
2136
|
{
|
|
2127
2137
|
role: 'user',
|
|
2128
2138
|
content: rawPromptContent,
|
|
@@ -2183,6 +2193,17 @@ class OpenAiCompatibleExecutionTools {
|
|
|
2183
2193
|
assertsError(error);
|
|
2184
2194
|
// Check if this is an unsupported parameter error
|
|
2185
2195
|
if (!isUnsupportedParameterError(error)) {
|
|
2196
|
+
// If we have attemptStack, include it in the error message
|
|
2197
|
+
if (attemptStack.length > 0) {
|
|
2198
|
+
throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
|
|
2199
|
+
attemptStack
|
|
2200
|
+
.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
|
|
2201
|
+
(a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
|
|
2202
|
+
`, Error: ${a.errorMessage}` +
|
|
2203
|
+
(a.stripped ? ' (stripped and retried)' : ''))
|
|
2204
|
+
.join('\n') +
|
|
2205
|
+
`\nFinal error: ${error.message}`);
|
|
2206
|
+
}
|
|
2186
2207
|
throw error;
|
|
2187
2208
|
}
|
|
2188
2209
|
// Parse which parameter is unsupported
|
|
@@ -2195,34 +2216,54 @@ class OpenAiCompatibleExecutionTools {
|
|
|
2195
2216
|
}
|
|
2196
2217
|
// Create a unique key for this model + parameter combination to prevent infinite loops
|
|
2197
2218
|
const retryKey = `${modelName}-${unsupportedParameter}`;
|
|
2198
|
-
if (
|
|
2199
|
-
// Already retried this parameter, throw the error
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2219
|
+
if (retriedUnsupportedParameters.has(retryKey)) {
|
|
2220
|
+
// Already retried this parameter, throw the error with attemptStack
|
|
2221
|
+
attemptStack.push({
|
|
2222
|
+
modelName,
|
|
2223
|
+
unsupportedParameter,
|
|
2224
|
+
errorMessage: error.message,
|
|
2225
|
+
stripped: true,
|
|
2226
|
+
});
|
|
2227
|
+
throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
|
|
2228
|
+
attemptStack
|
|
2229
|
+
.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
|
|
2230
|
+
(a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
|
|
2231
|
+
`, Error: ${a.errorMessage}` +
|
|
2232
|
+
(a.stripped ? ' (stripped and retried)' : ''))
|
|
2233
|
+
.join('\n') +
|
|
2234
|
+
`\nFinal error: ${error.message}`);
|
|
2204
2235
|
}
|
|
2205
2236
|
// Mark this parameter as retried
|
|
2206
|
-
|
|
2237
|
+
retriedUnsupportedParameters.add(retryKey);
|
|
2207
2238
|
// Log warning in verbose mode
|
|
2208
2239
|
if (this.options.isVerbose) {
|
|
2209
2240
|
console.warn(colors.bgYellow('Warning'), `Removing unsupported parameter '${unsupportedParameter}' for model '${modelName}' and retrying request`);
|
|
2210
2241
|
}
|
|
2242
|
+
// Add to attemptStack
|
|
2243
|
+
attemptStack.push({
|
|
2244
|
+
modelName,
|
|
2245
|
+
unsupportedParameter,
|
|
2246
|
+
errorMessage: error.message,
|
|
2247
|
+
stripped: true,
|
|
2248
|
+
});
|
|
2211
2249
|
// Remove the unsupported parameter and retry
|
|
2212
2250
|
const modifiedModelRequirements = removeUnsupportedModelRequirement(currentModelRequirements, unsupportedParameter);
|
|
2213
|
-
return this.callChatModelWithRetry(prompt, modifiedModelRequirements);
|
|
2251
|
+
return this.callChatModelWithRetry(prompt, modifiedModelRequirements, attemptStack, retriedUnsupportedParameters);
|
|
2214
2252
|
}
|
|
2215
2253
|
}
|
|
2216
2254
|
/**
|
|
2217
2255
|
* Calls OpenAI API to use a complete model.
|
|
2218
2256
|
*/
|
|
2219
2257
|
async callCompletionModel(prompt) {
|
|
2220
|
-
|
|
2258
|
+
// Deep clone prompt and modelRequirements to avoid mutation across calls
|
|
2259
|
+
const clonedPrompt = JSON.parse(JSON.stringify(prompt));
|
|
2260
|
+
const retriedUnsupportedParameters = new Set();
|
|
2261
|
+
return this.callCompletionModelWithRetry(clonedPrompt, clonedPrompt.modelRequirements, [], retriedUnsupportedParameters);
|
|
2221
2262
|
}
|
|
2222
2263
|
/**
|
|
2223
2264
|
* Internal method that handles parameter retry for completion model calls
|
|
2224
2265
|
*/
|
|
2225
|
-
async callCompletionModelWithRetry(prompt, currentModelRequirements) {
|
|
2266
|
+
async callCompletionModelWithRetry(prompt, currentModelRequirements, attemptStack = [], retriedUnsupportedParameters = new Set()) {
|
|
2226
2267
|
var _a;
|
|
2227
2268
|
if (this.options.isVerbose) {
|
|
2228
2269
|
console.info(`🖋 ${this.title} callCompletionModel call`, { prompt, currentModelRequirements });
|
|
@@ -2238,8 +2279,6 @@ class OpenAiCompatibleExecutionTools {
|
|
|
2238
2279
|
model: modelName,
|
|
2239
2280
|
max_tokens: currentModelRequirements.maxTokens,
|
|
2240
2281
|
temperature: currentModelRequirements.temperature,
|
|
2241
|
-
// <- TODO: [🈁] Use `seed` here AND/OR use is `isDeterministic` for entire execution tools
|
|
2242
|
-
// <- Note: [🧆]
|
|
2243
2282
|
};
|
|
2244
2283
|
const rawPromptContent = templateParameters(content, { ...parameters, modelName });
|
|
2245
2284
|
const rawRequest = {
|
|
@@ -2269,7 +2308,6 @@ class OpenAiCompatibleExecutionTools {
|
|
|
2269
2308
|
throw new PipelineExecutionError(`No choises from ${this.title}`);
|
|
2270
2309
|
}
|
|
2271
2310
|
if (rawResponse.choices.length > 1) {
|
|
2272
|
-
// TODO: This should be maybe only warning
|
|
2273
2311
|
throw new PipelineExecutionError(`More than one choise from ${this.title}`);
|
|
2274
2312
|
}
|
|
2275
2313
|
const resultContent = rawResponse.choices[0].text;
|
|
@@ -2289,17 +2327,24 @@ class OpenAiCompatibleExecutionTools {
|
|
|
2289
2327
|
rawPromptContent,
|
|
2290
2328
|
rawRequest,
|
|
2291
2329
|
rawResponse,
|
|
2292
|
-
// <- [🗯]
|
|
2293
2330
|
},
|
|
2294
2331
|
});
|
|
2295
2332
|
}
|
|
2296
2333
|
catch (error) {
|
|
2297
2334
|
assertsError(error);
|
|
2298
|
-
// Check if this is an unsupported parameter error
|
|
2299
2335
|
if (!isUnsupportedParameterError(error)) {
|
|
2336
|
+
if (attemptStack.length > 0) {
|
|
2337
|
+
throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
|
|
2338
|
+
attemptStack
|
|
2339
|
+
.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
|
|
2340
|
+
(a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
|
|
2341
|
+
`, Error: ${a.errorMessage}` +
|
|
2342
|
+
(a.stripped ? ' (stripped and retried)' : ''))
|
|
2343
|
+
.join('\n') +
|
|
2344
|
+
`\nFinal error: ${error.message}`);
|
|
2345
|
+
}
|
|
2300
2346
|
throw error;
|
|
2301
2347
|
}
|
|
2302
|
-
// Parse which parameter is unsupported
|
|
2303
2348
|
const unsupportedParameter = parseUnsupportedParameterError(error.message);
|
|
2304
2349
|
if (!unsupportedParameter) {
|
|
2305
2350
|
if (this.options.isVerbose) {
|
|
@@ -2307,40 +2352,59 @@ class OpenAiCompatibleExecutionTools {
|
|
|
2307
2352
|
}
|
|
2308
2353
|
throw error;
|
|
2309
2354
|
}
|
|
2310
|
-
// Create a unique key for this model + parameter combination to prevent infinite loops
|
|
2311
2355
|
const retryKey = `${modelName}-${unsupportedParameter}`;
|
|
2312
|
-
if (
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2356
|
+
if (retriedUnsupportedParameters.has(retryKey)) {
|
|
2357
|
+
attemptStack.push({
|
|
2358
|
+
modelName,
|
|
2359
|
+
unsupportedParameter,
|
|
2360
|
+
errorMessage: error.message,
|
|
2361
|
+
stripped: true,
|
|
2362
|
+
});
|
|
2363
|
+
throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
|
|
2364
|
+
attemptStack
|
|
2365
|
+
.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
|
|
2366
|
+
(a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
|
|
2367
|
+
`, Error: ${a.errorMessage}` +
|
|
2368
|
+
(a.stripped ? ' (stripped and retried)' : ''))
|
|
2369
|
+
.join('\n') +
|
|
2370
|
+
`\nFinal error: ${error.message}`);
|
|
2318
2371
|
}
|
|
2319
|
-
|
|
2320
|
-
this.retriedUnsupportedParameters.add(retryKey);
|
|
2321
|
-
// Log warning in verbose mode
|
|
2372
|
+
retriedUnsupportedParameters.add(retryKey);
|
|
2322
2373
|
if (this.options.isVerbose) {
|
|
2323
2374
|
console.warn(colors.bgYellow('Warning'), `Removing unsupported parameter '${unsupportedParameter}' for model '${modelName}' and retrying request`);
|
|
2324
2375
|
}
|
|
2325
|
-
|
|
2376
|
+
attemptStack.push({
|
|
2377
|
+
modelName,
|
|
2378
|
+
unsupportedParameter,
|
|
2379
|
+
errorMessage: error.message,
|
|
2380
|
+
stripped: true,
|
|
2381
|
+
});
|
|
2326
2382
|
const modifiedModelRequirements = removeUnsupportedModelRequirement(currentModelRequirements, unsupportedParameter);
|
|
2327
|
-
return this.callCompletionModelWithRetry(prompt, modifiedModelRequirements);
|
|
2383
|
+
return this.callCompletionModelWithRetry(prompt, modifiedModelRequirements, attemptStack, retriedUnsupportedParameters);
|
|
2328
2384
|
}
|
|
2329
2385
|
}
|
|
2330
2386
|
/**
|
|
2331
2387
|
* Calls OpenAI compatible API to use a embedding model
|
|
2332
2388
|
*/
|
|
2333
2389
|
async callEmbeddingModel(prompt) {
|
|
2390
|
+
// Deep clone prompt and modelRequirements to avoid mutation across calls
|
|
2391
|
+
const clonedPrompt = JSON.parse(JSON.stringify(prompt));
|
|
2392
|
+
const retriedUnsupportedParameters = new Set();
|
|
2393
|
+
return this.callEmbeddingModelWithRetry(clonedPrompt, clonedPrompt.modelRequirements, [], retriedUnsupportedParameters);
|
|
2394
|
+
}
|
|
2395
|
+
/**
|
|
2396
|
+
* Internal method that handles parameter retry for embedding model calls
|
|
2397
|
+
*/
|
|
2398
|
+
async callEmbeddingModelWithRetry(prompt, currentModelRequirements, attemptStack = [], retriedUnsupportedParameters = new Set()) {
|
|
2334
2399
|
if (this.options.isVerbose) {
|
|
2335
|
-
console.info(`🖋 ${this.title} embedding call`, { prompt });
|
|
2400
|
+
console.info(`🖋 ${this.title} embedding call`, { prompt, currentModelRequirements });
|
|
2336
2401
|
}
|
|
2337
|
-
const { content, parameters
|
|
2402
|
+
const { content, parameters } = prompt;
|
|
2338
2403
|
const client = await this.getClient();
|
|
2339
|
-
|
|
2340
|
-
if (modelRequirements.modelVariant !== 'EMBEDDING') {
|
|
2404
|
+
if (currentModelRequirements.modelVariant !== 'EMBEDDING') {
|
|
2341
2405
|
throw new PipelineExecutionError('Use embed only for EMBEDDING variant');
|
|
2342
2406
|
}
|
|
2343
|
-
const modelName =
|
|
2407
|
+
const modelName = currentModelRequirements.modelName || this.getDefaultEmbeddingModel().modelName;
|
|
2344
2408
|
const rawPromptContent = templateParameters(content, { ...parameters, modelName });
|
|
2345
2409
|
const rawRequest = {
|
|
2346
2410
|
input: rawPromptContent,
|
|
@@ -2350,44 +2414,95 @@ class OpenAiCompatibleExecutionTools {
|
|
|
2350
2414
|
if (this.options.isVerbose) {
|
|
2351
2415
|
console.info(colors.bgWhite('rawRequest'), JSON.stringify(rawRequest, null, 4));
|
|
2352
2416
|
}
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2417
|
+
try {
|
|
2418
|
+
const rawResponse = await this.limiter
|
|
2419
|
+
.schedule(() => this.makeRequestWithNetworkRetry(() => client.embeddings.create(rawRequest)))
|
|
2420
|
+
.catch((error) => {
|
|
2421
|
+
assertsError(error);
|
|
2422
|
+
if (this.options.isVerbose) {
|
|
2423
|
+
console.info(colors.bgRed('error'), error);
|
|
2424
|
+
}
|
|
2425
|
+
throw error;
|
|
2426
|
+
});
|
|
2357
2427
|
if (this.options.isVerbose) {
|
|
2358
|
-
console.info(colors.
|
|
2428
|
+
console.info(colors.bgWhite('rawResponse'), JSON.stringify(rawResponse, null, 4));
|
|
2359
2429
|
}
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2430
|
+
const complete = $getCurrentDate();
|
|
2431
|
+
if (rawResponse.data.length !== 1) {
|
|
2432
|
+
throw new PipelineExecutionError(`Expected exactly 1 data item in response, got ${rawResponse.data.length}`);
|
|
2433
|
+
}
|
|
2434
|
+
const resultContent = rawResponse.data[0].embedding;
|
|
2435
|
+
const usage = this.computeUsage(content || '', '', rawResponse);
|
|
2436
|
+
return exportJson({
|
|
2437
|
+
name: 'promptResult',
|
|
2438
|
+
message: `Result of \`OpenAiCompatibleExecutionTools.callEmbeddingModel\``,
|
|
2439
|
+
order: [],
|
|
2440
|
+
value: {
|
|
2441
|
+
content: resultContent,
|
|
2442
|
+
modelName: rawResponse.model || modelName,
|
|
2443
|
+
timing: {
|
|
2444
|
+
start,
|
|
2445
|
+
complete,
|
|
2446
|
+
},
|
|
2447
|
+
usage,
|
|
2448
|
+
rawPromptContent,
|
|
2449
|
+
rawRequest,
|
|
2450
|
+
rawResponse,
|
|
2451
|
+
},
|
|
2452
|
+
});
|
|
2364
2453
|
}
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2454
|
+
catch (error) {
|
|
2455
|
+
assertsError(error);
|
|
2456
|
+
if (!isUnsupportedParameterError(error)) {
|
|
2457
|
+
if (attemptStack.length > 0) {
|
|
2458
|
+
throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
|
|
2459
|
+
attemptStack
|
|
2460
|
+
.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
|
|
2461
|
+
(a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
|
|
2462
|
+
`, Error: ${a.errorMessage}` +
|
|
2463
|
+
(a.stripped ? ' (stripped and retried)' : ''))
|
|
2464
|
+
.join('\n') +
|
|
2465
|
+
`\nFinal error: ${error.message}`);
|
|
2466
|
+
}
|
|
2467
|
+
throw error;
|
|
2468
|
+
}
|
|
2469
|
+
const unsupportedParameter = parseUnsupportedParameterError(error.message);
|
|
2470
|
+
if (!unsupportedParameter) {
|
|
2471
|
+
if (this.options.isVerbose) {
|
|
2472
|
+
console.warn(colors.bgYellow('Warning'), 'Could not parse unsupported parameter from error:', error.message);
|
|
2473
|
+
}
|
|
2474
|
+
throw error;
|
|
2475
|
+
}
|
|
2476
|
+
const retryKey = `${modelName}-${unsupportedParameter}`;
|
|
2477
|
+
if (retriedUnsupportedParameters.has(retryKey)) {
|
|
2478
|
+
attemptStack.push({
|
|
2479
|
+
modelName,
|
|
2480
|
+
unsupportedParameter,
|
|
2481
|
+
errorMessage: error.message,
|
|
2482
|
+
stripped: true,
|
|
2483
|
+
});
|
|
2484
|
+
throw new PipelineExecutionError(`All attempts failed. Attempt history:\n` +
|
|
2485
|
+
attemptStack
|
|
2486
|
+
.map((a, i) => ` ${i + 1}. Model: ${a.modelName}` +
|
|
2487
|
+
(a.unsupportedParameter ? `, Stripped: ${a.unsupportedParameter}` : '') +
|
|
2488
|
+
`, Error: ${a.errorMessage}` +
|
|
2489
|
+
(a.stripped ? ' (stripped and retried)' : ''))
|
|
2490
|
+
.join('\n') +
|
|
2491
|
+
`\nFinal error: ${error.message}`);
|
|
2492
|
+
}
|
|
2493
|
+
retriedUnsupportedParameters.add(retryKey);
|
|
2494
|
+
if (this.options.isVerbose) {
|
|
2495
|
+
console.warn(colors.bgYellow('Warning'), `Removing unsupported parameter '${unsupportedParameter}' for model '${modelName}' and retrying request`);
|
|
2496
|
+
}
|
|
2497
|
+
attemptStack.push({
|
|
2498
|
+
modelName,
|
|
2499
|
+
unsupportedParameter,
|
|
2500
|
+
errorMessage: error.message,
|
|
2501
|
+
stripped: true,
|
|
2502
|
+
});
|
|
2503
|
+
const modifiedModelRequirements = removeUnsupportedModelRequirement(currentModelRequirements, unsupportedParameter);
|
|
2504
|
+
return this.callEmbeddingModelWithRetry(prompt, modifiedModelRequirements, attemptStack, retriedUnsupportedParameters);
|
|
2368
2505
|
}
|
|
2369
|
-
const resultContent = rawResponse.data[0].embedding;
|
|
2370
|
-
const usage = this.computeUsage(content || '', '',
|
|
2371
|
-
// <- Note: Embedding does not have result content
|
|
2372
|
-
rawResponse);
|
|
2373
|
-
return exportJson({
|
|
2374
|
-
name: 'promptResult',
|
|
2375
|
-
message: `Result of \`OpenAiCompatibleExecutionTools.callEmbeddingModel\``,
|
|
2376
|
-
order: [],
|
|
2377
|
-
value: {
|
|
2378
|
-
content: resultContent,
|
|
2379
|
-
modelName: rawResponse.model || modelName,
|
|
2380
|
-
timing: {
|
|
2381
|
-
start,
|
|
2382
|
-
complete,
|
|
2383
|
-
},
|
|
2384
|
-
usage,
|
|
2385
|
-
rawPromptContent,
|
|
2386
|
-
rawRequest,
|
|
2387
|
-
rawResponse,
|
|
2388
|
-
// <- [🗯]
|
|
2389
|
-
},
|
|
2390
|
-
});
|
|
2391
2506
|
}
|
|
2392
2507
|
// <- Note: [🤖] callXxxModel
|
|
2393
2508
|
/**
|