modelfusion 0.47.1 → 0.47.3
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 +38 -37
- package/core/api/loadApiKey.cjs +7 -1
- package/core/api/loadApiKey.js +7 -1
- package/core/structure/Schema.d.ts +5 -5
- package/core/structure/UncheckedSchema.cjs +2 -2
- package/core/structure/UncheckedSchema.d.ts +2 -2
- package/core/structure/UncheckedSchema.js +2 -2
- package/core/structure/ZodSchema.cjs +2 -5
- package/core/structure/ZodSchema.d.ts +2 -2
- package/core/structure/ZodSchema.js +2 -5
- package/event-source/readEventSource.cjs +5 -8
- package/event-source/readEventSource.d.ts +1 -1
- package/event-source/readEventSource.js +5 -5
- package/event-source/readEventSourceStream.cjs +3 -6
- package/event-source/readEventSourceStream.js +3 -3
- package/guard/fixStructure.cjs +50 -0
- package/guard/fixStructure.d.ts +50 -0
- package/guard/fixStructure.js +50 -0
- package/guard/guard.d.ts +1 -1
- package/model-function/generate-structure/generateStructure.cjs +1 -1
- package/model-function/generate-structure/generateStructure.js +1 -1
- package/model-function/generate-structure/generateStructureOrText.cjs +1 -1
- package/model-function/generate-structure/generateStructureOrText.js +1 -1
- package/model-function/generate-structure/streamStructure.cjs +1 -1
- package/model-function/generate-structure/streamStructure.js +1 -1
- package/model-provider/anthropic/AnthropicError.cjs +7 -14
- package/model-provider/anthropic/AnthropicError.js +7 -11
- package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +2 -15
- package/model-provider/anthropic/AnthropicTextGenerationModel.js +2 -12
- package/model-provider/automatic1111/Automatic1111Error.cjs +2 -5
- package/model-provider/automatic1111/Automatic1111Error.d.ts +1 -1
- package/model-provider/automatic1111/Automatic1111Error.js +2 -2
- package/model-provider/cohere/CohereError.cjs +2 -5
- package/model-provider/cohere/CohereError.js +2 -2
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +8 -11
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +1 -1
- package/model-provider/cohere/CohereTextEmbeddingModel.js +1 -1
- package/model-provider/cohere/CohereTextGenerationModel.cjs +22 -21
- package/model-provider/cohere/CohereTextGenerationModel.js +22 -18
- package/model-provider/cohere/CohereTokenizer.cjs +12 -15
- package/model-provider/cohere/CohereTokenizer.d.ts +1 -1
- package/model-provider/cohere/CohereTokenizer.js +1 -1
- package/model-provider/huggingface/HuggingFaceError.cjs +7 -31
- package/model-provider/huggingface/HuggingFaceError.js +7 -28
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.cjs +3 -6
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.d.ts +1 -1
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.js +1 -1
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +2 -5
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +1 -1
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +1 -1
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +3 -6
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +1 -1
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +1 -1
- package/model-provider/llamacpp/LlamaCppError.cjs +7 -30
- package/model-provider/llamacpp/LlamaCppError.js +7 -27
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +3 -6
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +1 -1
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +1 -1
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +53 -66
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +1 -1
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +3 -13
- package/model-provider/llamacpp/LlamaCppTokenizer.cjs +3 -6
- package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +1 -1
- package/model-provider/llamacpp/LlamaCppTokenizer.js +1 -1
- package/model-provider/openai/OpenAIError.cjs +2 -5
- package/model-provider/openai/OpenAIError.js +2 -2
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +11 -14
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +1 -1
- package/model-provider/openai/OpenAITextEmbeddingModel.js +1 -1
- package/model-provider/openai/OpenAITextGenerationModel.cjs +26 -39
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +1 -1
- package/model-provider/openai/OpenAITextGenerationModel.js +3 -13
- package/model-provider/openai/OpenAITranscriptionModel.cjs +20 -23
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +1 -1
- package/model-provider/openai/OpenAITranscriptionModel.js +1 -1
- package/model-provider/openai/TikTokenTokenizer.cjs +12 -8
- package/model-provider/openai/TikTokenTokenizer.d.ts +0 -3
- package/model-provider/openai/TikTokenTokenizer.js +9 -8
- package/model-provider/openai/chat/OpenAIChatModel.cjs +21 -21
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +1 -1
- package/model-provider/openai/chat/OpenAIChatModel.js +2 -2
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +2 -6
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +2 -3
- package/model-provider/stability/StabilityError.cjs +2 -5
- package/model-provider/stability/StabilityError.js +2 -2
- package/package.json +1 -1
- package/util/JSONParseError.cjs +33 -0
- package/util/JSONParseError.d.ts +9 -0
- package/util/JSONParseError.js +29 -0
- package/util/index.cjs +2 -0
- package/util/index.d.ts +2 -0
- package/util/index.js +2 -0
- package/util/parseJSON.cjs +67 -0
- package/util/parseJSON.d.ts +18 -0
- package/util/parseJSON.js +58 -0
- package/vector-index/memory/MemoryVectorIndex.cjs +7 -10
- package/vector-index/memory/MemoryVectorIndex.js +3 -3
@@ -1,12 +1,9 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.failedLlamaCppCallResponseHandler = exports.LlamaCppError = exports.llamaCppErrorDataSchema = void 0;
|
7
|
-
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
4
|
const zod_1 = require("zod");
|
9
5
|
const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
|
6
|
+
const parseJSON_js_1 = require("../../util/parseJSON.cjs");
|
10
7
|
exports.llamaCppErrorDataSchema = zod_1.z.object({
|
11
8
|
error: zod_1.z.string(),
|
12
9
|
});
|
@@ -23,30 +20,10 @@ class LlamaCppError extends ApiCallError_js_1.ApiCallError {
|
|
23
20
|
}
|
24
21
|
}
|
25
22
|
exports.LlamaCppError = LlamaCppError;
|
26
|
-
const failedLlamaCppCallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
requestBodyValues,
|
33
|
-
statusCode: response.status,
|
34
|
-
data: parsedError,
|
35
|
-
});
|
36
|
-
}
|
37
|
-
catch (error) {
|
38
|
-
if (error instanceof Error) {
|
39
|
-
if (error.name === "AbortError" || error instanceof ApiCallError_js_1.ApiCallError) {
|
40
|
-
throw error;
|
41
|
-
}
|
42
|
-
}
|
43
|
-
throw new ApiCallError_js_1.ApiCallError({
|
44
|
-
message: responseBody,
|
45
|
-
cause: error,
|
46
|
-
statusCode: response.status,
|
47
|
-
url,
|
48
|
-
requestBodyValues,
|
49
|
-
});
|
50
|
-
}
|
51
|
-
};
|
23
|
+
const failedLlamaCppCallResponseHandler = async ({ response, url, requestBodyValues }) => new LlamaCppError({
|
24
|
+
url,
|
25
|
+
requestBodyValues,
|
26
|
+
statusCode: response.status,
|
27
|
+
data: (0, parseJSON_js_1.parseJsonWithZod)(await response.text(), exports.llamaCppErrorDataSchema),
|
28
|
+
});
|
52
29
|
exports.failedLlamaCppCallResponseHandler = failedLlamaCppCallResponseHandler;
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import SecureJSON from "secure-json-parse";
|
2
1
|
import { z } from "zod";
|
3
2
|
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
3
|
+
import { parseJsonWithZod } from "../../util/parseJSON.js";
|
4
4
|
export const llamaCppErrorDataSchema = z.object({
|
5
5
|
error: z.string(),
|
6
6
|
});
|
@@ -16,29 +16,9 @@ export class LlamaCppError extends ApiCallError {
|
|
16
16
|
this.data = data;
|
17
17
|
}
|
18
18
|
}
|
19
|
-
export const failedLlamaCppCallResponseHandler = async ({ response, url, requestBodyValues }) => {
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
requestBodyValues,
|
26
|
-
statusCode: response.status,
|
27
|
-
data: parsedError,
|
28
|
-
});
|
29
|
-
}
|
30
|
-
catch (error) {
|
31
|
-
if (error instanceof Error) {
|
32
|
-
if (error.name === "AbortError" || error instanceof ApiCallError) {
|
33
|
-
throw error;
|
34
|
-
}
|
35
|
-
}
|
36
|
-
throw new ApiCallError({
|
37
|
-
message: responseBody,
|
38
|
-
cause: error,
|
39
|
-
statusCode: response.status,
|
40
|
-
url,
|
41
|
-
requestBodyValues,
|
42
|
-
});
|
43
|
-
}
|
44
|
-
};
|
19
|
+
export const failedLlamaCppCallResponseHandler = async ({ response, url, requestBodyValues }) => new LlamaCppError({
|
20
|
+
url,
|
21
|
+
requestBodyValues,
|
22
|
+
statusCode: response.status,
|
23
|
+
data: parseJsonWithZod(await response.text(), llamaCppErrorDataSchema),
|
24
|
+
});
|
@@ -1,10 +1,7 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.LlamaCppTextEmbeddingModel = void 0;
|
7
|
-
const zod_1 =
|
4
|
+
const zod_1 = require("zod");
|
8
5
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
9
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
10
7
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
@@ -84,8 +81,8 @@ class LlamaCppTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
84
81
|
}
|
85
82
|
}
|
86
83
|
exports.LlamaCppTextEmbeddingModel = LlamaCppTextEmbeddingModel;
|
87
|
-
const llamaCppTextEmbeddingResponseSchema = zod_1.
|
88
|
-
embedding: zod_1.
|
84
|
+
const llamaCppTextEmbeddingResponseSchema = zod_1.z.object({
|
85
|
+
embedding: zod_1.z.array(zod_1.z.number()),
|
89
86
|
});
|
90
87
|
async function callLlamaCppEmbeddingAPI({ api = new LlamaCppApiConfiguration_js_1.LlamaCppApiConfiguration(), abortSignal, content, }) {
|
91
88
|
return (0, postToApi_js_1.postJsonToApi)({
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import z from "zod";
|
1
|
+
import { z } from "zod";
|
2
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
4
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
@@ -1,17 +1,14 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.LlamaCppTextGenerationResponseFormat = exports.LlamaCppTextGenerationModel = void 0;
|
7
|
-
const
|
8
|
-
const zod_1 = __importDefault(require("zod"));
|
4
|
+
const zod_1 = require("zod");
|
9
5
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
10
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
11
7
|
const AsyncQueue_js_1 = require("../../event-source/AsyncQueue.cjs");
|
12
8
|
const parseEventSourceStream_js_1 = require("../../event-source/parseEventSourceStream.cjs");
|
13
9
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
14
10
|
const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
|
11
|
+
const parseJSON_js_1 = require("../../util/parseJSON.cjs");
|
15
12
|
const LlamaCppApiConfiguration_js_1 = require("./LlamaCppApiConfiguration.cjs");
|
16
13
|
const LlamaCppError_js_1 = require("./LlamaCppError.cjs");
|
17
14
|
const LlamaCppTokenizer_js_1 = require("./LlamaCppTokenizer.cjs");
|
@@ -118,59 +115,59 @@ class LlamaCppTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
118
115
|
}
|
119
116
|
}
|
120
117
|
exports.LlamaCppTextGenerationModel = LlamaCppTextGenerationModel;
|
121
|
-
const llamaCppTextGenerationResponseSchema = zod_1.
|
122
|
-
content: zod_1.
|
123
|
-
stop: zod_1.
|
124
|
-
generation_settings: zod_1.
|
125
|
-
frequency_penalty: zod_1.
|
126
|
-
ignore_eos: zod_1.
|
127
|
-
logit_bias: zod_1.
|
128
|
-
mirostat: zod_1.
|
129
|
-
mirostat_eta: zod_1.
|
130
|
-
mirostat_tau: zod_1.
|
131
|
-
model: zod_1.
|
132
|
-
n_ctx: zod_1.
|
133
|
-
n_keep: zod_1.
|
134
|
-
n_predict: zod_1.
|
135
|
-
n_probs: zod_1.
|
136
|
-
penalize_nl: zod_1.
|
137
|
-
presence_penalty: zod_1.
|
138
|
-
repeat_last_n: zod_1.
|
139
|
-
repeat_penalty: zod_1.
|
140
|
-
seed: zod_1.
|
141
|
-
stop: zod_1.
|
142
|
-
stream: zod_1.
|
143
|
-
temp: zod_1.
|
144
|
-
tfs_z: zod_1.
|
145
|
-
top_k: zod_1.
|
146
|
-
top_p: zod_1.
|
147
|
-
typical_p: zod_1.
|
118
|
+
const llamaCppTextGenerationResponseSchema = zod_1.z.object({
|
119
|
+
content: zod_1.z.string(),
|
120
|
+
stop: zod_1.z.literal(true),
|
121
|
+
generation_settings: zod_1.z.object({
|
122
|
+
frequency_penalty: zod_1.z.number(),
|
123
|
+
ignore_eos: zod_1.z.boolean(),
|
124
|
+
logit_bias: zod_1.z.array(zod_1.z.number()),
|
125
|
+
mirostat: zod_1.z.number(),
|
126
|
+
mirostat_eta: zod_1.z.number(),
|
127
|
+
mirostat_tau: zod_1.z.number(),
|
128
|
+
model: zod_1.z.string(),
|
129
|
+
n_ctx: zod_1.z.number(),
|
130
|
+
n_keep: zod_1.z.number(),
|
131
|
+
n_predict: zod_1.z.number(),
|
132
|
+
n_probs: zod_1.z.number(),
|
133
|
+
penalize_nl: zod_1.z.boolean(),
|
134
|
+
presence_penalty: zod_1.z.number(),
|
135
|
+
repeat_last_n: zod_1.z.number(),
|
136
|
+
repeat_penalty: zod_1.z.number(),
|
137
|
+
seed: zod_1.z.number(),
|
138
|
+
stop: zod_1.z.array(zod_1.z.string()),
|
139
|
+
stream: zod_1.z.boolean(),
|
140
|
+
temp: zod_1.z.number(),
|
141
|
+
tfs_z: zod_1.z.number(),
|
142
|
+
top_k: zod_1.z.number(),
|
143
|
+
top_p: zod_1.z.number(),
|
144
|
+
typical_p: zod_1.z.number(),
|
148
145
|
}),
|
149
|
-
model: zod_1.
|
150
|
-
prompt: zod_1.
|
151
|
-
stopped_eos: zod_1.
|
152
|
-
stopped_limit: zod_1.
|
153
|
-
stopped_word: zod_1.
|
154
|
-
stopping_word: zod_1.
|
155
|
-
timings: zod_1.
|
156
|
-
predicted_ms: zod_1.
|
157
|
-
predicted_n: zod_1.
|
158
|
-
predicted_per_second: zod_1.
|
159
|
-
predicted_per_token_ms: zod_1.
|
160
|
-
prompt_ms: zod_1.
|
161
|
-
prompt_n: zod_1.
|
162
|
-
prompt_per_second: zod_1.
|
163
|
-
prompt_per_token_ms: zod_1.
|
146
|
+
model: zod_1.z.string(),
|
147
|
+
prompt: zod_1.z.string(),
|
148
|
+
stopped_eos: zod_1.z.boolean(),
|
149
|
+
stopped_limit: zod_1.z.boolean(),
|
150
|
+
stopped_word: zod_1.z.boolean(),
|
151
|
+
stopping_word: zod_1.z.string(),
|
152
|
+
timings: zod_1.z.object({
|
153
|
+
predicted_ms: zod_1.z.number(),
|
154
|
+
predicted_n: zod_1.z.number(),
|
155
|
+
predicted_per_second: zod_1.z.number().nullable(),
|
156
|
+
predicted_per_token_ms: zod_1.z.number().nullable(),
|
157
|
+
prompt_ms: zod_1.z.number().nullable(),
|
158
|
+
prompt_n: zod_1.z.number(),
|
159
|
+
prompt_per_second: zod_1.z.number().nullable(),
|
160
|
+
prompt_per_token_ms: zod_1.z.number().nullable(),
|
164
161
|
}),
|
165
|
-
tokens_cached: zod_1.
|
166
|
-
tokens_evaluated: zod_1.
|
167
|
-
tokens_predicted: zod_1.
|
168
|
-
truncated: zod_1.
|
162
|
+
tokens_cached: zod_1.z.number(),
|
163
|
+
tokens_evaluated: zod_1.z.number(),
|
164
|
+
tokens_predicted: zod_1.z.number(),
|
165
|
+
truncated: zod_1.z.boolean(),
|
169
166
|
});
|
170
|
-
const llamaCppTextStreamingResponseSchema = zod_1.
|
171
|
-
zod_1.
|
172
|
-
content: zod_1.
|
173
|
-
stop: zod_1.
|
167
|
+
const llamaCppTextStreamingResponseSchema = zod_1.z.discriminatedUnion("stop", [
|
168
|
+
zod_1.z.object({
|
169
|
+
content: zod_1.z.string(),
|
170
|
+
stop: zod_1.z.literal(false),
|
174
171
|
}),
|
175
172
|
llamaCppTextGenerationResponseSchema,
|
176
173
|
]);
|
@@ -213,17 +210,7 @@ async function createLlamaCppFullDeltaIterableQueue(stream) {
|
|
213
210
|
try {
|
214
211
|
for await (const event of events) {
|
215
212
|
const data = event.data;
|
216
|
-
const
|
217
|
-
const parseResult = llamaCppTextStreamingResponseSchema.safeParse(json);
|
218
|
-
if (!parseResult.success) {
|
219
|
-
queue.push({
|
220
|
-
type: "error",
|
221
|
-
error: parseResult.error,
|
222
|
-
});
|
223
|
-
queue.close();
|
224
|
-
return;
|
225
|
-
}
|
226
|
-
const eventData = parseResult.data;
|
213
|
+
const eventData = (0, parseJSON_js_1.parseJsonWithZod)(data, llamaCppTextStreamingResponseSchema);
|
227
214
|
content += eventData.content;
|
228
215
|
queue.push({
|
229
216
|
type: "delta",
|
@@ -1,11 +1,11 @@
|
|
1
|
-
import
|
2
|
-
import z from "zod";
|
1
|
+
import { z } from "zod";
|
3
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
4
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
5
4
|
import { AsyncQueue } from "../../event-source/AsyncQueue.js";
|
6
5
|
import { parseEventSourceStream } from "../../event-source/parseEventSourceStream.js";
|
7
6
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
8
7
|
import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
8
|
+
import { parseJsonWithZod } from "../../util/parseJSON.js";
|
9
9
|
import { LlamaCppApiConfiguration } from "./LlamaCppApiConfiguration.js";
|
10
10
|
import { failedLlamaCppCallResponseHandler } from "./LlamaCppError.js";
|
11
11
|
import { LlamaCppTokenizer } from "./LlamaCppTokenizer.js";
|
@@ -206,17 +206,7 @@ async function createLlamaCppFullDeltaIterableQueue(stream) {
|
|
206
206
|
try {
|
207
207
|
for await (const event of events) {
|
208
208
|
const data = event.data;
|
209
|
-
const
|
210
|
-
const parseResult = llamaCppTextStreamingResponseSchema.safeParse(json);
|
211
|
-
if (!parseResult.success) {
|
212
|
-
queue.push({
|
213
|
-
type: "error",
|
214
|
-
error: parseResult.error,
|
215
|
-
});
|
216
|
-
queue.close();
|
217
|
-
return;
|
218
|
-
}
|
219
|
-
const eventData = parseResult.data;
|
209
|
+
const eventData = parseJsonWithZod(data, llamaCppTextStreamingResponseSchema);
|
220
210
|
content += eventData.content;
|
221
211
|
queue.push({
|
222
212
|
type: "delta",
|
@@ -1,10 +1,7 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.LlamaCppTokenizer = void 0;
|
7
|
-
const zod_1 =
|
4
|
+
const zod_1 = require("zod");
|
8
5
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
9
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
10
7
|
const LlamaCppApiConfiguration_js_1 = require("./LlamaCppApiConfiguration.cjs");
|
@@ -49,8 +46,8 @@ class LlamaCppTokenizer {
|
|
49
46
|
}
|
50
47
|
}
|
51
48
|
exports.LlamaCppTokenizer = LlamaCppTokenizer;
|
52
|
-
const llamaCppTokenizationResponseSchema = zod_1.
|
53
|
-
tokens: zod_1.
|
49
|
+
const llamaCppTokenizationResponseSchema = zod_1.z.object({
|
50
|
+
tokens: zod_1.z.array(zod_1.z.number()),
|
54
51
|
});
|
55
52
|
async function callLlamaCppTokenizeAPI({ api, abortSignal, text, }) {
|
56
53
|
return (0, postToApi_js_1.postJsonToApi)({
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import z from "zod";
|
1
|
+
import { z } from "zod";
|
2
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
4
|
import { LlamaCppApiConfiguration } from "./LlamaCppApiConfiguration.js";
|
@@ -1,12 +1,9 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.failedOpenAICallResponseHandler = exports.OpenAIError = exports.openAIErrorDataSchema = void 0;
|
7
|
-
const secure_json_parse_1 = __importDefault(require("secure-json-parse"));
|
8
4
|
const zod_1 = require("zod");
|
9
5
|
const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
|
6
|
+
const parseJSON_js_1 = require("../../util/parseJSON.cjs");
|
10
7
|
exports.openAIErrorDataSchema = zod_1.z.object({
|
11
8
|
error: zod_1.z.object({
|
12
9
|
message: zod_1.z.string(),
|
@@ -41,7 +38,7 @@ const failedOpenAICallResponseHandler = async ({ response, url, requestBodyValue
|
|
41
38
|
const responseBody = await response.text();
|
42
39
|
// resilient parsing in case the response is not JSON or does not match the schema:
|
43
40
|
try {
|
44
|
-
const parsedError =
|
41
|
+
const parsedError = (0, parseJSON_js_1.parseJsonWithZod)(responseBody, exports.openAIErrorDataSchema);
|
45
42
|
return new OpenAIError({
|
46
43
|
url,
|
47
44
|
requestBodyValues,
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import SecureJSON from "secure-json-parse";
|
2
1
|
import { z } from "zod";
|
3
2
|
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
3
|
+
import { parseJsonWithZod } from "../../util/parseJSON.js";
|
4
4
|
export const openAIErrorDataSchema = z.object({
|
5
5
|
error: z.object({
|
6
6
|
message: z.string(),
|
@@ -34,7 +34,7 @@ export const failedOpenAICallResponseHandler = async ({ response, url, requestBo
|
|
34
34
|
const responseBody = await response.text();
|
35
35
|
// resilient parsing in case the response is not JSON or does not match the schema:
|
36
36
|
try {
|
37
|
-
const parsedError =
|
37
|
+
const parsedError = parseJsonWithZod(responseBody, openAIErrorDataSchema);
|
38
38
|
return new OpenAIError({
|
39
39
|
url,
|
40
40
|
requestBodyValues,
|
@@ -1,10 +1,7 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.OpenAITextEmbeddingModel = exports.calculateOpenAIEmbeddingCostInMillicents = exports.isOpenAIEmbeddingModel = exports.OPENAI_TEXT_EMBEDDING_MODELS = void 0;
|
7
|
-
const zod_1 =
|
4
|
+
const zod_1 = require("zod");
|
8
5
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
9
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
10
7
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
@@ -122,17 +119,17 @@ class OpenAITextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
122
119
|
}
|
123
120
|
}
|
124
121
|
exports.OpenAITextEmbeddingModel = OpenAITextEmbeddingModel;
|
125
|
-
const openAITextEmbeddingResponseSchema = zod_1.
|
126
|
-
object: zod_1.
|
127
|
-
data: zod_1.
|
128
|
-
object: zod_1.
|
129
|
-
embedding: zod_1.
|
130
|
-
index: zod_1.
|
122
|
+
const openAITextEmbeddingResponseSchema = zod_1.z.object({
|
123
|
+
object: zod_1.z.literal("list"),
|
124
|
+
data: zod_1.z.array(zod_1.z.object({
|
125
|
+
object: zod_1.z.literal("embedding"),
|
126
|
+
embedding: zod_1.z.array(zod_1.z.number()),
|
127
|
+
index: zod_1.z.number(),
|
131
128
|
})),
|
132
|
-
model: zod_1.
|
133
|
-
usage: zod_1.
|
134
|
-
prompt_tokens: zod_1.
|
135
|
-
total_tokens: zod_1.
|
129
|
+
model: zod_1.z.string(),
|
130
|
+
usage: zod_1.z.object({
|
131
|
+
prompt_tokens: zod_1.z.number(),
|
132
|
+
total_tokens: zod_1.z.number(),
|
136
133
|
}),
|
137
134
|
});
|
138
135
|
async function callOpenAITextEmbeddingAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, model, input, user, }) {
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import z from "zod";
|
1
|
+
import { z } from "zod";
|
2
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
4
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
@@ -1,11 +1,7 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.OpenAITextResponseFormat = exports.OpenAITextGenerationModel = exports.calculateOpenAITextGenerationCostInMillicents = exports.isOpenAITextGenerationModel = exports.getOpenAITextGenerationModelInformation = exports.OPENAI_TEXT_GENERATION_MODELS = void 0;
|
7
|
-
const
|
8
|
-
const zod_1 = __importDefault(require("zod"));
|
4
|
+
const zod_1 = require("zod");
|
9
5
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
10
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
11
7
|
const AsyncQueue_js_1 = require("../../event-source/AsyncQueue.cjs");
|
@@ -14,6 +10,7 @@ const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
|
14
10
|
const PromptFormatTextStreamingModel_js_1 = require("../../model-function/generate-text/PromptFormatTextStreamingModel.cjs");
|
15
11
|
const TextPromptFormat_js_1 = require("../../model-function/generate-text/TextPromptFormat.cjs");
|
16
12
|
const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
|
13
|
+
const parseJSON_js_1 = require("../../util/parseJSON.cjs");
|
17
14
|
const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
|
18
15
|
const OpenAIError_js_1 = require("./OpenAIError.cjs");
|
19
16
|
const TikTokenTokenizer_js_1 = require("./TikTokenTokenizer.cjs");
|
@@ -267,21 +264,21 @@ class OpenAITextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
267
264
|
}
|
268
265
|
}
|
269
266
|
exports.OpenAITextGenerationModel = OpenAITextGenerationModel;
|
270
|
-
const openAITextGenerationResponseSchema = zod_1.
|
271
|
-
id: zod_1.
|
272
|
-
object: zod_1.
|
273
|
-
created: zod_1.
|
274
|
-
model: zod_1.
|
275
|
-
choices: zod_1.
|
276
|
-
text: zod_1.
|
277
|
-
index: zod_1.
|
278
|
-
logprobs: zod_1.
|
279
|
-
finish_reason: zod_1.
|
267
|
+
const openAITextGenerationResponseSchema = zod_1.z.object({
|
268
|
+
id: zod_1.z.string(),
|
269
|
+
object: zod_1.z.literal("text_completion"),
|
270
|
+
created: zod_1.z.number(),
|
271
|
+
model: zod_1.z.string(),
|
272
|
+
choices: zod_1.z.array(zod_1.z.object({
|
273
|
+
text: zod_1.z.string(),
|
274
|
+
index: zod_1.z.number(),
|
275
|
+
logprobs: zod_1.z.nullable(zod_1.z.any()),
|
276
|
+
finish_reason: zod_1.z.string(),
|
280
277
|
})),
|
281
|
-
usage: zod_1.
|
282
|
-
prompt_tokens: zod_1.
|
283
|
-
completion_tokens: zod_1.
|
284
|
-
total_tokens: zod_1.
|
278
|
+
usage: zod_1.z.object({
|
279
|
+
prompt_tokens: zod_1.z.number(),
|
280
|
+
completion_tokens: zod_1.z.number(),
|
281
|
+
total_tokens: zod_1.z.number(),
|
285
282
|
}),
|
286
283
|
});
|
287
284
|
async function callOpenAITextGenerationAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, responseFormat, model, prompt, suffix, maxTokens, temperature, topP, n, logprobs, echo, stop, presencePenalty, frequencyPenalty, bestOf, logitBias, user, }) {
|
@@ -332,16 +329,16 @@ exports.OpenAITextResponseFormat = {
|
|
332
329
|
handler: async ({ response }) => createOpenAITextFullDeltaIterableQueue(response.body),
|
333
330
|
},
|
334
331
|
};
|
335
|
-
const textResponseStreamEventSchema = zod_1.
|
336
|
-
choices: zod_1.
|
337
|
-
text: zod_1.
|
338
|
-
finish_reason: zod_1.
|
339
|
-
index: zod_1.
|
332
|
+
const textResponseStreamEventSchema = zod_1.z.object({
|
333
|
+
choices: zod_1.z.array(zod_1.z.object({
|
334
|
+
text: zod_1.z.string(),
|
335
|
+
finish_reason: zod_1.z.enum(["stop", "length"]).nullable(),
|
336
|
+
index: zod_1.z.number(),
|
340
337
|
})),
|
341
|
-
created: zod_1.
|
342
|
-
id: zod_1.
|
343
|
-
model: zod_1.
|
344
|
-
object: zod_1.
|
338
|
+
created: zod_1.z.number(),
|
339
|
+
id: zod_1.z.string(),
|
340
|
+
model: zod_1.z.string(),
|
341
|
+
object: zod_1.z.string(),
|
345
342
|
});
|
346
343
|
async function createOpenAITextFullDeltaIterableQueue(stream) {
|
347
344
|
const queue = new AsyncQueue_js_1.AsyncQueue();
|
@@ -356,17 +353,7 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
|
|
356
353
|
queue.close();
|
357
354
|
return;
|
358
355
|
}
|
359
|
-
const
|
360
|
-
const parseResult = textResponseStreamEventSchema.safeParse(json);
|
361
|
-
if (!parseResult.success) {
|
362
|
-
queue.push({
|
363
|
-
type: "error",
|
364
|
-
error: parseResult.error,
|
365
|
-
});
|
366
|
-
queue.close();
|
367
|
-
return;
|
368
|
-
}
|
369
|
-
const eventData = parseResult.data;
|
356
|
+
const eventData = (0, parseJSON_js_1.parseJsonWithZod)(data, textResponseStreamEventSchema);
|
370
357
|
for (let i = 0; i < eventData.choices.length; i++) {
|
371
358
|
const eventChoice = eventData.choices[i];
|
372
359
|
const delta = eventChoice.text;
|
@@ -1,5 +1,4 @@
|
|
1
|
-
import
|
2
|
-
import z from "zod";
|
1
|
+
import { z } from "zod";
|
3
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
4
3
|
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
5
4
|
import { AsyncQueue } from "../../event-source/AsyncQueue.js";
|
@@ -8,6 +7,7 @@ import { AbstractModel } from "../../model-function/AbstractModel.js";
|
|
8
7
|
import { PromptFormatTextStreamingModel } from "../../model-function/generate-text/PromptFormatTextStreamingModel.js";
|
9
8
|
import { mapChatPromptToTextFormat, mapInstructionPromptToTextFormat, } from "../../model-function/generate-text/TextPromptFormat.js";
|
10
9
|
import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
|
10
|
+
import { parseJsonWithZod } from "../../util/parseJSON.js";
|
11
11
|
import { OpenAIApiConfiguration } from "./OpenAIApiConfiguration.js";
|
12
12
|
import { failedOpenAICallResponseHandler } from "./OpenAIError.js";
|
13
13
|
import { TikTokenTokenizer } from "./TikTokenTokenizer.js";
|
@@ -346,17 +346,7 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
|
|
346
346
|
queue.close();
|
347
347
|
return;
|
348
348
|
}
|
349
|
-
const
|
350
|
-
const parseResult = textResponseStreamEventSchema.safeParse(json);
|
351
|
-
if (!parseResult.success) {
|
352
|
-
queue.push({
|
353
|
-
type: "error",
|
354
|
-
error: parseResult.error,
|
355
|
-
});
|
356
|
-
queue.close();
|
357
|
-
return;
|
358
|
-
}
|
359
|
-
const eventData = parseResult.data;
|
349
|
+
const eventData = parseJsonWithZod(data, textResponseStreamEventSchema);
|
360
350
|
for (let i = 0; i < eventData.choices.length; i++) {
|
361
351
|
const eventChoice = eventData.choices[i];
|
362
352
|
const delta = eventChoice.text;
|