modelfusion 0.20.0 → 0.21.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 +6 -8
- package/composed-function/summarize/SummarizationFunction.d.ts +3 -3
- package/model-function/Model.d.ts +5 -2
- package/model-function/ModelCallEvent.d.ts +3 -3
- package/model-function/ModelFunctionOptions.d.ts +4 -0
- package/model-function/SuccessfulModelCall.cjs +3 -15
- package/model-function/SuccessfulModelCall.d.ts +2 -2
- package/model-function/SuccessfulModelCall.js +3 -15
- package/model-function/embed-text/TextEmbeddingEvent.d.ts +6 -8
- package/model-function/embed-text/TextEmbeddingModel.d.ts +2 -2
- package/model-function/embed-text/embedText.cjs +16 -22
- package/model-function/embed-text/embedText.d.ts +3 -3
- package/model-function/embed-text/embedText.js +16 -22
- package/model-function/executeCall.cjs +30 -8
- package/model-function/executeCall.d.ts +16 -9
- package/model-function/executeCall.js +30 -8
- package/model-function/generate-image/ImageGenerationEvent.d.ts +6 -8
- package/model-function/generate-image/ImageGenerationModel.d.ts +2 -2
- package/model-function/generate-image/generateImage.cjs +8 -11
- package/model-function/generate-image/generateImage.d.ts +2 -2
- package/model-function/generate-image/generateImage.js +8 -11
- 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 -8
- package/model-function/generate-json/JsonTextGenerationModel.d.ts +2 -2
- package/model-function/generate-json/generateJson.cjs +8 -11
- package/model-function/generate-json/generateJson.d.ts +2 -2
- package/model-function/generate-json/generateJson.js +8 -11
- package/model-function/generate-json/generateJsonOrText.cjs +8 -11
- package/model-function/generate-json/generateJsonOrText.d.ts +2 -2
- package/model-function/generate-json/generateJsonOrText.js +8 -11
- package/model-function/generate-text/TextGenerationEvent.d.ts +6 -8
- package/model-function/generate-text/TextGenerationModel.d.ts +3 -3
- package/model-function/generate-text/TextStreamingEvent.d.ts +6 -8
- package/model-function/generate-text/generateText.cjs +8 -11
- package/model-function/generate-text/generateText.d.ts +2 -2
- package/model-function/generate-text/generateText.js +8 -11
- package/model-function/generate-text/streamText.cjs +38 -23
- package/model-function/generate-text/streamText.d.ts +5 -5
- package/model-function/generate-text/streamText.js +38 -23
- 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 +6 -8
- package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +2 -2
- package/model-function/synthesize-speech/synthesizeSpeech.cjs +8 -10
- package/model-function/synthesize-speech/synthesizeSpeech.d.ts +2 -2
- package/model-function/synthesize-speech/synthesizeSpeech.js +8 -10
- package/model-function/transcribe-speech/TranscriptionEvent.d.ts +6 -8
- package/model-function/transcribe-speech/TranscriptionModel.d.ts +2 -2
- package/model-function/transcribe-speech/transcribe.cjs +8 -11
- package/model-function/transcribe-speech/transcribe.d.ts +2 -2
- package/model-function/transcribe-speech/transcribe.js +8 -11
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +3 -3
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +4 -4
- 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/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/run/ConsoleLogger.cjs +1 -4
- package/run/ConsoleLogger.d.ts +4 -5
- package/run/ConsoleLogger.js +1 -4
- package/run/DefaultRun.cjs +1 -4
- package/run/DefaultRun.d.ts +5 -5
- package/run/DefaultRun.js +1 -4
- package/run/FunctionEvent.d.ts +51 -0
- package/run/{RunFunctionEventSource.js → FunctionEventSource.cjs} +7 -13
- package/run/FunctionEventSource.d.ts +12 -0
- package/run/{RunFunctionEventSource.cjs → FunctionEventSource.js} +3 -17
- package/run/FunctionObserver.d.ts +7 -0
- package/run/FunctionOptions.d.ts +19 -0
- package/run/GlobalFunctionObservers.cjs +12 -0
- package/run/GlobalFunctionObservers.d.ts +3 -0
- package/run/GlobalFunctionObservers.js +7 -0
- package/run/Run.d.ts +2 -2
- package/run/index.cjs +5 -5
- package/run/index.d.ts +5 -5
- package/run/index.js +5 -5
- 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 +6 -8
- package/tool/Tool.d.ts +3 -3
- package/tool/WebSearchTool.d.ts +2 -1
- package/tool/executeTool.cjs +28 -26
- package/tool/executeTool.d.ts +5 -4
- package/tool/executeTool.js +28 -26
- package/tool/index.cjs +1 -0
- package/tool/index.d.ts +1 -0
- package/tool/index.js +1 -0
- 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/model-function/FunctionOptions.d.ts +0 -6
- 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/model-function/{FunctionOptions.cjs → ModelFunctionOptions.cjs} +0 -0
- /package/model-function/{FunctionOptions.js → ModelFunctionOptions.js} +0 -0
- /package/run/{IdMetadata.cjs → FunctionEvent.cjs} +0 -0
- /package/run/{IdMetadata.js → FunctionEvent.js} +0 -0
- /package/run/{RunFunction.cjs → FunctionObserver.cjs} +0 -0
- /package/run/{RunFunction.js → FunctionObserver.js} +0 -0
- /package/run/{RunFunctionEvent.cjs → FunctionOptions.cjs} +0 -0
- /package/run/{RunFunctionEvent.js → FunctionOptions.js} +0 -0
package/README.md
CHANGED
@@ -252,7 +252,7 @@ Text is generated as a fallback.
|
|
252
252
|
```ts
|
253
253
|
const { tool, parameters, result, text } = await useToolOrGenerateText(
|
254
254
|
new OpenAIChatModel({ model: "gpt-3.5-turbo" }),
|
255
|
-
[calculator /* ... */],
|
255
|
+
[calculator /* and other tools... */],
|
256
256
|
OpenAIChatFunctionPrompt.forToolsCurried([
|
257
257
|
OpenAIChatMessage.user("What's fourteen times twelve?"),
|
258
258
|
])
|
@@ -372,18 +372,16 @@ const { chunks } = await retrieveTextChunks(
|
|
372
372
|
- [Transcribe Speech](https://modelfusion.dev/guide/function/transcribe-speech)
|
373
373
|
- [Synthesize Speech](https://modelfusion.dev/guide/function/synthesize-speech)
|
374
374
|
- [Generate images](https://modelfusion.dev/guide/function/generate-image)
|
375
|
-
- Summarize text
|
376
375
|
- [Tools](https://modelfusion.dev/guide/tools)
|
377
376
|
- [Text Chunks](https://modelfusion.dev/guide/text-chunk/)
|
378
377
|
- [Split Text](https://modelfusion.dev/guide/text-chunk/split)
|
379
|
-
- [
|
380
|
-
- [
|
381
|
-
- [Cost calculation](https://modelfusion.dev/guide/run/cost-calculation)
|
382
|
-
- Call recording
|
383
|
-
- Utilities
|
378
|
+
- [Utilities](https://modelfusion.dev/guide/util/)
|
379
|
+
- [Function observers](https://modelfusion.dev/guide/util/observer)
|
384
380
|
- [Retry strategies](https://modelfusion.dev/guide/util/retry)
|
385
381
|
- [Throttling strategies](https://modelfusion.dev/guide/util/throttle)
|
386
|
-
-
|
382
|
+
- [Run abstraction](https://modelfusion.dev/guide/util/run)
|
383
|
+
- [Abort signals](https://modelfusion.dev/guide/util/abort)
|
384
|
+
- [Cost calculation](https://modelfusion.dev/guide/util/cost-calculation)
|
387
385
|
|
388
386
|
## Integrations
|
389
387
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
2
|
-
export type SummarizationFunction =
|
1
|
+
import { FunctionOptions } from "../../run/FunctionOptions.js";
|
2
|
+
export type SummarizationFunction = (input: {
|
3
3
|
text: string;
|
4
|
-
}, string>;
|
4
|
+
}, options?: FunctionOptions) => PromiseLike<string>;
|
@@ -1,7 +1,10 @@
|
|
1
1
|
import { ModelInformation } from "./ModelInformation.js";
|
2
|
-
import {
|
2
|
+
import { FunctionObserver } from "../run/FunctionObserver.js";
|
3
3
|
export interface ModelSettings {
|
4
|
-
|
4
|
+
/**
|
5
|
+
* Observers that are called when the model is used in run functions.
|
6
|
+
*/
|
7
|
+
observers?: Array<FunctionObserver>;
|
5
8
|
}
|
6
9
|
export interface Model<SETTINGS> {
|
7
10
|
modelInformation: ModelInformation;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { BaseFunctionFinishedEvent, BaseFunctionStartedEvent } from "../run/FunctionEvent.js";
|
2
2
|
import { ModelInformation } from "./ModelInformation.js";
|
3
3
|
import { TextEmbeddingFinishedEvent, TextEmbeddingStartedEvent } from "./embed-text/TextEmbeddingEvent.js";
|
4
4
|
import { ImageGenerationFinishedEvent, ImageGenerationStartedEvent } from "./generate-image/ImageGenerationEvent.js";
|
@@ -7,11 +7,11 @@ import { TextGenerationFinishedEvent, TextGenerationStartedEvent } from "./gener
|
|
7
7
|
import { TextStreamingFinishedEvent, TextStreamingStartedEvent } from "./generate-text/TextStreamingEvent.js";
|
8
8
|
import { SpeechSynthesisFinishedEvent, SpeechSynthesisStartedEvent } from "./synthesize-speech/SpeechSynthesisEvent.js";
|
9
9
|
import { TranscriptionFinishedEvent, TranscriptionStartedEvent } from "./transcribe-speech/TranscriptionEvent.js";
|
10
|
-
export type
|
10
|
+
export type BaseModelCallStartedEvent = BaseFunctionStartedEvent & {
|
11
11
|
model: ModelInformation;
|
12
12
|
};
|
13
13
|
export type ModelCallStartedEvent = ImageGenerationStartedEvent | JsonGenerationStartedEvent | SpeechSynthesisStartedEvent | TextEmbeddingStartedEvent | TextGenerationStartedEvent | TextStreamingStartedEvent | TranscriptionStartedEvent;
|
14
|
-
export type
|
14
|
+
export type BaseModelCallFinishedEvent = BaseFunctionFinishedEvent & {
|
15
15
|
model: ModelInformation;
|
16
16
|
};
|
17
17
|
export type ModelCallFinishedEvent = ImageGenerationFinishedEvent | JsonGenerationFinishedEvent | SpeechSynthesisFinishedEvent | TextEmbeddingFinishedEvent | TextGenerationFinishedEvent | TextStreamingFinishedEvent | TranscriptionFinishedEvent;
|
@@ -3,24 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.extractSuccessfulModelCalls = void 0;
|
4
4
|
function extractSuccessfulModelCalls(runFunctionEvents) {
|
5
5
|
return runFunctionEvents
|
6
|
-
.filter((event) =>
|
7
|
-
"status" in event &&
|
8
|
-
event.status === "success")
|
6
|
+
.filter((event) => "status" in event && event.status === "success")
|
9
7
|
.map((event) => ({
|
10
|
-
model: event.
|
8
|
+
model: event.model,
|
11
9
|
settings: event.settings,
|
12
10
|
response: event.response,
|
13
|
-
type:
|
11
|
+
type: event.functionType,
|
14
12
|
}));
|
15
13
|
}
|
16
14
|
exports.extractSuccessfulModelCalls = extractSuccessfulModelCalls;
|
17
|
-
const eventTypeToCostType = {
|
18
|
-
"image-generation-finished": "image-generation",
|
19
|
-
"json-generation-finished": "json-generation",
|
20
|
-
"json-or-text-generation-finished": "json-or-text-generation",
|
21
|
-
"speech-synthesis-finished": "speech-synthesis",
|
22
|
-
"text-embedding-finished": "text-embedding",
|
23
|
-
"text-generation-finished": "text-generation",
|
24
|
-
"text-streaming-finished": "text-streaming",
|
25
|
-
"transcription-finished": "transcription",
|
26
|
-
};
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { FunctionEvent } from "../run/FunctionEvent.js";
|
2
2
|
import { ModelInformation } from "./ModelInformation.js";
|
3
3
|
export type SuccessfulModelCall = {
|
4
4
|
type: "image-generation" | "json-generation" | "json-or-text-generation" | "speech-synthesis" | "text-embedding" | "text-generation" | "text-streaming" | "transcription";
|
@@ -6,4 +6,4 @@ export type SuccessfulModelCall = {
|
|
6
6
|
settings: unknown;
|
7
7
|
response: unknown;
|
8
8
|
};
|
9
|
-
export declare function extractSuccessfulModelCalls(runFunctionEvents:
|
9
|
+
export declare function extractSuccessfulModelCalls(runFunctionEvents: FunctionEvent[]): SuccessfulModelCall[];
|
@@ -1,22 +1,10 @@
|
|
1
1
|
export function extractSuccessfulModelCalls(runFunctionEvents) {
|
2
2
|
return runFunctionEvents
|
3
|
-
.filter((event) =>
|
4
|
-
"status" in event &&
|
5
|
-
event.status === "success")
|
3
|
+
.filter((event) => "status" in event && event.status === "success")
|
6
4
|
.map((event) => ({
|
7
|
-
model: event.
|
5
|
+
model: event.model,
|
8
6
|
settings: event.settings,
|
9
7
|
response: event.response,
|
10
|
-
type:
|
8
|
+
type: event.functionType,
|
11
9
|
}));
|
12
10
|
}
|
13
|
-
const eventTypeToCostType = {
|
14
|
-
"image-generation-finished": "image-generation",
|
15
|
-
"json-generation-finished": "json-generation",
|
16
|
-
"json-or-text-generation-finished": "json-or-text-generation",
|
17
|
-
"speech-synthesis-finished": "speech-synthesis",
|
18
|
-
"text-embedding-finished": "text-embedding",
|
19
|
-
"text-generation-finished": "text-generation",
|
20
|
-
"text-streaming-finished": "text-streaming",
|
21
|
-
"transcription-finished": "transcription",
|
22
|
-
};
|
@@ -1,14 +1,12 @@
|
|
1
1
|
import { Vector } from "../../run/Vector.js";
|
2
|
-
import {
|
3
|
-
export type TextEmbeddingStartedEvent = {
|
4
|
-
|
5
|
-
metadata: ModelCallStartedEventMetadata;
|
2
|
+
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
|
3
|
+
export type TextEmbeddingStartedEvent = BaseModelCallStartedEvent & {
|
4
|
+
functionType: "text-embedding";
|
6
5
|
settings: unknown;
|
7
6
|
texts: Array<string>;
|
8
7
|
};
|
9
|
-
export type TextEmbeddingFinishedEvent = {
|
10
|
-
|
11
|
-
metadata: ModelCallFinishedEventMetadata;
|
8
|
+
export type TextEmbeddingFinishedEvent = BaseModelCallFinishedEvent & {
|
9
|
+
functionType: "text-embedding";
|
12
10
|
settings: unknown;
|
13
11
|
texts: Array<string>;
|
14
12
|
} & ({
|
@@ -16,7 +14,7 @@ export type TextEmbeddingFinishedEvent = {
|
|
16
14
|
response: unknown;
|
17
15
|
generatedEmbeddings: Array<Vector>;
|
18
16
|
} | {
|
19
|
-
status: "
|
17
|
+
status: "error";
|
20
18
|
error: unknown;
|
21
19
|
} | {
|
22
20
|
status: "abort";
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { Vector } from "../../run/Vector.js";
|
2
|
-
import {
|
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
|
}
|
@@ -35,30 +35,27 @@ function embedTexts(model, texts, options) {
|
|
35
35
|
return embeddings;
|
36
36
|
},
|
37
37
|
getStartEvent: (metadata, settings) => ({
|
38
|
-
|
39
|
-
|
38
|
+
...metadata,
|
39
|
+
functionType: "text-embedding",
|
40
40
|
settings,
|
41
41
|
texts,
|
42
42
|
}),
|
43
43
|
getAbortEvent: (metadata, settings) => ({
|
44
|
-
|
45
|
-
|
46
|
-
metadata,
|
44
|
+
...metadata,
|
45
|
+
functionType: "text-embedding",
|
47
46
|
settings,
|
48
47
|
texts,
|
49
48
|
}),
|
50
49
|
getFailureEvent: (metadata, settings, error) => ({
|
51
|
-
|
52
|
-
|
53
|
-
metadata,
|
50
|
+
...metadata,
|
51
|
+
functionType: "text-embedding",
|
54
52
|
settings,
|
55
53
|
error,
|
56
54
|
texts,
|
57
55
|
}),
|
58
56
|
getSuccessEvent: (metadata, settings, response, output) => ({
|
59
|
-
|
60
|
-
|
61
|
-
metadata,
|
57
|
+
...metadata,
|
58
|
+
functionType: "text-embedding",
|
62
59
|
settings,
|
63
60
|
texts,
|
64
61
|
response,
|
@@ -88,30 +85,27 @@ function embedText(model, text, options) {
|
|
88
85
|
return model.extractEmbeddings(result)[0];
|
89
86
|
},
|
90
87
|
getStartEvent: (metadata, settings) => ({
|
91
|
-
|
92
|
-
|
88
|
+
...metadata,
|
89
|
+
functionType: "text-embedding",
|
93
90
|
settings,
|
94
91
|
texts,
|
95
92
|
}),
|
96
93
|
getAbortEvent: (metadata, settings) => ({
|
97
|
-
|
98
|
-
|
99
|
-
metadata,
|
94
|
+
...metadata,
|
95
|
+
functionType: "text-embedding",
|
100
96
|
settings,
|
101
97
|
texts,
|
102
98
|
}),
|
103
99
|
getFailureEvent: (metadata, settings, error) => ({
|
104
|
-
|
105
|
-
|
106
|
-
metadata,
|
100
|
+
...metadata,
|
101
|
+
functionType: "text-embedding",
|
107
102
|
settings,
|
108
103
|
error,
|
109
104
|
texts,
|
110
105
|
}),
|
111
106
|
getSuccessEvent: (metadata, settings, response, output) => ({
|
112
|
-
|
113
|
-
|
114
|
-
metadata,
|
107
|
+
...metadata,
|
108
|
+
functionType: "text-embedding",
|
115
109
|
settings,
|
116
110
|
texts,
|
117
111
|
response,
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { Vector } from "../../run/Vector.js";
|
2
|
-
import {
|
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>;
|
@@ -32,30 +32,27 @@ export function embedTexts(model, texts, options) {
|
|
32
32
|
return embeddings;
|
33
33
|
},
|
34
34
|
getStartEvent: (metadata, settings) => ({
|
35
|
-
|
36
|
-
|
35
|
+
...metadata,
|
36
|
+
functionType: "text-embedding",
|
37
37
|
settings,
|
38
38
|
texts,
|
39
39
|
}),
|
40
40
|
getAbortEvent: (metadata, settings) => ({
|
41
|
-
|
42
|
-
|
43
|
-
metadata,
|
41
|
+
...metadata,
|
42
|
+
functionType: "text-embedding",
|
44
43
|
settings,
|
45
44
|
texts,
|
46
45
|
}),
|
47
46
|
getFailureEvent: (metadata, settings, error) => ({
|
48
|
-
|
49
|
-
|
50
|
-
metadata,
|
47
|
+
...metadata,
|
48
|
+
functionType: "text-embedding",
|
51
49
|
settings,
|
52
50
|
error,
|
53
51
|
texts,
|
54
52
|
}),
|
55
53
|
getSuccessEvent: (metadata, settings, response, output) => ({
|
56
|
-
|
57
|
-
|
58
|
-
metadata,
|
54
|
+
...metadata,
|
55
|
+
functionType: "text-embedding",
|
59
56
|
settings,
|
60
57
|
texts,
|
61
58
|
response,
|
@@ -84,30 +81,27 @@ export function embedText(model, text, options) {
|
|
84
81
|
return model.extractEmbeddings(result)[0];
|
85
82
|
},
|
86
83
|
getStartEvent: (metadata, settings) => ({
|
87
|
-
|
88
|
-
|
84
|
+
...metadata,
|
85
|
+
functionType: "text-embedding",
|
89
86
|
settings,
|
90
87
|
texts,
|
91
88
|
}),
|
92
89
|
getAbortEvent: (metadata, settings) => ({
|
93
|
-
|
94
|
-
|
95
|
-
metadata,
|
90
|
+
...metadata,
|
91
|
+
functionType: "text-embedding",
|
96
92
|
settings,
|
97
93
|
texts,
|
98
94
|
}),
|
99
95
|
getFailureEvent: (metadata, settings, error) => ({
|
100
|
-
|
101
|
-
|
102
|
-
metadata,
|
96
|
+
...metadata,
|
97
|
+
functionType: "text-embedding",
|
103
98
|
settings,
|
104
99
|
error,
|
105
100
|
texts,
|
106
101
|
}),
|
107
102
|
getSuccessEvent: (metadata, settings, response, output) => ({
|
108
|
-
|
109
|
-
|
110
|
-
metadata,
|
103
|
+
...metadata,
|
104
|
+
functionType: "text-embedding",
|
111
105
|
settings,
|
112
106
|
texts,
|
113
107
|
response,
|
@@ -2,7 +2,8 @@
|
|
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("../run/FunctionEventSource.cjs");
|
6
|
+
const GlobalFunctionObservers_js_1 = require("../run/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");
|
@@ -58,13 +59,19 @@ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, get
|
|
58
59
|
model = model.withSettings(options.settings);
|
59
60
|
options = {
|
60
61
|
functionId: options.functionId,
|
62
|
+
observers: options.observers,
|
61
63
|
run: options.run,
|
62
64
|
};
|
63
65
|
}
|
64
66
|
const run = options?.run;
|
65
67
|
const settings = model.settings;
|
66
|
-
const eventSource = new
|
67
|
-
observers: [
|
68
|
+
const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
|
69
|
+
observers: [
|
70
|
+
...(0, GlobalFunctionObservers_js_1.getGlobalFunctionObservers)(),
|
71
|
+
...(settings.observers ?? []),
|
72
|
+
...(run?.observers ?? []),
|
73
|
+
...(options?.observers ?? []),
|
74
|
+
],
|
68
75
|
errorHandler: run?.errorHandler,
|
69
76
|
});
|
70
77
|
const durationMeasurement = (0, DurationMeasurement_js_1.startDurationMeasurement)();
|
@@ -75,9 +82,13 @@ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, get
|
|
75
82
|
userId: run?.userId,
|
76
83
|
functionId: options?.functionId,
|
77
84
|
model: model.modelInformation,
|
78
|
-
|
85
|
+
timestamp: durationMeasurement.startDate,
|
86
|
+
startTimestamp: durationMeasurement.startDate,
|
79
87
|
};
|
80
|
-
eventSource.
|
88
|
+
eventSource.notify(getStartEvent({
|
89
|
+
...startMetadata,
|
90
|
+
eventType: "started",
|
91
|
+
}, settings));
|
81
92
|
const result = await (0, runSafe_js_1.runSafe)(() => generateResponse({
|
82
93
|
functionId: options?.functionId,
|
83
94
|
settings,
|
@@ -85,19 +96,30 @@ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, get
|
|
85
96
|
}));
|
86
97
|
const finishMetadata = {
|
87
98
|
...startMetadata,
|
99
|
+
eventType: "finished",
|
100
|
+
finishTimestamp: new Date(),
|
88
101
|
durationInMs: durationMeasurement.durationInMs,
|
89
102
|
};
|
90
103
|
if (!result.ok) {
|
91
104
|
if (result.isAborted) {
|
92
|
-
eventSource.
|
105
|
+
eventSource.notify(getAbortEvent({
|
106
|
+
...finishMetadata,
|
107
|
+
status: "abort",
|
108
|
+
}, settings));
|
93
109
|
throw new AbortError_js_1.AbortError();
|
94
110
|
}
|
95
|
-
eventSource.
|
111
|
+
eventSource.notify(getFailureEvent({
|
112
|
+
...finishMetadata,
|
113
|
+
status: "error",
|
114
|
+
}, settings, result.error));
|
96
115
|
throw result.error;
|
97
116
|
}
|
98
117
|
const response = result.output;
|
99
118
|
const output = extractOutputValue(response);
|
100
|
-
eventSource.
|
119
|
+
eventSource.notify(getSuccessEvent({
|
120
|
+
...finishMetadata,
|
121
|
+
status: "success",
|
122
|
+
}, settings, response, output));
|
101
123
|
return {
|
102
124
|
output,
|
103
125
|
response,
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import { FunctionOptions } from "./FunctionOptions.js";
|
2
1
|
import { Model, ModelSettings } from "./Model.js";
|
3
|
-
import {
|
2
|
+
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent, ModelCallFinishedEvent, 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,24 @@ 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
15
|
export declare function executeCall<SETTINGS extends ModelSettings, MODEL extends Model<SETTINGS>, OUTPUT, RESPONSE>({ model, options, getStartEvent, getAbortEvent, getFailureEvent, getSuccessEvent, generateResponse, extractOutputValue, }: {
|
15
16
|
model: MODEL;
|
16
|
-
options?:
|
17
|
-
getStartEvent: (metadata:
|
18
|
-
getAbortEvent: (metadata:
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
options?: ModelFunctionOptions<SETTINGS>;
|
18
|
+
getStartEvent: (metadata: Omit<BaseModelCallStartedEvent, "functionType">, settings: SETTINGS) => ModelCallStartedEvent;
|
19
|
+
getAbortEvent: (metadata: Omit<BaseModelCallFinishedEvent & {
|
20
|
+
status: "abort";
|
21
|
+
}, "functionType">, settings: SETTINGS) => ModelCallFinishedEvent;
|
22
|
+
getFailureEvent: (metadata: Omit<BaseModelCallFinishedEvent & {
|
23
|
+
status: "error";
|
24
|
+
}, "functionType">, settings: SETTINGS, error: unknown) => ModelCallFinishedEvent;
|
25
|
+
getSuccessEvent: (metadata: Omit<BaseModelCallFinishedEvent & {
|
26
|
+
status: "success";
|
27
|
+
}, "functionType">, settings: SETTINGS, response: RESPONSE, output: OUTPUT) => ModelCallFinishedEvent;
|
28
|
+
generateResponse: (options: ModelFunctionOptions<SETTINGS>) => PromiseLike<RESPONSE>;
|
22
29
|
extractOutputValue: (response: RESPONSE) => OUTPUT;
|
23
30
|
}): ModelFunctionPromise<MODEL, OUTPUT, RESPONSE>;
|
24
31
|
export declare class ModelFunctionPromise<MODEL extends Model<any>, OUTPUT, RESPONSE> extends Promise<OUTPUT> {
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import { nanoid as createId } from "nanoid";
|
2
|
-
import {
|
2
|
+
import { FunctionEventSource } from "../run/FunctionEventSource.js";
|
3
|
+
import { getGlobalFunctionObservers } from "../run/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";
|
@@ -53,13 +54,19 @@ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, get
|
|
53
54
|
model = model.withSettings(options.settings);
|
54
55
|
options = {
|
55
56
|
functionId: options.functionId,
|
57
|
+
observers: options.observers,
|
56
58
|
run: options.run,
|
57
59
|
};
|
58
60
|
}
|
59
61
|
const run = options?.run;
|
60
62
|
const settings = model.settings;
|
61
|
-
const eventSource = new
|
62
|
-
observers: [
|
63
|
+
const eventSource = new FunctionEventSource({
|
64
|
+
observers: [
|
65
|
+
...getGlobalFunctionObservers(),
|
66
|
+
...(settings.observers ?? []),
|
67
|
+
...(run?.observers ?? []),
|
68
|
+
...(options?.observers ?? []),
|
69
|
+
],
|
63
70
|
errorHandler: run?.errorHandler,
|
64
71
|
});
|
65
72
|
const durationMeasurement = startDurationMeasurement();
|
@@ -70,9 +77,13 @@ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, get
|
|
70
77
|
userId: run?.userId,
|
71
78
|
functionId: options?.functionId,
|
72
79
|
model: model.modelInformation,
|
73
|
-
|
80
|
+
timestamp: durationMeasurement.startDate,
|
81
|
+
startTimestamp: durationMeasurement.startDate,
|
74
82
|
};
|
75
|
-
eventSource.
|
83
|
+
eventSource.notify(getStartEvent({
|
84
|
+
...startMetadata,
|
85
|
+
eventType: "started",
|
86
|
+
}, settings));
|
76
87
|
const result = await runSafe(() => generateResponse({
|
77
88
|
functionId: options?.functionId,
|
78
89
|
settings,
|
@@ -80,19 +91,30 @@ async function doExecuteCall({ model, options, getStartEvent, getAbortEvent, get
|
|
80
91
|
}));
|
81
92
|
const finishMetadata = {
|
82
93
|
...startMetadata,
|
94
|
+
eventType: "finished",
|
95
|
+
finishTimestamp: new Date(),
|
83
96
|
durationInMs: durationMeasurement.durationInMs,
|
84
97
|
};
|
85
98
|
if (!result.ok) {
|
86
99
|
if (result.isAborted) {
|
87
|
-
eventSource.
|
100
|
+
eventSource.notify(getAbortEvent({
|
101
|
+
...finishMetadata,
|
102
|
+
status: "abort",
|
103
|
+
}, settings));
|
88
104
|
throw new AbortError();
|
89
105
|
}
|
90
|
-
eventSource.
|
106
|
+
eventSource.notify(getFailureEvent({
|
107
|
+
...finishMetadata,
|
108
|
+
status: "error",
|
109
|
+
}, settings, result.error));
|
91
110
|
throw result.error;
|
92
111
|
}
|
93
112
|
const response = result.output;
|
94
113
|
const output = extractOutputValue(response);
|
95
|
-
eventSource.
|
114
|
+
eventSource.notify(getSuccessEvent({
|
115
|
+
...finishMetadata,
|
116
|
+
status: "success",
|
117
|
+
}, settings, response, output));
|
96
118
|
return {
|
97
119
|
output,
|
98
120
|
response,
|
@@ -1,13 +1,11 @@
|
|
1
|
-
import {
|
2
|
-
export type ImageGenerationStartedEvent = {
|
3
|
-
|
4
|
-
metadata: ModelCallStartedEventMetadata;
|
1
|
+
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
|
2
|
+
export type ImageGenerationStartedEvent = BaseModelCallStartedEvent & {
|
3
|
+
functionType: "image-generation";
|
5
4
|
settings: unknown;
|
6
5
|
prompt: unknown;
|
7
6
|
};
|
8
|
-
export type ImageGenerationFinishedEvent = {
|
9
|
-
|
10
|
-
metadata: ModelCallFinishedEventMetadata;
|
7
|
+
export type ImageGenerationFinishedEvent = BaseModelCallFinishedEvent & {
|
8
|
+
functionType: "image-generation";
|
11
9
|
settings: unknown;
|
12
10
|
prompt: unknown;
|
13
11
|
} & ({
|
@@ -15,7 +13,7 @@ export type ImageGenerationFinishedEvent = {
|
|
15
13
|
response: unknown;
|
16
14
|
generatedImage: string;
|
17
15
|
} | {
|
18
|
-
status: "
|
16
|
+
status: "error";
|
19
17
|
error: unknown;
|
20
18
|
} | {
|
21
19
|
status: "abort";
|
@@ -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
|
}
|