modelfusion 0.105.0 → 0.106.0
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/CHANGELOG.md +11 -0
- package/README.md +8 -10
- package/core/DefaultRun.cjs +0 -4
- package/core/DefaultRun.d.ts +0 -2
- package/core/DefaultRun.js +0 -4
- package/core/ExtensionFunctionEvent.d.ts +11 -0
- package/core/FunctionEvent.d.ts +2 -2
- package/extension/index.cjs +22 -3
- package/extension/index.d.ts +5 -1
- package/extension/index.js +4 -1
- package/index.cjs +0 -3
- package/index.d.ts +0 -3
- package/index.js +0 -3
- package/model-function/generate-structure/jsonStructurePrompt.cjs +42 -6
- package/model-function/generate-structure/jsonStructurePrompt.d.ts +12 -1
- package/model-function/generate-structure/jsonStructurePrompt.js +42 -5
- package/model-function/generate-text/PromptTemplateTextGenerationModel.d.ts +2 -1
- package/model-function/generate-text/PromptTemplateTextGenerationModel.js +1 -1
- package/model-function/generate-text/prompt-template/Llama2PromptTemplate.cjs +1 -1
- package/model-function/generate-text/prompt-template/Llama2PromptTemplate.d.ts +1 -1
- package/model-function/generate-text/prompt-template/Llama2PromptTemplate.js +1 -1
- package/model-function/index.cjs +0 -1
- package/model-function/index.d.ts +0 -1
- package/model-function/index.js +0 -1
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +6 -6
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +23 -23
- package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +13 -13
- package/model-provider/ollama/OllamaChatModel.d.ts +9 -8
- package/model-provider/ollama/OllamaChatModel.js +1 -1
- package/model-provider/ollama/OllamaCompletionModel.d.ts +2 -1
- package/model-provider/ollama/OllamaCompletionModel.js +1 -1
- package/model-provider/ollama/OllamaCompletionModel.test.cjs +1 -7
- package/model-provider/ollama/OllamaCompletionModel.test.js +1 -7
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +12 -12
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +11 -11
- package/model-provider/openai/index.cjs +0 -1
- package/model-provider/openai/index.d.ts +0 -1
- package/model-provider/openai/index.js +0 -1
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +5 -5
- package/package.json +9 -20
- package/tool/generate-tool-call/TextGenerationToolCallModel.cjs +1 -1
- package/tool/generate-tool-call/TextGenerationToolCallModel.d.ts +1 -1
- package/tool/generate-tool-call/TextGenerationToolCallModel.js +1 -1
- package/tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.d.ts +1 -11
- package/tool/generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.d.ts +12 -0
- package/tool/generate-tool-calls-or-text/index.cjs +1 -0
- package/tool/generate-tool-calls-or-text/index.d.ts +1 -0
- package/tool/generate-tool-calls-or-text/index.js +1 -0
- package/util/index.cjs +0 -1
- package/util/index.d.ts +0 -1
- package/util/index.js +0 -1
- package/browser/MediaSourceAppender.cjs +0 -54
- package/browser/MediaSourceAppender.d.ts +0 -11
- package/browser/MediaSourceAppender.js +0 -50
- package/browser/convertAudioChunksToBase64.cjs +0 -8
- package/browser/convertAudioChunksToBase64.d.ts +0 -4
- package/browser/convertAudioChunksToBase64.js +0 -4
- package/browser/convertBlobToBase64.cjs +0 -23
- package/browser/convertBlobToBase64.d.ts +0 -1
- package/browser/convertBlobToBase64.js +0 -19
- package/browser/index.cjs +0 -22
- package/browser/index.d.ts +0 -6
- package/browser/index.js +0 -6
- package/browser/invokeFlow.cjs +0 -23
- package/browser/invokeFlow.d.ts +0 -8
- package/browser/invokeFlow.js +0 -19
- package/browser/readEventSource.cjs +0 -29
- package/browser/readEventSource.d.ts +0 -9
- package/browser/readEventSource.js +0 -25
- package/browser/readEventSourceStream.cjs +0 -35
- package/browser/readEventSourceStream.d.ts +0 -7
- package/browser/readEventSourceStream.js +0 -31
- package/composed-function/index.cjs +0 -19
- package/composed-function/index.d.ts +0 -3
- package/composed-function/index.js +0 -3
- package/composed-function/summarize/SummarizationFunction.d.ts +0 -4
- package/composed-function/summarize/summarizeRecursively.cjs +0 -19
- package/composed-function/summarize/summarizeRecursively.d.ts +0 -11
- package/composed-function/summarize/summarizeRecursively.js +0 -15
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.cjs +0 -25
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.d.ts +0 -24
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js +0 -21
- package/cost/Cost.cjs +0 -38
- package/cost/Cost.d.ts +0 -16
- package/cost/Cost.js +0 -34
- package/cost/CostCalculator.d.ts +0 -8
- package/cost/calculateCost.cjs +0 -28
- package/cost/calculateCost.d.ts +0 -7
- package/cost/calculateCost.js +0 -24
- package/cost/index.cjs +0 -19
- package/cost/index.d.ts +0 -3
- package/cost/index.js +0 -3
- package/guard/GuardEvent.cjs +0 -2
- package/guard/GuardEvent.d.ts +0 -7
- package/guard/GuardEvent.js +0 -1
- package/guard/fixStructure.cjs +0 -75
- package/guard/fixStructure.d.ts +0 -64
- package/guard/fixStructure.js +0 -71
- package/guard/guard.cjs +0 -79
- package/guard/guard.d.ts +0 -29
- package/guard/guard.js +0 -75
- package/guard/index.cjs +0 -19
- package/guard/index.d.ts +0 -3
- package/guard/index.js +0 -3
- package/model-function/SuccessfulModelCall.cjs +0 -10
- package/model-function/SuccessfulModelCall.d.ts +0 -12
- package/model-function/SuccessfulModelCall.js +0 -6
- package/model-provider/openai/OpenAICostCalculator.cjs +0 -89
- package/model-provider/openai/OpenAICostCalculator.d.ts +0 -6
- package/model-provider/openai/OpenAICostCalculator.js +0 -85
- package/server/fastify/AssetStorage.cjs +0 -2
- package/server/fastify/AssetStorage.d.ts +0 -17
- package/server/fastify/AssetStorage.js +0 -1
- package/server/fastify/DefaultFlow.cjs +0 -22
- package/server/fastify/DefaultFlow.d.ts +0 -16
- package/server/fastify/DefaultFlow.js +0 -18
- package/server/fastify/FileSystemAssetStorage.cjs +0 -60
- package/server/fastify/FileSystemAssetStorage.d.ts +0 -19
- package/server/fastify/FileSystemAssetStorage.js +0 -56
- package/server/fastify/FileSystemLogger.cjs +0 -49
- package/server/fastify/FileSystemLogger.d.ts +0 -18
- package/server/fastify/FileSystemLogger.js +0 -45
- package/server/fastify/Flow.cjs +0 -2
- package/server/fastify/Flow.d.ts +0 -9
- package/server/fastify/Flow.js +0 -1
- package/server/fastify/FlowRun.cjs +0 -71
- package/server/fastify/FlowRun.d.ts +0 -28
- package/server/fastify/FlowRun.js +0 -67
- package/server/fastify/FlowSchema.cjs +0 -2
- package/server/fastify/FlowSchema.d.ts +0 -5
- package/server/fastify/FlowSchema.js +0 -1
- package/server/fastify/Logger.cjs +0 -2
- package/server/fastify/Logger.d.ts +0 -13
- package/server/fastify/Logger.js +0 -1
- package/server/fastify/PathProvider.cjs +0 -34
- package/server/fastify/PathProvider.d.ts +0 -12
- package/server/fastify/PathProvider.js +0 -30
- package/server/fastify/index.cjs +0 -24
- package/server/fastify/index.d.ts +0 -8
- package/server/fastify/index.js +0 -8
- package/server/fastify/modelFusionFlowPlugin.cjs +0 -103
- package/server/fastify/modelFusionFlowPlugin.d.ts +0 -12
- package/server/fastify/modelFusionFlowPlugin.js +0 -99
- package/util/getAudioFileExtension.cjs +0 -29
- package/util/getAudioFileExtension.d.ts +0 -1
- package/util/getAudioFileExtension.js +0 -25
- /package/{composed-function/summarize/SummarizationFunction.cjs → core/ExtensionFunctionEvent.cjs} +0 -0
- /package/{composed-function/summarize/SummarizationFunction.js → core/ExtensionFunctionEvent.js} +0 -0
- /package/{cost/CostCalculator.cjs → tool/generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.cjs} +0 -0
- /package/{cost/CostCalculator.js → tool/generate-tool-calls-or-text/ToolCallsOrGenerateTextPromptTemplate.js} +0 -0
package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.js
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
import { generateText } from "../../model-function/generate-text/generateText.js";
|
2
|
-
import { splitAtToken } from "../../text-chunk/split/splitRecursively.js";
|
3
|
-
import { summarizeRecursively } from "./summarizeRecursively.js";
|
4
|
-
/**
|
5
|
-
* Recursively summarizes a text using a text generation model, e.g. for summarization or text extraction.
|
6
|
-
* It automatically splits the text into optimal chunks that are small enough to be processed by the model,
|
7
|
-
* while leaving enough space for the model to generate text.
|
8
|
-
*/
|
9
|
-
export async function summarizeRecursivelyWithTextGenerationAndTokenSplitting({ text, model, prompt, tokenLimit = model.contextWindowSize -
|
10
|
-
(model.settings.maxGenerationTokens ?? model.contextWindowSize / 4), join, }, options) {
|
11
|
-
const emptyPromptTokens = await model.countPromptTokens(await prompt({ text: "" }));
|
12
|
-
return summarizeRecursively({
|
13
|
-
split: splitAtToken({
|
14
|
-
tokenizer: model.tokenizer,
|
15
|
-
maxTokensPerChunk: tokenLimit - emptyPromptTokens,
|
16
|
-
}),
|
17
|
-
summarize: async (input) => generateText(model, await prompt(input), options),
|
18
|
-
join,
|
19
|
-
text,
|
20
|
-
}, options);
|
21
|
-
}
|
package/cost/Cost.cjs
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.Cost = void 0;
|
4
|
-
class Cost {
|
5
|
-
constructor({ costInMillicents, hasUnknownCost, callsWithUnknownCost, }) {
|
6
|
-
Object.defineProperty(this, "costInMillicents", {
|
7
|
-
enumerable: true,
|
8
|
-
configurable: true,
|
9
|
-
writable: true,
|
10
|
-
value: void 0
|
11
|
-
});
|
12
|
-
Object.defineProperty(this, "hasUnknownCost", {
|
13
|
-
enumerable: true,
|
14
|
-
configurable: true,
|
15
|
-
writable: true,
|
16
|
-
value: void 0
|
17
|
-
});
|
18
|
-
Object.defineProperty(this, "callsWithUnknownCost", {
|
19
|
-
enumerable: true,
|
20
|
-
configurable: true,
|
21
|
-
writable: true,
|
22
|
-
value: void 0
|
23
|
-
});
|
24
|
-
this.costInMillicents = costInMillicents;
|
25
|
-
this.hasUnknownCost = hasUnknownCost;
|
26
|
-
this.callsWithUnknownCost = callsWithUnknownCost;
|
27
|
-
}
|
28
|
-
get costInCent() {
|
29
|
-
return this.costInMillicents / 1000;
|
30
|
-
}
|
31
|
-
get costInDollar() {
|
32
|
-
return this.costInCent / 100;
|
33
|
-
}
|
34
|
-
formatAsDollarAmount({ decimals = 2 } = {}) {
|
35
|
-
return `$${this.costInDollar.toFixed(decimals)}`;
|
36
|
-
}
|
37
|
-
}
|
38
|
-
exports.Cost = Cost;
|
package/cost/Cost.d.ts
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
import { SuccessfulModelCall } from "../model-function/SuccessfulModelCall.js";
|
2
|
-
export declare class Cost {
|
3
|
-
readonly costInMillicents: number;
|
4
|
-
readonly hasUnknownCost: boolean;
|
5
|
-
readonly callsWithUnknownCost: SuccessfulModelCall[];
|
6
|
-
constructor({ costInMillicents, hasUnknownCost, callsWithUnknownCost, }: {
|
7
|
-
costInMillicents: number;
|
8
|
-
hasUnknownCost: boolean;
|
9
|
-
callsWithUnknownCost: SuccessfulModelCall[];
|
10
|
-
});
|
11
|
-
get costInCent(): number;
|
12
|
-
get costInDollar(): number;
|
13
|
-
formatAsDollarAmount({ decimals }?: {
|
14
|
-
decimals?: number;
|
15
|
-
}): string;
|
16
|
-
}
|
package/cost/Cost.js
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
export class Cost {
|
2
|
-
constructor({ costInMillicents, hasUnknownCost, callsWithUnknownCost, }) {
|
3
|
-
Object.defineProperty(this, "costInMillicents", {
|
4
|
-
enumerable: true,
|
5
|
-
configurable: true,
|
6
|
-
writable: true,
|
7
|
-
value: void 0
|
8
|
-
});
|
9
|
-
Object.defineProperty(this, "hasUnknownCost", {
|
10
|
-
enumerable: true,
|
11
|
-
configurable: true,
|
12
|
-
writable: true,
|
13
|
-
value: void 0
|
14
|
-
});
|
15
|
-
Object.defineProperty(this, "callsWithUnknownCost", {
|
16
|
-
enumerable: true,
|
17
|
-
configurable: true,
|
18
|
-
writable: true,
|
19
|
-
value: void 0
|
20
|
-
});
|
21
|
-
this.costInMillicents = costInMillicents;
|
22
|
-
this.hasUnknownCost = hasUnknownCost;
|
23
|
-
this.callsWithUnknownCost = callsWithUnknownCost;
|
24
|
-
}
|
25
|
-
get costInCent() {
|
26
|
-
return this.costInMillicents / 1000;
|
27
|
-
}
|
28
|
-
get costInDollar() {
|
29
|
-
return this.costInCent / 100;
|
30
|
-
}
|
31
|
-
formatAsDollarAmount({ decimals = 2 } = {}) {
|
32
|
-
return `$${this.costInDollar.toFixed(decimals)}`;
|
33
|
-
}
|
34
|
-
}
|
package/cost/CostCalculator.d.ts
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
import { SuccessfulModelCall } from "../model-function/SuccessfulModelCall.js";
|
2
|
-
export interface CostCalculator {
|
3
|
-
readonly provider: string;
|
4
|
-
/**
|
5
|
-
* @return null if the cost is unknown, otherwise the cost in Millicents (0 if free)
|
6
|
-
*/
|
7
|
-
calculateCostInMillicents(call: SuccessfulModelCall): PromiseLike<number | null>;
|
8
|
-
}
|
package/cost/calculateCost.cjs
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.calculateCost = void 0;
|
4
|
-
const Cost_js_1 = require("./Cost.cjs");
|
5
|
-
async function calculateCost({ calls, costCalculators, }) {
|
6
|
-
let costInMillicents = 0;
|
7
|
-
const callsWithUnknownCost = [];
|
8
|
-
for (const call of calls) {
|
9
|
-
const model = call.model;
|
10
|
-
const providerCostCalculator = costCalculators.find((providerCostCalculator) => providerCostCalculator.provider === model.provider);
|
11
|
-
if (!providerCostCalculator) {
|
12
|
-
callsWithUnknownCost.push(call);
|
13
|
-
continue;
|
14
|
-
}
|
15
|
-
const cost = await providerCostCalculator.calculateCostInMillicents(call);
|
16
|
-
if (cost === null) {
|
17
|
-
callsWithUnknownCost.push(call);
|
18
|
-
continue;
|
19
|
-
}
|
20
|
-
costInMillicents += cost;
|
21
|
-
}
|
22
|
-
return new Cost_js_1.Cost({
|
23
|
-
costInMillicents,
|
24
|
-
hasUnknownCost: callsWithUnknownCost.length > 0,
|
25
|
-
callsWithUnknownCost,
|
26
|
-
});
|
27
|
-
}
|
28
|
-
exports.calculateCost = calculateCost;
|
package/cost/calculateCost.d.ts
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
import { Cost } from "./Cost.js";
|
2
|
-
import { CostCalculator } from "./CostCalculator.js";
|
3
|
-
import { SuccessfulModelCall } from "../model-function/SuccessfulModelCall.js";
|
4
|
-
export declare function calculateCost({ calls, costCalculators, }: {
|
5
|
-
calls: SuccessfulModelCall[];
|
6
|
-
costCalculators: CostCalculator[];
|
7
|
-
}): Promise<Cost>;
|
package/cost/calculateCost.js
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
import { Cost } from "./Cost.js";
|
2
|
-
export async function calculateCost({ calls, costCalculators, }) {
|
3
|
-
let costInMillicents = 0;
|
4
|
-
const callsWithUnknownCost = [];
|
5
|
-
for (const call of calls) {
|
6
|
-
const model = call.model;
|
7
|
-
const providerCostCalculator = costCalculators.find((providerCostCalculator) => providerCostCalculator.provider === model.provider);
|
8
|
-
if (!providerCostCalculator) {
|
9
|
-
callsWithUnknownCost.push(call);
|
10
|
-
continue;
|
11
|
-
}
|
12
|
-
const cost = await providerCostCalculator.calculateCostInMillicents(call);
|
13
|
-
if (cost === null) {
|
14
|
-
callsWithUnknownCost.push(call);
|
15
|
-
continue;
|
16
|
-
}
|
17
|
-
costInMillicents += cost;
|
18
|
-
}
|
19
|
-
return new Cost({
|
20
|
-
costInMillicents,
|
21
|
-
hasUnknownCost: callsWithUnknownCost.length > 0,
|
22
|
-
callsWithUnknownCost,
|
23
|
-
});
|
24
|
-
}
|
package/cost/index.cjs
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
-
if (k2 === undefined) k2 = k;
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
-
}
|
8
|
-
Object.defineProperty(o, k2, desc);
|
9
|
-
}) : (function(o, m, k, k2) {
|
10
|
-
if (k2 === undefined) k2 = k;
|
11
|
-
o[k2] = m[k];
|
12
|
-
}));
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
-
};
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
__exportStar(require("./Cost.cjs"), exports);
|
18
|
-
__exportStar(require("./calculateCost.cjs"), exports);
|
19
|
-
__exportStar(require("./CostCalculator.cjs"), exports);
|
package/cost/index.d.ts
DELETED
package/cost/index.js
DELETED
package/guard/GuardEvent.cjs
DELETED
package/guard/GuardEvent.d.ts
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/FunctionEvent.js";
|
2
|
-
export interface GuardStartedEvent extends BaseFunctionStartedEvent {
|
3
|
-
functionType: "guard";
|
4
|
-
}
|
5
|
-
export interface GuardFinishedEvent extends BaseFunctionFinishedEvent {
|
6
|
-
functionType: "guard";
|
7
|
-
}
|
package/guard/GuardEvent.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
package/guard/fixStructure.cjs
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.fixStructure = void 0;
|
4
|
-
const StructureParseError_js_1 = require("../model-function/generate-structure/StructureParseError.cjs");
|
5
|
-
const StructureValidationError_js_1 = require("../model-function/generate-structure/StructureValidationError.cjs");
|
6
|
-
/**
|
7
|
-
* Attempts to correct and retry structure generation when encountering parsing or validation errors.
|
8
|
-
*
|
9
|
-
* This function acts as a guard within the structure generation process. If the generation results in
|
10
|
-
* an error, identified as either a `StructureParseError` or `StructureValidationError`, the function
|
11
|
-
* triggers a retry mechanism. It uses the `modifyInputForRetry` method, provided via options, to adjust
|
12
|
-
* the input parameters, aiming to resolve the issue that caused the initial error. The process continues
|
13
|
-
* until a valid structure is generated, or it exhausts the predefined retry limits.
|
14
|
-
*
|
15
|
-
* @template INPUT - The expected format/type of the input data used for structure generation.
|
16
|
-
* @template OUTPUT - The expected format/type of the output data, i.e., the successfully generated structure.
|
17
|
-
*
|
18
|
-
* @param {Object} options - Configuration options for modifying the input before retrying structure generation.
|
19
|
-
* @param {function} options.modifyInputForRetry - A function that takes the error type, original input, and error object.
|
20
|
-
* It modifies the input data based on the error information, aiming to correct the issue for the retry attempt.
|
21
|
-
* This function must return a promise that resolves with the modified input.
|
22
|
-
*
|
23
|
-
* @returns {Guard<INPUT, OUTPUT>} A guard function that intercepts the structure generation process, checking for
|
24
|
-
* errors, and initiating retries by modifying the input parameters. The guard can trigger multiple retries
|
25
|
-
* if the issues persist. If the process succeeds, it returns the valid structure; otherwise, it returns
|
26
|
-
* undefined, indicating the exhaustion of retry attempts or a non-recoverable error.
|
27
|
-
*
|
28
|
-
* @example
|
29
|
-
* const result = await guard(
|
30
|
-
* (input) =>
|
31
|
-
* generateStructure(
|
32
|
-
* openai
|
33
|
-
* .ChatTextGenerator(/* ... * /)
|
34
|
-
* .asFunctionCallStructureGenerationModel(/* ... * /),
|
35
|
-
*
|
36
|
-
* new ZodSchema({
|
37
|
-
* // ...
|
38
|
-
* }),
|
39
|
-
*
|
40
|
-
* input
|
41
|
-
* ),
|
42
|
-
* [
|
43
|
-
* // ...
|
44
|
-
* ],
|
45
|
-
* fixStructure({
|
46
|
-
* modifyInputForRetry: async ({ input, error }) => [
|
47
|
-
* ...input,
|
48
|
-
* openai.ChatMessage.assistant(null, {
|
49
|
-
* functionCall: {
|
50
|
-
* name: "sentiment",
|
51
|
-
* arguments: JSON.stringify(error.valueText),
|
52
|
-
* },
|
53
|
-
* }),
|
54
|
-
* openai.ChatMessage.user(error.message),
|
55
|
-
* openai.ChatMessage.user("Please fix the error and try again."),
|
56
|
-
* ],
|
57
|
-
* })
|
58
|
-
* );
|
59
|
-
*/
|
60
|
-
const fixStructure = ({ modifyInputForRetry }) => async (result) => {
|
61
|
-
if (result.type === "error" &&
|
62
|
-
(result.error instanceof StructureValidationError_js_1.StructureValidationError ||
|
63
|
-
result.error instanceof StructureParseError_js_1.StructureParseError)) {
|
64
|
-
return {
|
65
|
-
action: "retry",
|
66
|
-
input: await modifyInputForRetry({
|
67
|
-
type: "error",
|
68
|
-
input: result.input,
|
69
|
-
error: result.error,
|
70
|
-
}),
|
71
|
-
};
|
72
|
-
}
|
73
|
-
return undefined;
|
74
|
-
};
|
75
|
-
exports.fixStructure = fixStructure;
|
package/guard/fixStructure.d.ts
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
import { StructureParseError } from "../model-function/generate-structure/StructureParseError.js";
|
2
|
-
import { StructureValidationError } from "../model-function/generate-structure/StructureValidationError.js";
|
3
|
-
import { Guard } from "./guard.js";
|
4
|
-
/**
|
5
|
-
* Attempts to correct and retry structure generation when encountering parsing or validation errors.
|
6
|
-
*
|
7
|
-
* This function acts as a guard within the structure generation process. If the generation results in
|
8
|
-
* an error, identified as either a `StructureParseError` or `StructureValidationError`, the function
|
9
|
-
* triggers a retry mechanism. It uses the `modifyInputForRetry` method, provided via options, to adjust
|
10
|
-
* the input parameters, aiming to resolve the issue that caused the initial error. The process continues
|
11
|
-
* until a valid structure is generated, or it exhausts the predefined retry limits.
|
12
|
-
*
|
13
|
-
* @template INPUT - The expected format/type of the input data used for structure generation.
|
14
|
-
* @template OUTPUT - The expected format/type of the output data, i.e., the successfully generated structure.
|
15
|
-
*
|
16
|
-
* @param {Object} options - Configuration options for modifying the input before retrying structure generation.
|
17
|
-
* @param {function} options.modifyInputForRetry - A function that takes the error type, original input, and error object.
|
18
|
-
* It modifies the input data based on the error information, aiming to correct the issue for the retry attempt.
|
19
|
-
* This function must return a promise that resolves with the modified input.
|
20
|
-
*
|
21
|
-
* @returns {Guard<INPUT, OUTPUT>} A guard function that intercepts the structure generation process, checking for
|
22
|
-
* errors, and initiating retries by modifying the input parameters. The guard can trigger multiple retries
|
23
|
-
* if the issues persist. If the process succeeds, it returns the valid structure; otherwise, it returns
|
24
|
-
* undefined, indicating the exhaustion of retry attempts or a non-recoverable error.
|
25
|
-
*
|
26
|
-
* @example
|
27
|
-
* const result = await guard(
|
28
|
-
* (input) =>
|
29
|
-
* generateStructure(
|
30
|
-
* openai
|
31
|
-
* .ChatTextGenerator(/* ... * /)
|
32
|
-
* .asFunctionCallStructureGenerationModel(/* ... * /),
|
33
|
-
*
|
34
|
-
* new ZodSchema({
|
35
|
-
* // ...
|
36
|
-
* }),
|
37
|
-
*
|
38
|
-
* input
|
39
|
-
* ),
|
40
|
-
* [
|
41
|
-
* // ...
|
42
|
-
* ],
|
43
|
-
* fixStructure({
|
44
|
-
* modifyInputForRetry: async ({ input, error }) => [
|
45
|
-
* ...input,
|
46
|
-
* openai.ChatMessage.assistant(null, {
|
47
|
-
* functionCall: {
|
48
|
-
* name: "sentiment",
|
49
|
-
* arguments: JSON.stringify(error.valueText),
|
50
|
-
* },
|
51
|
-
* }),
|
52
|
-
* openai.ChatMessage.user(error.message),
|
53
|
-
* openai.ChatMessage.user("Please fix the error and try again."),
|
54
|
-
* ],
|
55
|
-
* })
|
56
|
-
* );
|
57
|
-
*/
|
58
|
-
export declare const fixStructure: <INPUT, OUTPUT>(options: {
|
59
|
-
modifyInputForRetry: (options: {
|
60
|
-
type: "error";
|
61
|
-
input: INPUT;
|
62
|
-
error: StructureValidationError | StructureParseError;
|
63
|
-
}) => PromiseLike<INPUT>;
|
64
|
-
}) => Guard<INPUT, OUTPUT>;
|
package/guard/fixStructure.js
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
import { StructureParseError } from "../model-function/generate-structure/StructureParseError.js";
|
2
|
-
import { StructureValidationError } from "../model-function/generate-structure/StructureValidationError.js";
|
3
|
-
/**
|
4
|
-
* Attempts to correct and retry structure generation when encountering parsing or validation errors.
|
5
|
-
*
|
6
|
-
* This function acts as a guard within the structure generation process. If the generation results in
|
7
|
-
* an error, identified as either a `StructureParseError` or `StructureValidationError`, the function
|
8
|
-
* triggers a retry mechanism. It uses the `modifyInputForRetry` method, provided via options, to adjust
|
9
|
-
* the input parameters, aiming to resolve the issue that caused the initial error. The process continues
|
10
|
-
* until a valid structure is generated, or it exhausts the predefined retry limits.
|
11
|
-
*
|
12
|
-
* @template INPUT - The expected format/type of the input data used for structure generation.
|
13
|
-
* @template OUTPUT - The expected format/type of the output data, i.e., the successfully generated structure.
|
14
|
-
*
|
15
|
-
* @param {Object} options - Configuration options for modifying the input before retrying structure generation.
|
16
|
-
* @param {function} options.modifyInputForRetry - A function that takes the error type, original input, and error object.
|
17
|
-
* It modifies the input data based on the error information, aiming to correct the issue for the retry attempt.
|
18
|
-
* This function must return a promise that resolves with the modified input.
|
19
|
-
*
|
20
|
-
* @returns {Guard<INPUT, OUTPUT>} A guard function that intercepts the structure generation process, checking for
|
21
|
-
* errors, and initiating retries by modifying the input parameters. The guard can trigger multiple retries
|
22
|
-
* if the issues persist. If the process succeeds, it returns the valid structure; otherwise, it returns
|
23
|
-
* undefined, indicating the exhaustion of retry attempts or a non-recoverable error.
|
24
|
-
*
|
25
|
-
* @example
|
26
|
-
* const result = await guard(
|
27
|
-
* (input) =>
|
28
|
-
* generateStructure(
|
29
|
-
* openai
|
30
|
-
* .ChatTextGenerator(/* ... * /)
|
31
|
-
* .asFunctionCallStructureGenerationModel(/* ... * /),
|
32
|
-
*
|
33
|
-
* new ZodSchema({
|
34
|
-
* // ...
|
35
|
-
* }),
|
36
|
-
*
|
37
|
-
* input
|
38
|
-
* ),
|
39
|
-
* [
|
40
|
-
* // ...
|
41
|
-
* ],
|
42
|
-
* fixStructure({
|
43
|
-
* modifyInputForRetry: async ({ input, error }) => [
|
44
|
-
* ...input,
|
45
|
-
* openai.ChatMessage.assistant(null, {
|
46
|
-
* functionCall: {
|
47
|
-
* name: "sentiment",
|
48
|
-
* arguments: JSON.stringify(error.valueText),
|
49
|
-
* },
|
50
|
-
* }),
|
51
|
-
* openai.ChatMessage.user(error.message),
|
52
|
-
* openai.ChatMessage.user("Please fix the error and try again."),
|
53
|
-
* ],
|
54
|
-
* })
|
55
|
-
* );
|
56
|
-
*/
|
57
|
-
export const fixStructure = ({ modifyInputForRetry }) => async (result) => {
|
58
|
-
if (result.type === "error" &&
|
59
|
-
(result.error instanceof StructureValidationError ||
|
60
|
-
result.error instanceof StructureParseError)) {
|
61
|
-
return {
|
62
|
-
action: "retry",
|
63
|
-
input: await modifyInputForRetry({
|
64
|
-
type: "error",
|
65
|
-
input: result.input,
|
66
|
-
error: result.error,
|
67
|
-
}),
|
68
|
-
};
|
69
|
-
}
|
70
|
-
return undefined;
|
71
|
-
};
|
package/guard/guard.cjs
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.guard = void 0;
|
4
|
-
const executeFunctionCall_js_1 = require("../core/executeFunctionCall.cjs");
|
5
|
-
async function guard(execute, input, guards, options) {
|
6
|
-
const guardList = Array.isArray(guards) ? guards : [guards];
|
7
|
-
const maxAttempts = options?.maxAttempts ?? 2;
|
8
|
-
return (0, executeFunctionCall_js_1.executeFunctionCall)({
|
9
|
-
options,
|
10
|
-
input,
|
11
|
-
functionType: "guard",
|
12
|
-
execute: async (options) => {
|
13
|
-
let attempts = 0;
|
14
|
-
while (attempts < maxAttempts) {
|
15
|
-
let result;
|
16
|
-
try {
|
17
|
-
result = {
|
18
|
-
type: "value",
|
19
|
-
input,
|
20
|
-
output: await execute(input, options),
|
21
|
-
};
|
22
|
-
}
|
23
|
-
catch (error) {
|
24
|
-
result = {
|
25
|
-
type: "error",
|
26
|
-
input,
|
27
|
-
error,
|
28
|
-
};
|
29
|
-
}
|
30
|
-
let isValid = true;
|
31
|
-
for (const guard of guardList) {
|
32
|
-
const guardResult = await guard(result);
|
33
|
-
if (guardResult === undefined) {
|
34
|
-
continue;
|
35
|
-
}
|
36
|
-
switch (guardResult.action) {
|
37
|
-
case "passThrough": {
|
38
|
-
break;
|
39
|
-
}
|
40
|
-
case "retry": {
|
41
|
-
input = guardResult.input;
|
42
|
-
isValid = false;
|
43
|
-
break;
|
44
|
-
}
|
45
|
-
case "return": {
|
46
|
-
result = {
|
47
|
-
type: "value",
|
48
|
-
input,
|
49
|
-
output: guardResult.output,
|
50
|
-
};
|
51
|
-
break;
|
52
|
-
}
|
53
|
-
case "throwError": {
|
54
|
-
result = {
|
55
|
-
type: "error",
|
56
|
-
input,
|
57
|
-
error: guardResult.error,
|
58
|
-
};
|
59
|
-
break;
|
60
|
-
}
|
61
|
-
}
|
62
|
-
}
|
63
|
-
if (isValid) {
|
64
|
-
if (result.type === "value") {
|
65
|
-
return result.output;
|
66
|
-
}
|
67
|
-
else {
|
68
|
-
throw result.error;
|
69
|
-
}
|
70
|
-
}
|
71
|
-
attempts++;
|
72
|
-
}
|
73
|
-
// TODO dedicated error type
|
74
|
-
throw new Error(`Maximum attempts of ${maxAttempts} reached ` +
|
75
|
-
`without producing a valid output or handling an error.`);
|
76
|
-
},
|
77
|
-
});
|
78
|
-
}
|
79
|
-
exports.guard = guard;
|
package/guard/guard.d.ts
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
import { FunctionOptions } from "../core/FunctionOptions.js";
|
2
|
-
type OutputResult<INPUT, OUTPUT> = {
|
3
|
-
type: "value";
|
4
|
-
input: INPUT;
|
5
|
-
output: OUTPUT;
|
6
|
-
error?: undefined;
|
7
|
-
} | {
|
8
|
-
type: "error";
|
9
|
-
input: INPUT;
|
10
|
-
output?: undefined;
|
11
|
-
error: unknown;
|
12
|
-
};
|
13
|
-
export type OutputValidator<INPUT, OUTPUT> = ({ type, input, output, error, }: OutputResult<INPUT, OUTPUT>) => PromiseLike<boolean>;
|
14
|
-
export type Guard<INPUT, OUTPUT> = ({ type, input, output, error, }: OutputResult<INPUT, OUTPUT>) => PromiseLike<{
|
15
|
-
action: "retry";
|
16
|
-
input: INPUT;
|
17
|
-
} | {
|
18
|
-
action: "return";
|
19
|
-
output: OUTPUT;
|
20
|
-
} | {
|
21
|
-
action: "throwError";
|
22
|
-
error: unknown;
|
23
|
-
} | {
|
24
|
-
action: "passThrough";
|
25
|
-
} | undefined>;
|
26
|
-
export declare function guard<INPUT, OUTPUT>(execute: (input: INPUT, options?: FunctionOptions) => PromiseLike<OUTPUT>, input: INPUT, guards: Guard<INPUT, OUTPUT> | Array<Guard<INPUT, OUTPUT>>, options?: FunctionOptions & {
|
27
|
-
maxAttempts: number;
|
28
|
-
}): Promise<OUTPUT | undefined>;
|
29
|
-
export {};
|
package/guard/guard.js
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
import { executeFunctionCall } from "../core/executeFunctionCall.js";
|
2
|
-
export async function guard(execute, input, guards, options) {
|
3
|
-
const guardList = Array.isArray(guards) ? guards : [guards];
|
4
|
-
const maxAttempts = options?.maxAttempts ?? 2;
|
5
|
-
return executeFunctionCall({
|
6
|
-
options,
|
7
|
-
input,
|
8
|
-
functionType: "guard",
|
9
|
-
execute: async (options) => {
|
10
|
-
let attempts = 0;
|
11
|
-
while (attempts < maxAttempts) {
|
12
|
-
let result;
|
13
|
-
try {
|
14
|
-
result = {
|
15
|
-
type: "value",
|
16
|
-
input,
|
17
|
-
output: await execute(input, options),
|
18
|
-
};
|
19
|
-
}
|
20
|
-
catch (error) {
|
21
|
-
result = {
|
22
|
-
type: "error",
|
23
|
-
input,
|
24
|
-
error,
|
25
|
-
};
|
26
|
-
}
|
27
|
-
let isValid = true;
|
28
|
-
for (const guard of guardList) {
|
29
|
-
const guardResult = await guard(result);
|
30
|
-
if (guardResult === undefined) {
|
31
|
-
continue;
|
32
|
-
}
|
33
|
-
switch (guardResult.action) {
|
34
|
-
case "passThrough": {
|
35
|
-
break;
|
36
|
-
}
|
37
|
-
case "retry": {
|
38
|
-
input = guardResult.input;
|
39
|
-
isValid = false;
|
40
|
-
break;
|
41
|
-
}
|
42
|
-
case "return": {
|
43
|
-
result = {
|
44
|
-
type: "value",
|
45
|
-
input,
|
46
|
-
output: guardResult.output,
|
47
|
-
};
|
48
|
-
break;
|
49
|
-
}
|
50
|
-
case "throwError": {
|
51
|
-
result = {
|
52
|
-
type: "error",
|
53
|
-
input,
|
54
|
-
error: guardResult.error,
|
55
|
-
};
|
56
|
-
break;
|
57
|
-
}
|
58
|
-
}
|
59
|
-
}
|
60
|
-
if (isValid) {
|
61
|
-
if (result.type === "value") {
|
62
|
-
return result.output;
|
63
|
-
}
|
64
|
-
else {
|
65
|
-
throw result.error;
|
66
|
-
}
|
67
|
-
}
|
68
|
-
attempts++;
|
69
|
-
}
|
70
|
-
// TODO dedicated error type
|
71
|
-
throw new Error(`Maximum attempts of ${maxAttempts} reached ` +
|
72
|
-
`without producing a valid output or handling an error.`);
|
73
|
-
},
|
74
|
-
});
|
75
|
-
}
|
package/guard/index.cjs
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
-
if (k2 === undefined) k2 = k;
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
-
}
|
8
|
-
Object.defineProperty(o, k2, desc);
|
9
|
-
}) : (function(o, m, k, k2) {
|
10
|
-
if (k2 === undefined) k2 = k;
|
11
|
-
o[k2] = m[k];
|
12
|
-
}));
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
-
};
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
__exportStar(require("./GuardEvent.cjs"), exports);
|
18
|
-
__exportStar(require("./fixStructure.cjs"), exports);
|
19
|
-
__exportStar(require("./guard.cjs"), exports);
|
package/guard/index.d.ts
DELETED
package/guard/index.js
DELETED