Elffuss BraidHub

kikocis/continue public

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