Elffuss BraidHub

kikocis/continue public

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