Upload rag_api.py with huggingface_hub
Browse files- rag_api.py +20 -9
rag_api.py
CHANGED
|
@@ -75,7 +75,12 @@ GROQ_MODEL = os.environ.get("GROQ_MODEL", "openai/gpt-oss-120b")
|
|
| 75 |
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY", "")
|
| 76 |
DEEPSEEK_MODEL = os.environ.get("DEEPSEEK_MODEL", "deepseek-flash")
|
| 77 |
DEEPSEEK_BASE_URL = os.environ.get("DEEPSEEK_BASE_URL", "https://api.deepseek.com")
|
| 78 |
-
DEEPSEEK_TIMEOUT = int(os.environ.get("DEEPSEEK_TIMEOUT", "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
# --------------------------------------------------------
|
| 81 |
# 2. PROMPTS
|
|
@@ -240,13 +245,15 @@ class _LLMDeepSeek:
|
|
| 240 |
self._url = f"{DEEPSEEK_BASE_URL.rstrip('/')}/chat/completions"
|
| 241 |
|
| 242 |
def invoke(self, prompt: str) -> _RespuestaLLM:
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
| 250 |
|
| 251 |
peticion = urllib.request.Request(
|
| 252 |
self._url,
|
|
@@ -393,7 +400,11 @@ def health():
|
|
| 393 |
|
| 394 |
|
| 395 |
@app.post("/query")
|
| 396 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
if llm is None:
|
| 398 |
return {"error": "El sistema RAG no se pudo cargar."}
|
| 399 |
|
|
|
|
| 75 |
DEEPSEEK_API_KEY = os.environ.get("DEEPSEEK_API_KEY", "")
|
| 76 |
DEEPSEEK_MODEL = os.environ.get("DEEPSEEK_MODEL", "deepseek-flash")
|
| 77 |
DEEPSEEK_BASE_URL = os.environ.get("DEEPSEEK_BASE_URL", "https://api.deepseek.com")
|
| 78 |
+
DEEPSEEK_TIMEOUT = int(os.environ.get("DEEPSEEK_TIMEOUT", "30"))
|
| 79 |
+
# Igual que el respaldo del portal. Sin esto, DeepSeek usa su modo "thinking"
|
| 80 |
+
# por defecto (esfuerzo alto) y tarda much铆simo m谩s: el Space pasaba de los 15 s
|
| 81 |
+
# que espera el portal, que acababa cortando y respondiendo por el respaldo.
|
| 82 |
+
# Cadena vac铆a = no enviar el par谩metro.
|
| 83 |
+
DEEPSEEK_REASONING_EFFORT = os.environ.get("DEEPSEEK_REASONING_EFFORT", "low")
|
| 84 |
|
| 85 |
# --------------------------------------------------------
|
| 86 |
# 2. PROMPTS
|
|
|
|
| 245 |
self._url = f"{DEEPSEEK_BASE_URL.rstrip('/')}/chat/completions"
|
| 246 |
|
| 247 |
def invoke(self, prompt: str) -> _RespuestaLLM:
|
| 248 |
+
parametros = {
|
| 249 |
+
"model": self._model,
|
| 250 |
+
"messages": [{"role": "user", "content": prompt}],
|
| 251 |
+
"temperature": 0.15,
|
| 252 |
+
}
|
| 253 |
+
if DEEPSEEK_REASONING_EFFORT:
|
| 254 |
+
parametros["reasoning_effort"] = DEEPSEEK_REASONING_EFFORT
|
| 255 |
+
|
| 256 |
+
cuerpo = json.dumps(parametros).encode("utf-8")
|
| 257 |
|
| 258 |
peticion = urllib.request.Request(
|
| 259 |
self._url,
|
|
|
|
| 400 |
|
| 401 |
|
| 402 |
@app.post("/query")
|
| 403 |
+
def process_query(request: QueryRequest):
|
| 404 |
+
# S铆ncrono A PROP脫SITO. `retriever.invoke` y `llm.invoke` son bloqueantes: en
|
| 405 |
+
# un `async def` bloquean el bucle de eventos y el Space entero se queda
|
| 406 |
+
# esperando a la petici贸n m谩s lenta (se midieron 120 s por petici贸n, en cola).
|
| 407 |
+
# Al declararlo `def`, FastAPI lo ejecuta en un hilo y no se serializan.
|
| 408 |
if llm is None:
|
| 409 |
return {"error": "El sistema RAG no se pudo cargar."}
|
| 410 |
|