-
Notifications
You must be signed in to change notification settings - Fork 29
feat(pkg-py): add ggsql visualization tool #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cpsievert
wants to merge
33
commits into
main
Choose a base branch
from
feat/ggsql-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
f8fea5e
feat: add ggsql visualization tool
cpsievert d7a3223
refactor: remove developer-facing viz API from public surface
cpsievert 9a87ee6
feat: viz polish, prompt best practices, and collapsed query param
cpsievert afebc15
refactor: simplify viz preload and trim brittle tests
cpsievert 610ea48
fix(prompts): polish ggsql-syntax.md — LABEL wording, PLACE/DRAW dist…
cpsievert 29e731b
refactor(viz): simplify chart feedback path
cpsievert ca6b75d
refactor(imports): hoist stdlib/hard-dep imports, keep only optional …
cpsievert fa6f966
refactor(prompts): restructure system and tool prompts for unified ca…
cpsievert 7cd9f9e
fix(prompts): favor viz over redundant tables, collapse preparatory q…
cpsievert 8c4a781
docs: add viz tool and collapsed query param to changelog
cpsievert 127a066
fix: address Copilot PR review feedback
cpsievert 014f279
chore: update github remotes
cpsievert 8623756
merge: resolve conflicts with main (deferred client + ggsql viz)
cpsievert a8aeae4
fix: add stream_content stub to DummyProvider for chatlas 0.16.0 compat
cpsievert b253363
fix(viz): use deepcopy in fit_chart_to_container to avoid mutating input
cpsievert f51c718
fix(viz): use as_narwhals in to_polars to fix ibis source compatibility
cpsievert 2e04dd2
fix(prompts): improve column casing guidance for Snowflake uppercase …
cpsievert 372b8e6
fix(viz): lowercase DataFrame columns before DuckDB registration
cpsievert db17b55
Merge branch 'main' into feat/ggsql-integration
cpsievert abdaa9a
feat: add truncate_error for capping tool error messages
cpsievert 878f42d
fix(prompts): update ggsql syntax guide and bump to v0.2.4
cpsievert b1ad65c
refactor: rename visualize_query tool to visualize
cpsievert 20fa032
Update example to use shiny express
cpsievert b6ba1a8
Update pkg-py/src/querychat/static/js/viz-preload.js
cpsievert e5f35be
Wrap ggsql syntax reference in XML tag
cpsievert 0bec143
fix(prompts): gate visual exploration mention on visualize tool presence
cpsievert eebd82a
fix(prompts): add trailing comma note to ggsql syntax important notes
cpsievert 231e3ef
fix(prompts): restore when-to-use/when-not-to-use in tool descriptions
cpsievert d4b9d20
refactor(prompts): move viz tool-use instructions into tool description
cpsievert 5fd68d2
fix(tests): update prompt tests for moved viz instructions
cpsievert 2fc4303
fix(prompts): add missing ggsql syntax from skill reference
cpsievert a5b2372
Fix pkg-py/docs/tools.qmd link
cpsievert 0f2314d
Fix typo
cpsievert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| --- | ||
| title: Visualizations | ||
| lightbox: true | ||
| --- | ||
|
|
||
| querychat can create charts inline in the chat. | ||
| When you ask a question that benefits from a visualization, the LLM writes a query using [ggsql](https://ggsql.org/) — a SQL-like visualization grammar — and renders an [Altair](https://altair-viz.github.io/) chart directly in the conversation. | ||
|
|
||
| ## Getting started | ||
|
|
||
| Visualization requires two steps: | ||
|
|
||
| 1. **Install the `viz` extras:** | ||
|
|
||
| ```bash | ||
| pip install "querychat[viz]" | ||
| ``` | ||
|
|
||
| 2. **Include `"visualize"` in the `tools` parameter:** | ||
|
|
||
| ```{.python filename="app.py"} | ||
| from querychat import QueryChat | ||
| from querychat.data import titanic | ||
|
|
||
| qc = QueryChat(titanic(), "titanic", tools=("query", "update", "visualize")) | ||
| app = qc.app() | ||
| ``` | ||
|
|
||
| Ask something like "Show me survival rate by passenger class as a bar chart" and querychat will generate and display the chart inline: | ||
|
|
||
| {fig-alt="Bar chart showing survival rate by passenger class." class="lightbox shadow rounded mb-3"} | ||
|
|
||
| ## Choosing tools | ||
|
|
||
| The `tools` parameter controls which capabilities the LLM has access to. | ||
| By default, only `"query"` and `"update"` are enabled — visualization must be opted into explicitly. | ||
|
|
||
| To enable only query and visualization (no dashboard filtering): | ||
|
|
||
| ```{.python} | ||
| qc = QueryChat(titanic(), "titanic", tools=("query", "visualize")) | ||
| ``` | ||
|
cpsievert marked this conversation as resolved.
|
||
|
|
||
| See [Tools](tools.qmd) for a full reference on available tools and what each one does. | ||
|
|
||
| ## Custom apps | ||
|
|
||
| The example below shows a minimal custom Shiny app using only the `"query"` and `"visualize"` tools. | ||
| It omits `"update"` to focus entirely on data analysis and visualization rather than dashboard filtering: | ||
|
|
||
| ```{.python filename="app.py"} | ||
| {{< include /../examples/10-viz-app.py >}} | ||
| ``` | ||
|
|
||
| ## What you can ask for | ||
|
|
||
| querychat can generate a wide range of chart types. | ||
| Some example prompts: | ||
|
|
||
| - "Show me a bar chart of survival rate by passenger class" | ||
| - "Scatter plot of age vs fare, colored by survival" | ||
| - "Line chart of average fare over time" | ||
| - "Histogram of passenger ages" | ||
| - "Facet survival rate by class and sex" | ||
|
|
||
| The LLM chooses an appropriate chart type based on your question, but you can always be specific. | ||
| If you ask for a bar chart, you'll get a bar chart. | ||
|
|
||
| {fig-alt="Scatter plot of age vs fare colored by survival status." class="lightbox shadow rounded mb-3"} | ||
|
|
||
| ::: {.callout-tip} | ||
| If you don't like the chart, ask the LLM to adjust it — for example, "make the dots bigger" or "use a log scale on the y-axis". | ||
| ::: | ||
|
|
||
| ## Chart controls | ||
|
|
||
| Each chart has controls in its footer: | ||
|
|
||
| **Fullscreen** — Click the expand icon to view the chart in fullscreen mode. | ||
|
|
||
| {fig-alt="A chart displayed in fullscreen mode." class="lightbox shadow rounded mb-3"} | ||
|
|
||
| **Save** — Download the chart as a PNG or SVG file. | ||
|
|
||
| **Show Query** — Expand the footer to see the ggsql query used to generate the chart. | ||
|
|
||
| {fig-alt="A chart with the Show Query footer expanded, showing the ggsql query." class="lightbox shadow rounded mb-3"} | ||
|
|
||
| ## How it works | ||
|
|
||
| 1. **The LLM generates a ggsql query** — a SQL-like grammar that describes both data transformation and visual encoding in a single statement. | ||
| 2. **The SQL is executed** — querychat runs the data portion of the query against your data source locally. | ||
| 3. **The VISUALISE clause is rendered** — the result is passed to Altair, which produces a Vega-Lite chart specification. | ||
| 4. **The chart appears inline** — the chart is streamed back into the conversation as an interactive widget. | ||
|
|
||
| Note that visualization queries are independent of any active dashboard filter set by the `update` tool. | ||
| They always run against the full dataset. | ||
|
|
||
| Learn more about the ggsql grammar at [ggsql.org](https://ggsql.org/). | ||
|
|
||
| ## See also | ||
|
|
||
| - [Tools](tools.qmd) — Understand what querychat can do under the hood | ||
| - [Provide context](context.qmd) — Help the LLM understand your data better | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from querychat.express import QueryChat | ||
| from querychat.data import titanic | ||
|
|
||
| from shiny.express import ui, app_opts | ||
|
|
||
| # Omits "update" tool — this demo focuses on query + visualization only | ||
| qc = QueryChat( | ||
| titanic(), | ||
| "titanic", | ||
| tools=("query", "visualize") | ||
| ) | ||
|
|
||
| qc.ui() | ||
|
|
||
| ui.page_opts(fillable=True, title="QueryChat Visualization Demo") | ||
|
|
||
| app_opts(bookmark_store="url") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.