forked from continuedev/continue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ollama.ts
407 lines (374 loc) Β· 12.5 KB
/
Ollama.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import {
ChatMessage,
CompletionOptions,
LLMOptions,
ModelProvider,
} from "../../index.js";
import { stripImages } from "../images.js";
import { BaseLLM } from "../index.js";
import { streamResponse } from "../stream.js";
interface OllamaChatMessage extends ChatMessage {
images?: string[];
}
// See https://github.com/ollama/ollama/blob/main/docs/modelfile.md for details on each parameter
interface ModelFileParams {
mirostat?: number;
mirostat_eta?: number;
mirostat_tau?: number;
num_ctx?: number;
repeat_last_n?: number;
repeat_penalty?: number;
temperature?: number;
seed?: number;
stop?: string | string[];
tfs_z?: number;
num_predict?: number;
top_k?: number;
top_p?: number;
min_p?: number;
// deprecated?
num_thread?: number;
use_mmap?: boolean;
num_gqa?: number;
num_gpu?: number;
}
// See https://github.com/ollama/ollama/blob/main/docs/api.md
interface BaseOptions {
model: string; // the model name
options?: ModelFileParams; // additional model parameters listed in the documentation for the Modelfile such as temperature
format?: "json"; // the format to return a response in. Currently, the only accepted value is json
stream?: boolean; // if false the response will be returned as a single response object, rather than a stream of objects
keep_alive?: number; // controls how long the model will stay loaded into memory following the request (default: 5m)
}
interface GenerateOptions extends BaseOptions {
prompt: string; // the prompt to generate a response for
suffix?: string; // the text after the model response
images?: string[]; // a list of base64-encoded images (for multimodal models such as llava)
system?: string; // system message to (overrides what is defined in the Modelfile)
template?: string; // the prompt template to use (overrides what is defined in the Modelfile)
context?: string; // the context parameter returned from a previous request to /generate, this can be used to keep a short conversational memory
raw?: boolean; // if true no formatting will be applied to the prompt. You may choose to use the raw parameter if you are specifying a full templated prompt in your request to the API
}
interface ChatOptions extends BaseOptions {
messages: OllamaChatMessage[]; // the messages of the chat, this can be used to keep a chat memory
// Not supported yet - tools: tools for the model to use if supported. Requires stream to be set to false
// And correspondingly, tool calls in OllamaChatMessage
}
class Ollama extends BaseLLM {
static providerName: ModelProvider = "ollama";
static defaultOptions: Partial<LLMOptions> = {
apiBase: "http://localhost:11434/",
model: "codellama-7b",
};
private fimSupported: boolean = false;
constructor(options: LLMOptions) {
super(options);
if (options.model === "AUTODETECT") {
return;
}
this.fetch(this.getEndpoint("api/show"), {
method: "POST",
headers: {
Authorization: `Bearer ${this.apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ name: this._getModel() }),
})
.then(async (response) => {
if (response?.status !== 200) {
// console.warn(
// "Error calling Ollama /api/show endpoint: ",
// await response.text(),
// );
return;
}
const body = await response.json();
if (body.parameters) {
const params = [];
for (const line of body.parameters.split("\n")) {
let parts = line.match(/^(\S+)\s+((?:".*")|\S+)$/);
if (parts.length < 2) {
continue;
}
let key = parts[1];
let value = parts[2];
switch (key) {
case "num_ctx":
this.contextLength =
options.contextLength ?? Number.parseInt(value);
break;
case "stop":
if (!this.completionOptions.stop) {
this.completionOptions.stop = [];
}
try {
this.completionOptions.stop.push(JSON.parse(value));
} catch (e) {
console.warn(
`Error parsing stop parameter value "{value}: ${e}`,
);
}
break;
default:
break;
}
}
}
/**
* There is no API to get the model's FIM capabilities, so we have to
* make an educated guess. If a ".Suffix" variable appears in the template
* it's a good indication the model supports FIM.
*/
this.fimSupported = !!body?.template?.includes(".Suffix");
})
.catch((e) => {
// console.warn("Error calling the Ollama /api/show endpoint: ", e);
});
}
private _getModel() {
return (
{
"mistral-7b": "mistral:7b",
"mixtral-8x7b": "mixtral:8x7b",
"llama2-7b": "llama2:7b",
"llama2-13b": "llama2:13b",
"codellama-7b": "codellama:7b",
"codellama-13b": "codellama:13b",
"codellama-34b": "codellama:34b",
"codellama-70b": "codellama:70b",
"llama3-8b": "llama3:8b",
"llama3-70b": "llama3:70b",
"llama3.1-8b": "llama3.1:8b",
"llama3.1-70b": "llama3.1:70b",
"llama3.1-405b": "llama3.1:405b",
"llama3.2-1b": "llama3.2:1b",
"llama3.2-3b": "llama3.2:3b",
"llama3.2-11b": "llama3.2:11b",
"llama3.2-90b": "llama3.2:90b",
"phi-2": "phi:2.7b",
"phind-codellama-34b": "phind-codellama:34b-v2",
"wizardcoder-7b": "wizardcoder:7b-python",
"wizardcoder-13b": "wizardcoder:13b-python",
"wizardcoder-34b": "wizardcoder:34b-python",
"zephyr-7b": "zephyr:7b",
"codeup-13b": "codeup:13b",
"deepseek-1b": "deepseek-coder:1.3b",
"deepseek-7b": "deepseek-coder:6.7b",
"deepseek-33b": "deepseek-coder:33b",
"neural-chat-7b": "neural-chat:7b-v3.3",
"starcoder-1b": "starcoder:1b",
"starcoder-3b": "starcoder:3b",
"starcoder2-3b": "starcoder2:3b",
"stable-code-3b": "stable-code:3b",
"granite-code-3b": "granite-code:3b",
"granite-code-8b": "granite-code:8b",
"granite-code-20b": "granite-code:20b",
"granite-code-34b": "granite-code:34b",
}[this.model] ?? this.model
);
}
private _getModelFileParams(options: CompletionOptions): ModelFileParams {
return {
temperature: options.temperature,
top_p: options.topP,
top_k: options.topK,
num_predict: options.maxTokens,
stop: options.stop,
num_ctx: this.contextLength,
mirostat: options.mirostat,
num_thread: options.numThreads,
use_mmap: options.useMmap,
min_p: options.minP,
};
}
private _convertMessage(
message: ChatMessage
) {
if (typeof message.content === "string") {
return message;
}
const images: string[] = [];
message.content.forEach((part) => {
if (part.type === "imageUrl" && part.imageUrl) {
const image = part.imageUrl?.url.split(",").at(-1);
if (image) {
images.push(image);
}
}
});
return {
role: message.role,
content: stripImages(message.content),
images
};
}
private _getChatOptions(
options: CompletionOptions,
messages: ChatMessage[]
): ChatOptions {
return {
model: this._getModel(),
messages: messages.map(this._convertMessage),
options: this._getModelFileParams(options),
keep_alive: options.keepAlive ?? 60 * 30, // 30 minutes
stream: options.stream,
// format: options.format, // Not currently in base completion options
};
}
private _getGenerateOptions(
options: CompletionOptions,
prompt: string,
suffix?: string
): GenerateOptions {
return {
model: this._getModel(),
prompt,
suffix,
raw: options.raw,
options: this._getModelFileParams(options),
keep_alive: options.keepAlive ?? 60 * 30, // 30 minutes
stream: options.stream,
// Not supported yet: context, images, system, template, format
};
}
private getEndpoint(endpoint: string): URL {
let base = this.apiBase;
if (process.env.IS_BINARY) {
base = base?.replace("localhost", "127.0.0.1");
}
return new URL(endpoint, base);
}
protected async *_streamComplete(
prompt: string,
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<string> {
const response = await this.fetch(this.getEndpoint("api/generate"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify(this._getGenerateOptions(options, prompt)),
signal
});
let buffer = "";
for await (const value of streamResponse(response)) {
// Append the received chunk to the buffer
buffer += value;
// Split the buffer into individual JSON chunks
const chunks = buffer.split("\n");
buffer = chunks.pop() ?? "";
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
if (chunk.trim() !== "") {
try {
const j = JSON.parse(chunk);
if ("response" in j) {
yield j.response;
} else if ("error" in j) {
throw new Error(j.error);
}
} catch (e) {
throw new Error(`Error parsing Ollama response: ${e} ${chunk}`);
}
}
}
}
}
protected async *_streamChat(
messages: ChatMessage[],
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<ChatMessage> {
const response = await this.fetch(this.getEndpoint("api/chat"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify(this._getChatOptions(options, messages)),
signal
});
let buffer = "";
for await (const value of streamResponse(response)) {
// Append the received chunk to the buffer
buffer += value;
// Split the buffer into individual JSON chunks
const chunks = buffer.split("\n");
buffer = chunks.pop() ?? "";
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
if (chunk.trim() !== "") {
try {
const j = JSON.parse(chunk);
if (j.message?.content) {
yield {
role: "assistant",
content: j.message.content,
};
} else if (j.error) {
throw new Error(j.error);
}
} catch (e) {
throw new Error(`Error parsing Ollama response: ${e} ${chunk}`);
}
}
}
}
}
supportsFim(): boolean {
return this.fimSupported;
}
protected async *_streamFim(
prefix: string,
suffix: string,
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<string> {
const response = await this.fetch(this.getEndpoint("api/generate"), {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify(this._getGenerateOptions(options, prefix, suffix)),
signal
});
let buffer = "";
for await (const value of streamResponse(response)) {
// Append the received chunk to the buffer
buffer += value;
// Split the buffer into individual JSON chunks
const chunks = buffer.split("\n");
buffer = chunks.pop() ?? "";
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
if (chunk.trim() !== "") {
try {
const j = JSON.parse(chunk);
if ("response" in j) {
yield j.response;
} else if ("error" in j) {
throw new Error(j.error);
}
} catch (e) {
throw new Error(`Error parsing Ollama response: ${e} ${chunk}`);
}
}
}
}
}
async listModels(): Promise<string[]> {
const response = await this.fetch(
// localhost was causing fetch failed in pkg binary only for this Ollama endpoint
this.getEndpoint("api/tags"),
{
method: "GET",
},
);
const data = await response.json();
return data.models.map((model: any) => model.name);
}
}
export default Ollama;