style: fix all flake8 linter errors across app/ directory
- Run Black formatter and isort on all app/ files - Remove unused imports (F401) across multiple files - Add # noqa: F401 for intentional re-exports in celery_worker.py, tasks/__init__.py, utils.py, frontend.py, views/base.py - Fix f-strings without placeholders (F541) in azure.py, notification.py, check_credentials.py, upload_to_onedrive.py, settings.py - Fix bare except (E722) in upload_to_sftp.py - Fix block comment format (E265) in models.py - Move imports to top of file to fix E402 in celery_app.py, celery_worker.py - Fix line-too-long (E501) by wrapping strings in multiple files - Remove unused variable (F841) in upload_to_nextcloud.py Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
+17
-25
@@ -1,10 +1,10 @@
|
||||
"""
|
||||
OpenAI API endpoints
|
||||
"""
|
||||
from fastapi import APIRouter, Request, HTTPException, status
|
||||
|
||||
import logging
|
||||
import os
|
||||
import requests
|
||||
|
||||
from fastapi import APIRouter, Request
|
||||
|
||||
from app.auth import require_login
|
||||
from app.config import settings
|
||||
@@ -14,6 +14,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/openai/test")
|
||||
@require_login
|
||||
async def test_openai_connection(request: Request):
|
||||
@@ -22,54 +23,45 @@ async def test_openai_connection(request: Request):
|
||||
"""
|
||||
try:
|
||||
import openai
|
||||
|
||||
|
||||
logger.info("Testing OpenAI API key validity")
|
||||
|
||||
|
||||
# Check if API key is configured
|
||||
if not settings.openai_api_key:
|
||||
logger.warning("No OpenAI API key configured")
|
||||
return {
|
||||
"status": "error",
|
||||
"message": "No OpenAI API key is configured"
|
||||
}
|
||||
|
||||
return {"status": "error", "message": "No OpenAI API key is configured"}
|
||||
|
||||
# Configure the client
|
||||
client = openai.OpenAI(api_key=settings.openai_api_key)
|
||||
|
||||
|
||||
# Try to make a simple request to validate the key
|
||||
try:
|
||||
# Use a models list endpoint as a simple validation
|
||||
models = client.models.list()
|
||||
|
||||
|
||||
# If we got here, the key is valid
|
||||
logger.info("OpenAI API key is valid")
|
||||
return {
|
||||
"status": "success",
|
||||
"message": "OpenAI API key is valid",
|
||||
"models_available": len(models.data) if hasattr(models, "data") else "Unknown"
|
||||
"models_available": len(models.data) if hasattr(models, "data") else "Unknown",
|
||||
}
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
logger.error(f"OpenAI API key test failed: {error_msg}")
|
||||
|
||||
|
||||
# Determine if this is an authentication error
|
||||
is_auth_error = "auth" in error_msg.lower() or "api key" in error_msg.lower()
|
||||
|
||||
|
||||
return {
|
||||
"status": "error",
|
||||
"message": f"API key validation failed: {error_msg}",
|
||||
"is_auth_error": is_auth_error
|
||||
"is_auth_error": is_auth_error,
|
||||
}
|
||||
|
||||
|
||||
except ImportError:
|
||||
logger.exception("OpenAI package not installed")
|
||||
return {
|
||||
"status": "error",
|
||||
"message": "OpenAI package not installed"
|
||||
}
|
||||
return {"status": "error", "message": "OpenAI package not installed"}
|
||||
except Exception as e:
|
||||
logger.exception("Unexpected error testing OpenAI connection")
|
||||
return {
|
||||
"status": "error",
|
||||
"message": f"Unexpected error: {str(e)}"
|
||||
}
|
||||
return {"status": "error", "message": f"Unexpected error: {str(e)}"}
|
||||
|
||||
Reference in New Issue
Block a user