modificari mari
This commit is contained in:
@@ -189,3 +189,126 @@ def update_ai_prompt(
|
||||
|
||||
db.commit()
|
||||
return {"status": "success", "file_updated": os.path.exists(PROMPT_FILE_PATH)}
|
||||
|
||||
from ..config_manager import ConfigManager
|
||||
|
||||
@router.get("/settings/ai")
|
||||
def get_ai_config(
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: auth.TokenData = Depends(auth.get_current_admin)
|
||||
):
|
||||
"""Check AI provider status and active provider."""
|
||||
gemini_key = os.environ.get("GEMINI_API_KEY")
|
||||
claude_key = os.environ.get("CLAUDE_API_KEY")
|
||||
|
||||
# Get active provider from DB
|
||||
provider_setting = db.query(models.SystemSetting).filter(models.SystemSetting.key == "ai_provider").first()
|
||||
active_provider = provider_setting.value if provider_setting else "gemini"
|
||||
|
||||
return {
|
||||
"active_provider": active_provider,
|
||||
"providers": [
|
||||
{
|
||||
"id": "gemini",
|
||||
"name": "Google Gemini 2.0",
|
||||
"configured": bool(gemini_key),
|
||||
"active": active_provider == "gemini",
|
||||
"masked_key": ConfigManager.get_masked_key("GEMINI_API_KEY")
|
||||
},
|
||||
{
|
||||
"id": "claude",
|
||||
"name": "Anthropic Claude 3.5",
|
||||
"configured": bool(claude_key),
|
||||
"active": active_provider == "claude",
|
||||
"masked_key": ConfigManager.get_masked_key("CLAUDE_API_KEY")
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@router.post("/settings/ai-keys")
|
||||
def update_ai_keys(
|
||||
payload: dict,
|
||||
current_admin: auth.TokenData = Depends(auth.get_current_admin)
|
||||
):
|
||||
"""Update AI API keys in inventory.env."""
|
||||
gemini_key = payload.get("gemini_api_key")
|
||||
claude_key = payload.get("claude_api_key")
|
||||
|
||||
updates = {}
|
||||
if gemini_key:
|
||||
updates["GEMINI_API_KEY"] = gemini_key
|
||||
if claude_key:
|
||||
updates["CLAUDE_API_KEY"] = claude_key
|
||||
|
||||
if not updates:
|
||||
return {"status": "no_change"}
|
||||
|
||||
ConfigManager.update_keys(updates)
|
||||
return {
|
||||
"status": "success",
|
||||
"gemini_configured": bool(os.environ.get("GEMINI_API_KEY")),
|
||||
"claude_configured": bool(os.environ.get("CLAUDE_API_KEY"))
|
||||
}
|
||||
|
||||
@router.post("/settings/test-ai-key")
|
||||
def test_ai_key(
|
||||
payload: dict,
|
||||
current_admin: auth.TokenData = Depends(auth.get_current_admin)
|
||||
):
|
||||
"""Test AI API key connectivity."""
|
||||
provider = payload.get("provider")
|
||||
key = payload.get("key")
|
||||
|
||||
if not provider or provider not in ["gemini", "claude"]:
|
||||
raise HTTPException(status_code=400, detail="Invalid provider")
|
||||
|
||||
# If key is masked or empty, use the one from environment
|
||||
if not key or "****" in key:
|
||||
key = os.environ.get("GEMINI_API_KEY" if provider == "gemini" else "CLAUDE_API_KEY")
|
||||
|
||||
if not key:
|
||||
raise HTTPException(status_code=400, detail="No API key provided or configured")
|
||||
|
||||
try:
|
||||
if provider == "gemini":
|
||||
from google import genai
|
||||
client = genai.Client(api_key=key, http_options={'api_version': 'v1beta'})
|
||||
# Listing models is the standard way to check key validity
|
||||
client.models.list()
|
||||
return {"status": "success", "message": "Google Gemini API connection verified!"}
|
||||
|
||||
elif provider == "claude":
|
||||
import anthropic
|
||||
client = anthropic.Anthropic(api_key=key)
|
||||
# Claude also supports model listing for key verification
|
||||
client.models.list(limit=1)
|
||||
return {"status": "success", "message": "Anthropic Claude API connection verified!"}
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
if "API_KEY_INVALID" in error_msg or "invalid_api_key" in error_msg:
|
||||
error_msg = "The API key provided is invalid."
|
||||
elif "PERMISSION_DENIED" in error_msg:
|
||||
error_msg = "Permission denied. Check if the key has correct permissions."
|
||||
|
||||
raise HTTPException(status_code=400, detail=f"{provider.capitalize()} Test Failed: {error_msg}")
|
||||
|
||||
@router.post("/settings/ai")
|
||||
def update_ai_provider(
|
||||
payload: dict,
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: auth.TokenData = Depends(auth.get_current_admin)
|
||||
):
|
||||
"""Update the active AI provider."""
|
||||
provider = payload.get("provider")
|
||||
if provider not in ["gemini", "claude"]:
|
||||
raise HTTPException(status_code=400, detail="Invalid provider. Must be 'gemini' or 'claude'.")
|
||||
|
||||
existing = db.query(models.SystemSetting).filter(models.SystemSetting.key == "ai_provider").first()
|
||||
if existing:
|
||||
existing.value = provider
|
||||
else:
|
||||
db.add(models.SystemSetting(key="ai_provider", value=provider))
|
||||
|
||||
db.commit()
|
||||
return {"status": "success", "active_provider": provider}
|
||||
|
||||
Reference in New Issue
Block a user