Elffuss BraidHub

kikocis/continue public

91fbf794ce85 · 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 ) {
192 return undefined;
193 }
194 if (lower.includes("llama3") || lower.includes("llama-3")) {
195 return "llama3";
196 }
197
198 if (lower.includes("llava")) {
199 return "llava";
200 }
201
202 if (lower.includes("tinyllama")) {
203 return "zephyr";
204 }
205
206 if (lower.includes("xwin")) {
207 return "xwin-coder";
208 }
209
210 if (lower.includes("dolphin")) {
211 return "chatml";
212 }
213
214 if (lower.includes("gemma")) {
215 return "gemma";
216 }
217
218 if (lower.includes("phi2")) {
219 return "phi2";
220 }
221
222 if (lower.includes("phind")) {
223 return "phind";
224 }
225
226 if (lower.includes("llama")) {
227 return "llama2";
228 }
229
230 if (lower.includes("zephyr")) {
231 return "zephyr";
232 }
233
234 // Claude requests always sent through Messages API, so formatting not necessary
235 if (lower.includes("claude")) {
236 return "none";
237 }
238
239 if (lower.includes("codestral")) {
240 return "none";
241 }
242
243 if (lower.includes("alpaca") || lower.includes("wizard")) {
244 return "alpaca";
245 }
246
247 if (lower.includes("mistral") || lower.includes("mixtral")) {
248 return "llama2";
249 }
250
251 if (lower.includes("deepseek")) {
252 return "deepseek";
253 }
254
255 if (lower.includes("ninja") || lower.includes("openchat")) {
256 return "openchat";
257 }
258
259 if (lower.includes("neural-chat")) {
260 return "neural-chat";
261 }
262
263 if (lower.includes("granite")) {
264 return "granite";
265 }
266
267 return "chatml";
268}
269
270function autodetectTemplateFunction(
271 model: string,
272 provider: string,
273 explicitTemplate: TemplateType | undefined = undefined,
274) {
275 if (
276 explicitTemplate === undefined &&
277 PROVIDER_HANDLES_TEMPLATING.includes(provider)
278 ) {
279 return null;
280 }
281
282 const templateType = explicitTemplate ?? autodetectTemplateType(model);
283
284 if (templateType) {
285 const mapping: Record<
286 TemplateType,
287 null | ((msg: ChatMessage[]) => string)
288 > = {
289 llama2: llama2TemplateMessages,
290 alpaca: templateAlpacaMessages,
291 phi2: phi2TemplateMessages,
292 phind: phindTemplateMessages,
293 zephyr: zephyrTemplateMessages,
294 anthropic: anthropicTemplateMessages,
295 chatml: chatmlTemplateMessages,
296 deepseek: deepseekTemplateMessages,
297 openchat: openchatTemplateMessages,
298 "xwin-coder": xWinCoderTemplateMessages,
299 "neural-chat": neuralChatTemplateMessages,
300 llava: llavaTemplateMessages,
301 "codellama-70b": codeLlama70bTemplateMessages,
302 gemma: gemmaTemplateMessage,
303 granite: graniteTemplateMessages,
304 llama3: llama3TemplateMessages,
305 codestral: codestralTemplateMessages,
306 none: null,
307 };
308
309 return mapping[templateType];
310 }
311
312 return null;
313}
314
315const USES_OS_MODELS_EDIT_PROMPT: TemplateType[] = [
316 "alpaca",
317 "chatml",
318 // "codellama-70b", Doesn't respond well to this prompt
319 "deepseek",
320 "gemma",
321 "llama2",
322 "llava",
323 "neural-chat",
324 "openchat",
325 "phi2",
326 "phind",
327 "xwin-coder",
328 "zephyr",
329 "llama3",
330];
331
332function autodetectPromptTemplates(
333 model: string,
334 explicitTemplate: TemplateType | undefined = undefined,
335) {
336 const templateType = explicitTemplate ?? autodetectTemplateType(model);
337 const templates: Record<string, any> = {};
338
339 let editTemplate = null;
340
341 if (templateType && USES_OS_MODELS_EDIT_PROMPT.includes(templateType)) {
342 // This is overriding basically everything else
343 // Will probably delete the rest later, but for now it's easy to revert
344 editTemplate = osModelsEditPrompt;
345 } else if (templateType === "phind") {
346 editTemplate = phindEditPrompt;
347 } else if (templateType === "phi2") {
348 editTemplate = simplifiedEditPrompt;
349 } else if (templateType === "zephyr") {
350 editTemplate = zephyrEditPrompt;
351 } else if (templateType === "llama2") {
352 if (model.includes("mistral")) {
353 editTemplate = mistralEditPrompt;
354 } else {
355 editTemplate = osModelsEditPrompt;
356 }
357 } else if (templateType === "alpaca") {
358 editTemplate = alpacaEditPrompt;
359 } else if (templateType === "deepseek") {
360 editTemplate = deepseekEditPrompt;
361 } else if (templateType === "openchat") {
362 editTemplate = openchatEditPrompt;
363 } else if (templateType === "xwin-coder") {
364 editTemplate = xWinCoderEditPrompt;
365 } else if (templateType === "neural-chat") {
366 editTemplate = neuralChatEditPrompt;
367 } else if (templateType === "codellama-70b") {
368 editTemplate = codeLlama70bEditPrompt;
369 } else if (templateType === "anthropic") {
370 editTemplate = claudeEditPrompt;
371 } else if (templateType === "gemma") {
372 editTemplate = gemmaEditPrompt;
373 } else if (templateType === "llama3") {
374 editTemplate = llama3EditPrompt;
375 } else if (templateType === "none") {
376 editTemplate = null;
377 } else if (templateType) {
378 editTemplate = gptEditPrompt;
379 } else if (model.includes("codestral")) {
380 editTemplate = osModelsEditPrompt;
381 }
382
383 if (editTemplate !== null) {
384 templates.edit = editTemplate;
385 }
386
387 return templates;
388}
389
390export {
391 autodetectPromptTemplates,
392 autodetectTemplateFunction,
393 autodetectTemplateType,
394 llmCanGenerateInParallel,
395 modelSupportsImages,
396 modelSupportsTools,
397};