Spaces:
Sleeping
Sleeping
Implement adaptive retry for K2 timeouts
Browse files- app/services/k2_think_engine.py +30 -12
app/services/k2_think_engine.py
CHANGED
|
@@ -121,20 +121,38 @@ class K2ThinkEngine:
|
|
| 121 |
Start directly with <think> if needed, then output the JSON inside [RESULT] tags.
|
| 122 |
"""
|
| 123 |
|
| 124 |
-
# 3. Appel au modèle
|
| 125 |
-
|
| 126 |
-
model
|
| 127 |
-
openai_api_key
|
| 128 |
-
openai_api_base
|
| 129 |
-
temperature
|
| 130 |
-
max_tokens
|
| 131 |
-
timeout
|
| 132 |
-
max_retries
|
| 133 |
-
|
| 134 |
|
| 135 |
logger.info("Sending request to K2 Think...")
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
logger.info(f"Raw K2 response length: {len(raw_content)}")
|
| 139 |
|
| 140 |
# 4. Extraction du JSON
|
|
|
|
| 121 |
Start directly with <think> if needed, then output the JSON inside [RESULT] tags.
|
| 122 |
"""
|
| 123 |
|
| 124 |
+
# 3. Appel au modèle (avec Retry Adaptatif en cas de Timeout)
|
| 125 |
+
chat_config = {
|
| 126 |
+
"model": "MBZUAI-IFM/K2-Think-v2",
|
| 127 |
+
"openai_api_key": settings.K2_THINK_API_KEY,
|
| 128 |
+
"openai_api_base": settings.K2_THINK_API_URL,
|
| 129 |
+
"temperature": 0.1,
|
| 130 |
+
"max_tokens": 12000,
|
| 131 |
+
"timeout": 110, # Juste en dessous des 120s de Cloudflare pour catcher l'erreur proprement
|
| 132 |
+
"max_retries": 0
|
| 133 |
+
}
|
| 134 |
|
| 135 |
logger.info("Sending request to K2 Think...")
|
| 136 |
+
raw_content = ""
|
| 137 |
+
try:
|
| 138 |
+
chat = ChatOpenAI(**chat_config)
|
| 139 |
+
response = await chat.ainvoke([HumanMessage(content=instruction_prompt)])
|
| 140 |
+
raw_content = response.content
|
| 141 |
+
except Exception as e:
|
| 142 |
+
if "524" in str(e) or "timeout" in str(e).lower():
|
| 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
|
| 146 |
+
chat_config["timeout"] = 115
|
| 147 |
+
# On réduit aussi le contexte dans le prompt pour la tentative de secours
|
| 148 |
+
emergency_context = "\n\n".join([f"--- DOC: {d.title} ---\n{d.content[:3000]}" for d in request.documents])
|
| 149 |
+
emergency_prompt = instruction_prompt.replace(context, emergency_context)
|
| 150 |
+
|
| 151 |
+
chat = ChatOpenAI(**chat_config)
|
| 152 |
+
response = await chat.ainvoke([HumanMessage(content=emergency_prompt)])
|
| 153 |
+
raw_content = response.content
|
| 154 |
+
else:
|
| 155 |
+
raise e
|
| 156 |
logger.info(f"Raw K2 response length: {len(raw_content)}")
|
| 157 |
|
| 158 |
# 4. Extraction du JSON
|