$ show posit-dev/querychat
QueryChat is a dual R/Python open-source library that turns tabular data into a natural-language chat interface: users ask questions in plain English, an LLM generates SQL via tool calls, and the SQL executes against an in-memory DuckDB engine, a SQLAlchemy-managed database, an Ibis backend, a Polars lazy frame, or a pins board. A defining safety property is that raw data is never sent to the LLM — only schema introspection (column names, types, ranges, categorical values) and query results flow through the model. The package ships with five LLM-visible tools (get_schema, query, update_dashboard, reset_dashboard, visualize) and a hardened DuckDB mode that blocks external filesystem access and extensions, making it a well-considered solution to the inherently risky problem of letting models write SQL against user data.
The LLM identified 6 files worth highlighting for reuse:
The DataSource ABC and its five concrete implementations (DataFrameSource, SQLAlchemySource, IbisSource, PolarsLazySource, PinSource) expose a uniform get_schema/execute_query/test_query/cleanup interface. It produces ColumnMeta schema summaries (types, ranges, categorical thresholds) for LLM consumption and includes a DuckDB hardening mode that disables external file access and extensions. This is an extractable pattern for any project needing safe, schema-aware SQL execution over heterogeneous tabular backends.
Executor hierarchy (DuckDBExecutor, PolarsSQLExecutor, DataSourceExecutor) that runs LLM-generated SQL with defensive validation: destructive keywords (INSERT/UPDATE/DELETE/DROP) are blocked by default, an opt-in escape hatch enables them, and test_query() fetches limited rows then verifies expected columns survive the query. Dialect portability is handled with LIMIT 1 fallback syntax and SQL Server workarounds — a reusable pattern for any agent tooling that lets models write SQL.
Assembles the full LLM system prompt from Mustache templates kept in markdown files, conditionally binding schema, YAML data dictionaries, tool descriptions, and ggsql syntax references. The prompt rebuilds when tables are added or configuration changes. This is a clean, testable prompt-assembly architecture applicable to any tool-using agent: content lives in templates, the Python layer handles context binding.
Implements a versioned YAML data-dictionary specification (v0.2.0) describing tables, column types/ranges/values, inter-table relationships, and a glossary. This metadata enriches the LLM's schema context without expensive live statistics queries. The YAML schema itself is a reusable documentation format for any LLM-facing data catalog or agent context layer.
Self-contained Snowflake integration that discovers semantic views via SHOW SEMANTIC VIEWS and GET_DDL, then formats them into LLM prompt sections. Demonstrates how to surface a warehouse's semantic layer (units, conversions, business definitions) to a chat agent — an uncommon, non-obvious integration pattern applicable to any LLM data agent targeting modern cloud warehouses.
A reusable Playwright E2E harness that launches example apps across Shiny, Streamlit, Gradio, and Dash with framework-specific launchers, retry logic for server startup, and shared interaction utilities. It drives real chat sessions against live apps and asserts on SQL updates, rendered data tables, and visualization footer behavior. The retrying server-start helper is a genuinely transferable E2E scaffolding pattern for multi-framework web applications.