This repository has been archived on 2025-11-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
egutierrez 28e50b921c feat: Implement new sales data analysis agents and utilities
- Added Router_de_agentes.py to manage agent interactions for sales data analysis.
- Created Analizador_de_datos_de_ventas.yaml for generating structured text reports from sales data.
- Developed Generador_sql_ventas.yaml for generating SQL queries to analyze sales data.
- Established Router_de_agente.yaml as a routing mechanism for agent requests.
- Compiled centros_disponibles.md listing available sales centers.
- Introduced primera_ejecucion_de_un_agente.py as an example for executing agents.
- Added ver_los_prompts_de_un_agente.py to inspect prompts sent to OpenAI.
- Included service account key for BigQuery access in rag-datasets-reader-sa-key.json.
- Defined schema for sales data in Objeto_ventas.json.
- Implemented utility functions for querying BigQuery in conseguir_datos_bq.py.
- Created data transformation utilities in transformar_datos.py for handling decimal and date formats.
2025-10-06 18:48:19 +02:00

68 lines
2.1 KiB
Python

from google.cloud import bigquery
from typing import Generator, List, Dict
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "rag-datasets-reader-sa-key.json"
def consultar_bigquery_paginado(
sql_query: str,
page_size: int = 1000,
project_id: str | None = None
) -> Generator[List[Dict], None, None]:
"""
Ejecuta una consulta SQL de BigQuery y devuelve los resultados paginados.
Limpia el texto SQL automáticamente antes de ejecutarlo.
Args:
sql_query (str): Texto SQL (puede incluir espacios o punto y coma al final).
page_size (int): Número de filas por página.
project_id (str | None): ID del proyecto GCP (opcional si está configurado el entorno).
Yields:
list[dict]: Página de resultados (cada fila convertida a dict).
"""
# 🧹 Limpieza del texto SQL
cleaned_query = (
sql_query.strip() # elimina espacios y saltos al inicio y fin
.removeprefix("```sql").removeprefix("```").removesuffix("```") # limpia delimitadores markdown si los hay
.strip() # vuelve a limpiar por si quedaron espacios
)
# Quita el punto y coma final si lo tiene
if cleaned_query.endswith(";"):
cleaned_query = cleaned_query[:-1].strip()
# Inicializa el cliente
client = bigquery.Client(project=project_id)
# Ejecuta la consulta
query_job = client.query(cleaned_query)
# Devuelve resultados paginados
iterator = query_job.result(page_size=page_size)
for page in iterator.pages:
yield [dict(row) for row in page]
if __name__ == "__main__":
resultado = consultar_bigquery_paginado("""```sql
SELECT
Productos___Producto_NavId__description AS producto,
Centros___Centro_NavId__name AS region,
SUM(cantidad) AS total_vendido,
SUM(cantidad * precioUnitario) AS ingreso_total
FROM
`autingo-159109.rag_datasets.Objeto_Ventas`
WHERE
Fecha >= DATE_SUB(CURRENT_DATE(), INTERVAL 3 MONTH)
GROUP BY
producto, region
ORDER BY
total_vendido DESC;
```""")
for pagina in resultado:
for fila in pagina:
print(fila)
print("----- Fin de página -----")