[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
|
from translation_utils import loadTranslatePromptConfig
|
||||||
translation_lang = loadTranslationLanguages(path=".", force=True)
|
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.
|
"""Check if the provided API key is valid by attempting to list models.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
client = OpenAI(
|
client = OpenAI(api_key=api_key, base_url=base_url)
|
||||||
api_key=api_key,
|
|
||||||
base_url="https://openrouter.ai/api/v1",
|
|
||||||
)
|
|
||||||
client.models.list()
|
client.models.list()
|
||||||
return True
|
return True
|
||||||
except Exception:
|
except Exception:
|
||||||
return False
|
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.
|
"""Extract only OpenRouter models suitable for translation and chat applications.
|
||||||
"""
|
"""
|
||||||
client = OpenAI(
|
client = OpenAI(api_key=api_key, base_url=base_url)
|
||||||
api_key=api_key,
|
|
||||||
base_url="https://openrouter.ai/api/v1",
|
|
||||||
)
|
|
||||||
res = client.models.list()
|
res = client.models.list()
|
||||||
allowed_models = []
|
allowed_models = []
|
||||||
|
|
||||||
@@ -90,13 +84,13 @@ class OpenRouterClient:
|
|||||||
self.openrouter_llm = None
|
self.openrouter_llm = None
|
||||||
|
|
||||||
def getModelList(self) -> list[str]:
|
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:
|
def getAuthKey(self) -> str:
|
||||||
return self.api_key
|
return self.api_key
|
||||||
|
|
||||||
def setAuthKey(self, api_key: str) -> bool:
|
def setAuthKey(self, api_key: str) -> bool:
|
||||||
result = _authentication_check(api_key)
|
result = _authentication_check(api_key, self.base_url)
|
||||||
if result:
|
if result:
|
||||||
self.api_key = api_key
|
self.api_key = api_key
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -48,6 +48,12 @@ export const _useBackendErrorHandling = () => {
|
|||||||
updateOpenAIAuthKey,
|
updateOpenAIAuthKey,
|
||||||
updateSelectedOpenAIModel,
|
updateSelectedOpenAIModel,
|
||||||
|
|
||||||
|
updateGroqAuthKey,
|
||||||
|
updateSelectedGroqModel,
|
||||||
|
|
||||||
|
updateOpenRouterAuthKey,
|
||||||
|
updateSelectedOpenRouterModel,
|
||||||
|
|
||||||
updateLMStudioURL,
|
updateLMStudioURL,
|
||||||
updateSelectedLMStudioModel,
|
updateSelectedLMStudioModel,
|
||||||
|
|
||||||
@@ -220,6 +226,52 @@ export const _useBackendErrorHandling = () => {
|
|||||||
}
|
}
|
||||||
return;
|
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":
|
case "/set/data/lmstudio_url":
|
||||||
if (message === "LMStudio URL is not valid") {
|
if (message === "LMStudio URL is not valid") {
|
||||||
updateLMStudioURL(data);
|
updateLMStudioURL(data);
|
||||||
|
|||||||
Reference in New Issue
Block a user