modelfusion 0.89.1 → 0.91.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 -3
- package/core/api/ApiCallError.cjs +9 -1
- package/core/api/ApiCallError.d.ts +4 -1
- package/core/api/ApiCallError.js +9 -1
- package/core/api/RetryError.cjs +12 -2
- package/core/api/RetryError.d.ts +3 -1
- package/core/api/RetryError.js +12 -2
- package/core/api/postToApi.cjs +8 -1
- package/core/api/postToApi.d.ts +1 -0
- package/core/api/postToApi.js +8 -1
- package/core/schema/JSONParseError.cjs +2 -7
- package/core/schema/JSONParseError.d.ts +1 -2
- package/core/schema/JSONParseError.js +2 -7
- package/model-provider/index.cjs +1 -0
- package/model-provider/index.d.ts +1 -0
- package/model-provider/index.js +1 -0
- package/model-provider/openai/OpenAITranscriptionModel.cjs +4 -1
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +15 -0
- package/model-provider/openai/OpenAITranscriptionModel.js +4 -1
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +1 -1
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +1 -1
- package/model-provider/whispercpp/WhisperCppApiConfiguration.cjs +15 -0
- package/model-provider/whispercpp/WhisperCppApiConfiguration.d.ts +11 -0
- package/model-provider/whispercpp/WhisperCppApiConfiguration.js +11 -0
- package/model-provider/whispercpp/WhisperCppFacade.cjs +13 -0
- package/model-provider/whispercpp/WhisperCppFacade.d.ts +4 -0
- package/model-provider/whispercpp/WhisperCppFacade.js +8 -0
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.cjs +117 -0
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.d.ts +29 -0
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.js +113 -0
- package/model-provider/whispercpp/index.cjs +32 -0
- package/model-provider/whispercpp/index.d.ts +3 -0
- package/model-provider/whispercpp/index.js +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
@@ -23,11 +23,11 @@
|
|
23
23
|
- **Resilience and Robustness**: ModelFusion ensures seamless operation through automatic retries, throttling, and error handling mechanisms.
|
24
24
|
- **Server**: ModelFusion provides a Fastify plugin that exposes a ModelFusion flow as a REST endpoint that uses server-sent events.
|
25
25
|
|
26
|
-
## Quick Install
|
27
|
-
|
28
26
|
> [!NOTE]
|
29
27
|
> ModelFusion is in its initial development phase. The main API is now mostly stable, but until version 1.0 there may be breaking changes. Feedback and suggestions are welcome.
|
30
28
|
|
29
|
+
## Quick Install
|
30
|
+
|
31
31
|
```sh
|
32
32
|
npm install modelfusion
|
33
33
|
```
|
@@ -40,6 +40,9 @@ Or use a template:
|
|
40
40
|
|
41
41
|
## Usage Examples
|
42
42
|
|
43
|
+
> 💡
|
44
|
+
> The basic examples are a great way to get started and to explore in parallel with the [documentation](https://modelfusion.dev/). You can find them in the [examples/basic](https://github.com/lgrammel/modelfusion/tree/main/examples/basic) folder.
|
45
|
+
|
43
46
|
You can provide API keys for the different [integrations](https://modelfusion.dev/integration/model-provider/) using environment variables (e.g., `OPENAI_API_KEY`) or pass them into the model constructors as options.
|
44
47
|
|
45
48
|
### [Generate Text](https://modelfusion.dev/guide/function/generate-text)
|
@@ -182,7 +185,7 @@ const transcription = await generateTranscription(
|
|
182
185
|
);
|
183
186
|
```
|
184
187
|
|
185
|
-
Providers: [OpenAI (Whisper)](https://modelfusion.dev/integration/model-provider/openai)
|
188
|
+
Providers: [OpenAI (Whisper)](https://modelfusion.dev/integration/model-provider/openai), [Whisper.cpp](https://modelfusion.dev/integration/model-provider/whispercpp)
|
186
189
|
|
187
190
|
### [Generate Structure](https://modelfusion.dev/guide/function/generate-structure#generatestructure)
|
188
191
|
|
@@ -2,7 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.ApiCallError = void 0;
|
4
4
|
class ApiCallError extends Error {
|
5
|
-
constructor({ message, url, requestBodyValues, statusCode, cause, isRetryable = statusCode != null &&
|
5
|
+
constructor({ message, url, requestBodyValues, statusCode, responseBody, cause, isRetryable = statusCode != null &&
|
6
6
|
(statusCode === 429 || statusCode >= 500), }) {
|
7
7
|
super(message);
|
8
8
|
Object.defineProperty(this, "url", {
|
@@ -23,6 +23,12 @@ class ApiCallError extends Error {
|
|
23
23
|
writable: true,
|
24
24
|
value: void 0
|
25
25
|
});
|
26
|
+
Object.defineProperty(this, "responseBody", {
|
27
|
+
enumerable: true,
|
28
|
+
configurable: true,
|
29
|
+
writable: true,
|
30
|
+
value: void 0
|
31
|
+
});
|
26
32
|
Object.defineProperty(this, "cause", {
|
27
33
|
enumerable: true,
|
28
34
|
configurable: true,
|
@@ -39,6 +45,7 @@ class ApiCallError extends Error {
|
|
39
45
|
this.url = url;
|
40
46
|
this.requestBodyValues = requestBodyValues;
|
41
47
|
this.statusCode = statusCode;
|
48
|
+
this.responseBody = responseBody;
|
42
49
|
this.cause = cause;
|
43
50
|
this.isRetryable = isRetryable;
|
44
51
|
}
|
@@ -49,6 +56,7 @@ class ApiCallError extends Error {
|
|
49
56
|
url: this.url,
|
50
57
|
requestBodyValues: this.requestBodyValues,
|
51
58
|
statusCode: this.statusCode,
|
59
|
+
responseBody: this.responseBody,
|
52
60
|
cause: this.cause,
|
53
61
|
isRetryable: this.isRetryable,
|
54
62
|
};
|
@@ -2,13 +2,15 @@ export declare class ApiCallError extends Error {
|
|
2
2
|
readonly url: string;
|
3
3
|
readonly requestBodyValues: unknown;
|
4
4
|
readonly statusCode?: number;
|
5
|
+
readonly responseBody?: string;
|
5
6
|
readonly cause?: unknown;
|
6
7
|
readonly isRetryable: boolean;
|
7
|
-
constructor({ message, url, requestBodyValues, statusCode, cause, isRetryable, }: {
|
8
|
+
constructor({ message, url, requestBodyValues, statusCode, responseBody, cause, isRetryable, }: {
|
8
9
|
message: string;
|
9
10
|
url: string;
|
10
11
|
requestBodyValues: unknown;
|
11
12
|
statusCode?: number;
|
13
|
+
responseBody?: string;
|
12
14
|
cause?: unknown;
|
13
15
|
isRetryable?: boolean;
|
14
16
|
});
|
@@ -18,6 +20,7 @@ export declare class ApiCallError extends Error {
|
|
18
20
|
url: string;
|
19
21
|
requestBodyValues: unknown;
|
20
22
|
statusCode: number | undefined;
|
23
|
+
responseBody: string | undefined;
|
21
24
|
cause: unknown;
|
22
25
|
isRetryable: boolean;
|
23
26
|
};
|
package/core/api/ApiCallError.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
export class ApiCallError extends Error {
|
2
|
-
constructor({ message, url, requestBodyValues, statusCode, cause, isRetryable = statusCode != null &&
|
2
|
+
constructor({ message, url, requestBodyValues, statusCode, responseBody, cause, isRetryable = statusCode != null &&
|
3
3
|
(statusCode === 429 || statusCode >= 500), }) {
|
4
4
|
super(message);
|
5
5
|
Object.defineProperty(this, "url", {
|
@@ -20,6 +20,12 @@ export class ApiCallError extends Error {
|
|
20
20
|
writable: true,
|
21
21
|
value: void 0
|
22
22
|
});
|
23
|
+
Object.defineProperty(this, "responseBody", {
|
24
|
+
enumerable: true,
|
25
|
+
configurable: true,
|
26
|
+
writable: true,
|
27
|
+
value: void 0
|
28
|
+
});
|
23
29
|
Object.defineProperty(this, "cause", {
|
24
30
|
enumerable: true,
|
25
31
|
configurable: true,
|
@@ -36,6 +42,7 @@ export class ApiCallError extends Error {
|
|
36
42
|
this.url = url;
|
37
43
|
this.requestBodyValues = requestBodyValues;
|
38
44
|
this.statusCode = statusCode;
|
45
|
+
this.responseBody = responseBody;
|
39
46
|
this.cause = cause;
|
40
47
|
this.isRetryable = isRetryable;
|
41
48
|
}
|
@@ -46,6 +53,7 @@ export class ApiCallError extends Error {
|
|
46
53
|
url: this.url,
|
47
54
|
requestBodyValues: this.requestBodyValues,
|
48
55
|
statusCode: this.statusCode,
|
56
|
+
responseBody: this.responseBody,
|
49
57
|
cause: this.cause,
|
50
58
|
isRetryable: this.isRetryable,
|
51
59
|
};
|
package/core/api/RetryError.cjs
CHANGED
@@ -4,13 +4,20 @@ exports.RetryError = void 0;
|
|
4
4
|
class RetryError extends Error {
|
5
5
|
constructor({ message, reason, errors, }) {
|
6
6
|
super(message);
|
7
|
-
|
7
|
+
// note: property order determines debugging output
|
8
|
+
Object.defineProperty(this, "reason", {
|
8
9
|
enumerable: true,
|
9
10
|
configurable: true,
|
10
11
|
writable: true,
|
11
12
|
value: void 0
|
12
13
|
});
|
13
|
-
Object.defineProperty(this, "
|
14
|
+
Object.defineProperty(this, "lastError", {
|
15
|
+
enumerable: true,
|
16
|
+
configurable: true,
|
17
|
+
writable: true,
|
18
|
+
value: void 0
|
19
|
+
});
|
20
|
+
Object.defineProperty(this, "errors", {
|
14
21
|
enumerable: true,
|
15
22
|
configurable: true,
|
16
23
|
writable: true,
|
@@ -19,12 +26,15 @@ class RetryError extends Error {
|
|
19
26
|
this.name = "RetryError";
|
20
27
|
this.reason = reason;
|
21
28
|
this.errors = errors;
|
29
|
+
// separate our last error to make debugging via log easier:
|
30
|
+
this.lastError = errors[errors.length - 1];
|
22
31
|
}
|
23
32
|
toJSON() {
|
24
33
|
return {
|
25
34
|
name: this.name,
|
26
35
|
message: this.message,
|
27
36
|
reason: this.reason,
|
37
|
+
lastError: this.lastError,
|
28
38
|
errors: this.errors,
|
29
39
|
};
|
30
40
|
}
|
package/core/api/RetryError.d.ts
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
export type RetryErrorReason = "maxTriesExceeded" | "errorNotRetryable" | "abort";
|
2
2
|
export declare class RetryError extends Error {
|
3
|
-
readonly errors: Array<unknown>;
|
4
3
|
readonly reason: RetryErrorReason;
|
4
|
+
readonly lastError: unknown;
|
5
|
+
readonly errors: Array<unknown>;
|
5
6
|
constructor({ message, reason, errors, }: {
|
6
7
|
message: string;
|
7
8
|
reason: RetryErrorReason;
|
@@ -11,6 +12,7 @@ export declare class RetryError extends Error {
|
|
11
12
|
name: string;
|
12
13
|
message: string;
|
13
14
|
reason: RetryErrorReason;
|
15
|
+
lastError: unknown;
|
14
16
|
errors: unknown[];
|
15
17
|
};
|
16
18
|
}
|
package/core/api/RetryError.js
CHANGED
@@ -1,13 +1,20 @@
|
|
1
1
|
export class RetryError extends Error {
|
2
2
|
constructor({ message, reason, errors, }) {
|
3
3
|
super(message);
|
4
|
-
|
4
|
+
// note: property order determines debugging output
|
5
|
+
Object.defineProperty(this, "reason", {
|
5
6
|
enumerable: true,
|
6
7
|
configurable: true,
|
7
8
|
writable: true,
|
8
9
|
value: void 0
|
9
10
|
});
|
10
|
-
Object.defineProperty(this, "
|
11
|
+
Object.defineProperty(this, "lastError", {
|
12
|
+
enumerable: true,
|
13
|
+
configurable: true,
|
14
|
+
writable: true,
|
15
|
+
value: void 0
|
16
|
+
});
|
17
|
+
Object.defineProperty(this, "errors", {
|
11
18
|
enumerable: true,
|
12
19
|
configurable: true,
|
13
20
|
writable: true,
|
@@ -16,12 +23,15 @@ export class RetryError extends Error {
|
|
16
23
|
this.name = "RetryError";
|
17
24
|
this.reason = reason;
|
18
25
|
this.errors = errors;
|
26
|
+
// separate our last error to make debugging via log easier:
|
27
|
+
this.lastError = errors[errors.length - 1];
|
19
28
|
}
|
20
29
|
toJSON() {
|
21
30
|
return {
|
22
31
|
name: this.name,
|
23
32
|
message: this.message,
|
24
33
|
reason: this.reason,
|
34
|
+
lastError: this.lastError,
|
25
35
|
errors: this.errors,
|
26
36
|
};
|
27
37
|
}
|
package/core/api/postToApi.cjs
CHANGED
@@ -1,14 +1,21 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.postToApi = exports.postJsonToApi = exports.createAudioMpegResponseHandler = exports.createTextResponseHandler = exports.createJsonResponseHandler = void 0;
|
4
|
+
const ZodSchema_js_1 = require("../schema/ZodSchema.cjs");
|
5
|
+
const parseJSON_js_1 = require("../schema/parseJSON.cjs");
|
4
6
|
const ApiCallError_js_1 = require("./ApiCallError.cjs");
|
5
7
|
const createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
6
|
-
const
|
8
|
+
const responseBody = await response.text();
|
9
|
+
const parsedResult = (0, parseJSON_js_1.safeParseJSON)({
|
10
|
+
text: responseBody,
|
11
|
+
schema: new ZodSchema_js_1.ZodSchema(responseSchema),
|
12
|
+
});
|
7
13
|
if (!parsedResult.success) {
|
8
14
|
throw new ApiCallError_js_1.ApiCallError({
|
9
15
|
message: "Invalid JSON response",
|
10
16
|
cause: parsedResult.error,
|
11
17
|
statusCode: response.status,
|
18
|
+
responseBody,
|
12
19
|
url,
|
13
20
|
requestBodyValues,
|
14
21
|
});
|
package/core/api/postToApi.d.ts
CHANGED
package/core/api/postToApi.js
CHANGED
@@ -1,11 +1,18 @@
|
|
1
|
+
import { ZodSchema } from "../schema/ZodSchema.js";
|
2
|
+
import { safeParseJSON } from "../schema/parseJSON.js";
|
1
3
|
import { ApiCallError } from "./ApiCallError.js";
|
2
4
|
export const createJsonResponseHandler = (responseSchema) => async ({ response, url, requestBodyValues }) => {
|
3
|
-
const
|
5
|
+
const responseBody = await response.text();
|
6
|
+
const parsedResult = safeParseJSON({
|
7
|
+
text: responseBody,
|
8
|
+
schema: new ZodSchema(responseSchema),
|
9
|
+
});
|
4
10
|
if (!parsedResult.success) {
|
5
11
|
throw new ApiCallError({
|
6
12
|
message: "Invalid JSON response",
|
7
13
|
cause: parsedResult.error,
|
8
14
|
statusCode: response.status,
|
15
|
+
responseBody,
|
9
16
|
url,
|
10
17
|
requestBodyValues,
|
11
18
|
});
|
@@ -7,7 +7,8 @@ class JSONParseError extends Error {
|
|
7
7
|
super(`JSON parsing failed: ` +
|
8
8
|
`Text: ${text}.\n` +
|
9
9
|
`Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
|
10
|
-
|
10
|
+
// note: property order determines debugging output
|
11
|
+
Object.defineProperty(this, "text", {
|
11
12
|
enumerable: true,
|
12
13
|
configurable: true,
|
13
14
|
writable: true,
|
@@ -19,12 +20,6 @@ class JSONParseError extends Error {
|
|
19
20
|
writable: true,
|
20
21
|
value: void 0
|
21
22
|
});
|
22
|
-
Object.defineProperty(this, "text", {
|
23
|
-
enumerable: true,
|
24
|
-
configurable: true,
|
25
|
-
writable: true,
|
26
|
-
value: void 0
|
27
|
-
});
|
28
23
|
this.name = "JSONParseError";
|
29
24
|
this.cause = cause;
|
30
25
|
this.text = text;
|
@@ -4,7 +4,8 @@ export class JSONParseError extends Error {
|
|
4
4
|
super(`JSON parsing failed: ` +
|
5
5
|
`Text: ${text}.\n` +
|
6
6
|
`Error message: ${getErrorMessage(cause)}`);
|
7
|
-
|
7
|
+
// note: property order determines debugging output
|
8
|
+
Object.defineProperty(this, "text", {
|
8
9
|
enumerable: true,
|
9
10
|
configurable: true,
|
10
11
|
writable: true,
|
@@ -16,12 +17,6 @@ export class JSONParseError extends Error {
|
|
16
17
|
writable: true,
|
17
18
|
value: void 0
|
18
19
|
});
|
19
|
-
Object.defineProperty(this, "text", {
|
20
|
-
enumerable: true,
|
21
|
-
configurable: true,
|
22
|
-
writable: true,
|
23
|
-
value: void 0
|
24
|
-
});
|
25
20
|
this.name = "JSONParseError";
|
26
21
|
this.cause = cause;
|
27
22
|
this.text = text;
|
package/model-provider/index.cjs
CHANGED
@@ -25,3 +25,4 @@ __exportStar(require("./ollama/index.cjs"), exports);
|
|
25
25
|
__exportStar(require("./openai/index.cjs"), exports);
|
26
26
|
__exportStar(require("./openai-compatible/index.cjs"), exports);
|
27
27
|
__exportStar(require("./stability/index.cjs"), exports);
|
28
|
+
__exportStar(require("./whispercpp/index.cjs"), exports);
|
package/model-provider/index.js
CHANGED
@@ -81,7 +81,10 @@ class OpenAITranscriptionModel extends AbstractModel_js_1.AbstractModel {
|
|
81
81
|
});
|
82
82
|
}
|
83
83
|
get settingsForEvent() {
|
84
|
-
return {
|
84
|
+
return {
|
85
|
+
language: this.settings.language,
|
86
|
+
temperature: this.settings.temperature,
|
87
|
+
};
|
85
88
|
}
|
86
89
|
withSettings(additionalSettings) {
|
87
90
|
return new OpenAITranscriptionModel(Object.assign({}, this.settings, additionalSettings));
|
@@ -20,7 +20,22 @@ export declare const calculateOpenAITranscriptionCostInMillicents: ({ model, res
|
|
20
20
|
}) => number | null;
|
21
21
|
export interface OpenAITranscriptionModelSettings extends TranscriptionModelSettings {
|
22
22
|
api?: ApiConfiguration;
|
23
|
+
/**
|
24
|
+
* ID of the model to use. Only whisper-1 is currently available.
|
25
|
+
*/
|
23
26
|
model: OpenAITranscriptionModelType;
|
27
|
+
/**
|
28
|
+
* The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.
|
29
|
+
*/
|
30
|
+
language?: string;
|
31
|
+
/**
|
32
|
+
* The sampling temperature, between 0 and 1.
|
33
|
+
* Higher values like 0.8 will make the output more random,
|
34
|
+
* while lower values like 0.2 will make it more focused and deterministic.
|
35
|
+
* If set to 0, the model will use log probability to automatically
|
36
|
+
* increase the temperature until certain thresholds are hit.
|
37
|
+
*/
|
38
|
+
temperature?: number;
|
24
39
|
}
|
25
40
|
export type OpenAITranscriptionInput = {
|
26
41
|
type: "flac" | "m4a" | "mp3" | "mp4" | "mpeg" | "mpga" | "ogg" | "wav" | "webm";
|
@@ -77,7 +77,10 @@ export class OpenAITranscriptionModel extends AbstractModel {
|
|
77
77
|
});
|
78
78
|
}
|
79
79
|
get settingsForEvent() {
|
80
|
-
return {
|
80
|
+
return {
|
81
|
+
language: this.settings.language,
|
82
|
+
temperature: this.settings.temperature,
|
83
|
+
};
|
81
84
|
}
|
82
85
|
withSettings(additionalSettings) {
|
83
86
|
return new OpenAITranscriptionModel(Object.assign({}, this.settings, additionalSettings));
|
@@ -50,7 +50,7 @@ const chatResponseStreamEventSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.union
|
|
50
50
|
chatCompletionChunkSchema,
|
51
51
|
zod_1.z.object({
|
52
52
|
object: zod_1.z.string().refine((obj) => obj !== "chat.completion.chunk", {
|
53
|
-
message: "Object must
|
53
|
+
message: "Object must be 'chat.completion.chunk'",
|
54
54
|
}),
|
55
55
|
}),
|
56
56
|
]));
|
@@ -47,7 +47,7 @@ const chatResponseStreamEventSchema = new ZodSchema(z.union([
|
|
47
47
|
chatCompletionChunkSchema,
|
48
48
|
z.object({
|
49
49
|
object: z.string().refine((obj) => obj !== "chat.completion.chunk", {
|
50
|
-
message: "Object must
|
50
|
+
message: "Object must be 'chat.completion.chunk'",
|
51
51
|
}),
|
52
52
|
}),
|
53
53
|
]));
|
@@ -0,0 +1,15 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.WhisperCppApiConfiguration = void 0;
|
4
|
+
const BaseUrlApiConfiguration_js_1 = require("../../core/api/BaseUrlApiConfiguration.cjs");
|
5
|
+
class WhisperCppApiConfiguration extends BaseUrlApiConfiguration_js_1.BaseUrlApiConfiguration {
|
6
|
+
constructor({ baseUrl = "http://127.0.0.1:8080", retry, throttle, } = {}) {
|
7
|
+
super({
|
8
|
+
baseUrl,
|
9
|
+
headers: {},
|
10
|
+
retry,
|
11
|
+
throttle,
|
12
|
+
});
|
13
|
+
}
|
14
|
+
}
|
15
|
+
exports.WhisperCppApiConfiguration = WhisperCppApiConfiguration;
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { BaseUrlApiConfiguration } from "../../core/api/BaseUrlApiConfiguration.js";
|
2
|
+
import { RetryFunction } from "../../core/api/RetryFunction.js";
|
3
|
+
import { ThrottleFunction } from "../../core/api/ThrottleFunction.js";
|
4
|
+
export type WhisperCppApiConfigurationSettings = {
|
5
|
+
baseUrl?: string;
|
6
|
+
retry?: RetryFunction;
|
7
|
+
throttle?: ThrottleFunction;
|
8
|
+
};
|
9
|
+
export declare class WhisperCppApiConfiguration extends BaseUrlApiConfiguration {
|
10
|
+
constructor({ baseUrl, retry, throttle, }?: WhisperCppApiConfigurationSettings);
|
11
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { BaseUrlApiConfiguration } from "../../core/api/BaseUrlApiConfiguration.js";
|
2
|
+
export class WhisperCppApiConfiguration extends BaseUrlApiConfiguration {
|
3
|
+
constructor({ baseUrl = "http://127.0.0.1:8080", retry, throttle, } = {}) {
|
4
|
+
super({
|
5
|
+
baseUrl,
|
6
|
+
headers: {},
|
7
|
+
retry,
|
8
|
+
throttle,
|
9
|
+
});
|
10
|
+
}
|
11
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Api = exports.Transcriber = void 0;
|
4
|
+
const WhisperCppApiConfiguration_js_1 = require("./WhisperCppApiConfiguration.cjs");
|
5
|
+
const WhisperCppTranscriptionModel_js_1 = require("./WhisperCppTranscriptionModel.cjs");
|
6
|
+
function Transcriber(settings = {}) {
|
7
|
+
return new WhisperCppTranscriptionModel_js_1.WhisperCppTranscriptionModel(settings);
|
8
|
+
}
|
9
|
+
exports.Transcriber = Transcriber;
|
10
|
+
function Api(settings) {
|
11
|
+
return new WhisperCppApiConfiguration_js_1.WhisperCppApiConfiguration(settings);
|
12
|
+
}
|
13
|
+
exports.Api = Api;
|
@@ -0,0 +1,4 @@
|
|
1
|
+
import { WhisperCppApiConfiguration, WhisperCppApiConfigurationSettings } from "./WhisperCppApiConfiguration.js";
|
2
|
+
import { WhisperCppTranscriptionModel, WhisperCppTranscriptionModelSettings } from "./WhisperCppTranscriptionModel.js";
|
3
|
+
export declare function Transcriber(settings?: WhisperCppTranscriptionModelSettings): WhisperCppTranscriptionModel;
|
4
|
+
export declare function Api(settings: WhisperCppApiConfigurationSettings): WhisperCppApiConfiguration;
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { WhisperCppApiConfiguration, } from "./WhisperCppApiConfiguration.js";
|
2
|
+
import { WhisperCppTranscriptionModel, } from "./WhisperCppTranscriptionModel.js";
|
3
|
+
export function Transcriber(settings = {}) {
|
4
|
+
return new WhisperCppTranscriptionModel(settings);
|
5
|
+
}
|
6
|
+
export function Api(settings) {
|
7
|
+
return new WhisperCppApiConfiguration(settings);
|
8
|
+
}
|
@@ -0,0 +1,117 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.WhisperCppTranscriptionModel = void 0;
|
4
|
+
const zod_1 = require("zod");
|
5
|
+
const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
|
6
|
+
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
7
|
+
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
8
|
+
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
9
|
+
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
10
|
+
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
11
|
+
const WhisperCppApiConfiguration_js_1 = require("./WhisperCppApiConfiguration.cjs");
|
12
|
+
class WhisperCppTranscriptionModel extends AbstractModel_js_1.AbstractModel {
|
13
|
+
constructor(settings) {
|
14
|
+
super({ settings });
|
15
|
+
Object.defineProperty(this, "provider", {
|
16
|
+
enumerable: true,
|
17
|
+
configurable: true,
|
18
|
+
writable: true,
|
19
|
+
value: "whispercpp"
|
20
|
+
});
|
21
|
+
Object.defineProperty(this, "modelName", {
|
22
|
+
enumerable: true,
|
23
|
+
configurable: true,
|
24
|
+
writable: true,
|
25
|
+
value: null
|
26
|
+
});
|
27
|
+
}
|
28
|
+
async doTranscribe(data, options) {
|
29
|
+
const response = await this.callAPI(data, {
|
30
|
+
functionId: options?.functionId,
|
31
|
+
run: options?.run,
|
32
|
+
});
|
33
|
+
return {
|
34
|
+
response,
|
35
|
+
transcription: response.text,
|
36
|
+
};
|
37
|
+
}
|
38
|
+
async callAPI(data, options) {
|
39
|
+
const { temperature } = this.settings;
|
40
|
+
const api = this.settings.api ?? new WhisperCppApiConfiguration_js_1.WhisperCppApiConfiguration();
|
41
|
+
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
42
|
+
retry: api.retry,
|
43
|
+
throttle: api.throttle,
|
44
|
+
call: async () => {
|
45
|
+
const formData = new FormData();
|
46
|
+
formData.append("file", new Blob([data.data]), `audio.${data.type}`);
|
47
|
+
formData.append("response_format", "json");
|
48
|
+
if (temperature != null) {
|
49
|
+
formData.append("temperature", temperature.toString());
|
50
|
+
}
|
51
|
+
return (0, postToApi_js_1.postToApi)({
|
52
|
+
url: api.assembleUrl("/inference"),
|
53
|
+
headers: api.headers,
|
54
|
+
body: {
|
55
|
+
content: formData,
|
56
|
+
values: { temperature },
|
57
|
+
},
|
58
|
+
failedResponseHandler,
|
59
|
+
successfulResponseHandler,
|
60
|
+
abortSignal: options?.run?.abortSignal,
|
61
|
+
});
|
62
|
+
},
|
63
|
+
});
|
64
|
+
}
|
65
|
+
get settingsForEvent() {
|
66
|
+
return {
|
67
|
+
temperature: this.settings.temperature,
|
68
|
+
};
|
69
|
+
}
|
70
|
+
withSettings(additionalSettings) {
|
71
|
+
return new WhisperCppTranscriptionModel(Object.assign({}, this.settings, additionalSettings));
|
72
|
+
}
|
73
|
+
}
|
74
|
+
exports.WhisperCppTranscriptionModel = WhisperCppTranscriptionModel;
|
75
|
+
const whisperCppTranscriptionJsonSchema = zod_1.z.union([
|
76
|
+
zod_1.z.object({ text: zod_1.z.string() }),
|
77
|
+
zod_1.z.object({ error: zod_1.z.string() }),
|
78
|
+
]);
|
79
|
+
const successfulResponseHandler = async ({ response, url, requestBodyValues }) => {
|
80
|
+
const responseBody = await response.text();
|
81
|
+
const parsedResult = (0, parseJSON_js_1.safeParseJSON)({
|
82
|
+
text: responseBody,
|
83
|
+
schema: new ZodSchema_js_1.ZodSchema(whisperCppTranscriptionJsonSchema),
|
84
|
+
});
|
85
|
+
if (!parsedResult.success) {
|
86
|
+
throw new ApiCallError_js_1.ApiCallError({
|
87
|
+
message: "Invalid JSON response",
|
88
|
+
cause: parsedResult.error,
|
89
|
+
statusCode: response.status,
|
90
|
+
responseBody,
|
91
|
+
url,
|
92
|
+
requestBodyValues,
|
93
|
+
});
|
94
|
+
}
|
95
|
+
if ("error" in parsedResult.data) {
|
96
|
+
throw new ApiCallError_js_1.ApiCallError({
|
97
|
+
message: parsedResult.data.error,
|
98
|
+
statusCode: response.status,
|
99
|
+
responseBody,
|
100
|
+
url,
|
101
|
+
requestBodyValues,
|
102
|
+
});
|
103
|
+
}
|
104
|
+
return {
|
105
|
+
text: parsedResult.data.text.trim(),
|
106
|
+
};
|
107
|
+
};
|
108
|
+
const failedResponseHandler = async ({ response, url, requestBodyValues, }) => {
|
109
|
+
const message = await response.text();
|
110
|
+
return new ApiCallError_js_1.ApiCallError({
|
111
|
+
message,
|
112
|
+
url,
|
113
|
+
requestBodyValues,
|
114
|
+
statusCode: response.status,
|
115
|
+
responseBody: message,
|
116
|
+
});
|
117
|
+
};
|
@@ -0,0 +1,29 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
3
|
+
import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
|
4
|
+
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
5
|
+
import { TranscriptionModel, TranscriptionModelSettings } from "../../model-function/generate-transcription/TranscriptionModel.js";
|
6
|
+
export interface WhisperCppTranscriptionModelSettings extends TranscriptionModelSettings {
|
7
|
+
api?: ApiConfiguration;
|
8
|
+
temperature?: number;
|
9
|
+
}
|
10
|
+
export type WhisperCppTranscriptionInput = {
|
11
|
+
type: "wav";
|
12
|
+
data: Buffer;
|
13
|
+
};
|
14
|
+
export declare class WhisperCppTranscriptionModel extends AbstractModel<WhisperCppTranscriptionModelSettings> implements TranscriptionModel<WhisperCppTranscriptionInput, WhisperCppTranscriptionModelSettings> {
|
15
|
+
constructor(settings: WhisperCppTranscriptionModelSettings);
|
16
|
+
readonly provider: "whispercpp";
|
17
|
+
readonly modelName: null;
|
18
|
+
doTranscribe(data: WhisperCppTranscriptionInput, options?: FunctionOptions): Promise<{
|
19
|
+
response: {
|
20
|
+
text: string;
|
21
|
+
};
|
22
|
+
transcription: string;
|
23
|
+
}>;
|
24
|
+
callAPI(data: WhisperCppTranscriptionInput, options: FunctionOptions): Promise<{
|
25
|
+
text: string;
|
26
|
+
}>;
|
27
|
+
get settingsForEvent(): Partial<WhisperCppTranscriptionModelSettings>;
|
28
|
+
withSettings(additionalSettings: WhisperCppTranscriptionModelSettings): this;
|
29
|
+
}
|
@@ -0,0 +1,113 @@
|
|
1
|
+
import { z } from "zod";
|
2
|
+
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
3
|
+
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
4
|
+
import { postToApi } from "../../core/api/postToApi.js";
|
5
|
+
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
6
|
+
import { safeParseJSON } from "../../core/schema/parseJSON.js";
|
7
|
+
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
8
|
+
import { WhisperCppApiConfiguration } from "./WhisperCppApiConfiguration.js";
|
9
|
+
export class WhisperCppTranscriptionModel extends AbstractModel {
|
10
|
+
constructor(settings) {
|
11
|
+
super({ settings });
|
12
|
+
Object.defineProperty(this, "provider", {
|
13
|
+
enumerable: true,
|
14
|
+
configurable: true,
|
15
|
+
writable: true,
|
16
|
+
value: "whispercpp"
|
17
|
+
});
|
18
|
+
Object.defineProperty(this, "modelName", {
|
19
|
+
enumerable: true,
|
20
|
+
configurable: true,
|
21
|
+
writable: true,
|
22
|
+
value: null
|
23
|
+
});
|
24
|
+
}
|
25
|
+
async doTranscribe(data, options) {
|
26
|
+
const response = await this.callAPI(data, {
|
27
|
+
functionId: options?.functionId,
|
28
|
+
run: options?.run,
|
29
|
+
});
|
30
|
+
return {
|
31
|
+
response,
|
32
|
+
transcription: response.text,
|
33
|
+
};
|
34
|
+
}
|
35
|
+
async callAPI(data, options) {
|
36
|
+
const { temperature } = this.settings;
|
37
|
+
const api = this.settings.api ?? new WhisperCppApiConfiguration();
|
38
|
+
return callWithRetryAndThrottle({
|
39
|
+
retry: api.retry,
|
40
|
+
throttle: api.throttle,
|
41
|
+
call: async () => {
|
42
|
+
const formData = new FormData();
|
43
|
+
formData.append("file", new Blob([data.data]), `audio.${data.type}`);
|
44
|
+
formData.append("response_format", "json");
|
45
|
+
if (temperature != null) {
|
46
|
+
formData.append("temperature", temperature.toString());
|
47
|
+
}
|
48
|
+
return postToApi({
|
49
|
+
url: api.assembleUrl("/inference"),
|
50
|
+
headers: api.headers,
|
51
|
+
body: {
|
52
|
+
content: formData,
|
53
|
+
values: { temperature },
|
54
|
+
},
|
55
|
+
failedResponseHandler,
|
56
|
+
successfulResponseHandler,
|
57
|
+
abortSignal: options?.run?.abortSignal,
|
58
|
+
});
|
59
|
+
},
|
60
|
+
});
|
61
|
+
}
|
62
|
+
get settingsForEvent() {
|
63
|
+
return {
|
64
|
+
temperature: this.settings.temperature,
|
65
|
+
};
|
66
|
+
}
|
67
|
+
withSettings(additionalSettings) {
|
68
|
+
return new WhisperCppTranscriptionModel(Object.assign({}, this.settings, additionalSettings));
|
69
|
+
}
|
70
|
+
}
|
71
|
+
const whisperCppTranscriptionJsonSchema = z.union([
|
72
|
+
z.object({ text: z.string() }),
|
73
|
+
z.object({ error: z.string() }),
|
74
|
+
]);
|
75
|
+
const successfulResponseHandler = async ({ response, url, requestBodyValues }) => {
|
76
|
+
const responseBody = await response.text();
|
77
|
+
const parsedResult = safeParseJSON({
|
78
|
+
text: responseBody,
|
79
|
+
schema: new ZodSchema(whisperCppTranscriptionJsonSchema),
|
80
|
+
});
|
81
|
+
if (!parsedResult.success) {
|
82
|
+
throw new ApiCallError({
|
83
|
+
message: "Invalid JSON response",
|
84
|
+
cause: parsedResult.error,
|
85
|
+
statusCode: response.status,
|
86
|
+
responseBody,
|
87
|
+
url,
|
88
|
+
requestBodyValues,
|
89
|
+
});
|
90
|
+
}
|
91
|
+
if ("error" in parsedResult.data) {
|
92
|
+
throw new ApiCallError({
|
93
|
+
message: parsedResult.data.error,
|
94
|
+
statusCode: response.status,
|
95
|
+
responseBody,
|
96
|
+
url,
|
97
|
+
requestBodyValues,
|
98
|
+
});
|
99
|
+
}
|
100
|
+
return {
|
101
|
+
text: parsedResult.data.text.trim(),
|
102
|
+
};
|
103
|
+
};
|
104
|
+
const failedResponseHandler = async ({ response, url, requestBodyValues, }) => {
|
105
|
+
const message = await response.text();
|
106
|
+
return new ApiCallError({
|
107
|
+
message,
|
108
|
+
url,
|
109
|
+
requestBodyValues,
|
110
|
+
statusCode: response.status,
|
111
|
+
responseBody: message,
|
112
|
+
});
|
113
|
+
};
|
@@ -0,0 +1,32 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
+
}) : function(o, v) {
|
16
|
+
o["default"] = v;
|
17
|
+
});
|
18
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
19
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
20
|
+
};
|
21
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
22
|
+
if (mod && mod.__esModule) return mod;
|
23
|
+
var result = {};
|
24
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
25
|
+
__setModuleDefault(result, mod);
|
26
|
+
return result;
|
27
|
+
};
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
29
|
+
exports.whispercpp = void 0;
|
30
|
+
__exportStar(require("./WhisperCppApiConfiguration.cjs"), exports);
|
31
|
+
exports.whispercpp = __importStar(require("./WhisperCppFacade.cjs"));
|
32
|
+
__exportStar(require("./WhisperCppTranscriptionModel.cjs"), exports);
|