ollama error handling

This commit is contained in:
Madhav160804 2024-11-05 02:49:24 +05:30
parent 794f35fc37
commit 1c739e521d
2 changed files with 16 additions and 15 deletions

View file

@ -237,8 +237,6 @@ export const sendOllamaMsg: SendLLMMessageFnTypeInternal = ({ messages, onText,
let didAbort = false
let fullText = ""
// if abort is called, onFinalMessage is NOT called, and no later onTexts are called either
abortRef.current = () => {
didAbort = true;
};
@ -248,15 +246,14 @@ export const sendOllamaMsg: SendLLMMessageFnTypeInternal = ({ messages, onText,
// First check if model exists
ollama.list()
.then(async models => {
const modelExists = models.models.some(m => m.name.startsWith(voidConfig.ollama.model));
const installedModels = models.models.map(m => m.name.replace(/:latest$/, ''))
const modelExists = installedModels.some(m => m.startsWith(voidConfig.ollama.model));
if (!modelExists) {
// Send the error message as part of the chat response
const errorMessage = `The model "${voidConfig.ollama.model}" is not available locally. Please run 'ollama pull ${voidConfig.ollama.model}' to download it first.
Try selecting one from the Installed models: ${installedModels.join(', ')}`;
const errorMessage = `The model "${voidConfig.ollama.model}" is not available locally. Please run 'ollama pull ${voidConfig.ollama.model}' to download it first or
try selecting one from the Installed models: ${installedModels.join(', ')}`;
onText(errorMessage, errorMessage);
onFinalMessage(errorMessage);
return Promise.reject(); // Skip the chat attempt
return Promise.reject();
}
return ollama.chat({
@ -267,12 +264,11 @@ export const sendOllamaMsg: SendLLMMessageFnTypeInternal = ({ messages, onText,
});
})
.then(async stream => {
if (!stream) return; // Skip if model check failed
if (!stream) return;
abortRef.current = () => {
didAbort = true
}
// iterate through the stream
for await (const chunk of stream) {
if (didAbort) return;
const newText = chunk.message.content;
@ -282,7 +278,12 @@ export const sendOllamaMsg: SendLLMMessageFnTypeInternal = ({ messages, onText,
onFinalMessage(fullText);
})
.catch(error => {
if (error) { // Only show other errors if they exist
// Check if the error is a connection error
if (error instanceof Error && error.message.includes('Failed to fetch')) {
const errorMessage = 'Ollama service is not running. Please start the Ollama service and try again.';
onText(errorMessage, errorMessage);
onFinalMessage(errorMessage);
} else if (error) {
onError(error);
}
});

View file

@ -119,11 +119,11 @@ const voidConfigInfo: Record<
'http://127.0.0.1:11434'
),
// TODO we should allow user to select model inside Void, but for now we'll just let them handle the Ollama setup on their own
// model: configEnum(
// 'Ollama model to use.',
// 'llama3.1',
// ["codegemma", "codegemma:2b", "codegemma:7b", "codellama", "codellama:7b", "codellama:13b", "codellama:34b", "codellama:70b", "codellama:code", "codellama:python", "command-r", "command-r:35b", "command-r-plus", "command-r-plus:104b", "deepseek-coder-v2", "deepseek-coder-v2:16b", "deepseek-coder-v2:236b", "falcon2", "falcon2:11b", "firefunction-v2", "firefunction-v2:70b", "gemma", "gemma:2b", "gemma:7b", "gemma2", "gemma2:2b", "gemma2:9b", "gemma2:27b", "llama2", "llama2:7b", "llama2:13b", "llama2:70b", "llama3", "llama3:8b", "llama3:70b", "llama3-chatqa", "llama3-chatqa:8b", "llama3-chatqa:70b", "llama3-gradient", "llama3-gradient:8b", "llama3-gradient:70b", "llama3.1", "llama3.1:8b", "llama3.1:70b", "llama3.1:405b", "llava", "llava:7b", "llava:13b", "llava:34b", "llava-llama3", "llava-llama3:8b", "llava-phi3", "llava-phi3:3.8b", "mistral", "mistral:7b", "mistral-large", "mistral-large:123b", "mistral-nemo", "mistral-nemo:12b", "mixtral", "mixtral:8x7b", "mixtral:8x22b", "moondream", "moondream:1.8b", "openhermes", "openhermes:v2.5", "phi3", "phi3:3.8b", "phi3:14b", "phi3.5", "phi3.5:3.8b", "qwen", "qwen:7b", "qwen:14b", "qwen:32b", "qwen:72b", "qwen:110b", "qwen2", "qwen2:0.5b", "qwen2:1.5b", "qwen2:7b", "qwen2:72b", "smollm", "smollm:135m", "smollm:360m", "smollm:1.7b"] as const
// ),
model: configEnum(
'Ollama model to use.',
'llama3.1',
["codegemma", "codegemma:2b", "codegemma:7b", "codellama", "codellama:7b", "codellama:13b", "codellama:34b", "codellama:70b", "codellama:code", "codellama:python", "command-r", "command-r:35b", "command-r-plus", "command-r-plus:104b", "deepseek-coder-v2", "deepseek-coder-v2:16b", "deepseek-coder-v2:236b", "falcon2", "falcon2:11b", "firefunction-v2", "firefunction-v2:70b", "gemma", "gemma:2b", "gemma:7b", "gemma2", "gemma2:2b", "gemma2:9b", "gemma2:27b", "llama2", "llama2:7b", "llama2:13b", "llama2:70b", "llama3", "llama3:8b", "llama3:70b", "llama3-chatqa", "llama3-chatqa:8b", "llama3-chatqa:70b", "llama3-gradient", "llama3-gradient:8b", "llama3-gradient:70b", "llama3.1", "llama3.2", "llama3.1:8b", "llama3.1:70b", "llama3.1:405b", "llava", "llava:7b", "llava:13b", "llava:34b", "llava-llama3", "llava-llama3:8b", "llava-phi3", "llava-phi3:3.8b", "mistral", "mistral:7b", "mistral-large", "mistral-large:123b", "mistral-nemo", "mistral-nemo:12b", "mixtral", "mixtral:8x7b", "mixtral:8x22b", "moondream", "moondream:1.8b", "openhermes", "openhermes:v2.5", "phi3", "phi3:3.8b", "phi3:14b", "phi3.5", "phi3.5:3.8b", "qwen", "qwen:7b", "qwen:14b", "qwen:32b", "qwen:72b", "qwen:110b", "qwen2", "qwen2:0.5b", "qwen2:1.5b", "qwen2:7b", "qwen2:72b", "smollm", "smollm:135m", "smollm:360m", "smollm:1.7b"] as const
),
},
openRouter: {
model: configString(