modelfusion 0.20.1 → 0.22.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 +5 -7
- package/composed-function/summarize/SummarizationFunction.d.ts +3 -3
- package/composed-function/summarize/summarizeRecursively.d.ts +1 -1
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.d.ts +1 -1
- package/{run → core}/ConsoleLogger.cjs +1 -4
- package/core/ConsoleLogger.d.ts +5 -0
- package/core/ConsoleLogger.js +5 -0
- package/{run → core}/DefaultRun.cjs +2 -5
- package/{run → core}/DefaultRun.d.ts +5 -5
- package/{run → core}/DefaultRun.js +2 -5
- package/core/FunctionEvent.d.ts +75 -0
- package/{run/RunFunctionEventSource.js → core/FunctionEventSource.cjs} +7 -13
- package/core/FunctionEventSource.d.ts +12 -0
- package/{run/RunFunctionEventSource.cjs → core/FunctionEventSource.js} +3 -17
- package/core/FunctionObserver.d.ts +7 -0
- package/core/FunctionOptions.d.ts +19 -0
- package/core/GlobalFunctionObservers.cjs +12 -0
- package/core/GlobalFunctionObservers.d.ts +3 -0
- package/core/GlobalFunctionObservers.js +7 -0
- package/{run → core}/Run.d.ts +2 -2
- package/{run → core}/index.cjs +5 -5
- package/core/index.d.ts +9 -0
- package/core/index.js +9 -0
- package/index.cjs +1 -1
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/model-function/Model.d.ts +5 -2
- package/model-function/ModelCallEvent.d.ts +20 -5
- package/model-function/ModelFunctionOptions.d.ts +4 -0
- package/model-function/SuccessfulModelCall.cjs +6 -16
- package/model-function/SuccessfulModelCall.d.ts +2 -2
- package/model-function/SuccessfulModelCall.js +6 -16
- package/model-function/embed-text/TextEmbeddingEvent.d.ts +15 -17
- package/model-function/embed-text/TextEmbeddingModel.d.ts +3 -3
- package/model-function/embed-text/embedText.cjs +6 -67
- package/model-function/embed-text/embedText.d.ts +4 -4
- package/model-function/embed-text/embedText.js +6 -67
- package/model-function/executeCall.cjs +49 -14
- package/model-function/executeCall.d.ts +9 -10
- package/model-function/executeCall.js +49 -14
- package/model-function/generate-image/ImageGenerationEvent.d.ts +12 -16
- package/model-function/generate-image/ImageGenerationModel.d.ts +2 -2
- package/model-function/generate-image/generateImage.cjs +2 -30
- package/model-function/generate-image/generateImage.d.ts +2 -2
- package/model-function/generate-image/generateImage.js +2 -30
- package/model-function/generate-json/GenerateJsonModel.d.ts +2 -2
- package/model-function/generate-json/GenerateJsonOrTextModel.d.ts +2 -2
- package/model-function/generate-json/JsonGenerationEvent.d.ts +6 -21
- package/model-function/generate-json/JsonTextGenerationModel.d.ts +2 -2
- package/model-function/generate-json/generateJson.cjs +2 -30
- package/model-function/generate-json/generateJson.d.ts +2 -2
- package/model-function/generate-json/generateJson.js +2 -30
- package/model-function/generate-json/generateJsonOrText.cjs +2 -30
- package/model-function/generate-json/generateJsonOrText.d.ts +2 -2
- package/model-function/generate-json/generateJsonOrText.js +2 -30
- package/model-function/generate-text/TextGenerationEvent.d.ts +13 -15
- package/model-function/generate-text/TextGenerationModel.d.ts +3 -3
- package/model-function/generate-text/TextStreamingEvent.d.ts +7 -22
- package/model-function/generate-text/generateText.cjs +2 -30
- package/model-function/generate-text/generateText.d.ts +2 -2
- package/model-function/generate-text/generateText.js +2 -30
- package/model-function/generate-text/streamText.cjs +52 -42
- package/model-function/generate-text/streamText.d.ts +5 -5
- package/model-function/generate-text/streamText.js +52 -42
- package/model-function/index.cjs +1 -1
- package/model-function/index.d.ts +1 -1
- package/model-function/index.js +1 -1
- package/model-function/synthesize-speech/SpeechSynthesisEvent.d.ts +14 -15
- package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +2 -2
- package/model-function/synthesize-speech/synthesizeSpeech.cjs +2 -30
- package/model-function/synthesize-speech/synthesizeSpeech.d.ts +2 -2
- package/model-function/synthesize-speech/synthesizeSpeech.js +2 -30
- package/model-function/transcribe-speech/TranscriptionEvent.d.ts +12 -16
- package/model-function/transcribe-speech/TranscriptionModel.d.ts +2 -2
- package/model-function/transcribe-speech/transcribe.cjs +2 -30
- package/model-function/transcribe-speech/transcribe.d.ts +2 -2
- package/model-function/transcribe-speech/transcribe.js +2 -30
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +6 -6
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +4 -4
- package/model-provider/cohere/CohereTokenizer.d.ts +1 -1
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.d.ts +2 -2
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +3 -3
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +3 -3
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +3 -3
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +4 -4
- package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +1 -1
- package/model-provider/openai/OpenAIImageGenerationModel.d.ts +3 -3
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +3 -3
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +4 -4
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +3 -3
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +5 -5
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +3 -3
- package/package.json +1 -1
- package/prompt/PromptFormatTextGenerationModel.d.ts +3 -3
- package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.cjs +1 -0
- package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.d.ts +2 -2
- package/text-chunk/SimilarTextChunksFromVectorIndexRetriever.js +1 -0
- package/text-chunk/retrieve-text-chunks/TextChunkRetriever.d.ts +2 -2
- package/text-chunk/retrieve-text-chunks/retrieveTextChunks.d.ts +2 -2
- package/text-chunk/split/SplitFunction.d.ts +3 -3
- package/text-chunk/upsertTextChunks.d.ts +2 -2
- package/tool/ExecuteToolEvent.d.ts +9 -20
- package/tool/Tool.d.ts +3 -3
- package/tool/WebSearchTool.cjs +25 -0
- package/tool/WebSearchTool.d.ts +57 -1
- package/tool/WebSearchTool.js +25 -0
- package/tool/executeTool.cjs +39 -31
- package/tool/executeTool.d.ts +5 -4
- package/tool/executeTool.js +39 -31
- package/tool/useTool.cjs +2 -6
- package/tool/useTool.d.ts +2 -2
- package/tool/useTool.js +2 -6
- package/tool/useToolOrGenerateText.cjs +1 -3
- package/tool/useToolOrGenerateText.d.ts +2 -2
- package/tool/useToolOrGenerateText.js +1 -3
- package/util/DurationMeasurement.cjs +6 -0
- package/util/DurationMeasurement.d.ts +1 -0
- package/util/DurationMeasurement.js +6 -0
- package/util/api/postToApi.cjs +8 -0
- package/util/api/postToApi.js +8 -0
- package/vector-index/VectorIndex.d.ts +1 -1
- package/vector-index/memory/MemoryVectorIndex.d.ts +1 -1
- package/vector-index/pinecone/PineconeVectorIndex.d.ts +1 -1
- package/model-function/FunctionOptions.d.ts +0 -6
- package/run/ConsoleLogger.d.ts +0 -6
- package/run/ConsoleLogger.js +0 -8
- package/run/IdMetadata.d.ts +0 -7
- package/run/RunFunction.d.ts +0 -9
- package/run/RunFunctionEvent.d.ts +0 -12
- package/run/RunFunctionEventSource.d.ts +0 -13
- package/run/RunFunctionObserver.cjs +0 -2
- package/run/RunFunctionObserver.d.ts +0 -5
- package/run/RunFunctionObserver.js +0 -1
- package/run/index.d.ts +0 -9
- package/run/index.js +0 -9
- /package/{model-function/FunctionOptions.cjs → core/FunctionEvent.cjs} +0 -0
- /package/{model-function/FunctionOptions.js → core/FunctionEvent.js} +0 -0
- /package/{run/IdMetadata.cjs → core/FunctionObserver.cjs} +0 -0
- /package/{run/IdMetadata.js → core/FunctionObserver.js} +0 -0
- /package/{run/Run.cjs → core/FunctionOptions.cjs} +0 -0
- /package/{run/Run.js → core/FunctionOptions.js} +0 -0
- /package/{run/RunFunction.cjs → core/Run.cjs} +0 -0
- /package/{run/RunFunction.js → core/Run.js} +0 -0
- /package/{run → core}/Vector.cjs +0 -0
- /package/{run → core}/Vector.d.ts +0 -0
- /package/{run → core}/Vector.js +0 -0
- /package/{run/RunFunctionEvent.cjs → model-function/ModelFunctionOptions.cjs} +0 -0
- /package/{run/RunFunctionEvent.js → model-function/ModelFunctionOptions.js} +0 -0
@@ -1,5 +1,5 @@
|
|
1
|
-
import { Vector } from "../../
|
2
|
-
import {
|
1
|
+
import { Vector } from "../../core/Vector.js";
|
2
|
+
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
3
3
|
import { Model, ModelSettings } from "../Model.js";
|
4
4
|
export interface TextEmbeddingModelSettings extends ModelSettings {
|
5
5
|
}
|
@@ -13,6 +13,6 @@ export interface TextEmbeddingModel<RESPONSE, SETTINGS extends TextEmbeddingMode
|
|
13
13
|
*/
|
14
14
|
readonly embeddingDimensions: number | undefined;
|
15
15
|
readonly maxTextsPerCall: number;
|
16
|
-
generateEmbeddingResponse(texts: string[], options?:
|
16
|
+
generateEmbeddingResponse(texts: string[], options?: ModelFunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
|
17
17
|
extractEmbeddings(response: RESPONSE): Vector[];
|
18
18
|
}
|
@@ -16,6 +16,8 @@ const executeCall_js_1 = require("../executeCall.cjs");
|
|
16
16
|
*/
|
17
17
|
function embedTexts(model, texts, options) {
|
18
18
|
return (0, executeCall_js_1.executeCall)({
|
19
|
+
functionType: "text-embedding",
|
20
|
+
input: texts,
|
19
21
|
model,
|
20
22
|
options,
|
21
23
|
generateResponse: (options) => {
|
@@ -34,36 +36,6 @@ function embedTexts(model, texts, options) {
|
|
34
36
|
}
|
35
37
|
return embeddings;
|
36
38
|
},
|
37
|
-
getStartEvent: (metadata, settings) => ({
|
38
|
-
type: "text-embedding-started",
|
39
|
-
metadata,
|
40
|
-
settings,
|
41
|
-
texts,
|
42
|
-
}),
|
43
|
-
getAbortEvent: (metadata, settings) => ({
|
44
|
-
type: "text-embedding-finished",
|
45
|
-
status: "abort",
|
46
|
-
metadata,
|
47
|
-
settings,
|
48
|
-
texts,
|
49
|
-
}),
|
50
|
-
getFailureEvent: (metadata, settings, error) => ({
|
51
|
-
type: "text-embedding-finished",
|
52
|
-
status: "failure",
|
53
|
-
metadata,
|
54
|
-
settings,
|
55
|
-
error,
|
56
|
-
texts,
|
57
|
-
}),
|
58
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
59
|
-
type: "text-embedding-finished",
|
60
|
-
status: "success",
|
61
|
-
metadata,
|
62
|
-
settings,
|
63
|
-
texts,
|
64
|
-
response,
|
65
|
-
generatedEmbeddings: output,
|
66
|
-
}),
|
67
39
|
});
|
68
40
|
}
|
69
41
|
exports.embedTexts = embedTexts;
|
@@ -77,46 +49,13 @@ exports.embedTexts = embedTexts;
|
|
77
49
|
* );
|
78
50
|
*/
|
79
51
|
function embedText(model, text, options) {
|
80
|
-
const texts = [text];
|
81
52
|
return (0, executeCall_js_1.executeCall)({
|
53
|
+
functionType: "text-embedding",
|
54
|
+
input: text,
|
82
55
|
model,
|
83
56
|
options,
|
84
|
-
generateResponse: (options) =>
|
85
|
-
|
86
|
-
},
|
87
|
-
extractOutputValue: (result) => {
|
88
|
-
return model.extractEmbeddings(result)[0];
|
89
|
-
},
|
90
|
-
getStartEvent: (metadata, settings) => ({
|
91
|
-
type: "text-embedding-started",
|
92
|
-
metadata,
|
93
|
-
settings,
|
94
|
-
texts,
|
95
|
-
}),
|
96
|
-
getAbortEvent: (metadata, settings) => ({
|
97
|
-
type: "text-embedding-finished",
|
98
|
-
status: "abort",
|
99
|
-
metadata,
|
100
|
-
settings,
|
101
|
-
texts,
|
102
|
-
}),
|
103
|
-
getFailureEvent: (metadata, settings, error) => ({
|
104
|
-
type: "text-embedding-finished",
|
105
|
-
status: "failure",
|
106
|
-
metadata,
|
107
|
-
settings,
|
108
|
-
error,
|
109
|
-
texts,
|
110
|
-
}),
|
111
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
112
|
-
type: "text-embedding-finished",
|
113
|
-
status: "success",
|
114
|
-
metadata,
|
115
|
-
settings,
|
116
|
-
texts,
|
117
|
-
response,
|
118
|
-
generatedEmbeddings: [output],
|
119
|
-
}),
|
57
|
+
generateResponse: (options) => model.generateEmbeddingResponse([text], options),
|
58
|
+
extractOutputValue: (result) => model.extractEmbeddings(result)[0],
|
120
59
|
});
|
121
60
|
}
|
122
61
|
exports.embedText = embedText;
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { Vector } from "../../
|
2
|
-
import {
|
1
|
+
import { Vector } from "../../core/Vector.js";
|
2
|
+
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
3
3
|
import { ModelFunctionPromise } from "../executeCall.js";
|
4
4
|
import { TextEmbeddingModel, TextEmbeddingModelSettings } from "./TextEmbeddingModel.js";
|
5
5
|
/**
|
@@ -14,7 +14,7 @@ import { TextEmbeddingModel, TextEmbeddingModelSettings } from "./TextEmbeddingM
|
|
14
14
|
* ]
|
15
15
|
* );
|
16
16
|
*/
|
17
|
-
export declare function embedTexts<RESPONSE, SETTINGS extends TextEmbeddingModelSettings>(model: TextEmbeddingModel<RESPONSE, SETTINGS>, texts: string[], options?:
|
17
|
+
export declare function embedTexts<RESPONSE, SETTINGS extends TextEmbeddingModelSettings>(model: TextEmbeddingModel<RESPONSE, SETTINGS>, texts: string[], options?: ModelFunctionOptions<SETTINGS>): ModelFunctionPromise<TextEmbeddingModel<RESPONSE, SETTINGS>, Vector[], RESPONSE[]>;
|
18
18
|
/**
|
19
19
|
* Generate an embedding for a single text.
|
20
20
|
*
|
@@ -24,4 +24,4 @@ export declare function embedTexts<RESPONSE, SETTINGS extends TextEmbeddingModel
|
|
24
24
|
* "At first, Nox didn't know what to do with the pup."
|
25
25
|
* );
|
26
26
|
*/
|
27
|
-
export declare function embedText<RESPONSE, SETTINGS extends TextEmbeddingModelSettings>(model: TextEmbeddingModel<RESPONSE, SETTINGS>, text: string, options?:
|
27
|
+
export declare function embedText<RESPONSE, SETTINGS extends TextEmbeddingModelSettings>(model: TextEmbeddingModel<RESPONSE, SETTINGS>, text: string, options?: ModelFunctionOptions<SETTINGS>): ModelFunctionPromise<TextEmbeddingModel<RESPONSE, SETTINGS>, Vector, RESPONSE>;
|
@@ -13,6 +13,8 @@ import { executeCall } from "../executeCall.js";
|
|
13
13
|
*/
|
14
14
|
export function embedTexts(model, texts, options) {
|
15
15
|
return executeCall({
|
16
|
+
functionType: "text-embedding",
|
17
|
+
input: texts,
|
16
18
|
model,
|
17
19
|
options,
|
18
20
|
generateResponse: (options) => {
|
@@ -31,36 +33,6 @@ export function embedTexts(model, texts, options) {
|
|
31
33
|
}
|
32
34
|
return embeddings;
|
33
35
|
},
|
34
|
-
getStartEvent: (metadata, settings) => ({
|
35
|
-
type: "text-embedding-started",
|
36
|
-
metadata,
|
37
|
-
settings,
|
38
|
-
texts,
|
39
|
-
}),
|
40
|
-
getAbortEvent: (metadata, settings) => ({
|
41
|
-
type: "text-embedding-finished",
|
42
|
-
status: "abort",
|
43
|
-
metadata,
|
44
|
-
settings,
|
45
|
-
texts,
|
46
|
-
}),
|
47
|
-
getFailureEvent: (metadata, settings, error) => ({
|
48
|
-
type: "text-embedding-finished",
|
49
|
-
status: "failure",
|
50
|
-
metadata,
|
51
|
-
settings,
|
52
|
-
error,
|
53
|
-
texts,
|
54
|
-
}),
|
55
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
56
|
-
type: "text-embedding-finished",
|
57
|
-
status: "success",
|
58
|
-
metadata,
|
59
|
-
settings,
|
60
|
-
texts,
|
61
|
-
response,
|
62
|
-
generatedEmbeddings: output,
|
63
|
-
}),
|
64
36
|
});
|
65
37
|
}
|
66
38
|
/**
|
@@ -73,45 +45,12 @@ export function embedTexts(model, texts, options) {
|
|
73
45
|
* );
|
74
46
|
*/
|
75
47
|
export function embedText(model, text, options) {
|
76
|
-
const texts = [text];
|
77
48
|
return executeCall({
|
49
|
+
functionType: "text-embedding",
|
50
|
+
input: text,
|
78
51
|
model,
|
79
52
|
options,
|
80
|
-
generateResponse: (options) =>
|
81
|
-
|
82
|
-
},
|
83
|
-
extractOutputValue: (result) => {
|
84
|
-
return model.extractEmbeddings(result)[0];
|
85
|
-
},
|
86
|
-
getStartEvent: (metadata, settings) => ({
|
87
|
-
type: "text-embedding-started",
|
88
|
-
metadata,
|
89
|
-
settings,
|
90
|
-
texts,
|
91
|
-
}),
|
92
|
-
getAbortEvent: (metadata, settings) => ({
|
93
|
-
type: "text-embedding-finished",
|
94
|
-
status: "abort",
|
95
|
-
metadata,
|
96
|
-
settings,
|
97
|
-
texts,
|
98
|
-
}),
|
99
|
-
getFailureEvent: (metadata, settings, error) => ({
|
100
|
-
type: "text-embedding-finished",
|
101
|
-
status: "failure",
|
102
|
-
metadata,
|
103
|
-
settings,
|
104
|
-
error,
|
105
|
-
texts,
|
106
|
-
}),
|
107
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
108
|
-
type: "text-embedding-finished",
|
109
|
-
status: "success",
|
110
|
-
metadata,
|
111
|
-
settings,
|
112
|
-
texts,
|
113
|
-
response,
|
114
|
-
generatedEmbeddings: [output],
|
115
|
-
}),
|
53
|
+
generateResponse: (options) => model.generateEmbeddingResponse([text], options),
|
54
|
+
extractOutputValue: (result) => model.extractEmbeddings(result)[0],
|
116
55
|
});
|
117
56
|
}
|
@@ -2,18 +2,17 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.ModelFunctionPromise = exports.executeCall = void 0;
|
4
4
|
const nanoid_1 = require("nanoid");
|
5
|
-
const
|
5
|
+
const FunctionEventSource_js_1 = require("../core/FunctionEventSource.cjs");
|
6
|
+
const GlobalFunctionObservers_js_1 = require("../core/GlobalFunctionObservers.cjs");
|
6
7
|
const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
|
7
8
|
const AbortError_js_1 = require("../util/api/AbortError.cjs");
|
8
9
|
const runSafe_js_1 = require("../util/runSafe.cjs");
|
9
|
-
function executeCall({ model, options,
|
10
|
+
function executeCall({ model, options, input, functionType, generateResponse, extractOutputValue, }) {
|
10
11
|
return new ModelFunctionPromise(doExecuteCall({
|
11
12
|
model,
|
12
13
|
options,
|
13
|
-
|
14
|
-
|
15
|
-
getFailureEvent,
|
16
|
-
getSuccessEvent,
|
14
|
+
input,
|
15
|
+
functionType,
|
17
16
|
generateResponse,
|
18
17
|
extractOutputValue,
|
19
18
|
}));
|
@@ -53,18 +52,24 @@ class ModelFunctionPromise extends Promise {
|
|
53
52
|
}
|
54
53
|
}
|
55
54
|
exports.ModelFunctionPromise = ModelFunctionPromise;
|
56
|
-
async function doExecuteCall({ model, options,
|
55
|
+
async function doExecuteCall({ model, options, input, functionType, generateResponse, extractOutputValue, }) {
|
57
56
|
if (options?.settings != null) {
|
58
57
|
model = model.withSettings(options.settings);
|
59
58
|
options = {
|
60
59
|
functionId: options.functionId,
|
60
|
+
observers: options.observers,
|
61
61
|
run: options.run,
|
62
62
|
};
|
63
63
|
}
|
64
64
|
const run = options?.run;
|
65
65
|
const settings = model.settings;
|
66
|
-
const eventSource = new
|
67
|
-
observers: [
|
66
|
+
const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
|
67
|
+
observers: [
|
68
|
+
...(0, GlobalFunctionObservers_js_1.getGlobalFunctionObservers)(),
|
69
|
+
...(settings.observers ?? []),
|
70
|
+
...(run?.observers ?? []),
|
71
|
+
...(options?.observers ?? []),
|
72
|
+
],
|
68
73
|
errorHandler: run?.errorHandler,
|
69
74
|
});
|
70
75
|
const durationMeasurement = (0, DurationMeasurement_js_1.startDurationMeasurement)();
|
@@ -75,9 +80,16 @@ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, get
|
|
75
80
|
userId: run?.userId,
|
76
81
|
functionId: options?.functionId,
|
77
82
|
model: model.modelInformation,
|
78
|
-
|
83
|
+
functionType,
|
84
|
+
input,
|
85
|
+
settings,
|
86
|
+
timestamp: durationMeasurement.startDate,
|
87
|
+
startTimestamp: durationMeasurement.startDate,
|
79
88
|
};
|
80
|
-
eventSource.
|
89
|
+
eventSource.notify({
|
90
|
+
...startMetadata,
|
91
|
+
eventType: "started",
|
92
|
+
});
|
81
93
|
const result = await (0, runSafe_js_1.runSafe)(() => generateResponse({
|
82
94
|
functionId: options?.functionId,
|
83
95
|
settings,
|
@@ -85,19 +97,42 @@ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, get
|
|
85
97
|
}));
|
86
98
|
const finishMetadata = {
|
87
99
|
...startMetadata,
|
100
|
+
eventType: "finished",
|
101
|
+
finishTimestamp: new Date(),
|
88
102
|
durationInMs: durationMeasurement.durationInMs,
|
89
103
|
};
|
90
104
|
if (!result.ok) {
|
91
105
|
if (result.isAborted) {
|
92
|
-
eventSource.
|
106
|
+
eventSource.notify({
|
107
|
+
...finishMetadata,
|
108
|
+
eventType: "finished",
|
109
|
+
result: {
|
110
|
+
status: "abort",
|
111
|
+
},
|
112
|
+
});
|
93
113
|
throw new AbortError_js_1.AbortError();
|
94
114
|
}
|
95
|
-
eventSource.
|
115
|
+
eventSource.notify({
|
116
|
+
...finishMetadata,
|
117
|
+
eventType: "finished",
|
118
|
+
result: {
|
119
|
+
status: "error",
|
120
|
+
error: result.error,
|
121
|
+
},
|
122
|
+
});
|
96
123
|
throw result.error;
|
97
124
|
}
|
98
125
|
const response = result.output;
|
99
126
|
const output = extractOutputValue(response);
|
100
|
-
eventSource.
|
127
|
+
eventSource.notify({
|
128
|
+
...finishMetadata,
|
129
|
+
eventType: "finished",
|
130
|
+
result: {
|
131
|
+
status: "success",
|
132
|
+
response,
|
133
|
+
output,
|
134
|
+
},
|
135
|
+
});
|
101
136
|
return {
|
102
137
|
output,
|
103
138
|
response,
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import { FunctionOptions } from "./FunctionOptions.js";
|
2
1
|
import { Model, ModelSettings } from "./Model.js";
|
3
|
-
import {
|
2
|
+
import { ModelCallStartedEvent } from "./ModelCallEvent.js";
|
3
|
+
import { ModelFunctionOptions } from "./ModelFunctionOptions.js";
|
4
4
|
export type CallMetadata<MODEL extends Model<unknown>> = {
|
5
5
|
callId: string;
|
6
6
|
runId?: string;
|
@@ -8,17 +8,16 @@ export type CallMetadata<MODEL extends Model<unknown>> = {
|
|
8
8
|
userId?: string;
|
9
9
|
functionId?: string;
|
10
10
|
model: MODEL["modelInformation"];
|
11
|
-
|
11
|
+
startTimestamp: Date;
|
12
|
+
finishTimestamp: Date;
|
12
13
|
durationInMs: number;
|
13
14
|
};
|
14
|
-
export declare function executeCall<SETTINGS extends ModelSettings, MODEL extends Model<SETTINGS>, OUTPUT, RESPONSE>({ model, options,
|
15
|
+
export declare function executeCall<SETTINGS extends ModelSettings, MODEL extends Model<SETTINGS>, OUTPUT, RESPONSE>({ model, options, input, functionType, generateResponse, extractOutputValue, }: {
|
15
16
|
model: MODEL;
|
16
|
-
options?:
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
getSuccessEvent: (metadata: ModelCallFinishedEventMetadata, settings: SETTINGS, response: RESPONSE, output: OUTPUT) => ModelCallFinishedEvent;
|
21
|
-
generateResponse: (options: FunctionOptions<SETTINGS>) => PromiseLike<RESPONSE>;
|
17
|
+
options?: ModelFunctionOptions<SETTINGS>;
|
18
|
+
input: unknown;
|
19
|
+
functionType: ModelCallStartedEvent["functionType"];
|
20
|
+
generateResponse: (options: ModelFunctionOptions<SETTINGS>) => PromiseLike<RESPONSE>;
|
22
21
|
extractOutputValue: (response: RESPONSE) => OUTPUT;
|
23
22
|
}): ModelFunctionPromise<MODEL, OUTPUT, RESPONSE>;
|
24
23
|
export declare class ModelFunctionPromise<MODEL extends Model<any>, OUTPUT, RESPONSE> extends Promise<OUTPUT> {
|
@@ -1,16 +1,15 @@
|
|
1
1
|
import { nanoid as createId } from "nanoid";
|
2
|
-
import {
|
2
|
+
import { FunctionEventSource } from "../core/FunctionEventSource.js";
|
3
|
+
import { getGlobalFunctionObservers } from "../core/GlobalFunctionObservers.js";
|
3
4
|
import { startDurationMeasurement } from "../util/DurationMeasurement.js";
|
4
5
|
import { AbortError } from "../util/api/AbortError.js";
|
5
6
|
import { runSafe } from "../util/runSafe.js";
|
6
|
-
export function executeCall({ model, options,
|
7
|
+
export function executeCall({ model, options, input, functionType, generateResponse, extractOutputValue, }) {
|
7
8
|
return new ModelFunctionPromise(doExecuteCall({
|
8
9
|
model,
|
9
10
|
options,
|
10
|
-
|
11
|
-
|
12
|
-
getFailureEvent,
|
13
|
-
getSuccessEvent,
|
11
|
+
input,
|
12
|
+
functionType,
|
14
13
|
generateResponse,
|
15
14
|
extractOutputValue,
|
16
15
|
}));
|
@@ -48,18 +47,24 @@ export class ModelFunctionPromise extends Promise {
|
|
48
47
|
return this.outputPromise.finally(onfinally);
|
49
48
|
}
|
50
49
|
}
|
51
|
-
async function doExecuteCall({ model, options,
|
50
|
+
async function doExecuteCall({ model, options, input, functionType, generateResponse, extractOutputValue, }) {
|
52
51
|
if (options?.settings != null) {
|
53
52
|
model = model.withSettings(options.settings);
|
54
53
|
options = {
|
55
54
|
functionId: options.functionId,
|
55
|
+
observers: options.observers,
|
56
56
|
run: options.run,
|
57
57
|
};
|
58
58
|
}
|
59
59
|
const run = options?.run;
|
60
60
|
const settings = model.settings;
|
61
|
-
const eventSource = new
|
62
|
-
observers: [
|
61
|
+
const eventSource = new FunctionEventSource({
|
62
|
+
observers: [
|
63
|
+
...getGlobalFunctionObservers(),
|
64
|
+
...(settings.observers ?? []),
|
65
|
+
...(run?.observers ?? []),
|
66
|
+
...(options?.observers ?? []),
|
67
|
+
],
|
63
68
|
errorHandler: run?.errorHandler,
|
64
69
|
});
|
65
70
|
const durationMeasurement = startDurationMeasurement();
|
@@ -70,9 +75,16 @@ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, get
|
|
70
75
|
userId: run?.userId,
|
71
76
|
functionId: options?.functionId,
|
72
77
|
model: model.modelInformation,
|
73
|
-
|
78
|
+
functionType,
|
79
|
+
input,
|
80
|
+
settings,
|
81
|
+
timestamp: durationMeasurement.startDate,
|
82
|
+
startTimestamp: durationMeasurement.startDate,
|
74
83
|
};
|
75
|
-
eventSource.
|
84
|
+
eventSource.notify({
|
85
|
+
...startMetadata,
|
86
|
+
eventType: "started",
|
87
|
+
});
|
76
88
|
const result = await runSafe(() => generateResponse({
|
77
89
|
functionId: options?.functionId,
|
78
90
|
settings,
|
@@ -80,19 +92,42 @@ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, get
|
|
80
92
|
}));
|
81
93
|
const finishMetadata = {
|
82
94
|
...startMetadata,
|
95
|
+
eventType: "finished",
|
96
|
+
finishTimestamp: new Date(),
|
83
97
|
durationInMs: durationMeasurement.durationInMs,
|
84
98
|
};
|
85
99
|
if (!result.ok) {
|
86
100
|
if (result.isAborted) {
|
87
|
-
eventSource.
|
101
|
+
eventSource.notify({
|
102
|
+
...finishMetadata,
|
103
|
+
eventType: "finished",
|
104
|
+
result: {
|
105
|
+
status: "abort",
|
106
|
+
},
|
107
|
+
});
|
88
108
|
throw new AbortError();
|
89
109
|
}
|
90
|
-
eventSource.
|
110
|
+
eventSource.notify({
|
111
|
+
...finishMetadata,
|
112
|
+
eventType: "finished",
|
113
|
+
result: {
|
114
|
+
status: "error",
|
115
|
+
error: result.error,
|
116
|
+
},
|
117
|
+
});
|
91
118
|
throw result.error;
|
92
119
|
}
|
93
120
|
const response = result.output;
|
94
121
|
const output = extractOutputValue(response);
|
95
|
-
eventSource.
|
122
|
+
eventSource.notify({
|
123
|
+
...finishMetadata,
|
124
|
+
eventType: "finished",
|
125
|
+
result: {
|
126
|
+
status: "success",
|
127
|
+
response,
|
128
|
+
output,
|
129
|
+
},
|
130
|
+
});
|
96
131
|
return {
|
97
132
|
output,
|
98
133
|
response,
|
@@ -1,22 +1,18 @@
|
|
1
|
-
import {
|
2
|
-
export
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
prompt: unknown;
|
7
|
-
};
|
8
|
-
export type ImageGenerationFinishedEvent = {
|
9
|
-
type: "image-generation-finished";
|
10
|
-
metadata: ModelCallFinishedEventMetadata;
|
11
|
-
settings: unknown;
|
12
|
-
prompt: unknown;
|
13
|
-
} & ({
|
1
|
+
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
|
2
|
+
export interface ImageGenerationStartedEvent extends BaseModelCallStartedEvent {
|
3
|
+
functionType: "image-generation";
|
4
|
+
}
|
5
|
+
export type ImageGenerationFinishedEventResult = {
|
14
6
|
status: "success";
|
15
7
|
response: unknown;
|
16
|
-
|
8
|
+
output: string;
|
17
9
|
} | {
|
18
|
-
status: "
|
10
|
+
status: "error";
|
19
11
|
error: unknown;
|
20
12
|
} | {
|
21
13
|
status: "abort";
|
22
|
-
}
|
14
|
+
};
|
15
|
+
export interface ImageGenerationFinishedEvent extends BaseModelCallFinishedEvent {
|
16
|
+
functionType: "image-generation";
|
17
|
+
result: ImageGenerationFinishedEventResult;
|
18
|
+
}
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import {
|
1
|
+
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
2
2
|
import { Model, ModelSettings } from "../Model.js";
|
3
3
|
export interface ImageGenerationModelSettings extends ModelSettings {
|
4
4
|
}
|
5
5
|
export interface ImageGenerationModel<PROMPT, RESPONSE, SETTINGS extends ImageGenerationModelSettings> extends Model<SETTINGS> {
|
6
|
-
generateImageResponse(prompt: PROMPT, options?:
|
6
|
+
generateImageResponse(prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
|
7
7
|
extractBase64Image(response: RESPONSE): string;
|
8
8
|
}
|
@@ -19,40 +19,12 @@ const executeCall_js_1 = require("../executeCall.cjs");
|
|
19
19
|
*/
|
20
20
|
function generateImage(model, prompt, options) {
|
21
21
|
return (0, executeCall_js_1.executeCall)({
|
22
|
+
functionType: "image-generation",
|
23
|
+
input: prompt,
|
22
24
|
model,
|
23
25
|
options,
|
24
26
|
generateResponse: (options) => model.generateImageResponse(prompt, options),
|
25
27
|
extractOutputValue: model.extractBase64Image,
|
26
|
-
getStartEvent: (metadata, settings) => ({
|
27
|
-
type: "image-generation-started",
|
28
|
-
metadata,
|
29
|
-
settings,
|
30
|
-
prompt,
|
31
|
-
}),
|
32
|
-
getAbortEvent: (metadata, settings) => ({
|
33
|
-
type: "image-generation-finished",
|
34
|
-
status: "abort",
|
35
|
-
metadata,
|
36
|
-
settings,
|
37
|
-
prompt,
|
38
|
-
}),
|
39
|
-
getFailureEvent: (metadata, settings, error) => ({
|
40
|
-
type: "image-generation-finished",
|
41
|
-
status: "failure",
|
42
|
-
metadata,
|
43
|
-
settings,
|
44
|
-
prompt,
|
45
|
-
error,
|
46
|
-
}),
|
47
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
48
|
-
type: "image-generation-finished",
|
49
|
-
status: "success",
|
50
|
-
metadata,
|
51
|
-
settings,
|
52
|
-
prompt,
|
53
|
-
response,
|
54
|
-
generatedImage: output,
|
55
|
-
}),
|
56
28
|
});
|
57
29
|
}
|
58
30
|
exports.generateImage = generateImage;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
2
2
|
import { ModelFunctionPromise } from "../executeCall.js";
|
3
3
|
import { ImageGenerationModel, ImageGenerationModelSettings } from "./ImageGenerationModel.js";
|
4
4
|
/**
|
@@ -16,4 +16,4 @@ import { ImageGenerationModel, ImageGenerationModelSettings } from "./ImageGener
|
|
16
16
|
* ]
|
17
17
|
* );
|
18
18
|
*/
|
19
|
-
export declare function generateImage<PROMPT, RESPONSE, SETTINGS extends ImageGenerationModelSettings>(model: ImageGenerationModel<PROMPT, RESPONSE, SETTINGS>, prompt: PROMPT, options?:
|
19
|
+
export declare function generateImage<PROMPT, RESPONSE, SETTINGS extends ImageGenerationModelSettings>(model: ImageGenerationModel<PROMPT, RESPONSE, SETTINGS>, prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>): ModelFunctionPromise<ImageGenerationModel<PROMPT, RESPONSE, SETTINGS>, string, RESPONSE>;
|
@@ -16,39 +16,11 @@ import { executeCall } from "../executeCall.js";
|
|
16
16
|
*/
|
17
17
|
export function generateImage(model, prompt, options) {
|
18
18
|
return executeCall({
|
19
|
+
functionType: "image-generation",
|
20
|
+
input: prompt,
|
19
21
|
model,
|
20
22
|
options,
|
21
23
|
generateResponse: (options) => model.generateImageResponse(prompt, options),
|
22
24
|
extractOutputValue: model.extractBase64Image,
|
23
|
-
getStartEvent: (metadata, settings) => ({
|
24
|
-
type: "image-generation-started",
|
25
|
-
metadata,
|
26
|
-
settings,
|
27
|
-
prompt,
|
28
|
-
}),
|
29
|
-
getAbortEvent: (metadata, settings) => ({
|
30
|
-
type: "image-generation-finished",
|
31
|
-
status: "abort",
|
32
|
-
metadata,
|
33
|
-
settings,
|
34
|
-
prompt,
|
35
|
-
}),
|
36
|
-
getFailureEvent: (metadata, settings, error) => ({
|
37
|
-
type: "image-generation-finished",
|
38
|
-
status: "failure",
|
39
|
-
metadata,
|
40
|
-
settings,
|
41
|
-
prompt,
|
42
|
-
error,
|
43
|
-
}),
|
44
|
-
getSuccessEvent: (metadata, settings, response, output) => ({
|
45
|
-
type: "image-generation-finished",
|
46
|
-
status: "success",
|
47
|
-
metadata,
|
48
|
-
settings,
|
49
|
-
prompt,
|
50
|
-
response,
|
51
|
-
generatedImage: output,
|
52
|
-
}),
|
53
25
|
});
|
54
26
|
}
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import {
|
1
|
+
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
2
2
|
import { Model, ModelSettings } from "../Model.js";
|
3
3
|
export interface GenerateJsonModelSettings extends ModelSettings {
|
4
4
|
}
|
5
5
|
export interface GenerateJsonModel<PROMPT, RESPONSE, SETTINGS extends GenerateJsonModelSettings> extends Model<SETTINGS> {
|
6
|
-
generateJsonResponse(prompt: PROMPT, options?:
|
6
|
+
generateJsonResponse(prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
|
7
7
|
extractJson(response: RESPONSE): unknown;
|
8
8
|
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
2
2
|
import { Model, ModelSettings } from "../Model.js";
|
3
3
|
export interface GenerateJsonOrTextModelSettings extends ModelSettings {
|
4
4
|
}
|
@@ -14,5 +14,5 @@ export interface GenerateJsonOrTextPrompt<RESPONSE> {
|
|
14
14
|
};
|
15
15
|
}
|
16
16
|
export interface GenerateJsonOrTextModel<PROMPT, RESPONSE, SETTINGS extends GenerateJsonOrTextModelSettings> extends Model<SETTINGS> {
|
17
|
-
generateJsonResponse(prompt: PROMPT & GenerateJsonOrTextPrompt<RESPONSE>, options?:
|
17
|
+
generateJsonResponse(prompt: PROMPT & GenerateJsonOrTextPrompt<RESPONSE>, options?: ModelFunctionOptions<SETTINGS>): PromiseLike<RESPONSE>;
|
18
18
|
}
|