[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()
|
||||
|
||||
if __name__ == "__main__":
|
||||
AUTH_KEY = "OPENAI_API_KEY"
|
||||
AUTH_KEY = input("OPENAI_API_KEY: ")
|
||||
client = OpenAIClient()
|
||||
client.setAuthKey(AUTH_KEY)
|
||||
models = client.getModelList()
|
||||
|
||||
@@ -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.
|
||||
@@ -192,4 +196,4 @@ if __name__ == "__main__":
|
||||
model = input("Select a model: ")
|
||||
client.setModel(model)
|
||||
client.updateClient()
|
||||
print(client.translate("こんにちは世界", "Japanese", "English"))
|
||||
print(client.translate("こんにちは世界", "Japanese", "English"))
|
||||
Reference in New Issue
Block a user