Spaces:
Sleeping
Sleeping
Fix K2 timeout detection and JSON parsing errors
Browse files- app/services/k2_think_engine.py +17 -4
- scratch/check_db.py +43 -0
app/services/k2_think_engine.py
CHANGED
|
@@ -139,7 +139,12 @@ Start directly with <think> if needed, then output the JSON inside [RESULT] tags
|
|
| 139 |
response = await chat.ainvoke([HumanMessage(content=instruction_prompt)])
|
| 140 |
raw_content = response.content
|
| 141 |
except Exception as e:
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
logger.warning("K2 API Timeout detected. Retrying with reduced token budget...")
|
| 144 |
# Réduction drastique pour passer le timeout
|
| 145 |
chat_config["max_tokens"] = 4000
|
|
@@ -196,9 +201,17 @@ Start directly with <think> if needed, then output the JSON inside [RESULT] tags
|
|
| 196 |
try:
|
| 197 |
# Tentative 1: Standard
|
| 198 |
k2_analysis = json.loads(clean_json)
|
| 199 |
-
except json.JSONDecodeError:
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
repaired = clean_json.strip()
|
| 203 |
open_braces = repaired.count('{')
|
| 204 |
close_braces = repaired.count('}')
|
|
|
|
| 139 |
response = await chat.ainvoke([HumanMessage(content=instruction_prompt)])
|
| 140 |
raw_content = response.content
|
| 141 |
except Exception as e:
|
| 142 |
+
is_timeout = (
|
| 143 |
+
"524" in str(e)
|
| 144 |
+
or "timeout" in str(e).lower()
|
| 145 |
+
or type(e).__name__ in ["APITimeoutError", "Timeout", "ReadTimeout", "TimeoutError"]
|
| 146 |
+
)
|
| 147 |
+
if is_timeout:
|
| 148 |
logger.warning("K2 API Timeout detected. Retrying with reduced token budget...")
|
| 149 |
# Réduction drastique pour passer le timeout
|
| 150 |
chat_config["max_tokens"] = 4000
|
|
|
|
| 201 |
try:
|
| 202 |
# Tentative 1: Standard
|
| 203 |
k2_analysis = json.loads(clean_json)
|
| 204 |
+
except json.JSONDecodeError as e:
|
| 205 |
+
if "Extra data" in str(e) and hasattr(e, "pos"):
|
| 206 |
+
try:
|
| 207 |
+
logger.warning(f"Extra data found at pos {e.pos}, attempting to truncate...")
|
| 208 |
+
k2_analysis = json.loads(clean_json[:e.pos].strip())
|
| 209 |
+
except Exception:
|
| 210 |
+
pass
|
| 211 |
+
|
| 212 |
+
if not k2_analysis:
|
| 213 |
+
# Tentative 2: Réparation (Fermeture des balises)
|
| 214 |
+
logger.warning("JSON parsing failed, attempting auto-repair...")
|
| 215 |
repaired = clean_json.strip()
|
| 216 |
open_braces = repaired.count('{')
|
| 217 |
close_braces = repaired.count('}')
|
scratch/check_db.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
from uuid import UUID
|
| 5 |
+
|
| 6 |
+
# Add the app directory to sys.path
|
| 7 |
+
backend_path = 'c:/Users/user/Desktop/ai-scientific-coinvestigator-backend'
|
| 8 |
+
if backend_path not in sys.path:
|
| 9 |
+
sys.path.append(backend_path)
|
| 10 |
+
|
| 11 |
+
from app.db.session import SessionLocal
|
| 12 |
+
from app.models.all_models import AnalysisRun
|
| 13 |
+
|
| 14 |
+
def check_analysis(analysis_id):
|
| 15 |
+
db = SessionLocal()
|
| 16 |
+
try:
|
| 17 |
+
analysis = db.query(AnalysisRun).filter(AnalysisRun.id == UUID(analysis_id)).first()
|
| 18 |
+
if not analysis:
|
| 19 |
+
print(f"Analysis {analysis_id} not found in DB")
|
| 20 |
+
return
|
| 21 |
+
|
| 22 |
+
print(f"ID: {analysis.id}")
|
| 23 |
+
print(f"Project ID: {analysis.project_id}")
|
| 24 |
+
print(f"Status: {analysis.status}")
|
| 25 |
+
print(f"Model: {analysis.model_used}")
|
| 26 |
+
print(f"Result Data Type: {type(analysis.result_data)}")
|
| 27 |
+
if analysis.result_data:
|
| 28 |
+
import json
|
| 29 |
+
print("Result Data JSON:")
|
| 30 |
+
print(json.dumps(analysis.result_data, indent=2))
|
| 31 |
+
else:
|
| 32 |
+
print("Result Data: None")
|
| 33 |
+
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print(f"Error: {e}")
|
| 36 |
+
finally:
|
| 37 |
+
db.close()
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
if len(sys.argv) > 1:
|
| 41 |
+
check_analysis(sys.argv[1])
|
| 42 |
+
else:
|
| 43 |
+
print("Usage: python check_db.py <analysis_id>")
|