[Fix] Error Handling: Update authentication check to use OpenRouter API for key validation.

This commit is contained in:
misyaguziya
2025-12-29 19:15:08 +09:00
parent bf1c0236b5
commit 4031a5556d
2 changed files with 12 additions and 8 deletions

View File

@@ -189,7 +189,7 @@ class OpenAIClient:
return content.strip()
if __name__ == "__main__":
AUTH_KEY = "OPENAI_API_KEY"
AUTH_KEY = input("OPENAI_API_KEY: ")
client = OpenAIClient()
client.setAuthKey(AUTH_KEY)
models = client.getModelList()

View File

@@ -1,3 +1,4 @@
import requests
from openai import OpenAI
from langchain_openai import ChatOpenAI
from pydantic import SecretStr
@@ -16,12 +17,15 @@ except Exception:
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=base_url)
client.models.list()
return True
except Exception:
return False
url = "https://openrouter.ai/api/v1/auth/key"
headers = {
"Authorization": f"Bearer {api_key}"
}
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]:
"""Extract only OpenRouter models suitable for translation and chat applications.