diff --git a/Caddyfile b/Caddyfile index af93fa4..bf81e5d 100644 --- a/Caddyfile +++ b/Caddyfile @@ -3,6 +3,27 @@ respond 200 } + # ask.xn--2dk.xyz - Natural language to SQL TUI + ask.xn--2dk.xyz { + @websocket { + header Connection *Upgrade* + header Upgrade websocket + } + + handle @websocket { + reverse_proxy localhost:7682 { + flush_interval -1 + } + } + + handle { + reverse_proxy localhost:7682 { + flush_interval -1 + } + } + } + + # db.xn--2dk.xyz - DuckDB SQL shell handle /login { reverse_proxy 127.0.0.1:8081 } diff --git a/Dockerfile b/Dockerfile index baef820..64fa2a7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,7 +26,8 @@ ENV LANG=C.UTF-8 \ WORKDIR /app COPY basedosdados.duckdb Caddyfile start.sh auth.py ./ -RUN chmod +x start.sh +COPY ask/target/release/ask /app/ask +RUN chmod +x start.sh /app/ask EXPOSE 8080 diff --git a/README.md b/README.md index 774fb6f..2f22bb1 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,68 @@ python prepara_db.py # gera basedosdados.duckdb com views apontando para o S3 duckdb basedosdados.duckdb ``` -Requer as credenciais do S3 no `.env` (veja seção de configuração abaixo). +As queries são executadas diretamente sobre os arquivos Parquet no S3 — não há download de dados. O DuckDB lê os arquivos remotos sob demanda via `httpfs`. + +--- + +## Ask: linguagem natural → SQL + +Interface TUI que permite fazer perguntas em português e obter SQL automaticamente. + +### No browser + +Acesse **https://ask.xn--2dk.xyz** → autentique → digite sua pergunta em português. + +### Local + +```bash +cd ask +cargo build --release +./target/release/ask # modo interativo +./target/release/ask "Quantos municípios tem SP?" # modo CLI +``` + +### Variáveis de ambiente + +| Variável | Descrição | +|---|---| +| `GEMINI_API_KEY` | Chave da API Gemini (obrigatória para usar modelos Gemini) | +| `OPENROUTER_API_KEY` | Chave para usar modelos via OpenRouter | +| `GEMINI_MODEL` | Modelo a usar (padrão: `gemini-flash-latest`) | +| `SCHEMA_FILE` | Arquivo de schema (padrão: `context/schema_compact_inline.txt`) | +| `DB_FILE` | Arquivo DuckDB (padrão: `basedosdados.duckdb`) | + +--- + +## Arquivos de schema + +O diretório `context/` contém artefatos gerados automaticamente para contexto do LLM e descoberta de tabelas: + +| Arquivo | Descrição | +|---|---| +| `schema_compact_inline.txt` | Schema condensado para contexto do LLM | +| `schema_compact.txt` | Schema mais verboso | +| `schema_ddl.sql` | DDL das views DuckDB | +| `join_graph.json` | Relacionamentos entre tabelas | +| `file_tree.md` | Estrutura de arquivos no S3 com tamanhos | +| `schemas.json` | Schema raw do BigQuery | + +--- + +## Descobrindo tabelas + +```sql +-- listar todos os datasets (schemas) +SHOW SCHEMAS; + +-- listar tabelas de um dataset +SHOW TABLES IN br_anatel_banda_larga_fixa; + +-- ver colunas de uma tabela +DESCRIBE br_anatel_banda_larga_fixa.densidade_brasil; +``` + +No shell do browser, `.tables` lista tudo de uma vez. Para descoberta programática, use os arquivos em `context/`. --- @@ -130,6 +191,7 @@ Resume automático: se interrompido, basta rodar novamente. | `AWS_ACCESS_KEY_ID` | Access key do Hetzner Object Storage | | `AWS_SECRET_ACCESS_KEY` | Secret key do Hetzner Object Storage | | `BASIC_AUTH_PASSWORD` | Senha do shell web e endpoint `/query` | +| `GEMINI_API_KEY` | Chave da API Gemini para o ask | ### Executando diff --git a/haloy.yml b/haloy.yml index a56a360..869d995 100644 --- a/haloy.yml +++ b/haloy.yml @@ -5,9 +5,15 @@ api_token: env: HALOY_TOKEN domains: - domain: db.xn--2dk.xyz -port: 8080 -health_check_path: /health + port: 8080 + health_check_path: /health + - domain: ask.xn--2dk.xyz + port: 8080 + health_check_path: /health env: + - name: GEMINI_API_KEY + from: + env: GEMINI_API_KEY - name: HETZNER_S3_ENDPOINT from: env: HETZNER_S3_ENDPOINT diff --git a/start.sh b/start.sh index fb3322b..8a4c7fd 100644 --- a/start.sh +++ b/start.sh @@ -18,9 +18,12 @@ SET memory_limit='4GB'; SQL chmod 600 /app/ssh_init.sql -echo "[start] Starting ttyd terminal..." +echo "[start] Starting ttyd terminal (db)..." ttyd --port 7681 --writable duckdb -readonly --init /app/ssh_init.sql /app/basedosdados.duckdb & +echo "[start] Starting ttyd terminal (ask)..." +ttyd --port 7682 --writable /app/ask & + echo "[start] Starting auth service..." python3 /app/auth.py &