Changesets · Branches · Tags · Compare
| Changeset | 1c73a1f84d7c1c3f227d337789ac4393bfac1b942056a2c0db34ce4432eaa899 |
|---|---|
| Author | Nate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-07-03 18:55 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-07-03 18:55 -07:00 |
| Parents | 429a28bc4ff0, cb835f8c632d |
3 files changed
modified core/llm/autodetect.ts +2 −1
| @@ -187,7 +187,8 @@ | ||
| 187 | 187 | lower.includes("gemini") || |
| 188 | 188 | lower.includes("grok") || |
| 189 | 189 | lower.includes("moonshot") || |
| 190 | - lower.includes("mercury") | |
| 190 | + lower.includes("mercury") || | |
| 191 | + /^o\d/.test(lower) | |
| 191 | 192 | ) { |
| 192 | 193 | return undefined; |
| 193 | 194 | } |
added core/llm/autodetect.vitest.ts +190 −0
| @@ -0,0 +1,190 @@ | ||
| 1 | +import { expect, test } from "vitest"; | |
| 2 | +import { autodetectTemplateType } from "./autodetect"; | |
| 3 | + | |
| 4 | +test("autodetectTemplateType returns 'codellama-70b' for CodeLlama 70B models", () => { | |
| 5 | + expect(autodetectTemplateType("codellama-70b")).toBe("codellama-70b"); | |
| 6 | + expect(autodetectTemplateType("CodeLlama-70B")).toBe("codellama-70b"); | |
| 7 | + expect(autodetectTemplateType("codellama-70b-instruct")).toBe( | |
| 8 | + "codellama-70b", | |
| 9 | + ); | |
| 10 | + expect(autodetectTemplateType("CodeLlama-70B-Python")).toBe("codellama-70b"); | |
| 11 | +}); | |
| 12 | + | |
| 13 | +test("autodetectTemplateType returns undefined for models that don't need templates", () => { | |
| 14 | + expect(autodetectTemplateType("gpt-4")).toBe(undefined); | |
| 15 | + expect(autodetectTemplateType("gpt-3.5-turbo")).toBe(undefined); | |
| 16 | + expect(autodetectTemplateType("GPT-4")).toBe(undefined); | |
| 17 | + expect(autodetectTemplateType("command-r")).toBe(undefined); | |
| 18 | + expect(autodetectTemplateType("aya-23")).toBe(undefined); | |
| 19 | + expect(autodetectTemplateType("chat-bison")).toBe(undefined); | |
| 20 | + expect(autodetectTemplateType("pplx-70b")).toBe(undefined); | |
| 21 | + expect(autodetectTemplateType("gemini-pro")).toBe(undefined); | |
| 22 | + expect(autodetectTemplateType("grok-beta")).toBe(undefined); | |
| 23 | + expect(autodetectTemplateType("moonshot-v1")).toBe(undefined); | |
| 24 | + expect(autodetectTemplateType("mercury-chat")).toBe(undefined); | |
| 25 | + expect(autodetectTemplateType("mercury-chat")).toBe(undefined); | |
| 26 | + expect(autodetectTemplateType("o3-mini")).toBe(undefined); | |
| 27 | + expect(autodetectTemplateType("o4")).toBe(undefined); | |
| 28 | + expect(autodetectTemplateType("claude-sonnet-4-20250514")).toBe("none"); | |
| 29 | + expect(autodetectTemplateType("claude-3-7-sonnet-latest")).toBe("none"); | |
| 30 | +}); | |
| 31 | + | |
| 32 | +test("autodetectTemplateType returns 'llama3' for Llama 3 models", () => { | |
| 33 | + expect(autodetectTemplateType("llama3")).toBe("llama3"); | |
| 34 | + expect(autodetectTemplateType("llama-3")).toBe("llama3"); | |
| 35 | + expect(autodetectTemplateType("Llama3-8B")).toBe("llama3"); | |
| 36 | + expect(autodetectTemplateType("llama-3:70b")).toBe("llama3"); | |
| 37 | +}); | |
| 38 | + | |
| 39 | +test("autodetectTemplateType returns 'llava' for LLaVA models", () => { | |
| 40 | + expect(autodetectTemplateType("llava")).toBe("llava"); | |
| 41 | + expect(autodetectTemplateType("LLaVA")).toBe("llava"); | |
| 42 | + expect(autodetectTemplateType("llava-1.5")).toBe("llava"); | |
| 43 | + expect(autodetectTemplateType("llava-v1.6")).toBe("llava"); | |
| 44 | +}); | |
| 45 | + | |
| 46 | +test("autodetectTemplateType returns 'zephyr' for TinyLlama models", () => { | |
| 47 | + expect(autodetectTemplateType("tinyllama")).toBe("zephyr"); | |
| 48 | + expect(autodetectTemplateType("TinyLlama")).toBe("zephyr"); | |
| 49 | + expect(autodetectTemplateType("tinyllama-1.1b")).toBe("zephyr"); | |
| 50 | +}); | |
| 51 | + | |
| 52 | +test("autodetectTemplateType returns 'xwin-coder' for XWin models", () => { | |
| 53 | + expect(autodetectTemplateType("xwin")).toBe("xwin-coder"); | |
| 54 | + expect(autodetectTemplateType("XWin")).toBe("xwin-coder"); | |
| 55 | + expect(autodetectTemplateType("xwin-coder")).toBe("xwin-coder"); | |
| 56 | +}); | |
| 57 | + | |
| 58 | +test("autodetectTemplateType returns 'chatml' for Dolphin models", () => { | |
| 59 | + expect(autodetectTemplateType("dolphin")).toBe("chatml"); | |
| 60 | + expect(autodetectTemplateType("Dolphin")).toBe("chatml"); | |
| 61 | + expect(autodetectTemplateType("dolphin-2.1")).toBe("chatml"); | |
| 62 | +}); | |
| 63 | + | |
| 64 | +test("autodetectTemplateType returns 'gemma' for Gemma models", () => { | |
| 65 | + expect(autodetectTemplateType("gemma")).toBe("gemma"); | |
| 66 | + expect(autodetectTemplateType("Gemma")).toBe("gemma"); | |
| 67 | + expect(autodetectTemplateType("gemma-7b")).toBe("gemma"); | |
| 68 | +}); | |
| 69 | + | |
| 70 | +test("autodetectTemplateType returns 'phi2' for Phi-2 models", () => { | |
| 71 | + expect(autodetectTemplateType("phi2")).toBe("phi2"); | |
| 72 | + expect(autodetectTemplateType("Phi2")).toBe("phi2"); | |
| 73 | + expect(autodetectTemplateType("microsoft-phi2")).toBe("phi2"); | |
| 74 | +}); | |
| 75 | + | |
| 76 | +test("autodetectTemplateType returns 'phind' for Phind models", () => { | |
| 77 | + expect(autodetectTemplateType("phind")).toBe("phind"); | |
| 78 | + expect(autodetectTemplateType("Phind")).toBe("phind"); | |
| 79 | + expect(autodetectTemplateType("phind-codellama")).toBe("phind"); | |
| 80 | +}); | |
| 81 | + | |
| 82 | +test("autodetectTemplateType returns 'llama2' for general Llama models", () => { | |
| 83 | + expect(autodetectTemplateType("llama")).toBe("llama2"); | |
| 84 | + expect(autodetectTemplateType("Llama")).toBe("llama2"); | |
| 85 | + expect(autodetectTemplateType("llama2")).toBe("llama2"); | |
| 86 | + expect(autodetectTemplateType("llama-2")).toBe("llama2"); | |
| 87 | + expect(autodetectTemplateType("codellama")).toBe("llama2"); | |
| 88 | +}); | |
| 89 | + | |
| 90 | +test("autodetectTemplateType returns 'zephyr' for Zephyr models", () => { | |
| 91 | + expect(autodetectTemplateType("zephyr")).toBe("zephyr"); | |
| 92 | + expect(autodetectTemplateType("Zephyr")).toBe("zephyr"); | |
| 93 | + expect(autodetectTemplateType("zephyr-7b")).toBe("zephyr"); | |
| 94 | +}); | |
| 95 | + | |
| 96 | +test("autodetectTemplateType returns 'none' for Claude models", () => { | |
| 97 | + expect(autodetectTemplateType("claude")).toBe("none"); | |
| 98 | + expect(autodetectTemplateType("Claude")).toBe("none"); | |
| 99 | + expect(autodetectTemplateType("claude-3")).toBe("none"); | |
| 100 | + expect(autodetectTemplateType("claude-3-opus")).toBe("none"); | |
| 101 | +}); | |
| 102 | + | |
| 103 | +test("autodetectTemplateType returns 'none' for Codestral models", () => { | |
| 104 | + expect(autodetectTemplateType("codestral")).toBe("none"); | |
| 105 | + expect(autodetectTemplateType("Codestral")).toBe("none"); | |
| 106 | + expect(autodetectTemplateType("codestral-22b")).toBe("none"); | |
| 107 | +}); | |
| 108 | + | |
| 109 | +test("autodetectTemplateType returns 'alpaca' for Alpaca and Wizard models", () => { | |
| 110 | + expect(autodetectTemplateType("alpaca")).toBe("alpaca"); | |
| 111 | + expect(autodetectTemplateType("Alpaca")).toBe("alpaca"); | |
| 112 | + expect(autodetectTemplateType("wizard")).toBe("alpaca"); | |
| 113 | + expect(autodetectTemplateType("Wizard")).toBe("alpaca"); | |
| 114 | + expect(autodetectTemplateType("wizardlm")).toBe("alpaca"); | |
| 115 | +}); | |
| 116 | + | |
| 117 | +test("autodetectTemplateType returns 'llama2' for Mistral and Mixtral models", () => { | |
| 118 | + expect(autodetectTemplateType("mistral")).toBe("llama2"); | |
| 119 | + expect(autodetectTemplateType("Mistral")).toBe("llama2"); | |
| 120 | + expect(autodetectTemplateType("mixtral")).toBe("llama2"); | |
| 121 | + expect(autodetectTemplateType("Mixtral")).toBe("llama2"); | |
| 122 | + expect(autodetectTemplateType("mistral-7b")).toBe("llama2"); | |
| 123 | +}); | |
| 124 | + | |
| 125 | +test("autodetectTemplateType returns 'deepseek' for DeepSeek models", () => { | |
| 126 | + expect(autodetectTemplateType("deepseek")).toBe("deepseek"); | |
| 127 | + expect(autodetectTemplateType("DeepSeek")).toBe("deepseek"); | |
| 128 | + expect(autodetectTemplateType("deepseek-coder")).toBe("deepseek"); | |
| 129 | +}); | |
| 130 | + | |
| 131 | +test("autodetectTemplateType returns 'openchat' for Ninja and OpenChat models", () => { | |
| 132 | + expect(autodetectTemplateType("ninja")).toBe("openchat"); | |
| 133 | + expect(autodetectTemplateType("Ninja")).toBe("openchat"); | |
| 134 | + expect(autodetectTemplateType("openchat")).toBe("openchat"); | |
| 135 | + expect(autodetectTemplateType("OpenChat")).toBe("openchat"); | |
| 136 | +}); | |
| 137 | + | |
| 138 | +test("autodetectTemplateType returns 'neural-chat' for Neural Chat models", () => { | |
| 139 | + expect(autodetectTemplateType("neural-chat")).toBe("neural-chat"); | |
| 140 | + expect(autodetectTemplateType("Neural-Chat")).toBe("neural-chat"); | |
| 141 | + expect(autodetectTemplateType("intel-neural-chat")).toBe("neural-chat"); | |
| 142 | +}); | |
| 143 | + | |
| 144 | +test("autodetectTemplateType returns 'granite' for Granite models", () => { | |
| 145 | + expect(autodetectTemplateType("granite")).toBe("granite"); | |
| 146 | + expect(autodetectTemplateType("granite2:8b")).toBe("granite"); | |
| 147 | + expect(autodetectTemplateType("Granite")).toBe("granite"); | |
| 148 | + expect(autodetectTemplateType("granite-code")).toBe("granite"); | |
| 149 | +}); | |
| 150 | + | |
| 151 | +test("autodetectTemplateType returns chatml for other models", () => { | |
| 152 | + expect(autodetectTemplateType("unknown-model")).toBe("chatml"); | |
| 153 | + expect(autodetectTemplateType("random-ai")).toBe("chatml"); | |
| 154 | + expect(autodetectTemplateType("")).toBe("chatml"); | |
| 155 | +}); | |
| 156 | + | |
| 157 | +test("autodetectTemplateType handles case sensitivity correctly", () => { | |
| 158 | + expect(autodetectTemplateType("LLAMA3")).toBe("llama3"); | |
| 159 | + expect(autodetectTemplateType("gpt-4")).toBe(undefined); | |
| 160 | + expect(autodetectTemplateType("GPT-4")).toBe(undefined); | |
| 161 | + expect(autodetectTemplateType("CLAUDE")).toBe("none"); | |
| 162 | +}); | |
| 163 | + | |
| 164 | +test("autodetectTemplateType follows correct precedence order", () => { | |
| 165 | + // CodeLlama 70B should be detected before general llama | |
| 166 | + expect(autodetectTemplateType("codellama-70b")).toBe("codellama-70b"); | |
| 167 | + | |
| 168 | + // Llama3 should be detected before general llama | |
| 169 | + expect(autodetectTemplateType("llama3")).toBe("llama3"); | |
| 170 | + | |
| 171 | + // General llama should be detected after specific cases | |
| 172 | + expect(autodetectTemplateType("llama2")).toBe("llama2"); | |
| 173 | + | |
| 174 | + // TinyLlama should be detected as zephyr, not llama | |
| 175 | + expect(autodetectTemplateType("tinyllama")).toBe("zephyr"); | |
| 176 | +}); | |
| 177 | + | |
| 178 | +test("autodetectTemplateType handles edge cases with special characters", () => { | |
| 179 | + expect(autodetectTemplateType("llama-3-8b-instruct")).toBe("llama3"); | |
| 180 | + expect(autodetectTemplateType("gpt-4-turbo-preview")).toBe(undefined); | |
| 181 | + expect(autodetectTemplateType("claude-3-opus-20240229")).toBe("none"); | |
| 182 | + expect(autodetectTemplateType("mistral-7b-instruct-v0.1")).toBe("llama2"); | |
| 183 | +}); | |
| 184 | + | |
| 185 | +test("autodetectTemplateType handles models with mixed keywords", () => { | |
| 186 | + // Should match the first applicable pattern | |
| 187 | + expect(autodetectTemplateType("llama3-chat")).toBe("llama3"); // llama3 comes before general patterns | |
| 188 | + expect(autodetectTemplateType("gpt-llama")).toBe(undefined); // gpt comes first, returns undefined | |
| 189 | + expect(autodetectTemplateType("claude-llama")).toBe("llama2"); // llama comes first, returns llama2 | |
| 190 | +}); |
modified core/llm/index.ts +47 −38
| @@ -384,6 +384,50 @@ | ||
| 384 | 384 | } |
| 385 | 385 | } |
| 386 | 386 | |
| 387 | + private async parseError(resp: any): Promise<Error> { | |
| 388 | + let text = await resp.text(); | |
| 389 | + | |
| 390 | + if (resp.status === 404 && !resp.url.includes("/v1")) { | |
| 391 | + const parsedError = JSON.parse(text); | |
| 392 | + const errorMessageRaw = parsedError?.error ?? parsedError?.message; | |
| 393 | + const error = | |
| 394 | + typeof errorMessageRaw === "string" | |
| 395 | + ? errorMessageRaw.replace(/"/g, "'") | |
| 396 | + : undefined; | |
| 397 | + let model = error?.match(/model '(.*)' not found/)?.[1]; | |
| 398 | + if (model && resp.url.match("127.0.0.1:11434")) { | |
| 399 | + text = `The model "${model}" was not found. To download it, run \`ollama run ${model}\`.`; | |
| 400 | + return new LLMError(text, this); // No need to add HTTP status details | |
| 401 | + } else if (text.includes("/api/chat")) { | |
| 402 | + text = | |
| 403 | + "The /api/chat endpoint was not found. This may mean that you are using an older version of Ollama that does not support /api/chat. Upgrading to the latest version will solve the issue."; | |
| 404 | + } else { | |
| 405 | + text = | |
| 406 | + "This may mean that you forgot to add '/v1' to the end of your 'apiBase' in config.json."; | |
| 407 | + } | |
| 408 | + } else if (resp.status === 404 && resp.url.includes("api.openai.com")) { | |
| 409 | + text = | |
| 410 | + "You may need to add pre-paid credits before using the OpenAI API."; | |
| 411 | + } else if ( | |
| 412 | + resp.status === 401 && | |
| 413 | + (resp.url.includes("api.mistral.ai") || | |
| 414 | + resp.url.includes("codestral.mistral.ai")) | |
| 415 | + ) { | |
| 416 | + if (resp.url.includes("codestral.mistral.ai")) { | |
| 417 | + return new Error( | |
| 418 | + "You are using a Mistral API key, which is not compatible with the Codestral API. Please either obtain a Codestral API key, or use the Mistral API by setting 'apiBase' to 'https://api.mistral.ai/v1' in config.json.", | |
| 419 | + ); | |
| 420 | + } else { | |
| 421 | + return new Error( | |
| 422 | + "You are using a Codestral API key, which is not compatible with the Mistral API. Please either obtain a Mistral API key, or use the the Codestral API by setting 'apiBase' to 'https://codestral.mistral.ai/v1' in config.json.", | |
| 423 | + ); | |
| 424 | + } | |
| 425 | + } | |
| 426 | + return new Error( | |
| 427 | + `HTTP ${resp.status} ${resp.statusText} from ${resp.url}\n\n${text}`, | |
| 428 | + ); | |
| 429 | + } | |
| 430 | + | |
| 387 | 431 | fetch(url: RequestInfo | URL, init?: RequestInit): Promise<Response> { |
| 388 | 432 | // Custom Node.js fetch |
| 389 | 433 | const customFetch = async (input: URL | RequestInfo, init: any) => { |
| @@ -399,44 +443,9 @@ | ||
| 399 | 443 | if (resp.status === 499) { |
| 400 | 444 | return resp; // client side cancellation |
| 401 | 445 | } |
| 402 | - let text = await resp.text(); | |
| 403 | - if (resp.status === 404 && !resp.url.includes("/v1")) { | |
| 404 | - const error = JSON.parse(text)?.error?.replace(/"/g, "'"); | |
| 405 | - let model = error?.match(/model '(.*)' not found/)?.[1]; | |
| 406 | - if (model && resp.url.match("127.0.0.1:11434")) { | |
| 407 | - text = `The model "${model}" was not found. To download it, run \`ollama run ${model}\`.`; | |
| 408 | - throw new LLMError(text, this); // No need to add HTTP status details | |
| 409 | - } else if (text.includes("/api/chat")) { | |
| 410 | - text = | |
| 411 | - "The /api/chat endpoint was not found. This may mean that you are using an older version of Ollama that does not support /api/chat. Upgrading to the latest version will solve the issue."; | |
| 412 | - } else { | |
| 413 | - text = | |
| 414 | - "This may mean that you forgot to add '/v1' to the end of your 'apiBase' in config.json."; | |
| 415 | - } | |
| 416 | - } else if ( | |
| 417 | - resp.status === 404 && | |
| 418 | - resp.url.includes("api.openai.com") | |
| 419 | - ) { | |
| 420 | - text = | |
| 421 | - "You may need to add pre-paid credits before using the OpenAI API."; | |
| 422 | - } else if ( | |
| 423 | - resp.status === 401 && | |
| 424 | - (resp.url.includes("api.mistral.ai") || | |
| 425 | - resp.url.includes("codestral.mistral.ai")) | |
| 426 | - ) { | |
| 427 | - if (resp.url.includes("codestral.mistral.ai")) { | |
| 428 | - throw new Error( | |
| 429 | - "You are using a Mistral API key, which is not compatible with the Codestral API. Please either obtain a Codestral API key, or use the Mistral API by setting 'apiBase' to 'https://api.mistral.ai/v1' in config.json.", | |
| 430 | - ); | |
| 431 | - } else { | |
| 432 | - throw new Error( | |
| 433 | - "You are using a Codestral API key, which is not compatible with the Mistral API. Please either obtain a Mistral API key, or use the the Codestral API by setting 'apiBase' to 'https://codestral.mistral.ai/v1' in config.json.", | |
| 434 | - ); | |
| 435 | - } | |
| 436 | - } | |
| 437 | - throw new Error( | |
| 438 | - `HTTP ${resp.status} ${resp.statusText} from ${resp.url}\n\n${text}`, | |
| 439 | - ); | |
| 446 | + | |
| 447 | + const error = await this.parseError(resp); | |
| 448 | + throw error; | |
| 440 | 449 | } |
| 441 | 450 | |
| 442 | 451 | return resp; |