Elffuss BraidHub

kikocis/continue public

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