Elffuss BraidHub

kikocis/continue public

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