modelfusion 0.99.0 → 0.101.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/CHANGELOG.md +1411 -0
- package/README.md +9 -15
- package/core/api/BaseUrlApiConfiguration.d.ts +7 -6
- package/core/api/BaseUrlPartsApiConfiguration.cjs +53 -0
- package/core/api/BaseUrlPartsApiConfiguration.d.ts +26 -0
- package/core/api/BaseUrlPartsApiConfiguration.js +49 -0
- package/core/api/index.cjs +1 -0
- package/core/api/index.d.ts +1 -0
- package/core/api/index.js +1 -0
- package/guard/fixStructure.cjs +3 -3
- package/guard/fixStructure.d.ts +3 -3
- package/guard/fixStructure.js +3 -3
- package/model-function/generate-structure/generateStructure.d.ts +2 -2
- package/model-function/generate-structure/streamStructure.d.ts +1 -1
- package/model-provider/automatic1111/Automatic1111ApiConfiguration.cjs +8 -9
- package/model-provider/automatic1111/Automatic1111ApiConfiguration.d.ts +7 -9
- package/model-provider/automatic1111/Automatic1111ApiConfiguration.js +8 -9
- package/model-provider/automatic1111/Automatic1111Error.cjs +7 -31
- package/model-provider/automatic1111/Automatic1111Error.d.ts +2 -11
- package/model-provider/automatic1111/Automatic1111Error.js +6 -28
- package/model-provider/automatic1111/Automatic1111Facade.cjs +10 -1
- package/model-provider/automatic1111/Automatic1111Facade.d.ts +7 -0
- package/model-provider/automatic1111/Automatic1111Facade.js +8 -0
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +22 -27
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +8 -8
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +22 -27
- package/model-provider/automatic1111/index.cjs +1 -3
- package/model-provider/automatic1111/index.d.ts +1 -1
- package/model-provider/automatic1111/index.js +0 -1
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +6 -6
- package/model-provider/mistral/{MistralTextGenerationModel.cjs → MistralChatModel.cjs} +13 -13
- package/model-provider/mistral/{MistralTextGenerationModel.d.ts → MistralChatModel.d.ts} +21 -20
- package/model-provider/mistral/{MistralTextGenerationModel.js → MistralChatModel.js} +11 -11
- package/model-provider/mistral/MistralFacade.cjs +5 -5
- package/model-provider/mistral/MistralFacade.d.ts +3 -2
- package/model-provider/mistral/MistralFacade.js +3 -3
- package/model-provider/mistral/MistralPromptTemplate.d.ts +4 -4
- package/model-provider/mistral/index.cjs +1 -1
- package/model-provider/mistral/index.d.ts +1 -1
- package/model-provider/mistral/index.js +1 -1
- package/model-provider/ollama/OllamaApiConfiguration.d.ts +6 -5
- package/model-provider/ollama/OllamaChatModel.cjs +303 -0
- package/model-provider/ollama/OllamaChatModel.d.ts +171 -0
- package/model-provider/ollama/OllamaChatModel.js +299 -0
- package/model-provider/ollama/OllamaChatPromptTemplate.cjs +76 -0
- package/model-provider/ollama/OllamaChatPromptTemplate.d.ts +20 -0
- package/model-provider/ollama/OllamaChatPromptTemplate.js +69 -0
- package/model-provider/ollama/{OllamaTextGenerationModel.cjs → OllamaCompletionModel.cjs} +13 -11
- package/model-provider/ollama/OllamaCompletionModel.d.ts +159 -0
- package/model-provider/ollama/{OllamaTextGenerationModel.js → OllamaCompletionModel.js} +11 -9
- package/model-provider/ollama/{OllamaTextGenerationModel.test.cjs → OllamaCompletionModel.test.cjs} +3 -3
- package/model-provider/ollama/{OllamaTextGenerationModel.test.js → OllamaCompletionModel.test.js} +3 -3
- package/model-provider/ollama/OllamaFacade.cjs +15 -5
- package/model-provider/ollama/OllamaFacade.d.ts +7 -2
- package/model-provider/ollama/OllamaFacade.js +11 -3
- package/model-provider/ollama/OllamaTextGenerationSettings.cjs +2 -0
- package/model-provider/ollama/OllamaTextGenerationSettings.d.ts +87 -0
- package/model-provider/ollama/OllamaTextGenerationSettings.js +1 -0
- package/model-provider/ollama/index.cjs +4 -1
- package/model-provider/ollama/index.d.ts +4 -1
- package/model-provider/ollama/index.js +4 -1
- package/model-provider/openai/OpenAIFacade.cjs +4 -2
- package/model-provider/openai/OpenAIFacade.d.ts +3 -1
- package/model-provider/openai/OpenAIFacade.js +2 -1
- package/model-provider/openai/chat/AbstractOpenAIChatModel.d.ts +1 -1
- package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts +3 -3
- package/model-provider/openai/chat/OpenAIChatModel.cjs +1 -1
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +2 -2
- package/model-provider/openai/chat/OpenAIChatModel.js +1 -1
- package/model-provider/openai/chat/OpenAIChatPromptTemplate.d.ts +5 -5
- package/model-provider/openai/chat/OpenAIChatPromptTemplate.js +1 -1
- package/model-provider/openai-compatible/OpenAICompatibleFacade.cjs +1 -1
- package/model-provider/openai-compatible/OpenAICompatibleFacade.d.ts +1 -1
- package/model-provider/openai-compatible/OpenAICompatibleFacade.js +1 -1
- package/package.json +15 -15
- package/model-provider/ollama/OllamaTextGenerationModel.d.ts +0 -230
- /package/model-provider/ollama/{OllamaTextGenerationModel.test.d.ts → OllamaCompletionModel.test.d.ts} +0 -0
@@ -0,0 +1,299 @@
|
|
1
|
+
import { z } from "zod";
|
2
|
+
import { ApiCallError } from "../../core/api/ApiCallError.js";
|
3
|
+
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
4
|
+
import { postJsonToApi } 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 { PromptTemplateTextStreamingModel } from "../../model-function/generate-text/PromptTemplateTextStreamingModel.js";
|
9
|
+
import { TextGenerationToolCallModel, } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
|
10
|
+
import { TextGenerationToolCallsOrGenerateTextModel, } from "../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js";
|
11
|
+
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
12
|
+
import { parseJsonStream } from "../../util/streaming/parseJsonStream.js";
|
13
|
+
import { OllamaApiConfiguration } from "./OllamaApiConfiguration.js";
|
14
|
+
import { chat, instruction, text } from "./OllamaChatPromptTemplate.js";
|
15
|
+
import { failedOllamaCallResponseHandler } from "./OllamaError.js";
|
16
|
+
/**
|
17
|
+
* Text generation model that uses the Ollama chat API.
|
18
|
+
*/
|
19
|
+
export class OllamaChatModel extends AbstractModel {
|
20
|
+
constructor(settings) {
|
21
|
+
super({ settings });
|
22
|
+
Object.defineProperty(this, "provider", {
|
23
|
+
enumerable: true,
|
24
|
+
configurable: true,
|
25
|
+
writable: true,
|
26
|
+
value: "ollama"
|
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
|
+
Object.defineProperty(this, "contextWindowSize", {
|
41
|
+
enumerable: true,
|
42
|
+
configurable: true,
|
43
|
+
writable: true,
|
44
|
+
value: undefined
|
45
|
+
});
|
46
|
+
}
|
47
|
+
get modelName() {
|
48
|
+
return this.settings.model;
|
49
|
+
}
|
50
|
+
async callAPI(prompt, options) {
|
51
|
+
const { responseFormat } = options;
|
52
|
+
const api = this.settings.api ?? new OllamaApiConfiguration();
|
53
|
+
const abortSignal = options.run?.abortSignal;
|
54
|
+
return callWithRetryAndThrottle({
|
55
|
+
retry: api.retry,
|
56
|
+
throttle: api.throttle,
|
57
|
+
call: async () => postJsonToApi({
|
58
|
+
url: api.assembleUrl(`/api/chat`),
|
59
|
+
headers: api.headers,
|
60
|
+
body: {
|
61
|
+
stream: responseFormat.stream,
|
62
|
+
model: this.settings.model,
|
63
|
+
messages: prompt,
|
64
|
+
format: this.settings.format,
|
65
|
+
options: {
|
66
|
+
mirostat: this.settings.mirostat,
|
67
|
+
mirostat_eta: this.settings.mirostatEta,
|
68
|
+
mirostat_tau: this.settings.mirostatTau,
|
69
|
+
num_gpu: this.settings.numGpu,
|
70
|
+
num_gqa: this.settings.numGqa,
|
71
|
+
num_predict: this.settings.maxGenerationTokens,
|
72
|
+
num_threads: this.settings.numThreads,
|
73
|
+
repeat_last_n: this.settings.repeatLastN,
|
74
|
+
repeat_penalty: this.settings.repeatPenalty,
|
75
|
+
seed: this.settings.seed,
|
76
|
+
stop: this.settings.stopSequences,
|
77
|
+
temperature: this.settings.temperature,
|
78
|
+
tfs_z: this.settings.tfsZ,
|
79
|
+
top_k: this.settings.topK,
|
80
|
+
top_p: this.settings.topP,
|
81
|
+
},
|
82
|
+
template: this.settings.template,
|
83
|
+
},
|
84
|
+
failedResponseHandler: failedOllamaCallResponseHandler,
|
85
|
+
successfulResponseHandler: responseFormat.handler,
|
86
|
+
abortSignal,
|
87
|
+
}),
|
88
|
+
});
|
89
|
+
}
|
90
|
+
get settingsForEvent() {
|
91
|
+
const eventSettingProperties = [
|
92
|
+
"maxGenerationTokens",
|
93
|
+
"stopSequences",
|
94
|
+
"temperature",
|
95
|
+
"mirostat",
|
96
|
+
"mirostatEta",
|
97
|
+
"mirostatTau",
|
98
|
+
"numGqa",
|
99
|
+
"numGpu",
|
100
|
+
"numThreads",
|
101
|
+
"repeatLastN",
|
102
|
+
"repeatPenalty",
|
103
|
+
"seed",
|
104
|
+
"tfsZ",
|
105
|
+
"topK",
|
106
|
+
"topP",
|
107
|
+
"template",
|
108
|
+
"format",
|
109
|
+
];
|
110
|
+
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
111
|
+
}
|
112
|
+
async doGenerateTexts(prompt, options) {
|
113
|
+
const response = await this.callAPI(prompt, {
|
114
|
+
...options,
|
115
|
+
responseFormat: OllamaChatResponseFormat.json,
|
116
|
+
});
|
117
|
+
return {
|
118
|
+
response,
|
119
|
+
texts: [response.message.content],
|
120
|
+
};
|
121
|
+
}
|
122
|
+
doStreamText(prompt, options) {
|
123
|
+
return this.callAPI(prompt, {
|
124
|
+
...options,
|
125
|
+
responseFormat: OllamaChatResponseFormat.deltaIterable,
|
126
|
+
});
|
127
|
+
}
|
128
|
+
asToolCallGenerationModel(promptTemplate) {
|
129
|
+
return new TextGenerationToolCallModel({
|
130
|
+
model: this,
|
131
|
+
format: promptTemplate,
|
132
|
+
});
|
133
|
+
}
|
134
|
+
asToolCallsOrTextGenerationModel(promptTemplate) {
|
135
|
+
return new TextGenerationToolCallsOrGenerateTextModel({
|
136
|
+
model: this,
|
137
|
+
template: promptTemplate,
|
138
|
+
});
|
139
|
+
}
|
140
|
+
/**
|
141
|
+
* Returns this model with a text prompt template.
|
142
|
+
*/
|
143
|
+
withTextPrompt() {
|
144
|
+
return this.withPromptTemplate(text());
|
145
|
+
}
|
146
|
+
/**
|
147
|
+
* Returns this model with an instruction prompt template.
|
148
|
+
*/
|
149
|
+
withInstructionPrompt() {
|
150
|
+
return this.withPromptTemplate(instruction());
|
151
|
+
}
|
152
|
+
/**
|
153
|
+
* Returns this model with a chat prompt template.
|
154
|
+
*/
|
155
|
+
withChatPrompt() {
|
156
|
+
return this.withPromptTemplate(chat());
|
157
|
+
}
|
158
|
+
withPromptTemplate(promptTemplate) {
|
159
|
+
return new PromptTemplateTextStreamingModel({
|
160
|
+
model: this.withSettings({
|
161
|
+
stopSequences: [
|
162
|
+
...(this.settings.stopSequences ?? []),
|
163
|
+
...promptTemplate.stopSequences,
|
164
|
+
],
|
165
|
+
}),
|
166
|
+
promptTemplate,
|
167
|
+
});
|
168
|
+
}
|
169
|
+
withSettings(additionalSettings) {
|
170
|
+
return new OllamaChatModel(Object.assign({}, this.settings, additionalSettings));
|
171
|
+
}
|
172
|
+
}
|
173
|
+
const ollamaChatResponseSchema = z.object({
|
174
|
+
model: z.string(),
|
175
|
+
created_at: z.string(),
|
176
|
+
done: z.literal(true),
|
177
|
+
message: z.object({
|
178
|
+
role: z.string(),
|
179
|
+
content: z.string(),
|
180
|
+
}),
|
181
|
+
total_duration: z.number(),
|
182
|
+
load_duration: z.number().optional(),
|
183
|
+
prompt_eval_count: z.number(),
|
184
|
+
prompt_eval_duration: z.number().optional(),
|
185
|
+
eval_count: z.number(),
|
186
|
+
eval_duration: z.number(),
|
187
|
+
});
|
188
|
+
const ollamaChatStreamSchema = new ZodSchema(z.discriminatedUnion("done", [
|
189
|
+
z.object({
|
190
|
+
done: z.literal(false),
|
191
|
+
model: z.string(),
|
192
|
+
created_at: z.string(),
|
193
|
+
message: z.object({
|
194
|
+
role: z.string(),
|
195
|
+
content: z.string(),
|
196
|
+
}),
|
197
|
+
}),
|
198
|
+
z.object({
|
199
|
+
done: z.literal(true),
|
200
|
+
model: z.string(),
|
201
|
+
created_at: z.string(),
|
202
|
+
total_duration: z.number(),
|
203
|
+
load_duration: z.number().optional(),
|
204
|
+
prompt_eval_count: z.number(),
|
205
|
+
prompt_eval_duration: z.number().optional(),
|
206
|
+
eval_count: z.number(),
|
207
|
+
eval_duration: z.number(),
|
208
|
+
}),
|
209
|
+
]));
|
210
|
+
async function createOllamaFullDeltaIterableQueue(stream) {
|
211
|
+
const queue = new AsyncQueue();
|
212
|
+
let accumulatedText = "";
|
213
|
+
// process the stream asynchonously (no 'await' on purpose):
|
214
|
+
parseJsonStream({
|
215
|
+
stream,
|
216
|
+
schema: ollamaChatStreamSchema,
|
217
|
+
process(event) {
|
218
|
+
if (event.done === true) {
|
219
|
+
queue.push({
|
220
|
+
type: "delta",
|
221
|
+
fullDelta: {
|
222
|
+
content: accumulatedText,
|
223
|
+
isComplete: true,
|
224
|
+
delta: "",
|
225
|
+
},
|
226
|
+
valueDelta: "",
|
227
|
+
});
|
228
|
+
}
|
229
|
+
else {
|
230
|
+
const deltaText = event.message.content;
|
231
|
+
accumulatedText += deltaText;
|
232
|
+
queue.push({
|
233
|
+
type: "delta",
|
234
|
+
fullDelta: {
|
235
|
+
content: accumulatedText,
|
236
|
+
isComplete: false,
|
237
|
+
delta: deltaText,
|
238
|
+
},
|
239
|
+
valueDelta: deltaText,
|
240
|
+
});
|
241
|
+
}
|
242
|
+
},
|
243
|
+
onDone() {
|
244
|
+
queue.close();
|
245
|
+
},
|
246
|
+
});
|
247
|
+
return queue;
|
248
|
+
}
|
249
|
+
export const OllamaChatResponseFormat = {
|
250
|
+
/**
|
251
|
+
* Returns the response as a JSON object.
|
252
|
+
*/
|
253
|
+
json: {
|
254
|
+
stream: false,
|
255
|
+
handler: (async ({ response, url, requestBodyValues }) => {
|
256
|
+
const responseBody = await response.text();
|
257
|
+
const parsedResult = safeParseJSON({
|
258
|
+
text: responseBody,
|
259
|
+
schema: new ZodSchema(z.union([
|
260
|
+
ollamaChatResponseSchema,
|
261
|
+
z.object({
|
262
|
+
done: z.literal(false),
|
263
|
+
model: z.string(),
|
264
|
+
created_at: z.string(),
|
265
|
+
}),
|
266
|
+
])),
|
267
|
+
});
|
268
|
+
if (!parsedResult.success) {
|
269
|
+
throw new ApiCallError({
|
270
|
+
message: "Invalid JSON response",
|
271
|
+
cause: parsedResult.error,
|
272
|
+
statusCode: response.status,
|
273
|
+
responseBody,
|
274
|
+
url,
|
275
|
+
requestBodyValues,
|
276
|
+
});
|
277
|
+
}
|
278
|
+
if (parsedResult.data.done === false) {
|
279
|
+
throw new ApiCallError({
|
280
|
+
message: "Incomplete Ollama response received",
|
281
|
+
statusCode: response.status,
|
282
|
+
responseBody,
|
283
|
+
url,
|
284
|
+
requestBodyValues,
|
285
|
+
isRetryable: true,
|
286
|
+
});
|
287
|
+
}
|
288
|
+
return parsedResult.data;
|
289
|
+
}),
|
290
|
+
},
|
291
|
+
/**
|
292
|
+
* Returns an async iterable over the full deltas (all choices, including full current state at time of event)
|
293
|
+
* of the response stream.
|
294
|
+
*/
|
295
|
+
deltaIterable: {
|
296
|
+
stream: true,
|
297
|
+
handler: async ({ response }) => createOllamaFullDeltaIterableQueue(response.body),
|
298
|
+
},
|
299
|
+
};
|
@@ -0,0 +1,76 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.chat = exports.instruction = exports.text = exports.identity = void 0;
|
4
|
+
const ChatPrompt_js_1 = require("../../model-function/generate-text/prompt-template/ChatPrompt.cjs");
|
5
|
+
/**
|
6
|
+
* OllamaChatPrompt identity chat format.
|
7
|
+
*/
|
8
|
+
function identity() {
|
9
|
+
return { format: (prompt) => prompt, stopSequences: [] };
|
10
|
+
}
|
11
|
+
exports.identity = identity;
|
12
|
+
/**
|
13
|
+
* Formats a text prompt as an Ollama chat prompt.
|
14
|
+
*/
|
15
|
+
function text() {
|
16
|
+
return {
|
17
|
+
format: (prompt) => [{ role: "user", content: prompt }],
|
18
|
+
stopSequences: [],
|
19
|
+
};
|
20
|
+
}
|
21
|
+
exports.text = text;
|
22
|
+
/**
|
23
|
+
* Formats an instruction prompt as an Ollama chat prompt.
|
24
|
+
*/
|
25
|
+
function instruction() {
|
26
|
+
return {
|
27
|
+
format(prompt) {
|
28
|
+
const messages = [];
|
29
|
+
if (prompt.system != null) {
|
30
|
+
messages.push({ role: "system", content: prompt.system });
|
31
|
+
}
|
32
|
+
messages.push({ role: "user", ...extractContent(prompt.instruction) });
|
33
|
+
return messages;
|
34
|
+
},
|
35
|
+
stopSequences: [],
|
36
|
+
};
|
37
|
+
}
|
38
|
+
exports.instruction = instruction;
|
39
|
+
/**
|
40
|
+
* Formats a chat prompt as an Ollama chat prompt.
|
41
|
+
*/
|
42
|
+
function chat() {
|
43
|
+
return {
|
44
|
+
format(prompt) {
|
45
|
+
(0, ChatPrompt_js_1.validateChatPrompt)(prompt);
|
46
|
+
const messages = [];
|
47
|
+
if (prompt.system != null) {
|
48
|
+
messages.push({ role: "system", content: prompt.system });
|
49
|
+
}
|
50
|
+
for (const { role, content } of prompt.messages) {
|
51
|
+
messages.push({ role, ...extractContent(content) });
|
52
|
+
}
|
53
|
+
return messages;
|
54
|
+
},
|
55
|
+
stopSequences: [],
|
56
|
+
};
|
57
|
+
}
|
58
|
+
exports.chat = chat;
|
59
|
+
function extractContent(input) {
|
60
|
+
if (typeof input === "string") {
|
61
|
+
return { content: input, images: undefined };
|
62
|
+
}
|
63
|
+
else {
|
64
|
+
const images = [];
|
65
|
+
let content = "";
|
66
|
+
for (const part of input) {
|
67
|
+
if (part.type === "text") {
|
68
|
+
content += part.text;
|
69
|
+
}
|
70
|
+
else {
|
71
|
+
images.push(part.base64Image);
|
72
|
+
}
|
73
|
+
}
|
74
|
+
return { content, images };
|
75
|
+
}
|
76
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { TextGenerationPromptTemplate } from "../../model-function/generate-text/TextGenerationPromptTemplate.js";
|
2
|
+
import { MultiModalChatPrompt, TextChatPrompt } from "../../model-function/generate-text/prompt-template/ChatPrompt.js";
|
3
|
+
import { MultiModalInstructionPrompt, TextInstructionPrompt } from "../../model-function/generate-text/prompt-template/InstructionPrompt.js";
|
4
|
+
import { OllamaChatPrompt } from "./OllamaChatModel.js";
|
5
|
+
/**
|
6
|
+
* OllamaChatPrompt identity chat format.
|
7
|
+
*/
|
8
|
+
export declare function identity(): TextGenerationPromptTemplate<OllamaChatPrompt, OllamaChatPrompt>;
|
9
|
+
/**
|
10
|
+
* Formats a text prompt as an Ollama chat prompt.
|
11
|
+
*/
|
12
|
+
export declare function text(): TextGenerationPromptTemplate<string, OllamaChatPrompt>;
|
13
|
+
/**
|
14
|
+
* Formats an instruction prompt as an Ollama chat prompt.
|
15
|
+
*/
|
16
|
+
export declare function instruction(): TextGenerationPromptTemplate<MultiModalInstructionPrompt | TextInstructionPrompt, OllamaChatPrompt>;
|
17
|
+
/**
|
18
|
+
* Formats a chat prompt as an Ollama chat prompt.
|
19
|
+
*/
|
20
|
+
export declare function chat(): TextGenerationPromptTemplate<MultiModalChatPrompt | TextChatPrompt, OllamaChatPrompt>;
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import { validateChatPrompt, } from "../../model-function/generate-text/prompt-template/ChatPrompt.js";
|
2
|
+
/**
|
3
|
+
* OllamaChatPrompt identity chat format.
|
4
|
+
*/
|
5
|
+
export function identity() {
|
6
|
+
return { format: (prompt) => prompt, stopSequences: [] };
|
7
|
+
}
|
8
|
+
/**
|
9
|
+
* Formats a text prompt as an Ollama chat prompt.
|
10
|
+
*/
|
11
|
+
export function text() {
|
12
|
+
return {
|
13
|
+
format: (prompt) => [{ role: "user", content: prompt }],
|
14
|
+
stopSequences: [],
|
15
|
+
};
|
16
|
+
}
|
17
|
+
/**
|
18
|
+
* Formats an instruction prompt as an Ollama chat prompt.
|
19
|
+
*/
|
20
|
+
export function instruction() {
|
21
|
+
return {
|
22
|
+
format(prompt) {
|
23
|
+
const messages = [];
|
24
|
+
if (prompt.system != null) {
|
25
|
+
messages.push({ role: "system", content: prompt.system });
|
26
|
+
}
|
27
|
+
messages.push({ role: "user", ...extractContent(prompt.instruction) });
|
28
|
+
return messages;
|
29
|
+
},
|
30
|
+
stopSequences: [],
|
31
|
+
};
|
32
|
+
}
|
33
|
+
/**
|
34
|
+
* Formats a chat prompt as an Ollama chat prompt.
|
35
|
+
*/
|
36
|
+
export function chat() {
|
37
|
+
return {
|
38
|
+
format(prompt) {
|
39
|
+
validateChatPrompt(prompt);
|
40
|
+
const messages = [];
|
41
|
+
if (prompt.system != null) {
|
42
|
+
messages.push({ role: "system", content: prompt.system });
|
43
|
+
}
|
44
|
+
for (const { role, content } of prompt.messages) {
|
45
|
+
messages.push({ role, ...extractContent(content) });
|
46
|
+
}
|
47
|
+
return messages;
|
48
|
+
},
|
49
|
+
stopSequences: [],
|
50
|
+
};
|
51
|
+
}
|
52
|
+
function extractContent(input) {
|
53
|
+
if (typeof input === "string") {
|
54
|
+
return { content: input, images: undefined };
|
55
|
+
}
|
56
|
+
else {
|
57
|
+
const images = [];
|
58
|
+
let content = "";
|
59
|
+
for (const part of input) {
|
60
|
+
if (part.type === "text") {
|
61
|
+
content += part.text;
|
62
|
+
}
|
63
|
+
else {
|
64
|
+
images.push(part.base64Image);
|
65
|
+
}
|
66
|
+
}
|
67
|
+
return { content, images };
|
68
|
+
}
|
69
|
+
}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.OllamaCompletionResponseFormat = exports.OllamaCompletionModel = void 0;
|
4
4
|
const zod_1 = require("zod");
|
5
5
|
const ApiCallError_js_1 = require("../../core/api/ApiCallError.cjs");
|
6
6
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
@@ -15,7 +15,7 @@ const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
|
15
15
|
const parseJsonStream_js_1 = require("../../util/streaming/parseJsonStream.cjs");
|
16
16
|
const OllamaApiConfiguration_js_1 = require("./OllamaApiConfiguration.cjs");
|
17
17
|
const OllamaError_js_1 = require("./OllamaError.cjs");
|
18
|
-
class
|
18
|
+
class OllamaCompletionModel extends AbstractModel_js_1.AbstractModel {
|
19
19
|
constructor(settings) {
|
20
20
|
super({ settings });
|
21
21
|
Object.defineProperty(this, "provider", {
|
@@ -117,7 +117,7 @@ class OllamaTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
117
117
|
async doGenerateTexts(prompt, options) {
|
118
118
|
const response = await this.callAPI(prompt, {
|
119
119
|
...options,
|
120
|
-
responseFormat: exports.
|
120
|
+
responseFormat: exports.OllamaCompletionResponseFormat.json,
|
121
121
|
});
|
122
122
|
return {
|
123
123
|
response,
|
@@ -127,7 +127,7 @@ class OllamaTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
127
127
|
doStreamText(prompt, options) {
|
128
128
|
return this.callAPI(prompt, {
|
129
129
|
...options,
|
130
|
-
responseFormat: exports.
|
130
|
+
responseFormat: exports.OllamaCompletionResponseFormat.deltaIterable,
|
131
131
|
});
|
132
132
|
}
|
133
133
|
asToolCallGenerationModel(promptTemplate) {
|
@@ -162,22 +162,24 @@ class OllamaTextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
162
162
|
});
|
163
163
|
}
|
164
164
|
withSettings(additionalSettings) {
|
165
|
-
return new
|
165
|
+
return new OllamaCompletionModel(Object.assign({}, this.settings, additionalSettings));
|
166
166
|
}
|
167
167
|
}
|
168
|
-
exports.
|
169
|
-
const
|
168
|
+
exports.OllamaCompletionModel = OllamaCompletionModel;
|
169
|
+
const ollamaCompletionResponseSchema = zod_1.z.object({
|
170
170
|
done: zod_1.z.literal(true),
|
171
171
|
model: zod_1.z.string(),
|
172
|
+
created_at: zod_1.z.string(),
|
172
173
|
response: zod_1.z.string(),
|
173
174
|
total_duration: zod_1.z.number(),
|
174
175
|
load_duration: zod_1.z.number().optional(),
|
175
176
|
prompt_eval_count: zod_1.z.number(),
|
177
|
+
prompt_eval_duration: zod_1.z.number().optional(),
|
176
178
|
eval_count: zod_1.z.number(),
|
177
179
|
eval_duration: zod_1.z.number(),
|
178
180
|
context: zod_1.z.array(zod_1.z.number()).optional(),
|
179
181
|
});
|
180
|
-
const
|
182
|
+
const ollamaCompletionStreamSchema = new ZodSchema_js_1.ZodSchema(zod_1.z.discriminatedUnion("done", [
|
181
183
|
zod_1.z.object({
|
182
184
|
done: zod_1.z.literal(false),
|
183
185
|
model: zod_1.z.string(),
|
@@ -205,7 +207,7 @@ async function createOllamaFullDeltaIterableQueue(stream) {
|
|
205
207
|
// process the stream asynchonously (no 'await' on purpose):
|
206
208
|
(0, parseJsonStream_js_1.parseJsonStream)({
|
207
209
|
stream,
|
208
|
-
schema:
|
210
|
+
schema: ollamaCompletionStreamSchema,
|
209
211
|
process(event) {
|
210
212
|
if (event.done === true) {
|
211
213
|
queue.push({
|
@@ -237,7 +239,7 @@ async function createOllamaFullDeltaIterableQueue(stream) {
|
|
237
239
|
});
|
238
240
|
return queue;
|
239
241
|
}
|
240
|
-
exports.
|
242
|
+
exports.OllamaCompletionResponseFormat = {
|
241
243
|
/**
|
242
244
|
* Returns the response as a JSON object.
|
243
245
|
*/
|
@@ -248,7 +250,7 @@ exports.OllamaTextGenerationResponseFormat = {
|
|
248
250
|
const parsedResult = (0, parseJSON_js_1.safeParseJSON)({
|
249
251
|
text: responseBody,
|
250
252
|
schema: new ZodSchema_js_1.ZodSchema(zod_1.z.union([
|
251
|
-
|
253
|
+
ollamaCompletionResponseSchema,
|
252
254
|
zod_1.z.object({
|
253
255
|
done: zod_1.z.literal(false),
|
254
256
|
model: zod_1.z.string(),
|