Update app.py
Browse files
app.py
CHANGED
|
@@ -898,41 +898,6 @@ async def rebuild_custom_ai(
|
|
| 898 |
logger.error(f"Rebuild custom AI error: {e}")
|
| 899 |
raise HTTPException(status_code=500, detail=f"Rebuild error: {str(e)}")
|
| 900 |
|
| 901 |
-
@app.post("/chat/custom/{ai_id}", response_model=QuestionResponse)
|
| 902 |
-
async def chat_custom_ai_enhanced(
|
| 903 |
-
ai_id: str,
|
| 904 |
-
request: QuestionRequest,
|
| 905 |
-
current_user: dict = Depends(get_current_user)
|
| 906 |
-
):
|
| 907 |
-
"""Chat with custom AI using enhanced fallback system"""
|
| 908 |
-
if not lightrag_manager:
|
| 909 |
-
raise HTTPException(status_code=503, detail="LightRAG system not initialized")
|
| 910 |
-
|
| 911 |
-
try:
|
| 912 |
-
# Generate conversation ID if not provided
|
| 913 |
-
conversation_id = request.conversation_id or str(uuid.uuid4())
|
| 914 |
-
|
| 915 |
-
# Query with enhanced custom AI fallback
|
| 916 |
-
result = await query_custom_ai_with_fallback(
|
| 917 |
-
lightrag_manager=lightrag_manager,
|
| 918 |
-
ai_id=ai_id,
|
| 919 |
-
question=request.question,
|
| 920 |
-
conversation_id=conversation_id,
|
| 921 |
-
user_id=current_user["id"],
|
| 922 |
-
preferred_mode=request.mode or "hybrid"
|
| 923 |
-
)
|
| 924 |
-
|
| 925 |
-
await update_message_stats(request.question, result["answer"])
|
| 926 |
-
return QuestionResponse(
|
| 927 |
-
answer=result["answer"],
|
| 928 |
-
mode=result["mode"],
|
| 929 |
-
status=result["status"],
|
| 930 |
-
conversation_id=conversation_id
|
| 931 |
-
)
|
| 932 |
-
|
| 933 |
-
except Exception as e:
|
| 934 |
-
logger.error(f"Custom AI chat error: {e}")
|
| 935 |
-
raise HTTPException(status_code=500, detail=f"Chat error: {str(e)}")
|
| 936 |
|
| 937 |
|
| 938 |
@app.post("/admin/stats/recalculate")
|
|
@@ -1495,8 +1460,16 @@ async def query_rag_with_fallback(
|
|
| 1495 |
) -> Dict[str, Any]:
|
| 1496 |
"""Query RAG with automatic fallback to working modes"""
|
| 1497 |
|
| 1498 |
-
#
|
| 1499 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1500 |
|
| 1501 |
# Start with user's preferred mode
|
| 1502 |
if preferred_mode in fallback_modes:
|
|
|
|
| 898 |
logger.error(f"Rebuild custom AI error: {e}")
|
| 899 |
raise HTTPException(status_code=500, detail=f"Rebuild error: {str(e)}")
|
| 900 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 901 |
|
| 902 |
|
| 903 |
@app.post("/admin/stats/recalculate")
|
|
|
|
| 1460 |
) -> Dict[str, Any]:
|
| 1461 |
"""Query RAG with automatic fallback to working modes"""
|
| 1462 |
|
| 1463 |
+
# AI-type specific fallback modes
|
| 1464 |
+
fallback_strategies = {
|
| 1465 |
+
"custom": ["hybrid", "naive", "local", "global"], # Custom AIs: Start with most reliable
|
| 1466 |
+
"fire-safety": ["hybrid", "local", "global", "naive"], # Fire safety: Complex modes first
|
| 1467 |
+
"general": ["local", "naive", "global", "hybrid"], # General: Local knowledge first
|
| 1468 |
+
"physics": ["global", "hybrid", "local", "naive"] # Physics: Global knowledge first
|
| 1469 |
+
}
|
| 1470 |
+
|
| 1471 |
+
# Get fallback modes for this AI type, default to original order
|
| 1472 |
+
fallback_modes = fallback_strategies.get(ai_type, ["hybrid", "naive", "local", "global"])
|
| 1473 |
|
| 1474 |
# Start with user's preferred mode
|
| 1475 |
if preferred_mode in fallback_modes:
|