Elffuss BraidHub

kikocis/continue public

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