modelfusion 0.91.0 → 0.92.1
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 +4 -2
- package/core/FunctionOptions.d.ts +3 -11
- package/core/LogFormat.cjs +10 -0
- package/core/LogFormat.d.ts +9 -0
- package/core/LogFormat.js +9 -0
- package/core/ModelFusionConfiguration.cjs +21 -0
- package/core/ModelFusionConfiguration.d.ts +6 -0
- package/core/ModelFusionConfiguration.js +14 -0
- package/core/api/ApiCallError.cjs +9 -1
- package/core/api/ApiCallError.d.ts +4 -1
- package/core/api/ApiCallError.js +9 -1
- package/core/executeFunctionCall.cjs +4 -4
- package/core/executeFunctionCall.js +4 -4
- package/core/getFunctionCallLogger.cjs +21 -5
- package/core/getFunctionCallLogger.js +21 -5
- package/core/index.cjs +15 -2
- package/core/index.d.ts +2 -2
- package/core/index.js +2 -2
- package/model-function/executeStandardCall.cjs +4 -4
- package/model-function/executeStandardCall.js +4 -4
- package/model-function/executeStreamCall.cjs +4 -4
- package/model-function/executeStreamCall.js +4 -4
- package/model-provider/ollama/OllamaError.cjs +25 -24
- package/model-provider/ollama/OllamaError.d.ts +1 -11
- package/model-provider/ollama/OllamaError.js +24 -22
- package/model-provider/ollama/OllamaTextGenerationModel.cjs +38 -1
- package/model-provider/ollama/OllamaTextGenerationModel.d.ts +5 -1
- package/model-provider/ollama/OllamaTextGenerationModel.js +39 -2
- package/model-provider/ollama/OllamaTextGenerationModel.test.cjs +63 -0
- package/model-provider/ollama/OllamaTextGenerationModel.test.d.ts +1 -0
- package/model-provider/ollama/OllamaTextGenerationModel.test.js +61 -0
- package/model-provider/ollama/index.cjs +1 -3
- package/model-provider/ollama/index.d.ts +1 -1
- package/model-provider/ollama/index.js +0 -1
- package/model-provider/openai/OpenAIError.cjs +13 -29
- package/model-provider/openai/OpenAIError.d.ts +2 -11
- package/model-provider/openai/OpenAIError.js +11 -26
- package/model-provider/openai/index.cjs +1 -3
- package/model-provider/openai/index.d.ts +1 -1
- package/model-provider/openai/index.js +0 -1
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.cjs +5 -8
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.js +5 -8
- package/package.json +5 -4
- package/tool/execute-tool/executeTool.cjs +3 -4
- package/tool/execute-tool/executeTool.js +3 -4
- package/util/AsyncQueue.test.cjs +20 -21
- package/util/AsyncQueue.test.js +9 -10
- package/util/isDeepEqualData.test.cjs +14 -15
- package/util/isDeepEqualData.test.js +14 -15
- package/util/runSafe.test.cjs +12 -13
- package/util/runSafe.test.js +6 -7
- package/core/GlobalFunctionLogging.cjs +0 -12
- package/core/GlobalFunctionLogging.d.ts +0 -3
- package/core/GlobalFunctionLogging.js +0 -7
- package/core/GlobalFunctionObservers.cjs +0 -12
- package/core/GlobalFunctionObservers.d.ts +0 -3
- package/core/GlobalFunctionObservers.js +0 -7
@@ -2,9 +2,11 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.OllamaTextGenerationResponseFormat = exports.OllamaTextGenerationModel = void 0;
|
4
4
|
const zod_1 = require("zod");
|
5
|
+
const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
|
5
6
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
6
7
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
7
8
|
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
9
|
+
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
8
10
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
9
11
|
const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
|
10
12
|
const TextGenerationToolCallModel_js_1 = require("../../tool/generate-tool-call/TextGenerationToolCallModel.cjs");
|
@@ -238,7 +240,42 @@ exports.OllamaTextGenerationResponseFormat = {
|
|
238
240
|
*/
|
239
241
|
json: {
|
240
242
|
stream: false,
|
241
|
-
handler: (
|
243
|
+
handler: (async ({ response, url, requestBodyValues }) => {
|
244
|
+
const responseBody = await response.text();
|
245
|
+
const parsedResult = (0, parseJSON_js_1.safeParseJSON)({
|
246
|
+
text: responseBody,
|
247
|
+
schema: new ZodSchema_js_1.ZodSchema(zod_1.z.union([
|
248
|
+
ollamaTextGenerationResponseSchema,
|
249
|
+
zod_1.z.object({
|
250
|
+
done: zod_1.z.literal(false),
|
251
|
+
model: zod_1.z.string(),
|
252
|
+
created_at: zod_1.z.string(),
|
253
|
+
response: zod_1.z.string(),
|
254
|
+
}),
|
255
|
+
])),
|
256
|
+
});
|
257
|
+
if (!parsedResult.success) {
|
258
|
+
throw new ApiCallError_js_1.ApiCallError({
|
259
|
+
message: "Invalid JSON response",
|
260
|
+
cause: parsedResult.error,
|
261
|
+
statusCode: response.status,
|
262
|
+
responseBody,
|
263
|
+
url,
|
264
|
+
requestBodyValues,
|
265
|
+
});
|
266
|
+
}
|
267
|
+
if (parsedResult.data.done === false) {
|
268
|
+
throw new ApiCallError_js_1.ApiCallError({
|
269
|
+
message: "Incomplete Ollama response received",
|
270
|
+
statusCode: response.status,
|
271
|
+
responseBody,
|
272
|
+
url,
|
273
|
+
requestBodyValues,
|
274
|
+
isRetryable: true,
|
275
|
+
});
|
276
|
+
}
|
277
|
+
return parsedResult.data;
|
278
|
+
}),
|
242
279
|
},
|
243
280
|
/**
|
244
281
|
* Returns an async iterable over the full deltas (all choices, including full current state at time of event)
|
@@ -189,7 +189,11 @@ export declare const OllamaTextGenerationResponseFormat: {
|
|
189
189
|
*/
|
190
190
|
json: {
|
191
191
|
stream: false;
|
192
|
-
handler:
|
192
|
+
handler: ({ response, url, requestBodyValues }: {
|
193
|
+
url: string;
|
194
|
+
requestBodyValues: unknown;
|
195
|
+
response: Response;
|
196
|
+
}) => Promise<{
|
193
197
|
response: string;
|
194
198
|
model: string;
|
195
199
|
done: true;
|
@@ -1,7 +1,9 @@
|
|
1
1
|
import { z } from "zod";
|
2
|
+
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
2
3
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
|
-
import {
|
4
|
+
import { postJsonToApi } from "../../core/api/postToApi.js";
|
4
5
|
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
6
|
+
import { safeParseJSON } from "../../core/schema/parseJSON.js";
|
5
7
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
6
8
|
import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
7
9
|
import { TextGenerationToolCallModel, } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
|
@@ -234,7 +236,42 @@ export const OllamaTextGenerationResponseFormat = {
|
|
234
236
|
*/
|
235
237
|
json: {
|
236
238
|
stream: false,
|
237
|
-
handler:
|
239
|
+
handler: (async ({ response, url, requestBodyValues }) => {
|
240
|
+
const responseBody = await response.text();
|
241
|
+
const parsedResult = safeParseJSON({
|
242
|
+
text: responseBody,
|
243
|
+
schema: new ZodSchema(z.union([
|
244
|
+
ollamaTextGenerationResponseSchema,
|
245
|
+
z.object({
|
246
|
+
done: z.literal(false),
|
247
|
+
model: z.string(),
|
248
|
+
created_at: z.string(),
|
249
|
+
response: z.string(),
|
250
|
+
}),
|
251
|
+
])),
|
252
|
+
});
|
253
|
+
if (!parsedResult.success) {
|
254
|
+
throw new ApiCallError({
|
255
|
+
message: "Invalid JSON response",
|
256
|
+
cause: parsedResult.error,
|
257
|
+
statusCode: response.status,
|
258
|
+
responseBody,
|
259
|
+
url,
|
260
|
+
requestBodyValues,
|
261
|
+
});
|
262
|
+
}
|
263
|
+
if (parsedResult.data.done === false) {
|
264
|
+
throw new ApiCallError({
|
265
|
+
message: "Incomplete Ollama response received",
|
266
|
+
statusCode: response.status,
|
267
|
+
responseBody,
|
268
|
+
url,
|
269
|
+
requestBodyValues,
|
270
|
+
isRetryable: true,
|
271
|
+
});
|
272
|
+
}
|
273
|
+
return parsedResult.data;
|
274
|
+
}),
|
238
275
|
},
|
239
276
|
/**
|
240
277
|
* Returns an async iterable over the full deltas (all choices, including full current state at time of event)
|
@@ -0,0 +1,63 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
const assert_1 = require("assert");
|
4
|
+
const msw_1 = require("msw");
|
5
|
+
const node_1 = require("msw/node");
|
6
|
+
const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
|
7
|
+
const retryNever_js_1 = require("../../core/api/retryNever.cjs");
|
8
|
+
const generateText_js_1 = require("../../model-function/generate-text/generateText.cjs");
|
9
|
+
const OllamaApiConfiguration_js_1 = require("./OllamaApiConfiguration.cjs");
|
10
|
+
const OllamaTextGenerationModel_js_1 = require("./OllamaTextGenerationModel.cjs");
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
12
|
+
let responseBodyJson = {};
|
13
|
+
const server = (0, node_1.setupServer)(msw_1.http.post("http://127.0.0.1:11434/api/generate", () => msw_1.HttpResponse.json(responseBodyJson)));
|
14
|
+
beforeAll(() => server.listen());
|
15
|
+
beforeEach(() => {
|
16
|
+
responseBodyJson = {};
|
17
|
+
});
|
18
|
+
afterEach(() => server.resetHandlers());
|
19
|
+
afterAll(() => server.close());
|
20
|
+
describe("generateText", () => {
|
21
|
+
it("should return the generated text", async () => {
|
22
|
+
responseBodyJson = {
|
23
|
+
model: "test-model",
|
24
|
+
created_at: "2023-08-04T19:22:45.499127Z",
|
25
|
+
response: "test response",
|
26
|
+
context: [1, 2, 3],
|
27
|
+
done: true,
|
28
|
+
total_duration: 5589157167,
|
29
|
+
load_duration: 3013701500,
|
30
|
+
sample_count: 114,
|
31
|
+
sample_duration: 81442000,
|
32
|
+
prompt_eval_count: 46,
|
33
|
+
prompt_eval_duration: 1160282000,
|
34
|
+
eval_count: 113,
|
35
|
+
eval_duration: 1325948000,
|
36
|
+
};
|
37
|
+
const result = await (0, generateText_js_1.generateText)(new OllamaTextGenerationModel_js_1.OllamaTextGenerationModel({
|
38
|
+
model: "test-model",
|
39
|
+
}), "test prompt");
|
40
|
+
expect(result).toEqual("test response");
|
41
|
+
});
|
42
|
+
it("should throw retryable ApiCallError when Ollama is overloaded", async () => {
|
43
|
+
responseBodyJson = {
|
44
|
+
model: "",
|
45
|
+
created_at: "0001-01-01T00:00:00Z",
|
46
|
+
response: "",
|
47
|
+
done: false,
|
48
|
+
};
|
49
|
+
try {
|
50
|
+
await (0, generateText_js_1.generateText)(new OllamaTextGenerationModel_js_1.OllamaTextGenerationModel({
|
51
|
+
api: new OllamaApiConfiguration_js_1.OllamaApiConfiguration({
|
52
|
+
retry: (0, retryNever_js_1.retryNever)(),
|
53
|
+
}),
|
54
|
+
model: "test-model",
|
55
|
+
}), "test prompt");
|
56
|
+
(0, assert_1.fail)("Should have thrown ApiCallError");
|
57
|
+
}
|
58
|
+
catch (expectedError) {
|
59
|
+
expect(expectedError).toBeInstanceOf(ApiCallError_js_1.ApiCallError);
|
60
|
+
expect(expectedError.isRetryable).toBe(true);
|
61
|
+
}
|
62
|
+
});
|
63
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,61 @@
|
|
1
|
+
import { fail } from "assert";
|
2
|
+
import { HttpResponse, http } from "msw";
|
3
|
+
import { setupServer } from "msw/node";
|
4
|
+
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
5
|
+
import { retryNever } from "../../core/api/retryNever.js";
|
6
|
+
import { generateText } from "../../model-function/generate-text/generateText.js";
|
7
|
+
import { OllamaApiConfiguration } from "./OllamaApiConfiguration.js";
|
8
|
+
import { OllamaTextGenerationModel } from "./OllamaTextGenerationModel.js";
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
10
|
+
let responseBodyJson = {};
|
11
|
+
const server = setupServer(http.post("http://127.0.0.1:11434/api/generate", () => HttpResponse.json(responseBodyJson)));
|
12
|
+
beforeAll(() => server.listen());
|
13
|
+
beforeEach(() => {
|
14
|
+
responseBodyJson = {};
|
15
|
+
});
|
16
|
+
afterEach(() => server.resetHandlers());
|
17
|
+
afterAll(() => server.close());
|
18
|
+
describe("generateText", () => {
|
19
|
+
it("should return the generated text", async () => {
|
20
|
+
responseBodyJson = {
|
21
|
+
model: "test-model",
|
22
|
+
created_at: "2023-08-04T19:22:45.499127Z",
|
23
|
+
response: "test response",
|
24
|
+
context: [1, 2, 3],
|
25
|
+
done: true,
|
26
|
+
total_duration: 5589157167,
|
27
|
+
load_duration: 3013701500,
|
28
|
+
sample_count: 114,
|
29
|
+
sample_duration: 81442000,
|
30
|
+
prompt_eval_count: 46,
|
31
|
+
prompt_eval_duration: 1160282000,
|
32
|
+
eval_count: 113,
|
33
|
+
eval_duration: 1325948000,
|
34
|
+
};
|
35
|
+
const result = await generateText(new OllamaTextGenerationModel({
|
36
|
+
model: "test-model",
|
37
|
+
}), "test prompt");
|
38
|
+
expect(result).toEqual("test response");
|
39
|
+
});
|
40
|
+
it("should throw retryable ApiCallError when Ollama is overloaded", async () => {
|
41
|
+
responseBodyJson = {
|
42
|
+
model: "",
|
43
|
+
created_at: "0001-01-01T00:00:00Z",
|
44
|
+
response: "",
|
45
|
+
done: false,
|
46
|
+
};
|
47
|
+
try {
|
48
|
+
await generateText(new OllamaTextGenerationModel({
|
49
|
+
api: new OllamaApiConfiguration({
|
50
|
+
retry: retryNever(),
|
51
|
+
}),
|
52
|
+
model: "test-model",
|
53
|
+
}), "test prompt");
|
54
|
+
fail("Should have thrown ApiCallError");
|
55
|
+
}
|
56
|
+
catch (expectedError) {
|
57
|
+
expect(expectedError).toBeInstanceOf(ApiCallError);
|
58
|
+
expect(expectedError.isRetryable).toBe(true);
|
59
|
+
}
|
60
|
+
});
|
61
|
+
});
|
@@ -26,10 +26,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
26
26
|
return result;
|
27
27
|
};
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
29
|
-
exports.ollama =
|
29
|
+
exports.ollama = void 0;
|
30
30
|
__exportStar(require("./OllamaApiConfiguration.cjs"), exports);
|
31
|
-
var OllamaError_js_1 = require("./OllamaError.cjs");
|
32
|
-
Object.defineProperty(exports, "OllamaError", { enumerable: true, get: function () { return OllamaError_js_1.OllamaError; } });
|
33
31
|
exports.ollama = __importStar(require("./OllamaFacade.cjs"));
|
34
32
|
__exportStar(require("./OllamaTextEmbeddingModel.cjs"), exports);
|
35
33
|
__exportStar(require("./OllamaTextGenerationModel.cjs"), exports);
|
@@ -1,5 +1,5 @@
|
|
1
1
|
export * from "./OllamaApiConfiguration.js";
|
2
|
-
export {
|
2
|
+
export { OllamaErrorData } from "./OllamaError.js";
|
3
3
|
export * as ollama from "./OllamaFacade.js";
|
4
4
|
export * from "./OllamaTextEmbeddingModel.js";
|
5
5
|
export * from "./OllamaTextGenerationModel.js";
|
@@ -1,11 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.failedOpenAICallResponseHandler =
|
3
|
+
exports.failedOpenAICallResponseHandler = void 0;
|
4
4
|
const zod_1 = require("zod");
|
5
5
|
const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
|
6
6
|
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
7
7
|
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
8
|
-
|
8
|
+
const openAIErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
|
9
9
|
error: zod_1.z.object({
|
10
10
|
message: zod_1.z.string(),
|
11
11
|
type: zod_1.z.string(),
|
@@ -13,50 +13,34 @@ exports.openAIErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
|
|
13
13
|
code: zod_1.z.string().nullable(),
|
14
14
|
}),
|
15
15
|
}));
|
16
|
-
class OpenAIError extends ApiCallError_js_1.ApiCallError {
|
17
|
-
constructor({ data, statusCode, url, requestBodyValues, message, }) {
|
18
|
-
super({
|
19
|
-
message,
|
20
|
-
statusCode,
|
21
|
-
requestBodyValues,
|
22
|
-
url,
|
23
|
-
isRetryable: (statusCode === 429 &&
|
24
|
-
// insufficient_quota is also reported as a 429, but it's not retryable:
|
25
|
-
data?.error.type !== "insufficient_quota") ||
|
26
|
-
statusCode >= 500,
|
27
|
-
});
|
28
|
-
Object.defineProperty(this, "data", {
|
29
|
-
enumerable: true,
|
30
|
-
configurable: true,
|
31
|
-
writable: true,
|
32
|
-
value: void 0
|
33
|
-
});
|
34
|
-
this.data = data;
|
35
|
-
}
|
36
|
-
}
|
37
|
-
exports.OpenAIError = OpenAIError;
|
38
16
|
const failedOpenAICallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
39
17
|
const responseBody = await response.text();
|
40
18
|
// resilient parsing in case the response is not JSON or does not match the schema:
|
41
19
|
try {
|
42
20
|
const parsedError = (0, parseJSON_js_1.parseJSON)({
|
43
21
|
text: responseBody,
|
44
|
-
schema:
|
22
|
+
schema: openAIErrorDataSchema,
|
45
23
|
});
|
46
|
-
return new
|
24
|
+
return new ApiCallError_js_1.ApiCallError({
|
25
|
+
message: parsedError.error.message,
|
47
26
|
url,
|
48
27
|
requestBodyValues,
|
49
28
|
statusCode: response.status,
|
50
|
-
|
29
|
+
responseBody,
|
51
30
|
data: parsedError,
|
31
|
+
isRetryable: (response.status === 429 &&
|
32
|
+
// insufficient_quota is also reported as a 429, but it's not retryable:
|
33
|
+
parsedError?.error.type !== "insufficient_quota") ||
|
34
|
+
response.status >= 500,
|
52
35
|
});
|
53
36
|
}
|
54
37
|
catch (parseError) {
|
55
|
-
return new
|
38
|
+
return new ApiCallError_js_1.ApiCallError({
|
39
|
+
message: responseBody.trim() !== "" ? responseBody : response.statusText,
|
56
40
|
url,
|
57
41
|
requestBodyValues,
|
58
42
|
statusCode: response.status,
|
59
|
-
|
43
|
+
responseBody,
|
60
44
|
});
|
61
45
|
}
|
62
46
|
};
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
2
2
|
import { ResponseHandler } from "../../core/api/postToApi.js";
|
3
3
|
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
4
|
-
|
4
|
+
declare const openAIErrorDataSchema: ZodSchema<{
|
5
5
|
error: {
|
6
6
|
message: string;
|
7
7
|
code: string | null;
|
@@ -10,14 +10,5 @@ export declare const openAIErrorDataSchema: ZodSchema<{
|
|
10
10
|
};
|
11
11
|
}>;
|
12
12
|
export type OpenAIErrorData = (typeof openAIErrorDataSchema)["_type"];
|
13
|
-
export declare class OpenAIError extends ApiCallError {
|
14
|
-
readonly data?: OpenAIErrorData;
|
15
|
-
constructor({ data, statusCode, url, requestBodyValues, message, }: {
|
16
|
-
message: string;
|
17
|
-
statusCode: number;
|
18
|
-
url: string;
|
19
|
-
requestBodyValues: unknown;
|
20
|
-
data?: OpenAIErrorData;
|
21
|
-
});
|
22
|
-
}
|
23
13
|
export declare const failedOpenAICallResponseHandler: ResponseHandler<ApiCallError>;
|
14
|
+
export {};
|
@@ -2,7 +2,7 @@ import { z } from "zod";
|
|
2
2
|
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
3
3
|
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
4
4
|
import { parseJSON } from "../../core/schema/parseJSON.js";
|
5
|
-
|
5
|
+
const openAIErrorDataSchema = new ZodSchema(z.object({
|
6
6
|
error: z.object({
|
7
7
|
message: z.string(),
|
8
8
|
type: z.string(),
|
@@ -10,27 +10,6 @@ export const openAIErrorDataSchema = new ZodSchema(z.object({
|
|
10
10
|
code: z.string().nullable(),
|
11
11
|
}),
|
12
12
|
}));
|
13
|
-
export class OpenAIError extends ApiCallError {
|
14
|
-
constructor({ data, statusCode, url, requestBodyValues, message, }) {
|
15
|
-
super({
|
16
|
-
message,
|
17
|
-
statusCode,
|
18
|
-
requestBodyValues,
|
19
|
-
url,
|
20
|
-
isRetryable: (statusCode === 429 &&
|
21
|
-
// insufficient_quota is also reported as a 429, but it's not retryable:
|
22
|
-
data?.error.type !== "insufficient_quota") ||
|
23
|
-
statusCode >= 500,
|
24
|
-
});
|
25
|
-
Object.defineProperty(this, "data", {
|
26
|
-
enumerable: true,
|
27
|
-
configurable: true,
|
28
|
-
writable: true,
|
29
|
-
value: void 0
|
30
|
-
});
|
31
|
-
this.data = data;
|
32
|
-
}
|
33
|
-
}
|
34
13
|
export const failedOpenAICallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
35
14
|
const responseBody = await response.text();
|
36
15
|
// resilient parsing in case the response is not JSON or does not match the schema:
|
@@ -39,20 +18,26 @@ export const failedOpenAICallResponseHandler = async ({ response, url, requestBo
|
|
39
18
|
text: responseBody,
|
40
19
|
schema: openAIErrorDataSchema,
|
41
20
|
});
|
42
|
-
return new
|
21
|
+
return new ApiCallError({
|
22
|
+
message: parsedError.error.message,
|
43
23
|
url,
|
44
24
|
requestBodyValues,
|
45
25
|
statusCode: response.status,
|
46
|
-
|
26
|
+
responseBody,
|
47
27
|
data: parsedError,
|
28
|
+
isRetryable: (response.status === 429 &&
|
29
|
+
// insufficient_quota is also reported as a 429, but it's not retryable:
|
30
|
+
parsedError?.error.type !== "insufficient_quota") ||
|
31
|
+
response.status >= 500,
|
48
32
|
});
|
49
33
|
}
|
50
34
|
catch (parseError) {
|
51
|
-
return new
|
35
|
+
return new ApiCallError({
|
36
|
+
message: responseBody.trim() !== "" ? responseBody : response.statusText,
|
52
37
|
url,
|
53
38
|
requestBodyValues,
|
54
39
|
statusCode: response.status,
|
55
|
-
|
40
|
+
responseBody,
|
56
41
|
});
|
57
42
|
}
|
58
43
|
};
|
@@ -26,13 +26,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
26
26
|
return result;
|
27
27
|
};
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
29
|
-
exports.OpenAIChatPromptFormat = exports.openai =
|
29
|
+
exports.OpenAIChatPromptFormat = exports.openai = void 0;
|
30
30
|
__exportStar(require("./AzureOpenAIApiConfiguration.cjs"), exports);
|
31
31
|
__exportStar(require("./OpenAIApiConfiguration.cjs"), exports);
|
32
32
|
__exportStar(require("./OpenAICompletionModel.cjs"), exports);
|
33
33
|
__exportStar(require("./OpenAICostCalculator.cjs"), exports);
|
34
|
-
var OpenAIError_js_1 = require("./OpenAIError.cjs");
|
35
|
-
Object.defineProperty(exports, "OpenAIError", { enumerable: true, get: function () { return OpenAIError_js_1.OpenAIError; } });
|
36
34
|
exports.openai = __importStar(require("./OpenAIFacade.cjs"));
|
37
35
|
__exportStar(require("./OpenAIImageGenerationModel.cjs"), exports);
|
38
36
|
__exportStar(require("./OpenAISpeechModel.cjs"), exports);
|
@@ -2,7 +2,7 @@ export * from "./AzureOpenAIApiConfiguration.js";
|
|
2
2
|
export * from "./OpenAIApiConfiguration.js";
|
3
3
|
export * from "./OpenAICompletionModel.js";
|
4
4
|
export * from "./OpenAICostCalculator.js";
|
5
|
-
export {
|
5
|
+
export { OpenAIErrorData } from "./OpenAIError.js";
|
6
6
|
export * as openai from "./OpenAIFacade.js";
|
7
7
|
export * from "./OpenAIImageGenerationModel.js";
|
8
8
|
export * from "./OpenAISpeechModel.js";
|
@@ -2,7 +2,6 @@ export * from "./AzureOpenAIApiConfiguration.js";
|
|
2
2
|
export * from "./OpenAIApiConfiguration.js";
|
3
3
|
export * from "./OpenAICompletionModel.js";
|
4
4
|
export * from "./OpenAICostCalculator.js";
|
5
|
-
export { OpenAIError } from "./OpenAIError.js";
|
6
5
|
export * as openai from "./OpenAIFacade.js";
|
7
6
|
export * from "./OpenAIImageGenerationModel.js";
|
8
7
|
export * from "./OpenAISpeechModel.js";
|
@@ -72,15 +72,12 @@ class WhisperCppTranscriptionModel extends AbstractModel_js_1.AbstractModel {
|
|
72
72
|
}
|
73
73
|
}
|
74
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
|
-
]);
|
75
|
+
const whisperCppTranscriptionJsonSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.union([zod_1.z.object({ text: zod_1.z.string() }), zod_1.z.object({ error: zod_1.z.string() })]));
|
79
76
|
const successfulResponseHandler = async ({ response, url, requestBodyValues }) => {
|
80
77
|
const responseBody = await response.text();
|
81
78
|
const parsedResult = (0, parseJSON_js_1.safeParseJSON)({
|
82
79
|
text: responseBody,
|
83
|
-
schema:
|
80
|
+
schema: whisperCppTranscriptionJsonSchema,
|
84
81
|
});
|
85
82
|
if (!parsedResult.success) {
|
86
83
|
throw new ApiCallError_js_1.ApiCallError({
|
@@ -106,12 +103,12 @@ const successfulResponseHandler = async ({ response, url, requestBodyValues }) =
|
|
106
103
|
};
|
107
104
|
};
|
108
105
|
const failedResponseHandler = async ({ response, url, requestBodyValues, }) => {
|
109
|
-
const
|
106
|
+
const responseBody = await response.text();
|
110
107
|
return new ApiCallError_js_1.ApiCallError({
|
111
|
-
message,
|
108
|
+
message: responseBody,
|
112
109
|
url,
|
113
110
|
requestBodyValues,
|
114
111
|
statusCode: response.status,
|
115
|
-
responseBody
|
112
|
+
responseBody,
|
116
113
|
});
|
117
114
|
};
|
@@ -68,15 +68,12 @@ export class WhisperCppTranscriptionModel extends AbstractModel {
|
|
68
68
|
return new WhisperCppTranscriptionModel(Object.assign({}, this.settings, additionalSettings));
|
69
69
|
}
|
70
70
|
}
|
71
|
-
const whisperCppTranscriptionJsonSchema = z.union([
|
72
|
-
z.object({ text: z.string() }),
|
73
|
-
z.object({ error: z.string() }),
|
74
|
-
]);
|
71
|
+
const whisperCppTranscriptionJsonSchema = new ZodSchema(z.union([z.object({ text: z.string() }), z.object({ error: z.string() })]));
|
75
72
|
const successfulResponseHandler = async ({ response, url, requestBodyValues }) => {
|
76
73
|
const responseBody = await response.text();
|
77
74
|
const parsedResult = safeParseJSON({
|
78
75
|
text: responseBody,
|
79
|
-
schema:
|
76
|
+
schema: whisperCppTranscriptionJsonSchema,
|
80
77
|
});
|
81
78
|
if (!parsedResult.success) {
|
82
79
|
throw new ApiCallError({
|
@@ -102,12 +99,12 @@ const successfulResponseHandler = async ({ response, url, requestBodyValues }) =
|
|
102
99
|
};
|
103
100
|
};
|
104
101
|
const failedResponseHandler = async ({ response, url, requestBodyValues, }) => {
|
105
|
-
const
|
102
|
+
const responseBody = await response.text();
|
106
103
|
return new ApiCallError({
|
107
|
-
message,
|
104
|
+
message: responseBody,
|
108
105
|
url,
|
109
106
|
requestBodyValues,
|
110
107
|
statusCode: response.status,
|
111
|
-
responseBody
|
108
|
+
responseBody,
|
112
109
|
});
|
113
110
|
};
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "modelfusion",
|
3
3
|
"description": "The TypeScript library for building multi-modal AI applications.",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.92.1",
|
5
5
|
"author": "Lars Grammel",
|
6
6
|
"license": "MIT",
|
7
7
|
"keywords": [
|
@@ -62,8 +62,8 @@
|
|
62
62
|
"build": "npm run build:esm && npm run build:cjs",
|
63
63
|
"build:esm": "tsc --outDir dist/",
|
64
64
|
"build:cjs": "tsc --outDir build/cjs/ -p tsconfig.cjs.json && node bin/prepare-cjs.js",
|
65
|
-
"test": "vitest run src",
|
66
|
-
"test
|
65
|
+
"test": "vitest --config vitest.config.js --run src",
|
66
|
+
"test:watch": "vitest watch--config vitest.config.js",
|
67
67
|
"dist": "npm run clean && npm run lint && npm run test && npm run build && npm run dist:copy-files",
|
68
68
|
"dist:copy-files": "copyfiles package.json README.md LICENSE dist"
|
69
69
|
},
|
@@ -87,9 +87,10 @@
|
|
87
87
|
"fastify": "^4.0.0",
|
88
88
|
"husky": "^8.0.3",
|
89
89
|
"lint-staged": "15.1.0",
|
90
|
+
"msw": "2.0.10",
|
90
91
|
"prettier": "3.1.0",
|
91
92
|
"rimraf": "5.0.5",
|
92
93
|
"typescript": "5.2.2",
|
93
|
-
"vitest": "
|
94
|
+
"vitest": "1.0.4"
|
94
95
|
}
|
95
96
|
}
|
@@ -3,8 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.executeTool = void 0;
|
4
4
|
const nanoid_1 = require("nanoid");
|
5
5
|
const FunctionEventSource_js_1 = require("../../core/FunctionEventSource.cjs");
|
6
|
-
const
|
7
|
-
const GlobalFunctionObservers_js_1 = require("../../core/GlobalFunctionObservers.cjs");
|
6
|
+
const ModelFusionConfiguration_js_1 = require("../../core/ModelFusionConfiguration.cjs");
|
8
7
|
const AbortError_js_1 = require("../../core/api/AbortError.cjs");
|
9
8
|
const getFunctionCallLogger_js_1 = require("../../core/getFunctionCallLogger.cjs");
|
10
9
|
const getRun_js_1 = require("../../core/getRun.cjs");
|
@@ -22,8 +21,8 @@ async function doExecuteTool(tool, args, options) {
|
|
22
21
|
const run = await (0, getRun_js_1.getRun)(options?.run);
|
23
22
|
const eventSource = new FunctionEventSource_js_1.FunctionEventSource({
|
24
23
|
observers: [
|
25
|
-
...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0,
|
26
|
-
...(0,
|
24
|
+
...(0, getFunctionCallLogger_js_1.getFunctionCallLogger)(options?.logging ?? (0, ModelFusionConfiguration_js_1.getLogFormat)()),
|
25
|
+
...(0, ModelFusionConfiguration_js_1.getFunctionObservers)(),
|
27
26
|
...(run?.functionObserver != null ? [run.functionObserver] : []),
|
28
27
|
...(options?.observers ?? []),
|
29
28
|
],
|
@@ -1,7 +1,6 @@
|
|
1
1
|
import { nanoid as createId } from "nanoid";
|
2
2
|
import { FunctionEventSource } from "../../core/FunctionEventSource.js";
|
3
|
-
import {
|
4
|
-
import { getGlobalFunctionObservers } from "../../core/GlobalFunctionObservers.js";
|
3
|
+
import { getFunctionObservers, getLogFormat, } from "../../core/ModelFusionConfiguration.js";
|
5
4
|
import { AbortError } from "../../core/api/AbortError.js";
|
6
5
|
import { getFunctionCallLogger } from "../../core/getFunctionCallLogger.js";
|
7
6
|
import { getRun } from "../../core/getRun.js";
|
@@ -18,8 +17,8 @@ async function doExecuteTool(tool, args, options) {
|
|
18
17
|
const run = await getRun(options?.run);
|
19
18
|
const eventSource = new FunctionEventSource({
|
20
19
|
observers: [
|
21
|
-
...getFunctionCallLogger(options?.logging ??
|
22
|
-
...
|
20
|
+
...getFunctionCallLogger(options?.logging ?? getLogFormat()),
|
21
|
+
...getFunctionObservers(),
|
23
22
|
...(run?.functionObserver != null ? [run.functionObserver] : []),
|
24
23
|
...(options?.observers ?? []),
|
25
24
|
],
|