[Fix] Error Handling: Enhance authentication checks and add support for Groq and OpenRouter models.
This commit is contained in:
@@ -13,26 +13,20 @@ except Exception:
|
||||
from translation_utils import loadTranslatePromptConfig
|
||||
translation_lang = loadTranslationLanguages(path=".", force=True)
|
||||
|
||||
def _authentication_check(api_key: str) -> bool:
|
||||
def _authentication_check(api_key: str, base_url: str | None = None) -> bool:
|
||||
"""Check if the provided API key is valid by attempting to list models.
|
||||
"""
|
||||
try:
|
||||
client = OpenAI(
|
||||
api_key=api_key,
|
||||
base_url="https://openrouter.ai/api/v1",
|
||||
)
|
||||
client = OpenAI(api_key=api_key, base_url=base_url)
|
||||
client.models.list()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def _get_available_text_models(api_key: str) -> list[str]:
|
||||
def _get_available_text_models(api_key: str, base_url: str | None = None) -> list[str]:
|
||||
"""Extract only OpenRouter models suitable for translation and chat applications.
|
||||
"""
|
||||
client = OpenAI(
|
||||
api_key=api_key,
|
||||
base_url="https://openrouter.ai/api/v1",
|
||||
)
|
||||
client = OpenAI(api_key=api_key, base_url=base_url)
|
||||
res = client.models.list()
|
||||
allowed_models = []
|
||||
|
||||
@@ -90,13 +84,13 @@ class OpenRouterClient:
|
||||
self.openrouter_llm = None
|
||||
|
||||
def getModelList(self) -> list[str]:
|
||||
return _get_available_text_models(self.api_key) if self.api_key else []
|
||||
return _get_available_text_models(self.api_key, self.base_url) if self.api_key else []
|
||||
|
||||
def getAuthKey(self) -> str:
|
||||
return self.api_key
|
||||
|
||||
def setAuthKey(self, api_key: str) -> bool:
|
||||
result = _authentication_check(api_key)
|
||||
result = _authentication_check(api_key, self.base_url)
|
||||
if result:
|
||||
self.api_key = api_key
|
||||
return result
|
||||
|
||||
@@ -48,6 +48,12 @@ export const _useBackendErrorHandling = () => {
|
||||
updateOpenAIAuthKey,
|
||||
updateSelectedOpenAIModel,
|
||||
|
||||
updateGroqAuthKey,
|
||||
updateSelectedGroqModel,
|
||||
|
||||
updateOpenRouterAuthKey,
|
||||
updateSelectedOpenRouterModel,
|
||||
|
||||
updateLMStudioURL,
|
||||
updateSelectedLMStudioModel,
|
||||
|
||||
@@ -220,6 +226,52 @@ export const _useBackendErrorHandling = () => {
|
||||
}
|
||||
return;
|
||||
|
||||
case "/set/data/groq_auth_key":
|
||||
if (message === "Groq auth key is not valid") {
|
||||
updateGroqAuthKey(data);
|
||||
showNotification_Error(message, { category_id: "groq_auth_key" });
|
||||
} else if (message === "Authentication failure of Groq auth key") {
|
||||
updateGroqAuthKey(data);
|
||||
showNotification_Error(message, { category_id: "groq_auth_key" });
|
||||
} else {
|
||||
updateGroqAuthKey(data);
|
||||
showNotification_Error(message, { category_id: "groq_auth_key" });
|
||||
}
|
||||
return;
|
||||
|
||||
case "/set/data/selected_groq_model":
|
||||
if (message === "Groq model is not valid") {
|
||||
updateSelectedGroqModel(data);
|
||||
showNotification_Error(message, { category_id: "selected_groq_model" });
|
||||
} else {
|
||||
updateSelectedGroqModel(data);
|
||||
showNotification_Error(message, { category_id: "selected_groq_model" });
|
||||
}
|
||||
return;
|
||||
|
||||
case "/set/data/openrouter_auth_key":
|
||||
if (message === "OpenRouter auth key is not valid") {
|
||||
updateOpenRouterAuthKey(data);
|
||||
showNotification_Error(message, { category_id: "openrouter_auth_key" });
|
||||
} else if (message === "Authentication failure of OpenRouter auth key") {
|
||||
updateOpenRouterAuthKey(data);
|
||||
showNotification_Error(message, { category_id: "openrouter_auth_key" });
|
||||
} else {
|
||||
updateOpenRouterAuthKey(data);
|
||||
showNotification_Error(message, { category_id: "openrouter_auth_key" });
|
||||
}
|
||||
return;
|
||||
|
||||
case "/set/data/selected_openrouter_model":
|
||||
if (message === "OpenRouter model is not valid") {
|
||||
updateSelectedOpenRouterModel(data);
|
||||
showNotification_Error(message, { category_id: "selected_openrouter_model" });
|
||||
} else {
|
||||
updateSelectedOpenRouterModel(data);
|
||||
showNotification_Error(message, { category_id: "selected_openrouter_model" });
|
||||
}
|
||||
return;
|
||||
|
||||
case "/set/data/lmstudio_url":
|
||||
if (message === "LMStudio URL is not valid") {
|
||||
updateLMStudioURL(data);
|
||||
|
||||
Reference in New Issue
Block a user