Spaces:
Sleeping
Sleeping
Update modules/agent_tools.py
Browse files- modules/agent_tools.py +137 -77
modules/agent_tools.py
CHANGED
|
@@ -2,15 +2,15 @@
|
|
| 2 |
Capa 3 · Agent Tools
|
| 3 |
====================
|
| 4 |
Tools formales para el orquestador LLaMA 3.1 70B.
|
| 5 |
-
Cada tool lee directamente del bucket /data — cero alucinaciones.
|
| 6 |
|
| 7 |
Tools disponibles:
|
| 8 |
-
- read_banxico
|
| 9 |
-
- search_historico
|
| 10 |
-
- read_worldbank
|
| 11 |
-
- read_inegi
|
| 12 |
-
- search_wikipedia
|
| 13 |
-
- read_ift
|
| 14 |
"""
|
| 15 |
|
| 16 |
import json
|
|
@@ -19,7 +19,7 @@ from datetime import datetime
|
|
| 19 |
|
| 20 |
DATA_PATH = "/data"
|
| 21 |
|
| 22 |
-
#
|
| 23 |
|
| 24 |
TOOLS = [
|
| 25 |
{
|
|
@@ -168,24 +168,46 @@ TOOLS = [
|
|
| 168 |
"function": {
|
| 169 |
"name": "read_inegi",
|
| 170 |
"description": (
|
| 171 |
-
"Lee
|
|
|
|
| 172 |
"Úsala para preguntas sobre población, IGAE (actividad económica), "
|
| 173 |
-
"ocupación/desempleo, índice de precios al consumidor
|
|
|
|
| 174 |
),
|
| 175 |
"parameters": {
|
| 176 |
"type": "object",
|
| 177 |
"properties": {
|
| 178 |
"indicador": {
|
| 179 |
"type": "string",
|
| 180 |
-
"enum": [
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
}
|
| 183 |
},
|
| 184 |
"required": ["indicador"]
|
| 185 |
}
|
| 186 |
}
|
| 187 |
},
|
| 188 |
-
# ── NUEVA TOOL: IFT Telecomunicaciones ────────────────────────────────────
|
| 189 |
{
|
| 190 |
"type": "function",
|
| 191 |
"function": {
|
|
@@ -232,7 +254,7 @@ TOOLS = [
|
|
| 232 |
]
|
| 233 |
|
| 234 |
|
| 235 |
-
#
|
| 236 |
|
| 237 |
def read_banxico(indicador: str) -> dict:
|
| 238 |
"""Lee banxico.json y retorna el dato solicitado."""
|
|
@@ -502,81 +524,121 @@ def read_worldbank(categoria: str, indicador: str = None) -> dict:
|
|
| 502 |
|
| 503 |
|
| 504 |
def read_inegi(indicador: str) -> dict:
|
| 505 |
-
"""
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
if indicador == "todos":
|
| 512 |
-
# Inyectar poblacion_total si no está (INEGI API falla frecuentemente)
|
| 513 |
-
if "poblacion_total" not in data:
|
| 514 |
-
data["poblacion_total"] = "128,455,567 personas (World Bank 2023 · SP.POP.TOTL)"
|
| 515 |
return {
|
| 516 |
-
"
|
| 517 |
-
"
|
|
|
|
| 518 |
"fecha_extraccion": data.get("timestamp", "N/A"),
|
|
|
|
| 519 |
"datos": data
|
| 520 |
}
|
| 521 |
|
| 522 |
mapa = {
|
| 523 |
-
|
| 524 |
-
"
|
| 525 |
-
"
|
| 526 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
}
|
| 528 |
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
if not isinstance(fuente_data, dict):
|
| 533 |
-
continue
|
| 534 |
-
for clave in claves:
|
| 535 |
-
if clave in fuente_data:
|
| 536 |
-
valor = fuente_data[clave]
|
| 537 |
-
if isinstance(valor, dict):
|
| 538 |
-
valor = valor.get("valor", str(valor))
|
| 539 |
-
return {
|
| 540 |
-
"fuente": "INEGI · INEGIpy",
|
| 541 |
-
"archivo": "inegi.json",
|
| 542 |
-
"indicador": indicador,
|
| 543 |
-
"valor": valor,
|
| 544 |
-
"fecha_extraccion": data.get("timestamp", "N/A")
|
| 545 |
-
}
|
| 546 |
-
|
| 547 |
-
claves_utiles = {k: v for k, v in data.items()
|
| 548 |
-
if k not in ["fuente", "timestamp", "detalle"] and not isinstance(v, dict)}
|
| 549 |
-
if claves_utiles:
|
| 550 |
-
datos_fmt = {}
|
| 551 |
-
for k, v in claves_utiles.items():
|
| 552 |
-
try:
|
| 553 |
-
n = float(v)
|
| 554 |
-
datos_fmt[k] = f"{n:,.0f}" if n > 1_000_000 else v
|
| 555 |
-
except (TypeError, ValueError):
|
| 556 |
-
datos_fmt[k] = v
|
| 557 |
-
# Agregar poblacion_total si no está en INEGI (API falla frecuentemente)
|
| 558 |
-
if "poblacion_total" not in datos_fmt:
|
| 559 |
-
datos_fmt["poblacion_total"] = "128,455,567 personas (World Bank 2023 · SP.POP.TOTL · usado como complemento porque INEGI API no devolvió este dato)"
|
| 560 |
return {
|
| 561 |
-
"
|
| 562 |
-
"
|
| 563 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 564 |
"fecha_extraccion": data.get("timestamp", "N/A")
|
| 565 |
}
|
| 566 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
return {
|
| 568 |
-
"
|
| 569 |
-
"
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
"fecha_extraccion": "2023"
|
| 574 |
}
|
| 575 |
|
| 576 |
-
except
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
|
| 581 |
|
| 582 |
def search_wikipedia(termino: str, idioma: str = "es") -> dict:
|
|
@@ -623,8 +685,6 @@ def search_wikipedia(termino: str, idioma: str = "es") -> dict:
|
|
| 623 |
return {"error": f"Error consultando Wikipedia: {str(e)}"}
|
| 624 |
|
| 625 |
|
| 626 |
-
# ── NUEVA FUNCIÓN: read_ift ───────────────────────────────────────────────────
|
| 627 |
-
|
| 628 |
def read_ift(categoria: str = "todo") -> dict:
|
| 629 |
"""Lee /data/ift.json con datos del sector telecomunicaciones México."""
|
| 630 |
path = os.path.join(DATA_PATH, "ift.json")
|
|
@@ -691,7 +751,7 @@ def read_ift(categoria: str = "todo") -> dict:
|
|
| 691 |
return {"error": str(e)}
|
| 692 |
|
| 693 |
|
| 694 |
-
#
|
| 695 |
|
| 696 |
def search_web_tool(query: str = "", max_results: int = 3, **kwargs) -> str:
|
| 697 |
"""Búsqueda web en tiempo real via Tavily API."""
|
|
@@ -746,7 +806,7 @@ def execute_tool(tool_name: str, tool_args: dict) -> str:
|
|
| 746 |
"read_worldbank": read_worldbank,
|
| 747 |
"read_inegi": read_inegi,
|
| 748 |
"search_wikipedia": search_wikipedia,
|
| 749 |
-
"read_ift": read_ift,
|
| 750 |
}
|
| 751 |
|
| 752 |
fn = REGISTRY.get(tool_name)
|
|
|
|
| 2 |
Capa 3 · Agent Tools
|
| 3 |
====================
|
| 4 |
Tools formales para el orquestador LLaMA 3.1 70B.
|
| 5 |
+
Cada tool lee directamente del bucket /data o de fuentes verificadas — cero alucinaciones.
|
| 6 |
|
| 7 |
Tools disponibles:
|
| 8 |
+
- read_banxico -> /data/banxico.json
|
| 9 |
+
- search_historico -> /data/banxico_historico.json
|
| 10 |
+
- read_worldbank -> /data/worldbank.json
|
| 11 |
+
- read_inegi -> Dataset HF Fercor95mx/mx-data-truth-inegi/inegi.json
|
| 12 |
+
- search_wikipedia -> Wikipedia API
|
| 13 |
+
- read_ift -> /data/ift.json
|
| 14 |
"""
|
| 15 |
|
| 16 |
import json
|
|
|
|
| 19 |
|
| 20 |
DATA_PATH = "/data"
|
| 21 |
|
| 22 |
+
# --- JSON Schema de Tools (para LLaMA tool calling) -------------------------
|
| 23 |
|
| 24 |
TOOLS = [
|
| 25 |
{
|
|
|
|
| 168 |
"function": {
|
| 169 |
"name": "read_inegi",
|
| 170 |
"description": (
|
| 171 |
+
"Lee indicadores del INEGI en tiempo real desde el Dataset HF "
|
| 172 |
+
"Fercor95mx/mx-data-truth-inegi (alimentado diariamente por Azure Function). "
|
| 173 |
"Úsala para preguntas sobre población, IGAE (actividad económica), "
|
| 174 |
+
"ocupación/desempleo, índice de precios al consumidor (INPC), "
|
| 175 |
+
"PIB trimestral, tasa de desocupación, inflación general y subyacente."
|
| 176 |
),
|
| 177 |
"parameters": {
|
| 178 |
"type": "object",
|
| 179 |
"properties": {
|
| 180 |
"indicador": {
|
| 181 |
"type": "string",
|
| 182 |
+
"enum": [
|
| 183 |
+
"poblacion",
|
| 184 |
+
"igae",
|
| 185 |
+
"ocupacion",
|
| 186 |
+
"precios",
|
| 187 |
+
"pib_trimestral",
|
| 188 |
+
"tasa_desocupacion",
|
| 189 |
+
"inflacion_general",
|
| 190 |
+
"inflacion_subyacente",
|
| 191 |
+
"inpc_subyacente",
|
| 192 |
+
"todos"
|
| 193 |
+
],
|
| 194 |
+
"description": (
|
| 195 |
+
"Indicador INEGI a consultar. "
|
| 196 |
+
"poblacion=población total, igae=actividad económica mensual, "
|
| 197 |
+
"ocupacion=población ocupada, precios=INPC general, "
|
| 198 |
+
"pib_trimestral=PIB trimestral a precios 2018, "
|
| 199 |
+
"tasa_desocupacion=desocupación nacional desestacionalizada, "
|
| 200 |
+
"inflacion_general=variación anual INPC, "
|
| 201 |
+
"inflacion_subyacente=variación anual subyacente, "
|
| 202 |
+
"inpc_subyacente=índice subyacente, "
|
| 203 |
+
"todos=todos los indicadores disponibles."
|
| 204 |
+
)
|
| 205 |
}
|
| 206 |
},
|
| 207 |
"required": ["indicador"]
|
| 208 |
}
|
| 209 |
}
|
| 210 |
},
|
|
|
|
| 211 |
{
|
| 212 |
"type": "function",
|
| 213 |
"function": {
|
|
|
|
| 254 |
]
|
| 255 |
|
| 256 |
|
| 257 |
+
# --- Implementación de cada tool ---------------------------------------------
|
| 258 |
|
| 259 |
def read_banxico(indicador: str) -> dict:
|
| 260 |
"""Lee banxico.json y retorna el dato solicitado."""
|
|
|
|
| 524 |
|
| 525 |
|
| 526 |
def read_inegi(indicador: str) -> dict:
|
| 527 |
+
"""
|
| 528 |
+
Lee inegi.json directamente desde el Dataset HF usando la libreria oficial
|
| 529 |
+
huggingface_hub. Version instrumentada: cada etapa queda registrada en
|
| 530 |
+
'stage' dentro del resultado de error, para diagnostico sin ambiguedad.
|
| 531 |
+
"""
|
| 532 |
+
import json as _json
|
| 533 |
+
import traceback as _traceback
|
| 534 |
+
|
| 535 |
+
INEGI_REPO = "Fercor95mx/mx-data-truth-inegi"
|
| 536 |
+
HF_TOKEN_LOCAL = os.getenv("HF_TOKEN", "")
|
| 537 |
+
stage = "initialization"
|
| 538 |
|
| 539 |
+
try:
|
| 540 |
+
stage = "import_huggingface_hub"
|
| 541 |
+
from huggingface_hub import HfApi, hf_hub_download
|
| 542 |
+
|
| 543 |
+
stage = "validate_token_presence"
|
| 544 |
+
if not HF_TOKEN_LOCAL:
|
| 545 |
+
raise RuntimeError("HF_TOKEN no esta disponible en os.environ dentro de este proceso")
|
| 546 |
+
|
| 547 |
+
stage = "authenticate_token"
|
| 548 |
+
api = HfApi(token=HF_TOKEN_LOCAL)
|
| 549 |
+
identity = api.whoami()
|
| 550 |
+
print(f"INEGI_AUTH_OK account={identity.get('name')} token_len={len(HF_TOKEN_LOCAL)}", flush=True)
|
| 551 |
+
|
| 552 |
+
stage = "dataset_metadata"
|
| 553 |
+
dataset_info = api.dataset_info(repo_id=INEGI_REPO, revision="main")
|
| 554 |
+
|
| 555 |
+
stage = "download_inegi_json"
|
| 556 |
+
local_path = hf_hub_download(
|
| 557 |
+
repo_id=INEGI_REPO,
|
| 558 |
+
filename="inegi.json",
|
| 559 |
+
repo_type="dataset",
|
| 560 |
+
revision=dataset_info.sha,
|
| 561 |
+
token=HF_TOKEN_LOCAL,
|
| 562 |
+
)
|
| 563 |
+
print(f"INEGI_DOWNLOAD_OK path={local_path} sha={dataset_info.sha}", flush=True)
|
| 564 |
+
|
| 565 |
+
stage = "open_inegi_json"
|
| 566 |
+
with open(local_path, "r", encoding="utf-8") as f:
|
| 567 |
+
data = _json.load(f)
|
| 568 |
+
|
| 569 |
+
stage = "build_response"
|
| 570 |
if indicador == "todos":
|
|
|
|
|
|
|
|
|
|
| 571 |
return {
|
| 572 |
+
"ok": True,
|
| 573 |
+
"fuente": "INEGI · Banco de Información Económica (BIE-BISE) · Azure extractor",
|
| 574 |
+
"archivo": f"inegi.json (Dataset {INEGI_REPO})",
|
| 575 |
"fecha_extraccion": data.get("timestamp", "N/A"),
|
| 576 |
+
"dataset_revision": dataset_info.sha,
|
| 577 |
"datos": data
|
| 578 |
}
|
| 579 |
|
| 580 |
mapa = {
|
| 581 |
+
# Indicadores originales
|
| 582 |
+
"poblacion": "poblacion_total",
|
| 583 |
+
"igae": "igae_mensual",
|
| 584 |
+
"ocupacion": "poblacion_ocupada",
|
| 585 |
+
"precios": "inpc",
|
| 586 |
+
# Indicadores nuevos (los 9 que extrae Azure Function)
|
| 587 |
+
"pib_trimestral": "pib_trimestral",
|
| 588 |
+
"tasa_desocupacion": "tasa_desocupacion",
|
| 589 |
+
"inflacion_general": "inflacion_general",
|
| 590 |
+
"inflacion_subyacente":"inflacion_subyacente",
|
| 591 |
+
"inpc_subyacente": "inpc_subyacente",
|
| 592 |
}
|
| 593 |
|
| 594 |
+
clave = mapa.get(indicador, indicador)
|
| 595 |
+
if clave in data:
|
| 596 |
+
indicador_data = data[clave]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 597 |
return {
|
| 598 |
+
"ok": True,
|
| 599 |
+
"fuente": indicador_data.get("fuente", "INEGI"),
|
| 600 |
+
"archivo": f"inegi.json (Dataset {INEGI_REPO})",
|
| 601 |
+
"indicador": indicador,
|
| 602 |
+
"clave": clave,
|
| 603 |
+
"nombre": indicador_data.get("nombre", clave),
|
| 604 |
+
"valor": indicador_data.get("valor"),
|
| 605 |
+
"fecha": indicador_data.get("fecha"),
|
| 606 |
+
"unidad": indicador_data.get("unidad"),
|
| 607 |
+
"variacion_pct": indicador_data.get("variacion_pct"),
|
| 608 |
+
"dataset_revision": dataset_info.sha,
|
| 609 |
"fecha_extraccion": data.get("timestamp", "N/A")
|
| 610 |
}
|
| 611 |
+
|
| 612 |
+
claves_disponibles = [
|
| 613 |
+
k for k in data.keys()
|
| 614 |
+
if k not in ["fuente", "timestamp", "extraido_desde", "nota",
|
| 615 |
+
"exitosos", "total_series", "errores"]
|
| 616 |
+
]
|
| 617 |
return {
|
| 618 |
+
"ok": False,
|
| 619 |
+
"stage": "indicator_lookup",
|
| 620 |
+
"error_code": "INDICATOR_NOT_FOUND",
|
| 621 |
+
"error": f"Indicador '{indicador}' (clave '{clave}') no encontrado en inegi.json",
|
| 622 |
+
"claves_disponibles": claves_disponibles
|
|
|
|
| 623 |
}
|
| 624 |
|
| 625 |
+
except Exception as exc:
|
| 626 |
+
error_result = {
|
| 627 |
+
"ok": False,
|
| 628 |
+
"stage": stage,
|
| 629 |
+
"error_code": "INEGI_READ_FAILED",
|
| 630 |
+
"error_type": type(exc).__name__,
|
| 631 |
+
"error": str(exc),
|
| 632 |
+
"repo_id": INEGI_REPO,
|
| 633 |
+
"token_present": bool(HF_TOKEN_LOCAL),
|
| 634 |
+
"token_length": len(HF_TOKEN_LOCAL),
|
| 635 |
+
}
|
| 636 |
+
tb = _traceback.format_exc()
|
| 637 |
+
print(f"INEGI_READ_FAILED stage={stage} type={type(exc).__name__} error={str(exc)}", flush=True)
|
| 638 |
+
print(tb, flush=True)
|
| 639 |
+
# Mantener compatibilidad: el orquestador busca la clave "error"
|
| 640 |
+
error_result["error"] = f"[{stage}] {type(exc).__name__}: {str(exc)}"
|
| 641 |
+
return error_result
|
| 642 |
|
| 643 |
|
| 644 |
def search_wikipedia(termino: str, idioma: str = "es") -> dict:
|
|
|
|
| 685 |
return {"error": f"Error consultando Wikipedia: {str(e)}"}
|
| 686 |
|
| 687 |
|
|
|
|
|
|
|
| 688 |
def read_ift(categoria: str = "todo") -> dict:
|
| 689 |
"""Lee /data/ift.json con datos del sector telecomunicaciones México."""
|
| 690 |
path = os.path.join(DATA_PATH, "ift.json")
|
|
|
|
| 751 |
return {"error": str(e)}
|
| 752 |
|
| 753 |
|
| 754 |
+
# --- Dispatcher: ejecuta la tool por nombre ----------------------------------
|
| 755 |
|
| 756 |
def search_web_tool(query: str = "", max_results: int = 3, **kwargs) -> str:
|
| 757 |
"""Búsqueda web en tiempo real via Tavily API."""
|
|
|
|
| 806 |
"read_worldbank": read_worldbank,
|
| 807 |
"read_inegi": read_inegi,
|
| 808 |
"search_wikipedia": search_wikipedia,
|
| 809 |
+
"read_ift": read_ift,
|
| 810 |
}
|
| 811 |
|
| 812 |
fn = REGISTRY.get(tool_name)
|