modelfusion 0.48.0 → 0.50.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/README.md +186 -183
- package/core/FunctionEvent.d.ts +9 -1
- package/core/FunctionOptions.d.ts +4 -0
- package/core/executeFunctionCall.cjs +85 -0
- package/core/executeFunctionCall.d.ts +10 -0
- package/core/executeFunctionCall.js +81 -0
- package/guard/GuardEvent.d.ts +7 -0
- package/guard/guard.cjs +60 -54
- package/guard/guard.d.ts +2 -1
- package/guard/guard.js +60 -54
- package/guard/index.cjs +1 -0
- package/guard/index.d.ts +1 -0
- package/guard/index.js +1 -0
- package/model-function/AsyncIterableResultPromise.cjs +5 -5
- package/model-function/AsyncIterableResultPromise.d.ts +3 -3
- package/model-function/AsyncIterableResultPromise.js +5 -5
- package/model-function/Model.d.ts +1 -1
- package/model-function/ModelCallEvent.d.ts +5 -7
- package/model-function/embed/EmbeddingEvent.d.ts +2 -2
- package/model-function/embed/embed.cjs +5 -5
- package/model-function/embed/embed.js +5 -5
- package/model-function/{executeCall.cjs → executeStandardCall.cjs} +5 -3
- package/model-function/{executeCall.d.ts → executeStandardCall.d.ts} +1 -1
- package/model-function/{executeCall.js → executeStandardCall.js} +3 -1
- package/model-function/executeStreamCall.cjs +134 -0
- package/model-function/executeStreamCall.d.ts +20 -0
- package/model-function/executeStreamCall.js +130 -0
- package/model-function/generate-image/ImageGenerationEvent.d.ts +2 -2
- package/model-function/generate-image/generateImage.cjs +3 -3
- package/model-function/generate-image/generateImage.js +3 -3
- package/model-function/generate-speech/SpeechGenerationEvent.d.ts +27 -0
- package/model-function/generate-speech/SpeechGenerationModel.d.ts +15 -0
- package/model-function/generate-speech/generateSpeech.cjs +24 -0
- package/model-function/generate-speech/generateSpeech.d.ts +8 -0
- package/model-function/generate-speech/generateSpeech.js +20 -0
- package/model-function/generate-speech/index.cjs +20 -0
- package/model-function/generate-speech/index.d.ts +4 -0
- package/model-function/generate-speech/index.js +4 -0
- package/model-function/generate-speech/streamSpeech.cjs +34 -0
- package/model-function/generate-speech/streamSpeech.d.ts +8 -0
- package/model-function/generate-speech/streamSpeech.js +30 -0
- package/model-function/generate-structure/StructureGenerationEvent.d.ts +2 -2
- package/model-function/generate-structure/StructureStreamingEvent.d.ts +2 -2
- package/model-function/generate-structure/generateStructure.cjs +3 -3
- package/model-function/generate-structure/generateStructure.js +3 -3
- package/model-function/generate-structure/generateStructureOrText.cjs +3 -3
- package/model-function/generate-structure/generateStructureOrText.js +3 -3
- package/model-function/generate-structure/index.cjs +27 -0
- package/model-function/generate-structure/index.d.ts +11 -0
- package/model-function/generate-structure/index.js +11 -0
- package/model-function/generate-structure/streamStructure.cjs +29 -137
- package/model-function/generate-structure/streamStructure.js +28 -136
- package/model-function/generate-text/TextGenerationEvent.d.ts +8 -2
- package/model-function/generate-text/generateText.cjs +4 -4
- package/model-function/generate-text/generateText.d.ts +1 -1
- package/model-function/generate-text/generateText.js +4 -4
- package/model-function/generate-text/index.cjs +0 -1
- package/model-function/generate-text/index.d.ts +0 -1
- package/model-function/generate-text/index.js +0 -1
- package/model-function/generate-text/streamText.cjs +22 -129
- package/model-function/generate-text/streamText.js +21 -128
- package/model-function/generate-text/trimChatPrompt.cjs +1 -1
- package/model-function/generate-text/trimChatPrompt.d.ts +1 -1
- package/model-function/generate-text/trimChatPrompt.js +1 -1
- package/model-function/{transcribe-speech → generate-transcription}/TranscriptionEvent.d.ts +2 -2
- package/model-function/{transcribe-speech/transcribe.cjs → generate-transcription/generateTranscription.cjs} +7 -7
- package/model-function/{transcribe-speech/transcribe.d.ts → generate-transcription/generateTranscription.d.ts} +2 -2
- package/model-function/{transcribe-speech/transcribe.js → generate-transcription/generateTranscription.js} +5 -5
- package/model-function/index.cjs +5 -20
- package/model-function/index.d.ts +5 -20
- package/model-function/index.js +5 -20
- package/model-provider/elevenlabs/{ElevenLabsSpeechSynthesisModel.cjs → ElevenLabsSpeechModel.cjs} +6 -6
- package/model-provider/elevenlabs/{ElevenLabsSpeechSynthesisModel.d.ts → ElevenLabsSpeechModel.d.ts} +8 -8
- package/model-provider/elevenlabs/{ElevenLabsSpeechSynthesisModel.js → ElevenLabsSpeechModel.js} +4 -4
- package/model-provider/elevenlabs/index.cjs +1 -1
- package/model-provider/elevenlabs/index.d.ts +1 -1
- package/model-provider/elevenlabs/index.js +1 -1
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.cjs +21 -2
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.d.ts +11 -6
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.js +21 -2
- package/model-provider/lmnt/{LmntSpeechSynthesisModel.cjs → LmntSpeechModel.cjs} +5 -5
- package/model-provider/lmnt/LmntSpeechModel.d.ts +26 -0
- package/model-provider/lmnt/{LmntSpeechSynthesisModel.js → LmntSpeechModel.js} +3 -3
- package/model-provider/lmnt/index.cjs +1 -1
- package/model-provider/lmnt/index.d.ts +1 -1
- package/model-provider/lmnt/index.js +1 -1
- package/model-provider/openai/{OpenAITextGenerationModel.cjs → OpenAICompletionModel.cjs} +17 -17
- package/model-provider/openai/{OpenAITextGenerationModel.d.ts → OpenAICompletionModel.d.ts} +29 -29
- package/model-provider/openai/{OpenAITextGenerationModel.js → OpenAICompletionModel.js} +12 -12
- package/model-provider/openai/OpenAICostCalculator.cjs +8 -8
- package/model-provider/openai/OpenAICostCalculator.js +8 -8
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +3 -3
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +1 -1
- package/model-provider/openai/TikTokenTokenizer.d.ts +2 -2
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +7 -7
- package/model-provider/openai/index.cjs +1 -1
- package/model-provider/openai/index.d.ts +1 -1
- package/model-provider/openai/index.js +1 -1
- package/package.json +1 -1
- package/retriever/retrieve.cjs +7 -75
- package/retriever/retrieve.js +7 -75
- package/tool/UseToolEvent.d.ts +7 -0
- package/tool/UseToolOrGenerateTextEvent.d.ts +7 -0
- package/tool/executeTool.cjs +2 -0
- package/tool/executeTool.js +2 -0
- package/tool/index.cjs +2 -0
- package/tool/index.d.ts +2 -0
- package/tool/index.js +2 -0
- package/tool/useTool.cjs +18 -10
- package/tool/useTool.js +18 -10
- package/tool/useToolOrGenerateText.cjs +34 -26
- package/tool/useToolOrGenerateText.js +34 -26
- package/vector-index/UpsertIntoVectorIndexEvent.cjs +2 -0
- package/vector-index/UpsertIntoVectorIndexEvent.d.ts +9 -0
- package/vector-index/UpsertIntoVectorIndexEvent.js +1 -0
- package/vector-index/VectorIndexRetriever.cjs +1 -4
- package/vector-index/VectorIndexRetriever.js +1 -4
- package/vector-index/index.cjs +1 -0
- package/vector-index/index.d.ts +1 -0
- package/vector-index/index.js +1 -0
- package/vector-index/upsertIntoVectorIndex.cjs +16 -7
- package/vector-index/upsertIntoVectorIndex.js +16 -7
- package/model-function/describe-image/ImageDescriptionEvent.d.ts +0 -18
- package/model-function/describe-image/ImageDescriptionModel.d.ts +0 -10
- package/model-function/describe-image/describeImage.cjs +0 -26
- package/model-function/describe-image/describeImage.d.ts +0 -9
- package/model-function/describe-image/describeImage.js +0 -22
- package/model-function/generate-text/TextStreamingEvent.d.ts +0 -7
- package/model-function/synthesize-speech/SpeechSynthesisEvent.d.ts +0 -21
- package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +0 -15
- package/model-function/synthesize-speech/synthesizeSpeech.cjs +0 -67
- package/model-function/synthesize-speech/synthesizeSpeech.d.ts +0 -14
- package/model-function/synthesize-speech/synthesizeSpeech.js +0 -63
- package/model-provider/lmnt/LmntSpeechSynthesisModel.d.ts +0 -26
- /package/{model-function/describe-image/ImageDescriptionEvent.cjs → guard/GuardEvent.cjs} +0 -0
- /package/{model-function/describe-image/ImageDescriptionEvent.js → guard/GuardEvent.js} +0 -0
- /package/model-function/{describe-image/ImageDescriptionModel.cjs → generate-speech/SpeechGenerationEvent.cjs} +0 -0
- /package/model-function/{describe-image/ImageDescriptionModel.js → generate-speech/SpeechGenerationEvent.js} +0 -0
- /package/model-function/{generate-text/TextStreamingEvent.cjs → generate-speech/SpeechGenerationModel.cjs} +0 -0
- /package/model-function/{generate-text/TextStreamingEvent.js → generate-speech/SpeechGenerationModel.js} +0 -0
- /package/model-function/{transcribe-speech → generate-transcription}/TranscriptionEvent.cjs +0 -0
- /package/model-function/{transcribe-speech → generate-transcription}/TranscriptionEvent.js +0 -0
- /package/model-function/{transcribe-speech → generate-transcription}/TranscriptionModel.cjs +0 -0
- /package/model-function/{transcribe-speech → generate-transcription}/TranscriptionModel.d.ts +0 -0
- /package/model-function/{transcribe-speech → generate-transcription}/TranscriptionModel.js +0 -0
- /package/{model-function/synthesize-speech/SpeechSynthesisEvent.cjs → tool/UseToolEvent.cjs} +0 -0
- /package/{model-function/synthesize-speech/SpeechSynthesisEvent.js → tool/UseToolEvent.js} +0 -0
- /package/{model-function/synthesize-speech/SpeechSynthesisModel.cjs → tool/UseToolOrGenerateTextEvent.cjs} +0 -0
- /package/{model-function/synthesize-speech/SpeechSynthesisModel.js → tool/UseToolOrGenerateTextEvent.js} +0 -0
@@ -0,0 +1,81 @@
|
|
1
|
+
import { nanoid as createId } from "nanoid";
|
2
|
+
import { FunctionEventSource } from "./FunctionEventSource.js";
|
3
|
+
import { getGlobalFunctionLogging } from "./GlobalFunctionLogging.js";
|
4
|
+
import { getGlobalFunctionObservers } from "./GlobalFunctionObservers.js";
|
5
|
+
import { AbortError } from "./api/AbortError.js";
|
6
|
+
import { getFunctionCallLogger } from "./getFunctionCallLogger.js";
|
7
|
+
import { getRun } from "./getRun.js";
|
8
|
+
import { startDurationMeasurement } from "../util/DurationMeasurement.js";
|
9
|
+
import { runSafe } from "../util/runSafe.js";
|
10
|
+
export async function executeFunctionCall({ options, input, functionType, execute, inputPropertyName = "input", outputPropertyName = "value", }) {
|
11
|
+
const run = await getRun(options?.run);
|
12
|
+
const eventSource = new FunctionEventSource({
|
13
|
+
observers: [
|
14
|
+
...getFunctionCallLogger(options?.logging ?? getGlobalFunctionLogging()),
|
15
|
+
...getGlobalFunctionObservers(),
|
16
|
+
...(run?.functionObserver != null ? [run.functionObserver] : []),
|
17
|
+
...(options?.observers ?? []),
|
18
|
+
],
|
19
|
+
errorHandler: run?.errorHandler,
|
20
|
+
});
|
21
|
+
const durationMeasurement = startDurationMeasurement();
|
22
|
+
const startMetadata = {
|
23
|
+
functionType,
|
24
|
+
callId: `call-${createId()}`,
|
25
|
+
parentCallId: options?.parentCallId,
|
26
|
+
runId: run?.runId,
|
27
|
+
sessionId: run?.sessionId,
|
28
|
+
userId: run?.userId,
|
29
|
+
functionId: options?.functionId,
|
30
|
+
[inputPropertyName]: input,
|
31
|
+
timestamp: durationMeasurement.startDate,
|
32
|
+
startTimestamp: durationMeasurement.startDate,
|
33
|
+
};
|
34
|
+
eventSource.notify({
|
35
|
+
eventType: "started",
|
36
|
+
...startMetadata,
|
37
|
+
});
|
38
|
+
const result = await runSafe(() => execute({
|
39
|
+
functionId: options?.functionId,
|
40
|
+
logging: options?.logging,
|
41
|
+
observers: options?.observers,
|
42
|
+
run,
|
43
|
+
parentCallId: startMetadata.callId,
|
44
|
+
}));
|
45
|
+
const finishMetadata = {
|
46
|
+
eventType: "finished",
|
47
|
+
...startMetadata,
|
48
|
+
finishTimestamp: new Date(),
|
49
|
+
durationInMs: durationMeasurement.durationInMs,
|
50
|
+
};
|
51
|
+
if (!result.ok) {
|
52
|
+
if (result.isAborted) {
|
53
|
+
eventSource.notify({
|
54
|
+
...finishMetadata,
|
55
|
+
eventType: "finished",
|
56
|
+
result: {
|
57
|
+
status: "abort",
|
58
|
+
},
|
59
|
+
});
|
60
|
+
throw new AbortError();
|
61
|
+
}
|
62
|
+
eventSource.notify({
|
63
|
+
...finishMetadata,
|
64
|
+
eventType: "finished",
|
65
|
+
result: {
|
66
|
+
status: "error",
|
67
|
+
error: result.error,
|
68
|
+
},
|
69
|
+
});
|
70
|
+
throw result.error;
|
71
|
+
}
|
72
|
+
eventSource.notify({
|
73
|
+
...finishMetadata,
|
74
|
+
eventType: "finished",
|
75
|
+
result: {
|
76
|
+
status: "success",
|
77
|
+
[outputPropertyName]: result.value,
|
78
|
+
},
|
79
|
+
});
|
80
|
+
return result.value;
|
81
|
+
}
|
@@ -0,0 +1,7 @@
|
|
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/guard.cjs
CHANGED
@@ -1,72 +1,78 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.guard = void 0;
|
4
|
+
const executeFunctionCall_js_1 = require("../core/executeFunctionCall.cjs");
|
4
5
|
async function guard(execute, input, guards, options) {
|
5
|
-
|
6
|
-
guards = [guards];
|
7
|
-
}
|
6
|
+
const guardList = Array.isArray(guards) ? guards : [guards];
|
8
7
|
const maxRetries = options?.maxRetries ?? 1;
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
}
|
19
|
-
catch (error) {
|
20
|
-
result = {
|
21
|
-
type: "error",
|
22
|
-
input,
|
23
|
-
error,
|
24
|
-
};
|
25
|
-
}
|
26
|
-
let isValid = true;
|
27
|
-
for (const guard of guards) {
|
28
|
-
const guardResult = await guard(result);
|
29
|
-
if (guardResult === undefined) {
|
30
|
-
continue;
|
31
|
-
}
|
32
|
-
switch (guardResult.action) {
|
33
|
-
case "passThrough": {
|
34
|
-
break;
|
35
|
-
}
|
36
|
-
case "retry": {
|
37
|
-
input = guardResult.input;
|
38
|
-
isValid = false;
|
39
|
-
break;
|
40
|
-
}
|
41
|
-
case "return": {
|
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 <= maxRetries) {
|
15
|
+
let result;
|
16
|
+
try {
|
42
17
|
result = {
|
43
18
|
type: "value",
|
44
19
|
input,
|
45
|
-
output:
|
20
|
+
output: await execute(input, options),
|
46
21
|
};
|
47
|
-
break;
|
48
22
|
}
|
49
|
-
|
23
|
+
catch (error) {
|
50
24
|
result = {
|
51
25
|
type: "error",
|
52
26
|
input,
|
53
|
-
error
|
27
|
+
error,
|
54
28
|
};
|
55
|
-
break;
|
56
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++;
|
57
72
|
}
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
}
|
63
|
-
else {
|
64
|
-
throw result.error;
|
65
|
-
}
|
66
|
-
}
|
67
|
-
attempts++;
|
68
|
-
}
|
69
|
-
throw new Error(`Maximum retry attempts of ${maxRetries} reached ` +
|
70
|
-
`without producing a valid output or handling an error after ${attempts} attempts.`);
|
73
|
+
throw new Error(`Maximum retry attempts of ${maxRetries} reached ` +
|
74
|
+
`without producing a valid output or handling an error after ${attempts} attempts.`);
|
75
|
+
},
|
76
|
+
});
|
71
77
|
}
|
72
78
|
exports.guard = guard;
|
package/guard/guard.d.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
import { FunctionOptions } from "../core/FunctionOptions.js";
|
1
2
|
type OutputResult<INPUT, OUTPUT> = {
|
2
3
|
type: "value";
|
3
4
|
input: INPUT;
|
@@ -22,7 +23,7 @@ export type Guard<INPUT, OUTPUT> = ({ type, input, output, error, }: OutputResul
|
|
22
23
|
} | {
|
23
24
|
action: "passThrough";
|
24
25
|
} | undefined>;
|
25
|
-
export declare function guard<INPUT, OUTPUT>(execute: (input: INPUT) => PromiseLike<OUTPUT>, input: INPUT, guards: Guard<INPUT, OUTPUT> | Array<Guard<INPUT, OUTPUT>>, options?: {
|
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 & {
|
26
27
|
maxRetries: number;
|
27
28
|
}): Promise<OUTPUT | undefined>;
|
28
29
|
export {};
|
package/guard/guard.js
CHANGED
@@ -1,68 +1,74 @@
|
|
1
|
+
import { executeFunctionCall } from "../core/executeFunctionCall.js";
|
1
2
|
export async function guard(execute, input, guards, options) {
|
2
|
-
|
3
|
-
guards = [guards];
|
4
|
-
}
|
3
|
+
const guardList = Array.isArray(guards) ? guards : [guards];
|
5
4
|
const maxRetries = options?.maxRetries ?? 1;
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
}
|
16
|
-
catch (error) {
|
17
|
-
result = {
|
18
|
-
type: "error",
|
19
|
-
input,
|
20
|
-
error,
|
21
|
-
};
|
22
|
-
}
|
23
|
-
let isValid = true;
|
24
|
-
for (const guard of guards) {
|
25
|
-
const guardResult = await guard(result);
|
26
|
-
if (guardResult === undefined) {
|
27
|
-
continue;
|
28
|
-
}
|
29
|
-
switch (guardResult.action) {
|
30
|
-
case "passThrough": {
|
31
|
-
break;
|
32
|
-
}
|
33
|
-
case "retry": {
|
34
|
-
input = guardResult.input;
|
35
|
-
isValid = false;
|
36
|
-
break;
|
37
|
-
}
|
38
|
-
case "return": {
|
5
|
+
return executeFunctionCall({
|
6
|
+
options,
|
7
|
+
input,
|
8
|
+
functionType: "guard",
|
9
|
+
execute: async (options) => {
|
10
|
+
let attempts = 0;
|
11
|
+
while (attempts <= maxRetries) {
|
12
|
+
let result;
|
13
|
+
try {
|
39
14
|
result = {
|
40
15
|
type: "value",
|
41
16
|
input,
|
42
|
-
output:
|
17
|
+
output: await execute(input, options),
|
43
18
|
};
|
44
|
-
break;
|
45
19
|
}
|
46
|
-
|
20
|
+
catch (error) {
|
47
21
|
result = {
|
48
22
|
type: "error",
|
49
23
|
input,
|
50
|
-
error
|
24
|
+
error,
|
51
25
|
};
|
52
|
-
break;
|
53
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++;
|
54
69
|
}
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
}
|
60
|
-
else {
|
61
|
-
throw result.error;
|
62
|
-
}
|
63
|
-
}
|
64
|
-
attempts++;
|
65
|
-
}
|
66
|
-
throw new Error(`Maximum retry attempts of ${maxRetries} reached ` +
|
67
|
-
`without producing a valid output or handling an error after ${attempts} attempts.`);
|
70
|
+
throw new Error(`Maximum retry attempts of ${maxRetries} reached ` +
|
71
|
+
`without producing a valid output or handling an error after ${attempts} attempts.`);
|
72
|
+
},
|
73
|
+
});
|
68
74
|
}
|
package/guard/index.cjs
CHANGED
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./GuardEvent.cjs"), exports);
|
17
18
|
__exportStar(require("./fixStructure.cjs"), exports);
|
18
19
|
__exportStar(require("./guard.cjs"), exports);
|
package/guard/index.d.ts
CHANGED
package/guard/index.js
CHANGED
@@ -13,25 +13,25 @@ class AsyncIterableResultPromise extends Promise {
|
|
13
13
|
writable: true,
|
14
14
|
value: fullPromise
|
15
15
|
});
|
16
|
-
Object.defineProperty(this, "
|
16
|
+
Object.defineProperty(this, "valuePromise", {
|
17
17
|
enumerable: true,
|
18
18
|
configurable: true,
|
19
19
|
writable: true,
|
20
20
|
value: void 0
|
21
21
|
});
|
22
|
-
this.
|
22
|
+
this.valuePromise = fullPromise.then((result) => result.value);
|
23
23
|
}
|
24
24
|
asFullResponse() {
|
25
25
|
return this.fullPromise;
|
26
26
|
}
|
27
27
|
then(onfulfilled, onrejected) {
|
28
|
-
return this.
|
28
|
+
return this.valuePromise.then(onfulfilled, onrejected);
|
29
29
|
}
|
30
30
|
catch(onrejected) {
|
31
|
-
return this.
|
31
|
+
return this.valuePromise.catch(onrejected);
|
32
32
|
}
|
33
33
|
finally(onfinally) {
|
34
|
-
return this.
|
34
|
+
return this.valuePromise.finally(onfinally);
|
35
35
|
}
|
36
36
|
}
|
37
37
|
exports.AsyncIterableResultPromise = AsyncIterableResultPromise;
|
@@ -1,13 +1,13 @@
|
|
1
1
|
import { ModelCallMetadata } from "./ModelCallMetadata.js";
|
2
2
|
export declare class AsyncIterableResultPromise<T> extends Promise<AsyncIterable<T>> {
|
3
3
|
private fullPromise;
|
4
|
-
private
|
4
|
+
private valuePromise;
|
5
5
|
constructor(fullPromise: Promise<{
|
6
|
-
|
6
|
+
value: AsyncIterable<T>;
|
7
7
|
metadata: Omit<ModelCallMetadata, "durationInMs" | "finishTimestamp">;
|
8
8
|
}>);
|
9
9
|
asFullResponse(): Promise<{
|
10
|
-
|
10
|
+
value: AsyncIterable<T>;
|
11
11
|
metadata: Omit<ModelCallMetadata, "durationInMs" | "finishTimestamp">;
|
12
12
|
}>;
|
13
13
|
then<TResult1 = AsyncIterable<T>, TResult2 = never>(onfulfilled?: ((value: AsyncIterable<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
|
@@ -10,24 +10,24 @@ export class AsyncIterableResultPromise extends Promise {
|
|
10
10
|
writable: true,
|
11
11
|
value: fullPromise
|
12
12
|
});
|
13
|
-
Object.defineProperty(this, "
|
13
|
+
Object.defineProperty(this, "valuePromise", {
|
14
14
|
enumerable: true,
|
15
15
|
configurable: true,
|
16
16
|
writable: true,
|
17
17
|
value: void 0
|
18
18
|
});
|
19
|
-
this.
|
19
|
+
this.valuePromise = fullPromise.then((result) => result.value);
|
20
20
|
}
|
21
21
|
asFullResponse() {
|
22
22
|
return this.fullPromise;
|
23
23
|
}
|
24
24
|
then(onfulfilled, onrejected) {
|
25
|
-
return this.
|
25
|
+
return this.valuePromise.then(onfulfilled, onrejected);
|
26
26
|
}
|
27
27
|
catch(onrejected) {
|
28
|
-
return this.
|
28
|
+
return this.valuePromise.catch(onrejected);
|
29
29
|
}
|
30
30
|
finally(onfinally) {
|
31
|
-
return this.
|
31
|
+
return this.valuePromise.finally(onfinally);
|
32
32
|
}
|
33
33
|
}
|
@@ -18,7 +18,7 @@ export interface Model<SETTINGS extends ModelSettings> {
|
|
18
18
|
* The `withSettings` method creates a new model with the same configuration as the original model, but with the specified settings changed.
|
19
19
|
*
|
20
20
|
* @example
|
21
|
-
* const model = new
|
21
|
+
* const model = new OpenAICompletionModel({
|
22
22
|
* model: "gpt-3.5-turbo-instruct",
|
23
23
|
* maxCompletionTokens: 500,
|
24
24
|
* });
|
@@ -1,14 +1,12 @@
|
|
1
1
|
import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../core/FunctionEvent.js";
|
2
2
|
import { ModelInformation } from "./ModelInformation.js";
|
3
|
-
import { ImageDescriptionFinishedEvent, ImageDescriptionStartedEvent } from "./describe-image/ImageDescriptionEvent.js";
|
4
3
|
import { EmbeddingFinishedEvent, EmbeddingStartedEvent } from "./embed/EmbeddingEvent.js";
|
5
4
|
import { ImageGenerationFinishedEvent, ImageGenerationStartedEvent } from "./generate-image/ImageGenerationEvent.js";
|
5
|
+
import { SpeechGenerationFinishedEvent, SpeechGenerationStartedEvent, SpeechStreamingFinishedEvent, SpeechStreamingStartedEvent } from "./generate-speech/SpeechGenerationEvent.js";
|
6
6
|
import { StructureGenerationFinishedEvent, StructureGenerationStartedEvent } from "./generate-structure/StructureGenerationEvent.js";
|
7
7
|
import { StructureStreamingFinishedEvent, StructureStreamingStartedEvent } from "./generate-structure/StructureStreamingEvent.js";
|
8
|
-
import { TextGenerationFinishedEvent, TextGenerationStartedEvent } from "./generate-text/TextGenerationEvent.js";
|
9
|
-
import {
|
10
|
-
import { SpeechSynthesisFinishedEvent, SpeechSynthesisStartedEvent } from "./synthesize-speech/SpeechSynthesisEvent.js";
|
11
|
-
import { TranscriptionFinishedEvent, TranscriptionStartedEvent } from "./transcribe-speech/TranscriptionEvent.js";
|
8
|
+
import { TextGenerationFinishedEvent, TextGenerationStartedEvent, TextStreamingFinishedEvent, TextStreamingStartedEvent } from "./generate-text/TextGenerationEvent.js";
|
9
|
+
import { TranscriptionFinishedEvent, TranscriptionStartedEvent } from "./generate-transcription/TranscriptionEvent.js";
|
12
10
|
export interface BaseModelCallStartedEvent extends BaseFunctionStartedEvent {
|
13
11
|
model: ModelInformation;
|
14
12
|
/**
|
@@ -49,5 +47,5 @@ export interface BaseModelCallFinishedEvent extends BaseFunctionFinishedEvent {
|
|
49
47
|
*/
|
50
48
|
result: BaseModelCallFinishedEventResult;
|
51
49
|
}
|
52
|
-
export type ModelCallStartedEvent = EmbeddingStartedEvent |
|
53
|
-
export type ModelCallFinishedEvent = EmbeddingFinishedEvent |
|
50
|
+
export type ModelCallStartedEvent = EmbeddingStartedEvent | ImageGenerationStartedEvent | SpeechGenerationStartedEvent | SpeechStreamingStartedEvent | StructureGenerationStartedEvent | StructureStreamingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | TranscriptionStartedEvent;
|
51
|
+
export type ModelCallFinishedEvent = EmbeddingFinishedEvent | ImageGenerationFinishedEvent | SpeechGenerationFinishedEvent | SpeechStreamingFinishedEvent | StructureGenerationFinishedEvent | StructureStreamingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | TranscriptionFinishedEvent;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { Vector } from "../../core/Vector.js";
|
2
2
|
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
|
3
3
|
export interface EmbeddingStartedEvent extends BaseModelCallStartedEvent {
|
4
|
-
functionType: "
|
4
|
+
functionType: "embed";
|
5
5
|
input: unknown | Array<unknown>;
|
6
6
|
}
|
7
7
|
export type EmbeddingFinishedEventResult = {
|
@@ -15,7 +15,7 @@ export type EmbeddingFinishedEventResult = {
|
|
15
15
|
status: "abort";
|
16
16
|
};
|
17
17
|
export interface EmbeddingFinishedEvent extends BaseModelCallFinishedEvent {
|
18
|
-
functionType: "
|
18
|
+
functionType: "embed";
|
19
19
|
input: unknown | Array<unknown>;
|
20
20
|
result: EmbeddingFinishedEventResult;
|
21
21
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.embed = exports.embedMany = void 0;
|
4
|
-
const
|
4
|
+
const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
|
5
5
|
const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
|
6
6
|
/**
|
7
7
|
* Generate embeddings for multiple values.
|
@@ -16,8 +16,8 @@ const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
|
|
16
16
|
* );
|
17
17
|
*/
|
18
18
|
function embedMany(model, values, options) {
|
19
|
-
return new ModelFunctionPromise_js_1.ModelFunctionPromise((0,
|
20
|
-
functionType: "
|
19
|
+
return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeStandardCall_js_1.executeStandardCall)({
|
20
|
+
functionType: "embed",
|
21
21
|
input: values,
|
22
22
|
model,
|
23
23
|
options,
|
@@ -57,8 +57,8 @@ exports.embedMany = embedMany;
|
|
57
57
|
* );
|
58
58
|
*/
|
59
59
|
function embed(model, value, options) {
|
60
|
-
return new ModelFunctionPromise_js_1.ModelFunctionPromise((0,
|
61
|
-
functionType: "
|
60
|
+
return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeStandardCall_js_1.executeStandardCall)({
|
61
|
+
functionType: "embed",
|
62
62
|
input: value,
|
63
63
|
model,
|
64
64
|
options,
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { executeStandardCall } from "../executeStandardCall.js";
|
2
2
|
import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
|
3
3
|
/**
|
4
4
|
* Generate embeddings for multiple values.
|
@@ -13,8 +13,8 @@ import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
|
|
13
13
|
* );
|
14
14
|
*/
|
15
15
|
export function embedMany(model, values, options) {
|
16
|
-
return new ModelFunctionPromise(
|
17
|
-
functionType: "
|
16
|
+
return new ModelFunctionPromise(executeStandardCall({
|
17
|
+
functionType: "embed",
|
18
18
|
input: values,
|
19
19
|
model,
|
20
20
|
options,
|
@@ -53,8 +53,8 @@ export function embedMany(model, values, options) {
|
|
53
53
|
* );
|
54
54
|
*/
|
55
55
|
export function embed(model, value, options) {
|
56
|
-
return new ModelFunctionPromise(
|
57
|
-
functionType: "
|
56
|
+
return new ModelFunctionPromise(executeStandardCall({
|
57
|
+
functionType: "embed",
|
58
58
|
input: value,
|
59
59
|
model,
|
60
60
|
options,
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.executeStandardCall = void 0;
|
4
4
|
const nanoid_1 = require("nanoid");
|
5
5
|
const FunctionEventSource_js_1 = require("../core/FunctionEventSource.cjs");
|
6
6
|
const GlobalFunctionLogging_js_1 = require("../core/GlobalFunctionLogging.cjs");
|
@@ -10,7 +10,7 @@ const getFunctionCallLogger_js_1 = require("../core/getFunctionCallLogger.cjs");
|
|
10
10
|
const getRun_js_1 = require("../core/getRun.cjs");
|
11
11
|
const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
|
12
12
|
const runSafe_js_1 = require("../util/runSafe.cjs");
|
13
|
-
async function
|
13
|
+
async function executeStandardCall({ model, options, input, functionType, generateResponse, }) {
|
14
14
|
const run = await (0, getRun_js_1.getRun)(options?.run);
|
15
15
|
const settings = model.settings;
|
16
16
|
const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
|
@@ -27,6 +27,7 @@ async function executeCall({ model, options, input, functionType, generateRespon
|
|
27
27
|
const startMetadata = {
|
28
28
|
functionType,
|
29
29
|
callId: `call-${(0, nanoid_1.nanoid)()}`,
|
30
|
+
parentCallId: options?.parentCallId,
|
30
31
|
runId: run?.runId,
|
31
32
|
sessionId: run?.sessionId,
|
32
33
|
userId: run?.userId,
|
@@ -46,6 +47,7 @@ async function executeCall({ model, options, input, functionType, generateRespon
|
|
46
47
|
logging: options?.logging,
|
47
48
|
observers: options?.observers,
|
48
49
|
run,
|
50
|
+
parentCallId: startMetadata.callId,
|
49
51
|
}));
|
50
52
|
const finishMetadata = {
|
51
53
|
eventType: "finished",
|
@@ -104,4 +106,4 @@ async function executeCall({ model, options, input, functionType, generateRespon
|
|
104
106
|
},
|
105
107
|
};
|
106
108
|
}
|
107
|
-
exports.
|
109
|
+
exports.executeStandardCall = executeStandardCall;
|
@@ -2,7 +2,7 @@ import { FunctionOptions } from "../core/FunctionOptions.js";
|
|
2
2
|
import { Model, ModelSettings } from "./Model.js";
|
3
3
|
import { ModelCallStartedEvent } from "./ModelCallEvent.js";
|
4
4
|
import { ModelCallMetadata } from "./ModelCallMetadata.js";
|
5
|
-
export declare function
|
5
|
+
export declare function executeStandardCall<VALUE, MODEL extends Model<ModelSettings>>({ model, options, input, functionType, generateResponse, }: {
|
6
6
|
model: MODEL;
|
7
7
|
options?: FunctionOptions;
|
8
8
|
input: unknown;
|
@@ -7,7 +7,7 @@ import { getFunctionCallLogger } from "../core/getFunctionCallLogger.js";
|
|
7
7
|
import { getRun } from "../core/getRun.js";
|
8
8
|
import { startDurationMeasurement } from "../util/DurationMeasurement.js";
|
9
9
|
import { runSafe } from "../util/runSafe.js";
|
10
|
-
export async function
|
10
|
+
export async function executeStandardCall({ model, options, input, functionType, generateResponse, }) {
|
11
11
|
const run = await getRun(options?.run);
|
12
12
|
const settings = model.settings;
|
13
13
|
const eventSource = new FunctionEventSource({
|
@@ -24,6 +24,7 @@ export async function executeCall({ model, options, input, functionType, generat
|
|
24
24
|
const startMetadata = {
|
25
25
|
functionType,
|
26
26
|
callId: `call-${createId()}`,
|
27
|
+
parentCallId: options?.parentCallId,
|
27
28
|
runId: run?.runId,
|
28
29
|
sessionId: run?.sessionId,
|
29
30
|
userId: run?.userId,
|
@@ -43,6 +44,7 @@ export async function executeCall({ model, options, input, functionType, generat
|
|
43
44
|
logging: options?.logging,
|
44
45
|
observers: options?.observers,
|
45
46
|
run,
|
47
|
+
parentCallId: startMetadata.callId,
|
46
48
|
}));
|
47
49
|
const finishMetadata = {
|
48
50
|
eventType: "finished",
|