[Fix] Error Handling: Update authentication check to use OpenRouter API for key validation.
This commit is contained in:
@@ -189,7 +189,7 @@ class OpenAIClient:
|
|||||||
return content.strip()
|
return content.strip()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
AUTH_KEY = "OPENAI_API_KEY"
|
AUTH_KEY = input("OPENAI_API_KEY: ")
|
||||||
client = OpenAIClient()
|
client = OpenAIClient()
|
||||||
client.setAuthKey(AUTH_KEY)
|
client.setAuthKey(AUTH_KEY)
|
||||||
models = client.getModelList()
|
models = client.getModelList()
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import requests
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
from langchain_openai import ChatOpenAI
|
from langchain_openai import ChatOpenAI
|
||||||
from pydantic import SecretStr
|
from pydantic import SecretStr
|
||||||
@@ -16,12 +17,15 @@ except Exception:
|
|||||||
def _authentication_check(api_key: str, base_url: str | None = None) -> 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:
|
|
||||||
client = OpenAI(api_key=api_key, base_url=base_url)
|
url = "https://openrouter.ai/api/v1/auth/key"
|
||||||
client.models.list()
|
headers = {
|
||||||
return True
|
"Authorization": f"Bearer {api_key}"
|
||||||
except Exception:
|
}
|
||||||
return False
|
|
||||||
|
r = requests.get(url, headers=headers, timeout=10)
|
||||||
|
|
||||||
|
return r.status_code == 200
|
||||||
|
|
||||||
def _get_available_text_models(api_key: str, base_url: str | None = None) -> 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.
|
||||||
|
|||||||
Reference in New Issue
Block a user