Elffuss BraidHub

kikocis/continue public

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