Elffuss BraidHub

kikocis/continue public

e31fcddb0dec · 10.1 KiBRawPermalink
1import {
2 ChatMessage,
3 ModelCapability,
4 ModelDescription,
5 TemplateType,
6} from "../index.js";
7import { NEXT_EDIT_MODELS } from "./constants.js";
8
9import {
10 anthropicTemplateMessages,
11 chatmlTemplateMessages,
12 codeLlama70bTemplateMessages,
13 codestralTemplateMessages,
14 deepseekTemplateMessages,
15 gemmaTemplateMessage,
16 graniteTemplateMessages,
17 llama2TemplateMessages,
18 llama3TemplateMessages,
19 llavaTemplateMessages,
20 neuralChatTemplateMessages,
21 openchatTemplateMessages,
22 phi2TemplateMessages,
23 phindTemplateMessages,
24 templateAlpacaMessages,
25 xWinCoderTemplateMessages,
26 zephyrTemplateMessages,
27} from "./templates/chat.js";
28import {
29 alpacaEditPrompt,
30 claudeEditPrompt,
31 codeLlama70bEditPrompt,
32 deepseekEditPrompt,
33 gemmaEditPrompt,
34 gptEditPrompt,
35 llama3EditPrompt,
36 mistralEditPrompt,
37 neuralChatEditPrompt,
38 openchatEditPrompt,
39 osModelsEditPrompt,
40 phindEditPrompt,
41 simplifiedEditPrompt,
42 xWinCoderEditPrompt,
43 zephyrEditPrompt,
44} from "./templates/edit.js";
45
46const PROVIDER_HANDLES_TEMPLATING: string[] = [
47 "lmstudio",
48 "lemonade",
49 "openai",
50 "nvidia",
51 "ollama",
52 "together",
53 "novita",
54 "msty",
55 "anthropic",
56 "bedrock",
57 "cohere",
58 "sagemaker",
59 "continue-proxy",
60 "mistral",
61 "sambanova",
62 "vertexai",
63 "watsonx",
64 "nebius",
65 "relace",
66];
67
68const PROVIDER_SUPPORTS_IMAGES: string[] = [
69 "openai",
70 "ollama",
71 "lemonade",
72 "cohere",
73 "gemini",
74 "msty",
75 "anthropic",
76 "bedrock",
77 "sagemaker",
78 "continue-proxy",
79 "openrouter",
80 "venice",
81 "sambanova",
82 "vertexai",
83 "azure",
84 "scaleway",
85 "nebius",
86 "ovhcloud",
87 "watsonx",
88];
89
90const MODEL_SUPPORTS_IMAGES: RegExp[] = [
91 /llava/,
92 /gpt-4-turbo/,
93 /gpt-4o/,
94 /gpt-4o-mini/,
95 /claude-3/,
96 /gemini-ultra/,
97 /gemini-1\.5-pro/,
98 /gemini-1\.5-flash/,
99 /sonnet/,
100 /opus/,
101 /haiku/,
102 /pixtral/,
103 /llama-?3\.2/,
104 /llama-?4/, // might use something like /llama-?(?:[4-9](?:\.\d+)?|\d{2,}(?:\.\d+)?)/ for forward compat, if needed
105 /\bgemma-?3(?!n)/, // gemma3 supports vision, but gemma3n doesn't!
106 /\b(pali|med)gemma/,
107 /qwen(.*)vl/,
108];
109
110function modelSupportsImages(
111 provider: string,
112 model: string,
113 title: string | undefined,
114 capabilities: ModelCapability | undefined,
115): boolean {
116 if (capabilities?.uploadImage !== undefined) {
117 return capabilities.uploadImage;
118 }
119 if (!PROVIDER_SUPPORTS_IMAGES.includes(provider)) {
120 return false;
121 }
122
123 const lowerModel = model.toLowerCase();
124 const lowerTitle = title?.toLowerCase() ?? "";
125
126 if (
127 lowerModel.includes("vision") ||
128 lowerTitle.includes("vision") ||
129 MODEL_SUPPORTS_IMAGES.some(
130 (modelrx) => modelrx.test(lowerModel) || modelrx.test(lowerTitle),
131 )
132 ) {
133 return true;
134 }
135
136 return false;
137}
138
139function modelSupportsReasoning(
140 model: ModelDescription | null | undefined,
141): boolean {
142 if (!model) {
143 return false;
144 }
145 if ("anthropic" === model.underlyingProviderName) {
146 return true;
147 }
148 if (model.model.includes("deepseek-r")) {
149 return true;
150 }
151 if (model.completionOptions?.reasoning) {
152 // Reasoning support is forced at the config level. Model might not necessarily support it though!
153 return true;
154 }
155 return false;
156}
157
158const PARALLEL_PROVIDERS: string[] = [
159 "anthropic",
160 "bedrock",
161 "cohere",
162 "sagemaker",
163 "deepinfra",
164 "gemini",
165 "huggingface-inference-api",
166 "huggingface-tgi",
167 "mistral",
168 "moonshot",
169 "replicate",
170 "together",
171 "novita",
172 "sambanova",
173 "ovhcloud",
174 "nebius",
175 "vertexai",
176 "function-network",
177 "scaleway",
178];
179
180function llmCanGenerateInParallel(provider: string, model: string): boolean {
181 if (provider === "openai") {
182 return model.includes("gpt");
183 }
184
185 return PARALLEL_PROVIDERS.includes(provider);
186}
187
188function isProviderHandlesTemplatingOrNoTemplateTypeRequired(
189 modelName: string,
190): boolean {
191 return (
192 modelName.includes("gpt") ||
193 modelName.includes("command") ||
194 modelName.includes("aya") ||
195 modelName.includes("chat-bison") ||
196 modelName.includes("pplx") ||
197 modelName.includes("gemini") ||
198 modelName.includes("grok") ||
199 modelName.includes("moonshot") ||
200 modelName.includes("kimi") ||
201 modelName.includes("mercury") ||
202 /^o\d/.test(modelName)
203 );
204}
205
206// NOTE: When updating this list,
207// update core/nextEdit/templating/NextEditPromptEngine.ts as well.
208const MODEL_SUPPORTS_NEXT_EDIT: string[] = [
209 NEXT_EDIT_MODELS.MERCURY_CODER,
210 NEXT_EDIT_MODELS.INSTINCT,
211];
212
213function modelSupportsNextEdit(
214 capabilities: ModelCapability | undefined,
215 model: string,
216 title: string | undefined,
217): boolean {
218 if (capabilities?.nextEdit !== undefined) {
219 return capabilities.nextEdit;
220 }
221
222 const lower = model.toLowerCase();
223 if (
224 MODEL_SUPPORTS_NEXT_EDIT.some(
225 (modelName) => lower.includes(modelName) || title?.includes(modelName),
226 )
227 ) {
228 return true;
229 }
230
231 return false;
232}
233
234function autodetectTemplateType(model: string): TemplateType | undefined {
235 const lower = model.toLowerCase();
236
237 if (lower.includes("codellama") && lower.includes("70b")) {
238 return "codellama-70b";
239 }
240
241 if (isProviderHandlesTemplatingOrNoTemplateTypeRequired(lower)) {
242 return undefined;
243 }
244
245 if (lower.includes("llama3") || lower.includes("llama-3")) {
246 return "llama3";
247 }
248
249 if (lower.includes("llava")) {
250 return "llava";
251 }
252
253 if (lower.includes("tinyllama")) {
254 return "zephyr";
255 }
256
257 if (lower.includes("xwin")) {
258 return "xwin-coder";
259 }
260
261 if (lower.includes("dolphin")) {
262 return "chatml";
263 }
264
265 if (lower.includes("gemma")) {
266 return "gemma";
267 }
268
269 if (lower.includes("phi2")) {
270 return "phi2";
271 }
272
273 if (lower.includes("phind")) {
274 return "phind";
275 }
276
277 if (lower.includes("llama")) {
278 return "llama2";
279 }
280
281 if (lower.includes("zephyr")) {
282 return "zephyr";
283 }
284
285 // Claude requests always sent through Messages API, so formatting not necessary
286 if (lower.includes("claude")) {
287 return "none";
288 }
289
290 // Nova Pro requests always sent through Converse API, so formatting not necessary
291 if (lower.includes("nova")) {
292 return "none";
293 }
294
295 if (lower.includes("codestral")) {
296 return "none";
297 }
298
299 if (lower.includes("alpaca") || lower.includes("wizard")) {
300 return "alpaca";
301 }
302
303 if (lower.includes("mistral") || lower.includes("mixtral")) {
304 return "llama2";
305 }
306
307 if (lower.includes("deepseek")) {
308 return "deepseek";
309 }
310
311 if (lower.includes("ninja") || lower.includes("openchat")) {
312 return "openchat";
313 }
314
315 if (lower.includes("neural-chat")) {
316 return "neural-chat";
317 }
318
319 if (lower.includes("granite")) {
320 return "granite";
321 }
322
323 return "chatml";
324}
325
326function autodetectTemplateFunction(
327 model: string,
328 provider: string,
329 explicitTemplate: TemplateType | undefined = undefined,
330) {
331 if (
332 explicitTemplate === undefined &&
333 PROVIDER_HANDLES_TEMPLATING.includes(provider)
334 ) {
335 return null;
336 }
337
338 const templateType = explicitTemplate ?? autodetectTemplateType(model);
339
340 if (templateType) {
341 const mapping: Record<
342 TemplateType,
343 null | ((msg: ChatMessage[]) => string)
344 > = {
345 llama2: llama2TemplateMessages,
346 alpaca: templateAlpacaMessages,
347 phi2: phi2TemplateMessages,
348 phind: phindTemplateMessages,
349 zephyr: zephyrTemplateMessages,
350 anthropic: anthropicTemplateMessages,
351 chatml: chatmlTemplateMessages,
352 deepseek: deepseekTemplateMessages,
353 openchat: openchatTemplateMessages,
354 "xwin-coder": xWinCoderTemplateMessages,
355 "neural-chat": neuralChatTemplateMessages,
356 llava: llavaTemplateMessages,
357 "codellama-70b": codeLlama70bTemplateMessages,
358 gemma: gemmaTemplateMessage,
359 granite: graniteTemplateMessages,
360 llama3: llama3TemplateMessages,
361 codestral: codestralTemplateMessages,
362 none: null,
363 };
364
365 return mapping[templateType];
366 }
367
368 return null;
369}
370
371const USES_OS_MODELS_EDIT_PROMPT: TemplateType[] = [
372 "alpaca",
373 "chatml",
374 // "codellama-70b", Doesn't respond well to this prompt
375 "deepseek",
376 "gemma",
377 "llama2",
378 "llava",
379 "neural-chat",
380 "openchat",
381 "phi2",
382 "phind",
383 "xwin-coder",
384 "zephyr",
385 "llama3",
386];
387
388function autodetectPromptTemplates(
389 model: string,
390 explicitTemplate: TemplateType | undefined = undefined,
391) {
392 const templateType = explicitTemplate ?? autodetectTemplateType(model);
393 const templates: Record<string, any> = {};
394
395 let editTemplate = null;
396
397 if (templateType && USES_OS_MODELS_EDIT_PROMPT.includes(templateType)) {
398 // This is overriding basically everything else
399 // Will probably delete the rest later, but for now it's easy to revert
400 editTemplate = osModelsEditPrompt;
401 } else if (templateType === "phind") {
402 editTemplate = phindEditPrompt;
403 } else if (templateType === "phi2") {
404 editTemplate = simplifiedEditPrompt;
405 } else if (templateType === "zephyr") {
406 editTemplate = zephyrEditPrompt;
407 } else if (templateType === "llama2") {
408 if (model.includes("mistral")) {
409 editTemplate = mistralEditPrompt;
410 } else {
411 editTemplate = osModelsEditPrompt;
412 }
413 } else if (templateType === "alpaca") {
414 editTemplate = alpacaEditPrompt;
415 } else if (templateType === "deepseek") {
416 editTemplate = deepseekEditPrompt;
417 } else if (templateType === "openchat") {
418 editTemplate = openchatEditPrompt;
419 } else if (templateType === "xwin-coder") {
420 editTemplate = xWinCoderEditPrompt;
421 } else if (templateType === "neural-chat") {
422 editTemplate = neuralChatEditPrompt;
423 } else if (templateType === "codellama-70b") {
424 editTemplate = codeLlama70bEditPrompt;
425 } else if (templateType === "anthropic") {
426 editTemplate = claudeEditPrompt;
427 } else if (templateType === "gemma") {
428 editTemplate = gemmaEditPrompt;
429 } else if (templateType === "llama3") {
430 editTemplate = llama3EditPrompt;
431 } else if (templateType === "none") {
432 editTemplate = null;
433 } else if (templateType) {
434 editTemplate = gptEditPrompt;
435 } else if (model.includes("codestral")) {
436 editTemplate = osModelsEditPrompt;
437 }
438
439 if (editTemplate !== null) {
440 templates.edit = editTemplate;
441 }
442
443 return templates;
444}
445
446export {
447 autodetectPromptTemplates,
448 autodetectTemplateFunction,
449 autodetectTemplateType,
450 llmCanGenerateInParallel,
451 modelSupportsImages,
452 modelSupportsNextEdit,
453 modelSupportsReasoning,
454};