modelfusion 0.48.0 → 0.49.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 +30 -45
- 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/embed.cjs +3 -3
- package/model-function/embed/embed.js +3 -3
- package/model-function/{executeCall.cjs → executeStandardCall.cjs} +3 -3
- package/model-function/{executeCall.d.ts → executeStandardCall.d.ts} +1 -1
- package/model-function/{executeCall.js → executeStandardCall.js} +1 -1
- package/model-function/executeStreamCall.cjs +132 -0
- package/model-function/executeStreamCall.d.ts +20 -0
- package/model-function/executeStreamCall.js +128 -0
- package/model-function/generate-image/generateImage.cjs +2 -2
- package/model-function/generate-image/generateImage.js +2 -2
- 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/generateStructure.cjs +2 -2
- package/model-function/generate-structure/generateStructure.js +2 -2
- package/model-function/generate-structure/generateStructureOrText.cjs +2 -2
- package/model-function/generate-structure/generateStructureOrText.js +2 -2
- 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 +28 -136
- package/model-function/generate-structure/streamStructure.js +27 -135
- package/model-function/generate-text/TextGenerationEvent.d.ts +6 -0
- package/model-function/generate-text/generateText.cjs +3 -3
- package/model-function/generate-text/generateText.d.ts +1 -1
- package/model-function/generate-text/generateText.js +3 -3
- 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 +21 -128
- package/model-function/generate-text/streamText.js +20 -127
- 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/transcribe.cjs → generate-transcription/generateTranscription.cjs} +6 -6
- 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} +4 -4
- 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} +25 -25
- package/model-provider/openai/{OpenAITextGenerationModel.js → OpenAICompletionModel.js} +12 -12
- package/model-provider/openai/OpenAICostCalculator.cjs +3 -3
- package/model-provider/openai/OpenAICostCalculator.js +3 -3
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +1 -1
- package/model-provider/openai/TikTokenTokenizer.d.ts +2 -2
- 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/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.cjs +0 -2
- package/model-function/generate-text/TextStreamingEvent.d.ts +0 -7
- package/model-function/generate-text/TextStreamingEvent.js +0 -1
- package/model-function/synthesize-speech/SpeechSynthesisEvent.cjs +0 -2
- package/model-function/synthesize-speech/SpeechSynthesisEvent.d.ts +0 -21
- package/model-function/synthesize-speech/SpeechSynthesisEvent.js +0 -1
- package/model-function/synthesize-speech/SpeechSynthesisModel.cjs +0 -2
- package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +0 -15
- package/model-function/synthesize-speech/SpeechSynthesisModel.js +0 -1
- 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 → generate-speech/SpeechGenerationEvent.cjs} +0 -0
- /package/model-function/{describe-image/ImageDescriptionEvent.js → generate-speech/SpeechGenerationEvent.js} +0 -0
- /package/model-function/{describe-image/ImageDescriptionModel.cjs → generate-speech/SpeechGenerationModel.cjs} +0 -0
- /package/model-function/{describe-image/ImageDescriptionModel.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.d.ts +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
@@ -1,156 +1,48 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.streamStructure = void 0;
|
4
|
-
const nanoid_1 = require("nanoid");
|
5
|
-
const FunctionEventSource_js_1 = require("../../core/FunctionEventSource.cjs");
|
6
|
-
const GlobalFunctionLogging_js_1 = require("../../core/GlobalFunctionLogging.cjs");
|
7
|
-
const GlobalFunctionObservers_js_1 = require("../../core/GlobalFunctionObservers.cjs");
|
8
|
-
const AbortError_js_1 = require("../../core/api/AbortError.cjs");
|
9
|
-
const getFunctionCallLogger_js_1 = require("../../core/getFunctionCallLogger.cjs");
|
10
|
-
const getRun_js_1 = require("../../core/getRun.cjs");
|
11
|
-
const DurationMeasurement_js_1 = require("../../util/DurationMeasurement.cjs");
|
12
4
|
const isDeepEqualData_js_1 = require("../../util/isDeepEqualData.cjs");
|
13
|
-
const runSafe_js_1 = require("../../util/runSafe.cjs");
|
14
5
|
const AsyncIterableResultPromise_js_1 = require("../AsyncIterableResultPromise.cjs");
|
6
|
+
const executeStreamCall_js_1 = require("../executeStreamCall.cjs");
|
15
7
|
function streamStructure(model, structureDefinition, prompt, options) {
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
async function doStreamStructure(model, structureDefinition, prompt, options) {
|
20
|
-
const run = await (0, getRun_js_1.getRun)(options?.run);
|
21
|
-
const settings = model.settings;
|
22
|
-
const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
|
23
|
-
observers: [
|
24
|
-
...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0, GlobalFunctionLogging_js_1.getGlobalFunctionLogging)()),
|
25
|
-
...(0, GlobalFunctionObservers_js_1.getGlobalFunctionObservers)(),
|
26
|
-
...(settings.observers ?? []),
|
27
|
-
...(run?.functionObserver != null ? [run.functionObserver] : []),
|
28
|
-
...(options?.observers ?? []),
|
29
|
-
],
|
30
|
-
errorHandler: run?.errorHandler,
|
31
|
-
});
|
32
|
-
const durationMeasurement = (0, DurationMeasurement_js_1.startDurationMeasurement)();
|
33
|
-
const startMetadata = {
|
8
|
+
let lastStructure;
|
9
|
+
let lastFullDelta;
|
10
|
+
return new AsyncIterableResultPromise_js_1.AsyncIterableResultPromise((0, executeStreamCall_js_1.executeStreamCall)({
|
34
11
|
functionType: "structure-streaming",
|
35
|
-
callId: `call-${(0, nanoid_1.nanoid)()}`,
|
36
|
-
runId: run?.runId,
|
37
|
-
sessionId: run?.sessionId,
|
38
|
-
userId: run?.userId,
|
39
|
-
functionId: options?.functionId,
|
40
|
-
model: model.modelInformation,
|
41
|
-
settings: model.settingsForEvent,
|
42
12
|
input: prompt,
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
});
|
57
|
-
return (async function* () {
|
58
|
-
function reportError(error) {
|
59
|
-
const finishMetadata = {
|
60
|
-
eventType: "finished",
|
61
|
-
...startMetadata,
|
62
|
-
finishTimestamp: new Date(),
|
63
|
-
durationInMs: durationMeasurement.durationInMs,
|
13
|
+
model,
|
14
|
+
options,
|
15
|
+
startStream: async (options) => model.doStreamStructure(structureDefinition, prompt, options),
|
16
|
+
processDelta: (delta) => {
|
17
|
+
const latestFullDelta = delta.fullDelta;
|
18
|
+
const latestStructure = delta.valueDelta;
|
19
|
+
// only send a new part into the stream when the partial structure has changed:
|
20
|
+
if (!(0, isDeepEqualData_js_1.isDeepEqualData)(lastStructure, latestStructure)) {
|
21
|
+
lastFullDelta = latestFullDelta;
|
22
|
+
lastStructure = latestStructure;
|
23
|
+
return {
|
24
|
+
isComplete: false,
|
25
|
+
value: lastStructure,
|
64
26
|
};
|
65
|
-
eventSource.notify(error instanceof AbortError_js_1.AbortError
|
66
|
-
? {
|
67
|
-
...finishMetadata,
|
68
|
-
result: {
|
69
|
-
status: "abort",
|
70
|
-
},
|
71
|
-
}
|
72
|
-
: {
|
73
|
-
...finishMetadata,
|
74
|
-
result: {
|
75
|
-
status: "error",
|
76
|
-
error,
|
77
|
-
},
|
78
|
-
});
|
79
|
-
}
|
80
|
-
let lastStructure;
|
81
|
-
let lastFullDelta;
|
82
|
-
for await (const event of deltaIterable) {
|
83
|
-
if (event?.type === "error") {
|
84
|
-
reportError(event.error);
|
85
|
-
throw event.error;
|
86
|
-
}
|
87
|
-
if (event?.type === "delta") {
|
88
|
-
const latestFullDelta = event.fullDelta;
|
89
|
-
const latestStructure = event.valueDelta;
|
90
|
-
// only send a new part into the stream when the partial structure has changed:
|
91
|
-
if (!(0, isDeepEqualData_js_1.isDeepEqualData)(lastStructure, latestStructure)) {
|
92
|
-
lastFullDelta = latestFullDelta;
|
93
|
-
lastStructure = latestStructure;
|
94
|
-
yield {
|
95
|
-
isComplete: false,
|
96
|
-
value: lastStructure,
|
97
|
-
};
|
98
|
-
}
|
99
|
-
}
|
100
27
|
}
|
28
|
+
return undefined;
|
29
|
+
},
|
30
|
+
processFinished: () => {
|
101
31
|
// process the final result (full type validation):
|
102
32
|
const parseResult = structureDefinition.schema.validate(lastStructure);
|
103
33
|
if (!parseResult.success) {
|
104
34
|
reportError(parseResult.error);
|
105
35
|
throw parseResult.error;
|
106
36
|
}
|
107
|
-
|
37
|
+
return {
|
108
38
|
isComplete: true,
|
109
39
|
value: parseResult.data,
|
110
40
|
};
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
eventSource.notify({
|
118
|
-
...finishMetadata,
|
119
|
-
result: {
|
120
|
-
status: "success",
|
121
|
-
response: lastFullDelta,
|
122
|
-
value: lastStructure,
|
123
|
-
},
|
124
|
-
});
|
125
|
-
})();
|
126
|
-
});
|
127
|
-
if (!result.ok) {
|
128
|
-
const finishMetadata = {
|
129
|
-
eventType: "finished",
|
130
|
-
...startMetadata,
|
131
|
-
finishTimestamp: new Date(),
|
132
|
-
durationInMs: durationMeasurement.durationInMs,
|
133
|
-
};
|
134
|
-
if (result.isAborted) {
|
135
|
-
eventSource.notify({
|
136
|
-
...finishMetadata,
|
137
|
-
result: {
|
138
|
-
status: "abort",
|
139
|
-
},
|
140
|
-
});
|
141
|
-
throw new AbortError_js_1.AbortError();
|
142
|
-
}
|
143
|
-
eventSource.notify({
|
144
|
-
...finishMetadata,
|
145
|
-
result: {
|
146
|
-
status: "error",
|
147
|
-
error: result.error,
|
148
|
-
},
|
149
|
-
});
|
150
|
-
throw result.error;
|
151
|
-
}
|
152
|
-
return {
|
153
|
-
output: result.value,
|
154
|
-
metadata: startMetadata,
|
155
|
-
};
|
41
|
+
},
|
42
|
+
getResult: () => ({
|
43
|
+
response: lastFullDelta,
|
44
|
+
value: lastStructure,
|
45
|
+
}),
|
46
|
+
}));
|
156
47
|
}
|
48
|
+
exports.streamStructure = streamStructure;
|
@@ -1,152 +1,44 @@
|
|
1
|
-
import { nanoid as createId } from "nanoid";
|
2
|
-
import { FunctionEventSource } from "../../core/FunctionEventSource.js";
|
3
|
-
import { getGlobalFunctionLogging } from "../../core/GlobalFunctionLogging.js";
|
4
|
-
import { getGlobalFunctionObservers } from "../../core/GlobalFunctionObservers.js";
|
5
|
-
import { AbortError } from "../../core/api/AbortError.js";
|
6
|
-
import { getFunctionCallLogger } from "../../core/getFunctionCallLogger.js";
|
7
|
-
import { getRun } from "../../core/getRun.js";
|
8
|
-
import { startDurationMeasurement } from "../../util/DurationMeasurement.js";
|
9
1
|
import { isDeepEqualData } from "../../util/isDeepEqualData.js";
|
10
|
-
import { runSafe } from "../../util/runSafe.js";
|
11
2
|
import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
|
3
|
+
import { executeStreamCall } from "../executeStreamCall.js";
|
12
4
|
export function streamStructure(model, structureDefinition, prompt, options) {
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
const run = await getRun(options?.run);
|
17
|
-
const settings = model.settings;
|
18
|
-
const eventSource = new FunctionEventSource({
|
19
|
-
observers: [
|
20
|
-
...getFunctionCallLogger(options?.logging ?? getGlobalFunctionLogging()),
|
21
|
-
...getGlobalFunctionObservers(),
|
22
|
-
...(settings.observers ?? []),
|
23
|
-
...(run?.functionObserver != null ? [run.functionObserver] : []),
|
24
|
-
...(options?.observers ?? []),
|
25
|
-
],
|
26
|
-
errorHandler: run?.errorHandler,
|
27
|
-
});
|
28
|
-
const durationMeasurement = startDurationMeasurement();
|
29
|
-
const startMetadata = {
|
5
|
+
let lastStructure;
|
6
|
+
let lastFullDelta;
|
7
|
+
return new AsyncIterableResultPromise(executeStreamCall({
|
30
8
|
functionType: "structure-streaming",
|
31
|
-
callId: `call-${createId()}`,
|
32
|
-
runId: run?.runId,
|
33
|
-
sessionId: run?.sessionId,
|
34
|
-
userId: run?.userId,
|
35
|
-
functionId: options?.functionId,
|
36
|
-
model: model.modelInformation,
|
37
|
-
settings: model.settingsForEvent,
|
38
9
|
input: prompt,
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
});
|
53
|
-
return (async function* () {
|
54
|
-
function reportError(error) {
|
55
|
-
const finishMetadata = {
|
56
|
-
eventType: "finished",
|
57
|
-
...startMetadata,
|
58
|
-
finishTimestamp: new Date(),
|
59
|
-
durationInMs: durationMeasurement.durationInMs,
|
10
|
+
model,
|
11
|
+
options,
|
12
|
+
startStream: async (options) => model.doStreamStructure(structureDefinition, prompt, options),
|
13
|
+
processDelta: (delta) => {
|
14
|
+
const latestFullDelta = delta.fullDelta;
|
15
|
+
const latestStructure = delta.valueDelta;
|
16
|
+
// only send a new part into the stream when the partial structure has changed:
|
17
|
+
if (!isDeepEqualData(lastStructure, latestStructure)) {
|
18
|
+
lastFullDelta = latestFullDelta;
|
19
|
+
lastStructure = latestStructure;
|
20
|
+
return {
|
21
|
+
isComplete: false,
|
22
|
+
value: lastStructure,
|
60
23
|
};
|
61
|
-
eventSource.notify(error instanceof AbortError
|
62
|
-
? {
|
63
|
-
...finishMetadata,
|
64
|
-
result: {
|
65
|
-
status: "abort",
|
66
|
-
},
|
67
|
-
}
|
68
|
-
: {
|
69
|
-
...finishMetadata,
|
70
|
-
result: {
|
71
|
-
status: "error",
|
72
|
-
error,
|
73
|
-
},
|
74
|
-
});
|
75
|
-
}
|
76
|
-
let lastStructure;
|
77
|
-
let lastFullDelta;
|
78
|
-
for await (const event of deltaIterable) {
|
79
|
-
if (event?.type === "error") {
|
80
|
-
reportError(event.error);
|
81
|
-
throw event.error;
|
82
|
-
}
|
83
|
-
if (event?.type === "delta") {
|
84
|
-
const latestFullDelta = event.fullDelta;
|
85
|
-
const latestStructure = event.valueDelta;
|
86
|
-
// only send a new part into the stream when the partial structure has changed:
|
87
|
-
if (!isDeepEqualData(lastStructure, latestStructure)) {
|
88
|
-
lastFullDelta = latestFullDelta;
|
89
|
-
lastStructure = latestStructure;
|
90
|
-
yield {
|
91
|
-
isComplete: false,
|
92
|
-
value: lastStructure,
|
93
|
-
};
|
94
|
-
}
|
95
|
-
}
|
96
24
|
}
|
25
|
+
return undefined;
|
26
|
+
},
|
27
|
+
processFinished: () => {
|
97
28
|
// process the final result (full type validation):
|
98
29
|
const parseResult = structureDefinition.schema.validate(lastStructure);
|
99
30
|
if (!parseResult.success) {
|
100
31
|
reportError(parseResult.error);
|
101
32
|
throw parseResult.error;
|
102
33
|
}
|
103
|
-
|
34
|
+
return {
|
104
35
|
isComplete: true,
|
105
36
|
value: parseResult.data,
|
106
37
|
};
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
eventSource.notify({
|
114
|
-
...finishMetadata,
|
115
|
-
result: {
|
116
|
-
status: "success",
|
117
|
-
response: lastFullDelta,
|
118
|
-
value: lastStructure,
|
119
|
-
},
|
120
|
-
});
|
121
|
-
})();
|
122
|
-
});
|
123
|
-
if (!result.ok) {
|
124
|
-
const finishMetadata = {
|
125
|
-
eventType: "finished",
|
126
|
-
...startMetadata,
|
127
|
-
finishTimestamp: new Date(),
|
128
|
-
durationInMs: durationMeasurement.durationInMs,
|
129
|
-
};
|
130
|
-
if (result.isAborted) {
|
131
|
-
eventSource.notify({
|
132
|
-
...finishMetadata,
|
133
|
-
result: {
|
134
|
-
status: "abort",
|
135
|
-
},
|
136
|
-
});
|
137
|
-
throw new AbortError();
|
138
|
-
}
|
139
|
-
eventSource.notify({
|
140
|
-
...finishMetadata,
|
141
|
-
result: {
|
142
|
-
status: "error",
|
143
|
-
error: result.error,
|
144
|
-
},
|
145
|
-
});
|
146
|
-
throw result.error;
|
147
|
-
}
|
148
|
-
return {
|
149
|
-
output: result.value,
|
150
|
-
metadata: startMetadata,
|
151
|
-
};
|
38
|
+
},
|
39
|
+
getResult: () => ({
|
40
|
+
response: lastFullDelta,
|
41
|
+
value: lastStructure,
|
42
|
+
}),
|
43
|
+
}));
|
152
44
|
}
|
@@ -21,3 +21,9 @@ export interface TextGenerationFinishedEvent extends BaseModelCallFinishedEvent
|
|
21
21
|
functionType: "text-generation";
|
22
22
|
result: TextGenerationFinishedEventResult;
|
23
23
|
}
|
24
|
+
export interface TextStreamingStartedEvent extends BaseModelCallStartedEvent {
|
25
|
+
functionType: "text-streaming";
|
26
|
+
}
|
27
|
+
export interface TextStreamingFinishedEvent extends BaseModelCallFinishedEvent {
|
28
|
+
functionType: "text-streaming";
|
29
|
+
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.generateText = 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
|
* Generates a text using a prompt.
|
@@ -9,7 +9,7 @@ const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
|
|
9
9
|
* For example, OpenAI text models expect a string prompt, and OpenAI chat models expect an array of chat messages.
|
10
10
|
*
|
11
11
|
* @example
|
12
|
-
* const model = new
|
12
|
+
* const model = new OpenAICompletionModel(...);
|
13
13
|
*
|
14
14
|
* const text = await generateText(
|
15
15
|
* model,
|
@@ -17,7 +17,7 @@ const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
|
|
17
17
|
* );
|
18
18
|
*/
|
19
19
|
function generateText(model, prompt, options) {
|
20
|
-
return new ModelFunctionPromise_js_1.ModelFunctionPromise((0,
|
20
|
+
return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeStandardCall_js_1.executeStandardCall)({
|
21
21
|
functionType: "text-generation",
|
22
22
|
input: prompt,
|
23
23
|
model,
|
@@ -7,7 +7,7 @@ import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerati
|
|
7
7
|
* For example, OpenAI text models expect a string prompt, and OpenAI chat models expect an array of chat messages.
|
8
8
|
*
|
9
9
|
* @example
|
10
|
-
* const model = new
|
10
|
+
* const model = new OpenAICompletionModel(...);
|
11
11
|
*
|
12
12
|
* const text = await generateText(
|
13
13
|
* model,
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { executeStandardCall } from "../executeStandardCall.js";
|
2
2
|
import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
|
3
3
|
/**
|
4
4
|
* Generates a text using a prompt.
|
@@ -6,7 +6,7 @@ import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
|
|
6
6
|
* For example, OpenAI text models expect a string prompt, and OpenAI chat models expect an array of chat messages.
|
7
7
|
*
|
8
8
|
* @example
|
9
|
-
* const model = new
|
9
|
+
* const model = new OpenAICompletionModel(...);
|
10
10
|
*
|
11
11
|
* const text = await generateText(
|
12
12
|
* model,
|
@@ -14,7 +14,7 @@ import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
|
|
14
14
|
* );
|
15
15
|
*/
|
16
16
|
export function generateText(model, prompt, options) {
|
17
|
-
return new ModelFunctionPromise(
|
17
|
+
return new ModelFunctionPromise(executeStandardCall({
|
18
18
|
functionType: "text-generation",
|
19
19
|
input: prompt,
|
20
20
|
model,
|
@@ -24,7 +24,6 @@ __exportStar(require("./TextGenerationEvent.cjs"), exports);
|
|
24
24
|
__exportStar(require("./TextGenerationModel.cjs"), exports);
|
25
25
|
__exportStar(require("./TextGenerationPromptFormat.cjs"), exports);
|
26
26
|
__exportStar(require("./TextPromptFormat.cjs"), exports);
|
27
|
-
__exportStar(require("./TextStreamingEvent.cjs"), exports);
|
28
27
|
__exportStar(require("./VicunaPromptFormat.cjs"), exports);
|
29
28
|
__exportStar(require("./generateText.cjs"), exports);
|
30
29
|
__exportStar(require("./streamText.cjs"), exports);
|
@@ -8,7 +8,6 @@ export * from "./TextGenerationEvent.js";
|
|
8
8
|
export * from "./TextGenerationModel.js";
|
9
9
|
export * from "./TextGenerationPromptFormat.js";
|
10
10
|
export * from "./TextPromptFormat.js";
|
11
|
-
export * from "./TextStreamingEvent.js";
|
12
11
|
export * from "./VicunaPromptFormat.js";
|
13
12
|
export * from "./generateText.js";
|
14
13
|
export * from "./streamText.js";
|
@@ -8,7 +8,6 @@ export * from "./TextGenerationEvent.js";
|
|
8
8
|
export * from "./TextGenerationModel.js";
|
9
9
|
export * from "./TextGenerationPromptFormat.js";
|
10
10
|
export * from "./TextPromptFormat.js";
|
11
|
-
export * from "./TextStreamingEvent.js";
|
12
11
|
export * from "./VicunaPromptFormat.js";
|
13
12
|
export * from "./generateText.js";
|
14
13
|
export * from "./streamText.js";
|
@@ -1,137 +1,30 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.streamText = void 0;
|
4
|
-
const nanoid_1 = require("nanoid");
|
5
|
-
const FunctionEventSource_js_1 = require("../../core/FunctionEventSource.cjs");
|
6
|
-
const GlobalFunctionLogging_js_1 = require("../../core/GlobalFunctionLogging.cjs");
|
7
|
-
const GlobalFunctionObservers_js_1 = require("../../core/GlobalFunctionObservers.cjs");
|
8
|
-
const AbortError_js_1 = require("../../core/api/AbortError.cjs");
|
9
|
-
const getFunctionCallLogger_js_1 = require("../../core/getFunctionCallLogger.cjs");
|
10
|
-
const getRun_js_1 = require("../../core/getRun.cjs");
|
11
|
-
const DurationMeasurement_js_1 = require("../../util/DurationMeasurement.cjs");
|
12
|
-
const runSafe_js_1 = require("../../util/runSafe.cjs");
|
13
4
|
const AsyncIterableResultPromise_js_1 = require("../AsyncIterableResultPromise.cjs");
|
5
|
+
const executeStreamCall_js_1 = require("../executeStreamCall.cjs");
|
14
6
|
function streamText(model, prompt, options) {
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
async function doStreamText(model, prompt, options) {
|
19
|
-
const run = await (0, getRun_js_1.getRun)(options?.run);
|
20
|
-
const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
|
21
|
-
observers: [
|
22
|
-
...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0, GlobalFunctionLogging_js_1.getGlobalFunctionLogging)()),
|
23
|
-
...(0, GlobalFunctionObservers_js_1.getGlobalFunctionObservers)(),
|
24
|
-
...(model.settings.observers ?? []),
|
25
|
-
...(run?.functionObserver != null ? [run.functionObserver] : []),
|
26
|
-
...(options?.observers ?? []),
|
27
|
-
],
|
28
|
-
errorHandler: run?.errorHandler,
|
29
|
-
});
|
30
|
-
const durationMeasurement = (0, DurationMeasurement_js_1.startDurationMeasurement)();
|
31
|
-
const startMetadata = {
|
7
|
+
let accumulatedText = "";
|
8
|
+
let lastFullDelta;
|
9
|
+
return new AsyncIterableResultPromise_js_1.AsyncIterableResultPromise((0, executeStreamCall_js_1.executeStreamCall)({
|
32
10
|
functionType: "text-streaming",
|
33
|
-
callId: `call-${(0, nanoid_1.nanoid)()}`,
|
34
|
-
runId: run?.runId,
|
35
|
-
sessionId: run?.sessionId,
|
36
|
-
userId: run?.userId,
|
37
|
-
functionId: options?.functionId,
|
38
|
-
model: model.modelInformation,
|
39
|
-
settings: model.settingsForEvent,
|
40
11
|
input: prompt,
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
functionId: options?.functionId,
|
51
|
-
logging: options?.logging,
|
52
|
-
observers: options?.observers,
|
53
|
-
run,
|
54
|
-
});
|
55
|
-
return (async function* () {
|
56
|
-
let accumulatedText = "";
|
57
|
-
let lastFullDelta;
|
58
|
-
for await (const event of deltaIterable) {
|
59
|
-
if (event?.type === "error") {
|
60
|
-
const error = event.error;
|
61
|
-
const finishMetadata = {
|
62
|
-
eventType: "finished",
|
63
|
-
...startMetadata,
|
64
|
-
finishTimestamp: new Date(),
|
65
|
-
durationInMs: durationMeasurement.durationInMs,
|
66
|
-
};
|
67
|
-
eventSource.notify(error instanceof AbortError_js_1.AbortError
|
68
|
-
? {
|
69
|
-
...finishMetadata,
|
70
|
-
result: {
|
71
|
-
status: "abort",
|
72
|
-
},
|
73
|
-
}
|
74
|
-
: {
|
75
|
-
...finishMetadata,
|
76
|
-
result: {
|
77
|
-
status: "error",
|
78
|
-
error,
|
79
|
-
},
|
80
|
-
});
|
81
|
-
throw error;
|
82
|
-
}
|
83
|
-
if (event?.type === "delta") {
|
84
|
-
lastFullDelta = event.fullDelta;
|
85
|
-
const textDelta = event.valueDelta;
|
86
|
-
if (textDelta != null && textDelta.length > 0) {
|
87
|
-
accumulatedText += textDelta;
|
88
|
-
yield textDelta;
|
89
|
-
}
|
90
|
-
}
|
12
|
+
model,
|
13
|
+
options,
|
14
|
+
startStream: async (options) => model.doStreamText(prompt, options),
|
15
|
+
processDelta: (delta) => {
|
16
|
+
lastFullDelta = delta.fullDelta;
|
17
|
+
const textDelta = delta.valueDelta;
|
18
|
+
if (textDelta != null && textDelta.length > 0) {
|
19
|
+
accumulatedText += textDelta;
|
20
|
+
return textDelta;
|
91
21
|
}
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
...finishMetadata,
|
100
|
-
result: {
|
101
|
-
status: "success",
|
102
|
-
response: lastFullDelta,
|
103
|
-
value: accumulatedText,
|
104
|
-
},
|
105
|
-
});
|
106
|
-
})();
|
107
|
-
});
|
108
|
-
if (!result.ok) {
|
109
|
-
const finishMetadata = {
|
110
|
-
eventType: "finished",
|
111
|
-
...startMetadata,
|
112
|
-
finishTimestamp: new Date(),
|
113
|
-
durationInMs: durationMeasurement.durationInMs,
|
114
|
-
};
|
115
|
-
if (result.isAborted) {
|
116
|
-
eventSource.notify({
|
117
|
-
...finishMetadata,
|
118
|
-
result: {
|
119
|
-
status: "abort",
|
120
|
-
},
|
121
|
-
});
|
122
|
-
throw new AbortError_js_1.AbortError();
|
123
|
-
}
|
124
|
-
eventSource.notify({
|
125
|
-
...finishMetadata,
|
126
|
-
result: {
|
127
|
-
status: "error",
|
128
|
-
error: result.error,
|
129
|
-
},
|
130
|
-
});
|
131
|
-
throw result.error;
|
132
|
-
}
|
133
|
-
return {
|
134
|
-
output: result.value,
|
135
|
-
metadata: startMetadata,
|
136
|
-
};
|
22
|
+
return undefined;
|
23
|
+
},
|
24
|
+
getResult: () => ({
|
25
|
+
response: lastFullDelta,
|
26
|
+
value: accumulatedText,
|
27
|
+
}),
|
28
|
+
}));
|
137
29
|
}
|
30
|
+
exports.streamText = streamText;
|