modelfusion 0.95.0 → 0.97.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 +3 -3
- package/core/api/postToApi.cjs +30 -1
- package/core/api/postToApi.d.ts +7 -1
- package/core/api/postToApi.js +29 -1
- 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/mistral/MistralApiConfiguration.cjs +22 -0
- package/model-provider/mistral/MistralApiConfiguration.d.ts +12 -0
- package/model-provider/mistral/MistralApiConfiguration.js +18 -0
- package/model-provider/mistral/MistralError.cjs +17 -0
- package/model-provider/mistral/MistralError.d.ts +13 -0
- package/model-provider/mistral/MistralError.js +14 -0
- package/model-provider/mistral/MistralFacade.cjs +18 -0
- package/model-provider/mistral/MistralFacade.d.ts +6 -0
- package/model-provider/mistral/MistralFacade.js +12 -0
- package/model-provider/mistral/MistralPromptTemplate.cjs +64 -0
- package/model-provider/mistral/MistralPromptTemplate.d.ts +16 -0
- package/model-provider/mistral/MistralPromptTemplate.js +58 -0
- package/model-provider/mistral/MistralTextEmbeddingModel.cjs +100 -0
- package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +106 -0
- package/model-provider/mistral/MistralTextEmbeddingModel.js +96 -0
- package/model-provider/mistral/MistralTextGenerationModel.cjs +254 -0
- package/model-provider/mistral/MistralTextGenerationModel.d.ts +231 -0
- package/model-provider/mistral/MistralTextGenerationModel.js +250 -0
- package/model-provider/mistral/index.cjs +34 -0
- package/model-provider/mistral/index.d.ts +6 -0
- package/model-provider/mistral/index.js +5 -0
- package/model-provider/ollama/OllamaError.cjs +5 -30
- package/model-provider/ollama/OllamaError.js +5 -29
- package/model-provider/ollama/OllamaTextEmbeddingModel.cjs +1 -7
- package/model-provider/ollama/OllamaTextEmbeddingModel.d.ts +0 -1
- package/model-provider/ollama/OllamaTextEmbeddingModel.js +1 -7
- package/model-provider/ollama/OllamaTextGenerationModel.cjs +60 -57
- package/model-provider/ollama/OllamaTextGenerationModel.d.ts +33 -22
- package/model-provider/ollama/OllamaTextGenerationModel.js +60 -57
- package/model-provider/ollama/OllamaTextGenerationModel.test.cjs +2 -2
- package/model-provider/ollama/OllamaTextGenerationModel.test.js +2 -2
- package/model-provider/openai/OpenAICompletionModel.d.ts +4 -4
- package/model-provider/openai/OpenAIError.cjs +9 -34
- package/model-provider/openai/OpenAIError.d.ts +1 -3
- package/model-provider/openai/OpenAIError.js +9 -33
- package/model-provider/openai/chat/AbstractOpenAIChatModel.d.ts +6 -6
- package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts +1 -1
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.cjs +2 -1
- package/model-provider/whispercpp/WhisperCppTranscriptionModel.js +2 -1
- package/package.json +1 -1
@@ -0,0 +1,250 @@
|
|
1
|
+
import { z } from "zod";
|
2
|
+
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
|
+
import { createJsonResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
|
+
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
5
|
+
import { safeParseJSON } from "../../core/schema/parseJSON.js";
|
6
|
+
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
7
|
+
import { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
|
8
|
+
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
9
|
+
import { parseEventSourceStream } from "../../util/streaming/parseEventSourceStream.js";
|
10
|
+
import { MistralApiConfiguration } from "./MistralApiConfiguration.js";
|
11
|
+
import { failedMistralCallResponseHandler } from "./MistralError.js";
|
12
|
+
import { chat, instruction, text } from "./MistralPromptTemplate.js";
|
13
|
+
export class MistralTextGenerationModel extends AbstractModel {
|
14
|
+
constructor(settings) {
|
15
|
+
super({ settings });
|
16
|
+
Object.defineProperty(this, "provider", {
|
17
|
+
enumerable: true,
|
18
|
+
configurable: true,
|
19
|
+
writable: true,
|
20
|
+
value: "mistral"
|
21
|
+
});
|
22
|
+
Object.defineProperty(this, "contextWindowSize", {
|
23
|
+
enumerable: true,
|
24
|
+
configurable: true,
|
25
|
+
writable: true,
|
26
|
+
value: undefined
|
27
|
+
});
|
28
|
+
Object.defineProperty(this, "tokenizer", {
|
29
|
+
enumerable: true,
|
30
|
+
configurable: true,
|
31
|
+
writable: true,
|
32
|
+
value: undefined
|
33
|
+
});
|
34
|
+
Object.defineProperty(this, "countPromptTokens", {
|
35
|
+
enumerable: true,
|
36
|
+
configurable: true,
|
37
|
+
writable: true,
|
38
|
+
value: undefined
|
39
|
+
});
|
40
|
+
}
|
41
|
+
get modelName() {
|
42
|
+
return this.settings.model;
|
43
|
+
}
|
44
|
+
async callAPI(prompt, options) {
|
45
|
+
const { model, temperature, topP, safeMode, randomSeed, maxCompletionTokens, } = this.settings;
|
46
|
+
const api = this.settings.api ?? new MistralApiConfiguration();
|
47
|
+
const abortSignal = options.run?.abortSignal;
|
48
|
+
const stream = options.responseFormat.stream;
|
49
|
+
const successfulResponseHandler = options.responseFormat.handler;
|
50
|
+
return callWithRetryAndThrottle({
|
51
|
+
retry: api.retry,
|
52
|
+
throttle: api.throttle,
|
53
|
+
call: async () => postJsonToApi({
|
54
|
+
url: api.assembleUrl(`/chat/completions`),
|
55
|
+
headers: api.headers,
|
56
|
+
body: {
|
57
|
+
stream,
|
58
|
+
messages: prompt,
|
59
|
+
model,
|
60
|
+
temperature,
|
61
|
+
top_p: topP,
|
62
|
+
max_tokens: maxCompletionTokens,
|
63
|
+
safe_mode: safeMode,
|
64
|
+
random_seed: randomSeed,
|
65
|
+
},
|
66
|
+
failedResponseHandler: failedMistralCallResponseHandler,
|
67
|
+
successfulResponseHandler,
|
68
|
+
abortSignal,
|
69
|
+
}),
|
70
|
+
});
|
71
|
+
}
|
72
|
+
get settingsForEvent() {
|
73
|
+
const eventSettingProperties = [
|
74
|
+
"maxCompletionTokens",
|
75
|
+
"temperature",
|
76
|
+
"topP",
|
77
|
+
"safeMode",
|
78
|
+
"randomSeed",
|
79
|
+
];
|
80
|
+
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
81
|
+
}
|
82
|
+
async doGenerateText(prompt, options) {
|
83
|
+
const response = await this.callAPI(prompt, {
|
84
|
+
...options,
|
85
|
+
responseFormat: MistralTextGenerationResponseFormat.json,
|
86
|
+
});
|
87
|
+
return {
|
88
|
+
response,
|
89
|
+
text: response.choices[0].message.content,
|
90
|
+
};
|
91
|
+
}
|
92
|
+
doStreamText(prompt, options) {
|
93
|
+
return this.callAPI(prompt, {
|
94
|
+
...options,
|
95
|
+
responseFormat: MistralTextGenerationResponseFormat.textDeltaIterable,
|
96
|
+
});
|
97
|
+
}
|
98
|
+
/**
|
99
|
+
* Returns this model with a text prompt template.
|
100
|
+
*/
|
101
|
+
withTextPrompt() {
|
102
|
+
return this.withPromptTemplate(text());
|
103
|
+
}
|
104
|
+
/**
|
105
|
+
* Returns this model with an instruction prompt template.
|
106
|
+
*/
|
107
|
+
withInstructionPrompt() {
|
108
|
+
return this.withPromptTemplate(instruction());
|
109
|
+
}
|
110
|
+
/**
|
111
|
+
* Returns this model with a chat prompt template.
|
112
|
+
*/
|
113
|
+
withChatPrompt() {
|
114
|
+
return this.withPromptTemplate(chat());
|
115
|
+
}
|
116
|
+
withPromptTemplate(promptTemplate) {
|
117
|
+
return new PromptTemplateTextStreamingModel({
|
118
|
+
model: this,
|
119
|
+
promptTemplate,
|
120
|
+
});
|
121
|
+
}
|
122
|
+
withSettings(additionalSettings) {
|
123
|
+
return new MistralTextGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
124
|
+
}
|
125
|
+
}
|
126
|
+
const mistralTextGenerationResponseSchema = z.object({
|
127
|
+
id: z.string(),
|
128
|
+
object: z.string(),
|
129
|
+
created: z.number(),
|
130
|
+
model: z.string(),
|
131
|
+
choices: z.array(z.object({
|
132
|
+
index: z.number(),
|
133
|
+
message: z.object({
|
134
|
+
role: z.enum(["user", "assistant"]),
|
135
|
+
content: z.string(),
|
136
|
+
}),
|
137
|
+
finish_reason: z.enum(["stop", "length", "model_length"]),
|
138
|
+
})),
|
139
|
+
usage: z.object({
|
140
|
+
prompt_tokens: z.number(),
|
141
|
+
completion_tokens: z.number(),
|
142
|
+
total_tokens: z.number(),
|
143
|
+
}),
|
144
|
+
});
|
145
|
+
export const MistralTextGenerationResponseFormat = {
|
146
|
+
/**
|
147
|
+
* Returns the response as a JSON object.
|
148
|
+
*/
|
149
|
+
json: {
|
150
|
+
stream: false,
|
151
|
+
handler: createJsonResponseHandler(mistralTextGenerationResponseSchema),
|
152
|
+
},
|
153
|
+
/**
|
154
|
+
* Returns an async iterable over the text deltas (only the tex different of the first choice).
|
155
|
+
*/
|
156
|
+
textDeltaIterable: {
|
157
|
+
stream: true,
|
158
|
+
handler: async ({ response }) => createMistralTextGenerationDeltaIterableQueue(response.body, (delta) => delta[0]?.delta.content ?? ""),
|
159
|
+
},
|
160
|
+
};
|
161
|
+
const mistralTextGenerationChunkSchema = new ZodSchema(z.object({
|
162
|
+
id: z.string(),
|
163
|
+
object: z.string().optional(),
|
164
|
+
created: z.number().optional(),
|
165
|
+
model: z.string(),
|
166
|
+
choices: z.array(z.object({
|
167
|
+
index: z.number(),
|
168
|
+
delta: z.object({
|
169
|
+
role: z.enum(["assistant", "user"]).optional().nullable(),
|
170
|
+
content: z.string().nullable().optional(),
|
171
|
+
}),
|
172
|
+
finish_reason: z
|
173
|
+
.enum(["stop", "length", "model_length"])
|
174
|
+
.nullable()
|
175
|
+
.optional(),
|
176
|
+
})),
|
177
|
+
}));
|
178
|
+
async function createMistralTextGenerationDeltaIterableQueue(stream, extractDeltaValue) {
|
179
|
+
const queue = new AsyncQueue();
|
180
|
+
const streamDelta = [];
|
181
|
+
// process the stream asynchonously (no 'await' on purpose):
|
182
|
+
parseEventSourceStream({ stream })
|
183
|
+
.then(async (events) => {
|
184
|
+
try {
|
185
|
+
for await (const event of events) {
|
186
|
+
const data = event.data;
|
187
|
+
if (data === "[DONE]") {
|
188
|
+
queue.close();
|
189
|
+
return;
|
190
|
+
}
|
191
|
+
const parseResult = safeParseJSON({
|
192
|
+
text: data,
|
193
|
+
schema: mistralTextGenerationChunkSchema,
|
194
|
+
});
|
195
|
+
if (!parseResult.success) {
|
196
|
+
queue.push({
|
197
|
+
type: "error",
|
198
|
+
error: parseResult.error,
|
199
|
+
});
|
200
|
+
// Note: the queue is not closed on purpose. Some providers might add additional
|
201
|
+
// chunks that are not parsable, and ModelFusion should be resilient to that.
|
202
|
+
continue;
|
203
|
+
}
|
204
|
+
const completionChunk = parseResult.data;
|
205
|
+
for (let i = 0; i < completionChunk.choices.length; i++) {
|
206
|
+
const eventChoice = completionChunk.choices[i];
|
207
|
+
const delta = eventChoice.delta;
|
208
|
+
if (streamDelta[i] == null) {
|
209
|
+
streamDelta[i] = {
|
210
|
+
role: undefined,
|
211
|
+
content: "",
|
212
|
+
isComplete: false,
|
213
|
+
delta,
|
214
|
+
};
|
215
|
+
}
|
216
|
+
const choice = streamDelta[i];
|
217
|
+
choice.delta = delta;
|
218
|
+
if (eventChoice.finish_reason != null) {
|
219
|
+
choice.isComplete = true;
|
220
|
+
}
|
221
|
+
if (delta.content != undefined) {
|
222
|
+
choice.content += delta.content;
|
223
|
+
}
|
224
|
+
if (delta.role != undefined) {
|
225
|
+
choice.role = delta.role;
|
226
|
+
}
|
227
|
+
}
|
228
|
+
// Since we're mutating the choices array in an async scenario,
|
229
|
+
// we need to make a deep copy:
|
230
|
+
const streamDeltaDeepCopy = JSON.parse(JSON.stringify(streamDelta));
|
231
|
+
queue.push({
|
232
|
+
type: "delta",
|
233
|
+
fullDelta: streamDeltaDeepCopy,
|
234
|
+
valueDelta: extractDeltaValue(streamDeltaDeepCopy),
|
235
|
+
});
|
236
|
+
}
|
237
|
+
}
|
238
|
+
catch (error) {
|
239
|
+
queue.push({ type: "error", error });
|
240
|
+
queue.close();
|
241
|
+
return;
|
242
|
+
}
|
243
|
+
})
|
244
|
+
.catch((error) => {
|
245
|
+
queue.push({ type: "error", error });
|
246
|
+
queue.close();
|
247
|
+
return;
|
248
|
+
});
|
249
|
+
return queue;
|
250
|
+
}
|
@@ -0,0 +1,34 @@
|
|
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.MistralPrompt = exports.mistral = void 0;
|
30
|
+
__exportStar(require("./MistralApiConfiguration.cjs"), exports);
|
31
|
+
exports.mistral = __importStar(require("./MistralFacade.cjs"));
|
32
|
+
exports.MistralPrompt = __importStar(require("./MistralPromptTemplate.cjs"));
|
33
|
+
__exportStar(require("./MistralTextEmbeddingModel.cjs"), exports);
|
34
|
+
__exportStar(require("./MistralTextGenerationModel.cjs"), exports);
|
@@ -0,0 +1,6 @@
|
|
1
|
+
export * from "./MistralApiConfiguration.js";
|
2
|
+
export { MistralErrorData } from "./MistralError.js";
|
3
|
+
export * as mistral from "./MistralFacade.js";
|
4
|
+
export * as MistralPrompt from "./MistralPromptTemplate.js";
|
5
|
+
export * from "./MistralTextEmbeddingModel.js";
|
6
|
+
export * from "./MistralTextGenerationModel.js";
|
@@ -2,37 +2,12 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.failedOllamaCallResponseHandler = void 0;
|
4
4
|
const zod_1 = require("zod");
|
5
|
-
const
|
5
|
+
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
6
6
|
const ZodSchema_js_1 = require("../../core/schema/ZodSchema.cjs");
|
7
|
-
const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
|
8
7
|
const ollamaErrorDataSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.object({
|
9
8
|
error: zod_1.z.string(),
|
10
9
|
}));
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
const parsedError = (0, parseJSON_js_1.parseJSON)({
|
16
|
-
text: responseBody,
|
17
|
-
schema: ollamaErrorDataSchema,
|
18
|
-
});
|
19
|
-
return new ApiCallError_js_1.ApiCallError({
|
20
|
-
message: parsedError.error,
|
21
|
-
url,
|
22
|
-
requestBodyValues,
|
23
|
-
statusCode: response.status,
|
24
|
-
responseBody,
|
25
|
-
data: parsedError,
|
26
|
-
});
|
27
|
-
}
|
28
|
-
catch (parseError) {
|
29
|
-
return new ApiCallError_js_1.ApiCallError({
|
30
|
-
message: responseBody.trim() !== "" ? responseBody : response.statusText,
|
31
|
-
url,
|
32
|
-
requestBodyValues,
|
33
|
-
statusCode: response.status,
|
34
|
-
responseBody,
|
35
|
-
});
|
36
|
-
}
|
37
|
-
};
|
38
|
-
exports.failedOllamaCallResponseHandler = failedOllamaCallResponseHandler;
|
10
|
+
exports.failedOllamaCallResponseHandler = (0, postToApi_js_1.createJsonErrorResponseHandler)({
|
11
|
+
errorSchema: ollamaErrorDataSchema,
|
12
|
+
errorToMessage: (error) => error.error,
|
13
|
+
});
|
@@ -1,34 +1,10 @@
|
|
1
1
|
import { z } from "zod";
|
2
|
-
import {
|
2
|
+
import { createJsonErrorResponseHandler, } from "../../core/api/postToApi.js";
|
3
3
|
import { ZodSchema } from "../../core/schema/ZodSchema.js";
|
4
|
-
import { parseJSON } from "../../core/schema/parseJSON.js";
|
5
4
|
const ollamaErrorDataSchema = new ZodSchema(z.object({
|
6
5
|
error: z.string(),
|
7
6
|
}));
|
8
|
-
export const failedOllamaCallResponseHandler =
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
const parsedError = parseJSON({
|
13
|
-
text: responseBody,
|
14
|
-
schema: ollamaErrorDataSchema,
|
15
|
-
});
|
16
|
-
return new ApiCallError({
|
17
|
-
message: parsedError.error,
|
18
|
-
url,
|
19
|
-
requestBodyValues,
|
20
|
-
statusCode: response.status,
|
21
|
-
responseBody,
|
22
|
-
data: parsedError,
|
23
|
-
});
|
24
|
-
}
|
25
|
-
catch (parseError) {
|
26
|
-
return new ApiCallError({
|
27
|
-
message: responseBody.trim() !== "" ? responseBody : response.statusText,
|
28
|
-
url,
|
29
|
-
requestBodyValues,
|
30
|
-
statusCode: response.status,
|
31
|
-
responseBody,
|
32
|
-
});
|
33
|
-
}
|
34
|
-
};
|
7
|
+
export const failedOllamaCallResponseHandler = createJsonErrorResponseHandler({
|
8
|
+
errorSchema: ollamaErrorDataSchema,
|
9
|
+
errorToMessage: (error) => error.error,
|
10
|
+
});
|
@@ -22,12 +22,6 @@ class OllamaTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
22
22
|
writable: true,
|
23
23
|
value: 1
|
24
24
|
});
|
25
|
-
Object.defineProperty(this, "contextWindowSize", {
|
26
|
-
enumerable: true,
|
27
|
-
configurable: true,
|
28
|
-
writable: true,
|
29
|
-
value: undefined
|
30
|
-
});
|
31
25
|
}
|
32
26
|
get modelName() {
|
33
27
|
return null;
|
@@ -40,7 +34,7 @@ class OllamaTextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
|
|
40
34
|
}
|
41
35
|
async callAPI(texts, options) {
|
42
36
|
if (texts.length > this.maxValuesPerCall) {
|
43
|
-
throw new Error(`The
|
37
|
+
throw new Error(`The Ollama embedding API only supports ${this.maxValuesPerCall} texts per API call.`);
|
44
38
|
}
|
45
39
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
46
40
|
retry: this.settings.api?.retry,
|
@@ -15,7 +15,6 @@ export declare class OllamaTextEmbeddingModel extends AbstractModel<OllamaTextEm
|
|
15
15
|
get modelName(): null;
|
16
16
|
readonly maxValuesPerCall = 1;
|
17
17
|
get isParallelizable(): boolean;
|
18
|
-
readonly contextWindowSize: undefined;
|
19
18
|
get embeddingDimensions(): number | undefined;
|
20
19
|
callAPI(texts: Array<string>, options?: FunctionOptions): Promise<OllamaTextEmbeddingResponse>;
|
21
20
|
get settingsForEvent(): Partial<OllamaTextEmbeddingModelSettings>;
|
@@ -19,12 +19,6 @@ export class OllamaTextEmbeddingModel extends AbstractModel {
|
|
19
19
|
writable: true,
|
20
20
|
value: 1
|
21
21
|
});
|
22
|
-
Object.defineProperty(this, "contextWindowSize", {
|
23
|
-
enumerable: true,
|
24
|
-
configurable: true,
|
25
|
-
writable: true,
|
26
|
-
value: undefined
|
27
|
-
});
|
28
22
|
}
|
29
23
|
get modelName() {
|
30
24
|
return null;
|
@@ -37,7 +31,7 @@ export class OllamaTextEmbeddingModel extends AbstractModel {
|
|
37
31
|
}
|
38
32
|
async callAPI(texts, options) {
|
39
33
|
if (texts.length > this.maxValuesPerCall) {
|
40
|
-
throw new Error(`The
|
34
|
+
throw new Error(`The Ollama embedding API only supports ${this.maxValuesPerCall} texts per API call.`);
|
41
35
|
}
|
42
36
|
return callWithRetryAndThrottle({
|
43
37
|
retry: this.settings.api?.retry,
|
@@ -44,15 +44,47 @@ class OllamaTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
44
44
|
return this.settings.contextWindowSize;
|
45
45
|
}
|
46
46
|
async callAPI(prompt, options) {
|
47
|
+
const { responseFormat } = options;
|
48
|
+
const api = this.settings.api ?? new OllamaApiConfiguration_js_1.OllamaApiConfiguration();
|
49
|
+
const abortSignal = options.run?.abortSignal;
|
47
50
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
48
|
-
retry:
|
49
|
-
throttle:
|
50
|
-
call: async () =>
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
retry: api.retry,
|
52
|
+
throttle: api.throttle,
|
53
|
+
call: async () => (0, postToApi_js_1.postJsonToApi)({
|
54
|
+
url: api.assembleUrl(`/api/generate`),
|
55
|
+
headers: api.headers,
|
56
|
+
body: {
|
57
|
+
stream: responseFormat.stream,
|
58
|
+
model: this.settings.model,
|
59
|
+
prompt: prompt.prompt,
|
60
|
+
images: prompt.images,
|
61
|
+
format: this.settings.format,
|
62
|
+
options: {
|
63
|
+
mirostat: this.settings.mirostat,
|
64
|
+
mirostat_eta: this.settings.mirostatEta,
|
65
|
+
mirostat_tau: this.settings.mirostatTau,
|
66
|
+
num_ctx: this.settings.contextWindowSize,
|
67
|
+
num_gpu: this.settings.numGpu,
|
68
|
+
num_gqa: this.settings.numGqa,
|
69
|
+
num_predict: this.settings.maxCompletionTokens,
|
70
|
+
num_threads: this.settings.numThreads,
|
71
|
+
repeat_last_n: this.settings.repeatLastN,
|
72
|
+
repeat_penalty: this.settings.repeatPenalty,
|
73
|
+
seed: this.settings.seed,
|
74
|
+
stop: this.settings.stopSequences,
|
75
|
+
temperature: this.settings.temperature,
|
76
|
+
tfs_z: this.settings.tfsZ,
|
77
|
+
top_k: this.settings.topK,
|
78
|
+
top_p: this.settings.topP,
|
79
|
+
},
|
80
|
+
system: this.settings.system,
|
81
|
+
template: this.settings.template,
|
82
|
+
context: this.settings.context,
|
83
|
+
raw: this.settings.raw,
|
84
|
+
},
|
85
|
+
failedResponseHandler: OllamaError_js_1.failedOllamaCallResponseHandler,
|
86
|
+
successfulResponseHandler: responseFormat.handler,
|
87
|
+
abortSignal,
|
56
88
|
}),
|
57
89
|
});
|
58
90
|
}
|
@@ -63,17 +95,17 @@ class OllamaTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
63
95
|
"contextWindowSize",
|
64
96
|
"temperature",
|
65
97
|
"mirostat",
|
66
|
-
"
|
67
|
-
"
|
68
|
-
"
|
69
|
-
"
|
70
|
-
"
|
71
|
-
"
|
72
|
-
"
|
98
|
+
"mirostatEta",
|
99
|
+
"mirostatTau",
|
100
|
+
"numGqa",
|
101
|
+
"numGpu",
|
102
|
+
"numThreads",
|
103
|
+
"repeatLastN",
|
104
|
+
"repeatPenalty",
|
73
105
|
"seed",
|
74
|
-
"
|
75
|
-
"
|
76
|
-
"
|
106
|
+
"tfsZ",
|
107
|
+
"topK",
|
108
|
+
"topP",
|
77
109
|
"system",
|
78
110
|
"template",
|
79
111
|
"context",
|
@@ -110,6 +142,14 @@ class OllamaTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
110
142
|
template: promptTemplate,
|
111
143
|
});
|
112
144
|
}
|
145
|
+
withTextPrompt() {
|
146
|
+
return this.withPromptTemplate({
|
147
|
+
format(prompt) {
|
148
|
+
return { prompt: prompt };
|
149
|
+
},
|
150
|
+
stopSequences: [],
|
151
|
+
});
|
152
|
+
}
|
113
153
|
withPromptTemplate(promptTemplate) {
|
114
154
|
return new PromptTemplateTextStreamingModel_js_1.PromptTemplateTextStreamingModel({
|
115
155
|
model: this.withSettings({
|
@@ -131,7 +171,7 @@ const ollamaTextGenerationResponseSchema = zod_1.z.object({
|
|
131
171
|
model: zod_1.z.string(),
|
132
172
|
response: zod_1.z.string(),
|
133
173
|
total_duration: zod_1.z.number(),
|
134
|
-
load_duration: zod_1.z.number(),
|
174
|
+
load_duration: zod_1.z.number().optional(),
|
135
175
|
prompt_eval_count: zod_1.z.number(),
|
136
176
|
eval_count: zod_1.z.number(),
|
137
177
|
eval_duration: zod_1.z.number(),
|
@@ -149,7 +189,7 @@ const ollamaTextStreamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.d
|
|
149
189
|
model: zod_1.z.string(),
|
150
190
|
created_at: zod_1.z.string(),
|
151
191
|
total_duration: zod_1.z.number(),
|
152
|
-
load_duration: zod_1.z.number(),
|
192
|
+
load_duration: zod_1.z.number().optional(),
|
153
193
|
sample_count: zod_1.z.number().optional(),
|
154
194
|
sample_duration: zod_1.z.number().optional(),
|
155
195
|
prompt_eval_count: zod_1.z.number(),
|
@@ -159,43 +199,6 @@ const ollamaTextStreamingResponseSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.d
|
|
159
199
|
context: zod_1.z.array(zod_1.z.number()).optional(),
|
160
200
|
}),
|
161
201
|
]));
|
162
|
-
async function callOllamaTextGenerationAPI({ api = new OllamaApiConfiguration_js_1.OllamaApiConfiguration(), abortSignal, responseFormat, prompt, model, format, contextWindowSize, maxCompletionTokens, mirostat, mirostat_eta, mirostat_tau, num_gpu, num_gqa, num_threads, repeat_last_n, repeat_penalty, seed, stopSequences, temperature, tfs_z, top_k, top_p, system, template, context, raw, }) {
|
163
|
-
return (0, postToApi_js_1.postJsonToApi)({
|
164
|
-
url: api.assembleUrl(`/api/generate`),
|
165
|
-
headers: api.headers,
|
166
|
-
body: {
|
167
|
-
stream: responseFormat.stream,
|
168
|
-
model,
|
169
|
-
prompt,
|
170
|
-
format,
|
171
|
-
options: {
|
172
|
-
mirostat,
|
173
|
-
mirostat_eta,
|
174
|
-
mirostat_tau,
|
175
|
-
num_ctx: contextWindowSize,
|
176
|
-
num_gpu,
|
177
|
-
num_gqa,
|
178
|
-
num_predict: maxCompletionTokens,
|
179
|
-
num_threads,
|
180
|
-
repeat_last_n,
|
181
|
-
repeat_penalty,
|
182
|
-
seed,
|
183
|
-
stop: stopSequences,
|
184
|
-
temperature,
|
185
|
-
tfs_z,
|
186
|
-
top_k,
|
187
|
-
top_p,
|
188
|
-
},
|
189
|
-
system,
|
190
|
-
template,
|
191
|
-
context,
|
192
|
-
raw,
|
193
|
-
},
|
194
|
-
failedResponseHandler: OllamaError_js_1.failedOllamaCallResponseHandler,
|
195
|
-
successfulResponseHandler: responseFormat.handler,
|
196
|
-
abortSignal,
|
197
|
-
});
|
198
|
-
}
|
199
202
|
async function createOllamaFullDeltaIterableQueue(stream) {
|
200
203
|
const queue = new AsyncQueue_js_1.AsyncQueue();
|
201
204
|
let accumulatedText = "";
|