refactor and add safer parsing to error messages
This commit is contained in:
parent
faed694877
commit
0d7e8ce896
1 changed files with 48 additions and 38 deletions
|
|
@ -7,6 +7,7 @@ import {
|
|||
constructLlmApi,
|
||||
} from "@continuedev/openai-adapters";
|
||||
import Handlebars from "handlebars";
|
||||
import { Response } from "node-fetch";
|
||||
|
||||
import { DevDataSqliteDb } from "../data/devdataSqlite.js";
|
||||
import { DataLogger } from "../data/log.js";
|
||||
|
|
@ -384,6 +385,50 @@ export abstract class BaseLLM implements ILLM {
|
|||
}
|
||||
}
|
||||
|
||||
private async parseError(resp: Response): Promise<Error> {
|
||||
let text = await resp.text();
|
||||
|
||||
if (resp.status === 404 && !resp.url.includes("/v1")) {
|
||||
const parsedError = JSON.parse(text);
|
||||
const errorMessageRaw = parsedError?.error ?? parsedError?.message;
|
||||
const error =
|
||||
typeof errorMessageRaw === "string"
|
||||
? errorMessageRaw.replace(/"/g, "'")
|
||||
: undefined;
|
||||
let model = error?.match(/model '(.*)' not found/)?.[1];
|
||||
if (model && resp.url.match("127.0.0.1:11434")) {
|
||||
text = `The model "${model}" was not found. To download it, run \`ollama run ${model}\`.`;
|
||||
return new LLMError(text, this); // No need to add HTTP status details
|
||||
} else if (text.includes("/api/chat")) {
|
||||
text =
|
||||
"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.";
|
||||
} else {
|
||||
text =
|
||||
"This may mean that you forgot to add '/v1' to the end of your 'apiBase' in config.json.";
|
||||
}
|
||||
} else if (resp.status === 404 && resp.url.includes("api.openai.com")) {
|
||||
text =
|
||||
"You may need to add pre-paid credits before using the OpenAI API.";
|
||||
} else if (
|
||||
resp.status === 401 &&
|
||||
(resp.url.includes("api.mistral.ai") ||
|
||||
resp.url.includes("codestral.mistral.ai"))
|
||||
) {
|
||||
if (resp.url.includes("codestral.mistral.ai")) {
|
||||
return new Error(
|
||||
"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.",
|
||||
);
|
||||
} else {
|
||||
return new Error(
|
||||
"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.",
|
||||
);
|
||||
}
|
||||
}
|
||||
return new Error(
|
||||
`HTTP ${resp.status} ${resp.statusText} from ${resp.url}\n\n${text}`,
|
||||
);
|
||||
}
|
||||
|
||||
fetch(url: RequestInfo | URL, init?: RequestInit): Promise<Response> {
|
||||
// Custom Node.js fetch
|
||||
const customFetch = async (input: URL | RequestInfo, init: any) => {
|
||||
|
|
@ -399,44 +444,9 @@ export abstract class BaseLLM implements ILLM {
|
|||
if (resp.status === 499) {
|
||||
return resp; // client side cancellation
|
||||
}
|
||||
let text = await resp.text();
|
||||
if (resp.status === 404 && !resp.url.includes("/v1")) {
|
||||
const error = JSON.parse(text)?.error?.replace(/"/g, "'");
|
||||
let model = error?.match(/model '(.*)' not found/)?.[1];
|
||||
if (model && resp.url.match("127.0.0.1:11434")) {
|
||||
text = `The model "${model}" was not found. To download it, run \`ollama run ${model}\`.`;
|
||||
throw new LLMError(text, this); // No need to add HTTP status details
|
||||
} else if (text.includes("/api/chat")) {
|
||||
text =
|
||||
"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.";
|
||||
} else {
|
||||
text =
|
||||
"This may mean that you forgot to add '/v1' to the end of your 'apiBase' in config.json.";
|
||||
}
|
||||
} else if (
|
||||
resp.status === 404 &&
|
||||
resp.url.includes("api.openai.com")
|
||||
) {
|
||||
text =
|
||||
"You may need to add pre-paid credits before using the OpenAI API.";
|
||||
} else if (
|
||||
resp.status === 401 &&
|
||||
(resp.url.includes("api.mistral.ai") ||
|
||||
resp.url.includes("codestral.mistral.ai"))
|
||||
) {
|
||||
if (resp.url.includes("codestral.mistral.ai")) {
|
||||
throw new Error(
|
||||
"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.",
|
||||
);
|
||||
} else {
|
||||
throw new Error(
|
||||
"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.",
|
||||
);
|
||||
}
|
||||
}
|
||||
throw new Error(
|
||||
`HTTP ${resp.status} ${resp.statusText} from ${resp.url}\n\n${text}`,
|
||||
);
|
||||
|
||||
const error = await this.parseError(resp);
|
||||
throw error;
|
||||
}
|
||||
|
||||
return resp;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue