modelfusion 0.34.0 → 0.35.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 +47 -1
- package/model-function/AsyncIterableResultPromise.cjs +37 -0
- package/model-function/AsyncIterableResultPromise.d.ts +16 -0
- package/model-function/AsyncIterableResultPromise.js +33 -0
- package/model-function/{generate-text/DeltaEvent.d.ts → DeltaEvent.d.ts} +1 -1
- package/model-function/ModelCallEvent.d.ts +3 -2
- package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +1 -1
- package/model-function/generate-structure/StructureGenerationModel.d.ts +10 -1
- package/model-function/generate-structure/StructureStreamingEvent.cjs +2 -0
- package/model-function/generate-structure/StructureStreamingEvent.d.ts +7 -0
- package/model-function/generate-structure/StructureStreamingEvent.js +1 -0
- package/model-function/generate-structure/fixJson.cjs +215 -0
- package/model-function/generate-structure/fixJson.d.ts +1 -0
- package/model-function/generate-structure/fixJson.js +211 -0
- package/model-function/generate-structure/fixJson.test.cjs +130 -0
- package/model-function/generate-structure/fixJson.test.d.ts +1 -0
- package/model-function/generate-structure/fixJson.test.js +128 -0
- package/model-function/generate-structure/generateStructure.cjs +3 -1
- package/model-function/generate-structure/generateStructure.d.ts +1 -1
- package/model-function/generate-structure/generateStructure.js +3 -1
- package/model-function/generate-structure/parsePartialJson.cjs +29 -0
- package/model-function/generate-structure/parsePartialJson.d.ts +1 -0
- package/model-function/generate-structure/parsePartialJson.js +22 -0
- package/model-function/generate-structure/streamStructure.cjs +167 -0
- package/model-function/generate-structure/streamStructure.d.ts +17 -0
- package/model-function/generate-structure/streamStructure.js +160 -0
- package/model-function/generate-text/TextGenerationModel.d.ts +4 -4
- package/model-function/generate-text/streamText.cjs +47 -68
- package/model-function/generate-text/streamText.d.ts +3 -18
- package/model-function/generate-text/streamText.js +46 -66
- package/model-function/index.cjs +3 -1
- package/model-function/index.d.ts +3 -1
- package/model-function/index.js +3 -1
- package/model-provider/cohere/CohereTextGenerationModel.cjs +3 -3
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +3 -3
- package/model-provider/cohere/CohereTextGenerationModel.js +3 -3
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +0 -12
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +0 -2
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +0 -12
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +3 -3
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +3 -3
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +3 -3
- package/model-provider/openai/OpenAITextGenerationModel.cjs +3 -3
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +3 -3
- package/model-provider/openai/OpenAITextGenerationModel.js +3 -3
- package/model-provider/openai/chat/OpenAIChatModel.cjs +23 -2
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +4 -2
- package/model-provider/openai/chat/OpenAIChatModel.js +23 -2
- package/model-provider/openai/chat/OpenAIChatStreamIterable.d.ts +1 -1
- package/package.json +8 -4
- package/prompt/PromptFormatTextGenerationModel.d.ts +1 -1
- package/tool/useTool.cjs +3 -4
- package/tool/useTool.d.ts +1 -1
- package/tool/useTool.js +3 -4
- package/model-function/generate-text/extractTextDeltas.cjs +0 -23
- package/model-function/generate-text/extractTextDeltas.d.ts +0 -7
- package/model-function/generate-text/extractTextDeltas.js +0 -19
- /package/model-function/{generate-text/DeltaEvent.cjs → DeltaEvent.cjs} +0 -0
- /package/model-function/{generate-text/DeltaEvent.js → DeltaEvent.js} +0 -0
@@ -0,0 +1,160 @@
|
|
1
|
+
import deepEqual from "deep-equal";
|
2
|
+
import { nanoid as createId } from "nanoid";
|
3
|
+
import { FunctionEventSource } from "../../core/FunctionEventSource.js";
|
4
|
+
import { getGlobalFunctionLogging } from "../../core/GlobalFunctionLogging.js";
|
5
|
+
import { getGlobalFunctionObservers } from "../../core/GlobalFunctionObservers.js";
|
6
|
+
import { AbortError } from "../../core/api/AbortError.js";
|
7
|
+
import { getFunctionCallLogger } from "../../core/getFunctionCallLogger.js";
|
8
|
+
import { startDurationMeasurement } from "../../util/DurationMeasurement.js";
|
9
|
+
import { runSafe } from "../../util/runSafe.js";
|
10
|
+
import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
|
11
|
+
export function streamStructure(model, structureDefinition, prompt, options) {
|
12
|
+
return new AsyncIterableResultPromise(doStreamStructure(model, structureDefinition, prompt, options));
|
13
|
+
}
|
14
|
+
async function doStreamStructure(model, structureDefinition, prompt, options) {
|
15
|
+
if (options?.settings != null) {
|
16
|
+
model = model.withSettings(options.settings);
|
17
|
+
options = {
|
18
|
+
functionId: options.functionId,
|
19
|
+
observers: options.observers,
|
20
|
+
run: options.run,
|
21
|
+
};
|
22
|
+
}
|
23
|
+
const run = options?.run;
|
24
|
+
const settings = model.settings;
|
25
|
+
const eventSource = new FunctionEventSource({
|
26
|
+
observers: [
|
27
|
+
...getFunctionCallLogger(options?.logging ?? getGlobalFunctionLogging()),
|
28
|
+
...getGlobalFunctionObservers(),
|
29
|
+
...(settings.observers ?? []),
|
30
|
+
...(run?.functionObserver != null ? [run.functionObserver] : []),
|
31
|
+
...(options?.observers ?? []),
|
32
|
+
],
|
33
|
+
errorHandler: run?.errorHandler,
|
34
|
+
});
|
35
|
+
const durationMeasurement = startDurationMeasurement();
|
36
|
+
const startMetadata = {
|
37
|
+
functionType: "structure-streaming",
|
38
|
+
callId: `call-${createId()}`,
|
39
|
+
runId: run?.runId,
|
40
|
+
sessionId: run?.sessionId,
|
41
|
+
userId: run?.userId,
|
42
|
+
functionId: options?.functionId,
|
43
|
+
model: model.modelInformation,
|
44
|
+
settings: model.settingsForEvent,
|
45
|
+
input: prompt,
|
46
|
+
timestamp: durationMeasurement.startDate,
|
47
|
+
startTimestamp: durationMeasurement.startDate,
|
48
|
+
};
|
49
|
+
eventSource.notify({
|
50
|
+
eventType: "started",
|
51
|
+
...startMetadata,
|
52
|
+
});
|
53
|
+
const result = await runSafe(async () => {
|
54
|
+
const deltaIterable = await model.generateStructureStreamResponse(structureDefinition, prompt, {
|
55
|
+
functionId: options?.functionId,
|
56
|
+
settings,
|
57
|
+
run,
|
58
|
+
});
|
59
|
+
return (async function* () {
|
60
|
+
function reportError(error) {
|
61
|
+
const finishMetadata = {
|
62
|
+
eventType: "finished",
|
63
|
+
...startMetadata,
|
64
|
+
finishTimestamp: new Date(),
|
65
|
+
durationInMs: durationMeasurement.durationInMs,
|
66
|
+
};
|
67
|
+
eventSource.notify(error instanceof AbortError
|
68
|
+
? {
|
69
|
+
...finishMetadata,
|
70
|
+
result: {
|
71
|
+
status: "abort",
|
72
|
+
},
|
73
|
+
}
|
74
|
+
: {
|
75
|
+
...finishMetadata,
|
76
|
+
result: {
|
77
|
+
status: "error",
|
78
|
+
error,
|
79
|
+
},
|
80
|
+
});
|
81
|
+
}
|
82
|
+
let lastStructure;
|
83
|
+
let lastFullDelta;
|
84
|
+
for await (const event of deltaIterable) {
|
85
|
+
if (event?.type === "error") {
|
86
|
+
reportError(event.error);
|
87
|
+
throw event.error;
|
88
|
+
}
|
89
|
+
if (event?.type === "delta") {
|
90
|
+
const latestFullDelta = event.fullDelta;
|
91
|
+
const latestStructure = model.extractPartialStructure(latestFullDelta);
|
92
|
+
// only send a new part into the stream when the partial structure has changed:
|
93
|
+
if (!deepEqual(lastStructure, latestStructure, {
|
94
|
+
strict: true,
|
95
|
+
})) {
|
96
|
+
lastFullDelta = latestFullDelta;
|
97
|
+
lastStructure = latestStructure;
|
98
|
+
yield {
|
99
|
+
isComplete: false,
|
100
|
+
value: lastStructure,
|
101
|
+
};
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
// process the final result (full type validation):
|
106
|
+
const parseResult = structureDefinition.schema.validate(lastStructure);
|
107
|
+
if (!parseResult.success) {
|
108
|
+
reportError(parseResult.error);
|
109
|
+
throw parseResult.error;
|
110
|
+
}
|
111
|
+
yield {
|
112
|
+
isComplete: true,
|
113
|
+
value: parseResult.value,
|
114
|
+
};
|
115
|
+
const finishMetadata = {
|
116
|
+
eventType: "finished",
|
117
|
+
...startMetadata,
|
118
|
+
finishTimestamp: new Date(),
|
119
|
+
durationInMs: durationMeasurement.durationInMs,
|
120
|
+
};
|
121
|
+
eventSource.notify({
|
122
|
+
...finishMetadata,
|
123
|
+
result: {
|
124
|
+
status: "success",
|
125
|
+
response: lastFullDelta,
|
126
|
+
output: lastStructure,
|
127
|
+
},
|
128
|
+
});
|
129
|
+
})();
|
130
|
+
});
|
131
|
+
if (!result.ok) {
|
132
|
+
const finishMetadata = {
|
133
|
+
eventType: "finished",
|
134
|
+
...startMetadata,
|
135
|
+
finishTimestamp: new Date(),
|
136
|
+
durationInMs: durationMeasurement.durationInMs,
|
137
|
+
};
|
138
|
+
if (result.isAborted) {
|
139
|
+
eventSource.notify({
|
140
|
+
...finishMetadata,
|
141
|
+
result: {
|
142
|
+
status: "abort",
|
143
|
+
},
|
144
|
+
});
|
145
|
+
throw new AbortError();
|
146
|
+
}
|
147
|
+
eventSource.notify({
|
148
|
+
...finishMetadata,
|
149
|
+
result: {
|
150
|
+
status: "error",
|
151
|
+
error: result.error,
|
152
|
+
},
|
153
|
+
});
|
154
|
+
throw result.error;
|
155
|
+
}
|
156
|
+
return {
|
157
|
+
output: result.output,
|
158
|
+
metadata: startMetadata,
|
159
|
+
};
|
160
|
+
}
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import { PromptFormat } from "../../prompt/PromptFormat.js";
|
2
2
|
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
3
|
-
import {
|
3
|
+
import { DeltaEvent } from "../DeltaEvent.js";
|
4
4
|
import { Model, ModelSettings } from "../Model.js";
|
5
|
+
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
5
6
|
import { BasicTokenizer, FullTokenizer } from "../tokenize-text/Tokenizer.js";
|
6
|
-
import { DeltaEvent } from "./DeltaEvent.js";
|
7
7
|
export interface TextGenerationModelSettings extends ModelSettings {
|
8
8
|
/**
|
9
9
|
* Maximum number of tokens to generate.
|
@@ -33,11 +33,11 @@ export interface TextGenerationModel<PROMPT, RESPONSE, FULL_DELTA, SETTINGS exte
|
|
33
33
|
/**
|
34
34
|
* Optional. Implement for streaming support.
|
35
35
|
*/
|
36
|
-
readonly generateDeltaStreamResponse
|
36
|
+
readonly generateDeltaStreamResponse?: (prompt: PROMPT, options: ModelFunctionOptions<SETTINGS>) => PromiseLike<AsyncIterable<DeltaEvent<FULL_DELTA>>>;
|
37
37
|
/**
|
38
38
|
* Optional. Implement for streaming support.
|
39
39
|
*/
|
40
|
-
readonly extractTextDelta
|
40
|
+
readonly extractTextDelta?: (fullDelta: FULL_DELTA) => string | undefined;
|
41
41
|
extractUsage?(response: RESPONSE): {
|
42
42
|
promptTokens: number;
|
43
43
|
completionTokens: number;
|
@@ -1,51 +1,17 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.streamText =
|
3
|
+
exports.streamText = void 0;
|
4
4
|
const nanoid_1 = require("nanoid");
|
5
5
|
const FunctionEventSource_js_1 = require("../../core/FunctionEventSource.cjs");
|
6
6
|
const GlobalFunctionLogging_js_1 = require("../../core/GlobalFunctionLogging.cjs");
|
7
7
|
const GlobalFunctionObservers_js_1 = require("../../core/GlobalFunctionObservers.cjs");
|
8
|
+
const AbortError_js_1 = require("../../core/api/AbortError.cjs");
|
8
9
|
const getFunctionCallLogger_js_1 = require("../../core/getFunctionCallLogger.cjs");
|
9
10
|
const DurationMeasurement_js_1 = require("../../util/DurationMeasurement.cjs");
|
10
|
-
const AbortError_js_1 = require("../../core/api/AbortError.cjs");
|
11
11
|
const runSafe_js_1 = require("../../util/runSafe.cjs");
|
12
|
-
const
|
13
|
-
class StreamTextPromise extends Promise {
|
14
|
-
constructor(fullPromise) {
|
15
|
-
super((resolve) => {
|
16
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
17
|
-
resolve(null); // we override the resolve function
|
18
|
-
});
|
19
|
-
Object.defineProperty(this, "fullPromise", {
|
20
|
-
enumerable: true,
|
21
|
-
configurable: true,
|
22
|
-
writable: true,
|
23
|
-
value: fullPromise
|
24
|
-
});
|
25
|
-
Object.defineProperty(this, "outputPromise", {
|
26
|
-
enumerable: true,
|
27
|
-
configurable: true,
|
28
|
-
writable: true,
|
29
|
-
value: void 0
|
30
|
-
});
|
31
|
-
this.outputPromise = fullPromise.then((result) => result.output);
|
32
|
-
}
|
33
|
-
asFullResponse() {
|
34
|
-
return this.fullPromise;
|
35
|
-
}
|
36
|
-
then(onfulfilled, onrejected) {
|
37
|
-
return this.outputPromise.then(onfulfilled, onrejected);
|
38
|
-
}
|
39
|
-
catch(onrejected) {
|
40
|
-
return this.outputPromise.catch(onrejected);
|
41
|
-
}
|
42
|
-
finally(onfinally) {
|
43
|
-
return this.outputPromise.finally(onfinally);
|
44
|
-
}
|
45
|
-
}
|
46
|
-
exports.StreamTextPromise = StreamTextPromise;
|
12
|
+
const AsyncIterableResultPromise_js_1 = require("../AsyncIterableResultPromise.cjs");
|
47
13
|
function streamText(model, prompt, options) {
|
48
|
-
return new
|
14
|
+
return new AsyncIterableResultPromise_js_1.AsyncIterableResultPromise(doStreamText(model, prompt, options));
|
49
15
|
}
|
50
16
|
exports.streamText = streamText;
|
51
17
|
async function doStreamText(model, prompt, options) {
|
@@ -87,14 +53,49 @@ async function doStreamText(model, prompt, options) {
|
|
87
53
|
eventType: "started",
|
88
54
|
...startMetadata,
|
89
55
|
});
|
90
|
-
const result = await (0, runSafe_js_1.runSafe)(async () =>
|
91
|
-
deltaIterable
|
56
|
+
const result = await (0, runSafe_js_1.runSafe)(async () => {
|
57
|
+
const deltaIterable = await model.generateDeltaStreamResponse(prompt, {
|
92
58
|
functionId: options?.functionId,
|
93
59
|
settings,
|
94
60
|
run,
|
95
|
-
})
|
96
|
-
|
97
|
-
|
61
|
+
});
|
62
|
+
return (async function* () {
|
63
|
+
let accumulatedText = "";
|
64
|
+
let lastFullDelta;
|
65
|
+
for await (const event of deltaIterable) {
|
66
|
+
if (event?.type === "error") {
|
67
|
+
const error = event.error;
|
68
|
+
const finishMetadata = {
|
69
|
+
eventType: "finished",
|
70
|
+
...startMetadata,
|
71
|
+
finishTimestamp: new Date(),
|
72
|
+
durationInMs: durationMeasurement.durationInMs,
|
73
|
+
};
|
74
|
+
eventSource.notify(error instanceof AbortError_js_1.AbortError
|
75
|
+
? {
|
76
|
+
...finishMetadata,
|
77
|
+
result: {
|
78
|
+
status: "abort",
|
79
|
+
},
|
80
|
+
}
|
81
|
+
: {
|
82
|
+
...finishMetadata,
|
83
|
+
result: {
|
84
|
+
status: "error",
|
85
|
+
error,
|
86
|
+
},
|
87
|
+
});
|
88
|
+
throw error;
|
89
|
+
}
|
90
|
+
if (event?.type === "delta") {
|
91
|
+
lastFullDelta = event.fullDelta;
|
92
|
+
const delta = model.extractTextDelta(lastFullDelta);
|
93
|
+
if (delta != null && delta.length > 0) {
|
94
|
+
accumulatedText += delta;
|
95
|
+
yield delta;
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}
|
98
99
|
const finishMetadata = {
|
99
100
|
eventType: "finished",
|
100
101
|
...startMetadata,
|
@@ -106,33 +107,11 @@ async function doStreamText(model, prompt, options) {
|
|
106
107
|
result: {
|
107
108
|
status: "success",
|
108
109
|
response: lastFullDelta,
|
109
|
-
output:
|
110
|
+
output: accumulatedText,
|
110
111
|
},
|
111
112
|
});
|
112
|
-
}
|
113
|
-
|
114
|
-
const finishMetadata = {
|
115
|
-
eventType: "finished",
|
116
|
-
...startMetadata,
|
117
|
-
finishTimestamp: new Date(),
|
118
|
-
durationInMs: durationMeasurement.durationInMs,
|
119
|
-
};
|
120
|
-
eventSource.notify(error instanceof AbortError_js_1.AbortError
|
121
|
-
? {
|
122
|
-
...finishMetadata,
|
123
|
-
result: {
|
124
|
-
status: "abort",
|
125
|
-
},
|
126
|
-
}
|
127
|
-
: {
|
128
|
-
...finishMetadata,
|
129
|
-
result: {
|
130
|
-
status: "error",
|
131
|
-
error,
|
132
|
-
},
|
133
|
-
});
|
134
|
-
},
|
135
|
-
}));
|
113
|
+
})();
|
114
|
+
});
|
136
115
|
if (!result.ok) {
|
137
116
|
const finishMetadata = {
|
138
117
|
eventType: "finished",
|
@@ -1,23 +1,8 @@
|
|
1
|
+
import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
|
2
|
+
import { DeltaEvent } from "../DeltaEvent.js";
|
1
3
|
import { ModelFunctionOptions } from "../ModelFunctionOptions.js";
|
2
|
-
import { ModelCallMetadata } from "../executeCall.js";
|
3
|
-
import { DeltaEvent } from "./DeltaEvent.js";
|
4
4
|
import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
|
5
|
-
export declare class StreamTextPromise extends Promise<AsyncIterable<string>> {
|
6
|
-
private fullPromise;
|
7
|
-
private outputPromise;
|
8
|
-
constructor(fullPromise: Promise<{
|
9
|
-
output: AsyncIterable<string>;
|
10
|
-
metadata: Omit<ModelCallMetadata, "durationInMs" | "finishTimestamp">;
|
11
|
-
}>);
|
12
|
-
asFullResponse(): Promise<{
|
13
|
-
output: AsyncIterable<string>;
|
14
|
-
metadata: Omit<ModelCallMetadata, "durationInMs" | "finishTimestamp">;
|
15
|
-
}>;
|
16
|
-
then<TResult1 = AsyncIterable<string>, TResult2 = never>(onfulfilled?: ((value: AsyncIterable<string>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
|
17
|
-
catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | undefined | null): Promise<AsyncIterable<string> | TResult>;
|
18
|
-
finally(onfinally?: (() => void) | undefined | null): Promise<AsyncIterable<string>>;
|
19
|
-
}
|
20
5
|
export declare function streamText<PROMPT, FULL_DELTA, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS> & {
|
21
6
|
generateDeltaStreamResponse: (prompt: PROMPT, options: ModelFunctionOptions<SETTINGS>) => PromiseLike<AsyncIterable<DeltaEvent<FULL_DELTA>>>;
|
22
7
|
extractTextDelta: (fullDelta: FULL_DELTA) => string | undefined;
|
23
|
-
}, prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>):
|
8
|
+
}, prompt: PROMPT, options?: ModelFunctionOptions<SETTINGS>): AsyncIterableResultPromise<string>;
|
@@ -2,46 +2,13 @@ import { nanoid as createId } from "nanoid";
|
|
2
2
|
import { FunctionEventSource } from "../../core/FunctionEventSource.js";
|
3
3
|
import { getGlobalFunctionLogging } from "../../core/GlobalFunctionLogging.js";
|
4
4
|
import { getGlobalFunctionObservers } from "../../core/GlobalFunctionObservers.js";
|
5
|
+
import { AbortError } from "../../core/api/AbortError.js";
|
5
6
|
import { getFunctionCallLogger } from "../../core/getFunctionCallLogger.js";
|
6
7
|
import { startDurationMeasurement } from "../../util/DurationMeasurement.js";
|
7
|
-
import { AbortError } from "../../core/api/AbortError.js";
|
8
8
|
import { runSafe } from "../../util/runSafe.js";
|
9
|
-
import {
|
10
|
-
export class StreamTextPromise extends Promise {
|
11
|
-
constructor(fullPromise) {
|
12
|
-
super((resolve) => {
|
13
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
14
|
-
resolve(null); // we override the resolve function
|
15
|
-
});
|
16
|
-
Object.defineProperty(this, "fullPromise", {
|
17
|
-
enumerable: true,
|
18
|
-
configurable: true,
|
19
|
-
writable: true,
|
20
|
-
value: fullPromise
|
21
|
-
});
|
22
|
-
Object.defineProperty(this, "outputPromise", {
|
23
|
-
enumerable: true,
|
24
|
-
configurable: true,
|
25
|
-
writable: true,
|
26
|
-
value: void 0
|
27
|
-
});
|
28
|
-
this.outputPromise = fullPromise.then((result) => result.output);
|
29
|
-
}
|
30
|
-
asFullResponse() {
|
31
|
-
return this.fullPromise;
|
32
|
-
}
|
33
|
-
then(onfulfilled, onrejected) {
|
34
|
-
return this.outputPromise.then(onfulfilled, onrejected);
|
35
|
-
}
|
36
|
-
catch(onrejected) {
|
37
|
-
return this.outputPromise.catch(onrejected);
|
38
|
-
}
|
39
|
-
finally(onfinally) {
|
40
|
-
return this.outputPromise.finally(onfinally);
|
41
|
-
}
|
42
|
-
}
|
9
|
+
import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
|
43
10
|
export function streamText(model, prompt, options) {
|
44
|
-
return new
|
11
|
+
return new AsyncIterableResultPromise(doStreamText(model, prompt, options));
|
45
12
|
}
|
46
13
|
async function doStreamText(model, prompt, options) {
|
47
14
|
if (options?.settings != null) {
|
@@ -82,14 +49,49 @@ async function doStreamText(model, prompt, options) {
|
|
82
49
|
eventType: "started",
|
83
50
|
...startMetadata,
|
84
51
|
});
|
85
|
-
const result = await runSafe(async () =>
|
86
|
-
deltaIterable
|
52
|
+
const result = await runSafe(async () => {
|
53
|
+
const deltaIterable = await model.generateDeltaStreamResponse(prompt, {
|
87
54
|
functionId: options?.functionId,
|
88
55
|
settings,
|
89
56
|
run,
|
90
|
-
})
|
91
|
-
|
92
|
-
|
57
|
+
});
|
58
|
+
return (async function* () {
|
59
|
+
let accumulatedText = "";
|
60
|
+
let lastFullDelta;
|
61
|
+
for await (const event of deltaIterable) {
|
62
|
+
if (event?.type === "error") {
|
63
|
+
const error = event.error;
|
64
|
+
const finishMetadata = {
|
65
|
+
eventType: "finished",
|
66
|
+
...startMetadata,
|
67
|
+
finishTimestamp: new Date(),
|
68
|
+
durationInMs: durationMeasurement.durationInMs,
|
69
|
+
};
|
70
|
+
eventSource.notify(error instanceof AbortError
|
71
|
+
? {
|
72
|
+
...finishMetadata,
|
73
|
+
result: {
|
74
|
+
status: "abort",
|
75
|
+
},
|
76
|
+
}
|
77
|
+
: {
|
78
|
+
...finishMetadata,
|
79
|
+
result: {
|
80
|
+
status: "error",
|
81
|
+
error,
|
82
|
+
},
|
83
|
+
});
|
84
|
+
throw error;
|
85
|
+
}
|
86
|
+
if (event?.type === "delta") {
|
87
|
+
lastFullDelta = event.fullDelta;
|
88
|
+
const delta = model.extractTextDelta(lastFullDelta);
|
89
|
+
if (delta != null && delta.length > 0) {
|
90
|
+
accumulatedText += delta;
|
91
|
+
yield delta;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
}
|
93
95
|
const finishMetadata = {
|
94
96
|
eventType: "finished",
|
95
97
|
...startMetadata,
|
@@ -101,33 +103,11 @@ async function doStreamText(model, prompt, options) {
|
|
101
103
|
result: {
|
102
104
|
status: "success",
|
103
105
|
response: lastFullDelta,
|
104
|
-
output:
|
106
|
+
output: accumulatedText,
|
105
107
|
},
|
106
108
|
});
|
107
|
-
}
|
108
|
-
|
109
|
-
const finishMetadata = {
|
110
|
-
eventType: "finished",
|
111
|
-
...startMetadata,
|
112
|
-
finishTimestamp: new Date(),
|
113
|
-
durationInMs: durationMeasurement.durationInMs,
|
114
|
-
};
|
115
|
-
eventSource.notify(error instanceof AbortError
|
116
|
-
? {
|
117
|
-
...finishMetadata,
|
118
|
-
result: {
|
119
|
-
status: "abort",
|
120
|
-
},
|
121
|
-
}
|
122
|
-
: {
|
123
|
-
...finishMetadata,
|
124
|
-
result: {
|
125
|
-
status: "error",
|
126
|
-
error,
|
127
|
-
},
|
128
|
-
});
|
129
|
-
},
|
130
|
-
}));
|
109
|
+
})();
|
110
|
+
});
|
131
111
|
if (!result.ok) {
|
132
112
|
const finishMetadata = {
|
133
113
|
eventType: "finished",
|
package/model-function/index.cjs
CHANGED
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./DeltaEvent.cjs"), exports);
|
17
18
|
__exportStar(require("./Model.cjs"), exports);
|
18
19
|
__exportStar(require("./ModelCallEvent.cjs"), exports);
|
19
20
|
__exportStar(require("./ModelFunctionOptions.cjs"), exports);
|
@@ -33,10 +34,11 @@ __exportStar(require("./generate-structure/StructureFromTextGenerationModel.cjs"
|
|
33
34
|
__exportStar(require("./generate-structure/StructureGenerationEvent.cjs"), exports);
|
34
35
|
__exportStar(require("./generate-structure/StructureGenerationModel.cjs"), exports);
|
35
36
|
__exportStar(require("./generate-structure/StructureOrTextGenerationModel.cjs"), exports);
|
37
|
+
__exportStar(require("./generate-structure/StructureStreamingEvent.cjs"), exports);
|
36
38
|
__exportStar(require("./generate-structure/StructureValidationError.cjs"), exports);
|
37
39
|
__exportStar(require("./generate-structure/generateStructure.cjs"), exports);
|
38
40
|
__exportStar(require("./generate-structure/generateStructureOrText.cjs"), exports);
|
39
|
-
__exportStar(require("./generate-
|
41
|
+
__exportStar(require("./generate-structure/streamStructure.cjs"), exports);
|
40
42
|
__exportStar(require("./generate-text/TextGenerationEvent.cjs"), exports);
|
41
43
|
__exportStar(require("./generate-text/TextGenerationModel.cjs"), exports);
|
42
44
|
__exportStar(require("./generate-text/TextStreamingEvent.cjs"), exports);
|
@@ -1,3 +1,4 @@
|
|
1
|
+
export * from "./DeltaEvent.js";
|
1
2
|
export * from "./Model.js";
|
2
3
|
export * from "./ModelCallEvent.js";
|
3
4
|
export * from "./ModelFunctionOptions.js";
|
@@ -17,10 +18,11 @@ export * from "./generate-structure/StructureFromTextGenerationModel.js";
|
|
17
18
|
export * from "./generate-structure/StructureGenerationEvent.js";
|
18
19
|
export * from "./generate-structure/StructureGenerationModel.js";
|
19
20
|
export * from "./generate-structure/StructureOrTextGenerationModel.js";
|
21
|
+
export * from "./generate-structure/StructureStreamingEvent.js";
|
20
22
|
export * from "./generate-structure/StructureValidationError.js";
|
21
23
|
export * from "./generate-structure/generateStructure.js";
|
22
24
|
export * from "./generate-structure/generateStructureOrText.js";
|
23
|
-
export * from "./generate-
|
25
|
+
export * from "./generate-structure/streamStructure.js";
|
24
26
|
export * from "./generate-text/TextGenerationEvent.js";
|
25
27
|
export * from "./generate-text/TextGenerationModel.js";
|
26
28
|
export * from "./generate-text/TextStreamingEvent.js";
|
package/model-function/index.js
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
export * from "./DeltaEvent.js";
|
1
2
|
export * from "./Model.js";
|
2
3
|
export * from "./ModelCallEvent.js";
|
3
4
|
export * from "./ModelFunctionOptions.js";
|
@@ -17,10 +18,11 @@ export * from "./generate-structure/StructureFromTextGenerationModel.js";
|
|
17
18
|
export * from "./generate-structure/StructureGenerationEvent.js";
|
18
19
|
export * from "./generate-structure/StructureGenerationModel.js";
|
19
20
|
export * from "./generate-structure/StructureOrTextGenerationModel.js";
|
21
|
+
export * from "./generate-structure/StructureStreamingEvent.js";
|
20
22
|
export * from "./generate-structure/StructureValidationError.js";
|
21
23
|
export * from "./generate-structure/generateStructure.js";
|
22
24
|
export * from "./generate-structure/generateStructureOrText.js";
|
23
|
-
export * from "./generate-
|
25
|
+
export * from "./generate-structure/streamStructure.js";
|
24
26
|
export * from "./generate-text/TextGenerationEvent.js";
|
25
27
|
export * from "./generate-text/TextGenerationModel.js";
|
26
28
|
export * from "./generate-text/TextStreamingEvent.js";
|
@@ -6,12 +6,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.CohereTextGenerationResponseFormat = exports.CohereTextGenerationModel = exports.COHERE_TEXT_GENERATION_MODELS = void 0;
|
7
7
|
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
8
|
const zod_1 = require("zod");
|
9
|
-
const
|
9
|
+
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
10
|
+
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
10
11
|
const AsyncQueue_js_1 = require("../../event-source/AsyncQueue.cjs");
|
12
|
+
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
11
13
|
const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
|
12
14
|
const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
|
13
|
-
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
14
|
-
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
15
15
|
const CohereApiConfiguration_js_1 = require("./CohereApiConfiguration.cjs");
|
16
16
|
const CohereError_js_1 = require("./CohereError.cjs");
|
17
17
|
const CohereTokenizer_js_1 = require("./CohereTokenizer.cjs");
|
@@ -1,12 +1,12 @@
|
|
1
1
|
import { z } from "zod";
|
2
|
-
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
3
2
|
import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
|
3
|
+
import { ResponseHandler } from "../../core/api/postToApi.js";
|
4
|
+
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
5
|
+
import { DeltaEvent } from "../../model-function/DeltaEvent.js";
|
4
6
|
import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
|
5
|
-
import { DeltaEvent } from "../../model-function/generate-text/DeltaEvent.js";
|
6
7
|
import { TextGenerationModel, TextGenerationModelSettings } from "../../model-function/generate-text/TextGenerationModel.js";
|
7
8
|
import { PromptFormat } from "../../prompt/PromptFormat.js";
|
8
9
|
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
9
|
-
import { ResponseHandler } from "../../core/api/postToApi.js";
|
10
10
|
import { CohereTokenizer } from "./CohereTokenizer.js";
|
11
11
|
export declare const COHERE_TEXT_GENERATION_MODELS: {
|
12
12
|
command: {
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import SecureJSON from "secure-json-parse";
|
2
2
|
import { z } from "zod";
|
3
|
-
import {
|
3
|
+
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
4
|
+
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
5
|
import { AsyncQueue } from "../../event-source/AsyncQueue.js";
|
6
|
+
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
5
7
|
import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
|
6
8
|
import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
|
7
|
-
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
8
|
-
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
9
9
|
import { CohereApiConfiguration } from "./CohereApiConfiguration.js";
|
10
10
|
import { failedCohereCallResponseHandler } from "./CohereError.js";
|
11
11
|
import { CohereTokenizer } from "./CohereTokenizer.js";
|
@@ -56,18 +56,6 @@ class HuggingFaceTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
56
56
|
writable: true,
|
57
57
|
value: undefined
|
58
58
|
});
|
59
|
-
Object.defineProperty(this, "generateDeltaStreamResponse", {
|
60
|
-
enumerable: true,
|
61
|
-
configurable: true,
|
62
|
-
writable: true,
|
63
|
-
value: undefined
|
64
|
-
});
|
65
|
-
Object.defineProperty(this, "extractTextDelta", {
|
66
|
-
enumerable: true,
|
67
|
-
configurable: true,
|
68
|
-
writable: true,
|
69
|
-
value: undefined
|
70
|
-
});
|
71
59
|
}
|
72
60
|
get modelName() {
|
73
61
|
return this.settings.model;
|
@@ -51,8 +51,6 @@ export declare class HuggingFaceTextGenerationModel extends AbstractModel<Huggin
|
|
51
51
|
generated_text: string;
|
52
52
|
}[]>;
|
53
53
|
extractText(response: HuggingFaceTextGenerationResponse): string;
|
54
|
-
generateDeltaStreamResponse: undefined;
|
55
|
-
extractTextDelta: undefined;
|
56
54
|
withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, string>): PromptFormatTextGenerationModel<INPUT_PROMPT, string, HuggingFaceTextGenerationResponse, undefined, HuggingFaceTextGenerationModelSettings, this>;
|
57
55
|
withSettings(additionalSettings: Partial<HuggingFaceTextGenerationModelSettings>): this;
|
58
56
|
}
|
@@ -50,18 +50,6 @@ export class HuggingFaceTextGenerationModel extends AbstractModel {
|
|
50
50
|
writable: true,
|
51
51
|
value: undefined
|
52
52
|
});
|
53
|
-
Object.defineProperty(this, "generateDeltaStreamResponse", {
|
54
|
-
enumerable: true,
|
55
|
-
configurable: true,
|
56
|
-
writable: true,
|
57
|
-
value: undefined
|
58
|
-
});
|
59
|
-
Object.defineProperty(this, "extractTextDelta", {
|
60
|
-
enumerable: true,
|
61
|
-
configurable: true,
|
62
|
-
writable: true,
|
63
|
-
value: undefined
|
64
|
-
});
|
65
53
|
}
|
66
54
|
get modelName() {
|
67
55
|
return this.settings.model;
|