Skip to content

AI exploration guide: make the selection-to-chat example earn its LLM - #519

Open
cpsievert wants to merge 5 commits into
mainfrom
docs-llm-examples-as-tools
Open

cpsievert wants to merge 5 commits into
mainfrom
docs-llm-examples-as-tools

Conversation

@cpsievert

@cpsievert cpsievert commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Chat examples should show an LLM doing something a plain output can't. Example 2 in the AI exploration guide ("A widget's selection becomes chat context") handed the model a pre-computed count, mean, and top continents for a lasso selection; the model just restated those numbers, which a @render.text could already show.

It now hands the model the selected countries themselves and asks something only world knowledge can answer: lasso the countries well below the trend (Botswana, Equatorial Guinea, Gabon, South Africa) and ask "Why are these below the curve?" The answer covers HIV/AIDS, resource income that doesn't reach most people, and inequality, none of which is in the data. It also uses the modern Chat(id, client=...) + register_tool() pattern instead of @chat.on_user_submit + stream_async + append_message_stream, matching examples 3 and 4 on the same page.

What changed

  • ai-exploration/examples/02_selection_to_chat_context.py and its code block: new get_selection() tool and question, re-shot screenshots, and the real model answer quoted.
  • Removed the claim that Chat(id, client=...) "only works when the model's reply doesn't need anything beyond the user's literal text"; examples 3 and 4 and the loan-chat showcase already disprove it.
  • reacting-to-widgets/index.md: fixed a cross-reference that pointed at the removed on_user_submit handler.
  • gallery/showcases/map-chart-chat/index.md: corrected the stated reason summarize_view wraps its read in reactive.isolate().

reactive.isolate() in tools

A chat's tool calls run inside the reactive.extended_task shinychat streams the reply in, and an extended task raises RuntimeError: You're not allowed to read reactive sources from inside a Extended Task on any reactive read. So a tool that reads an accessor must wrap the read in reactive.isolate(); a write-only tool (fly_to) doesn't need it. This is now explained once, in the guide's Pitfalls, with a short pointer at each site. A shinychat-side change that would make this unnecessary is being looked into separately.

Uses listen_to=["selected_rows"], so merge after #520.

The old example handed the model a pre-digested summary (count, mean,
top continents) of a lasso selection, which it just paraphrased back --
a @render.text could have shown the same thing. The new version hands
the model the selected countries themselves and asks a question only
world knowledge can answer ("why are these below the trend curve?"),
switching from manual @chat.on_user_submit + stream_async to the
Chat(client=...) + register_tool pattern examples 3 and 4 already use.

Also documents, in one place, that a chat's tool calls run inside a
reactive.extended_task and therefore require reactive.isolate() to read
an accessor at all (confirmed empirically: omitting it raises
"not allowed to read reactive sources from inside a Extended Task").
Pointed at the now-removed @chat.on_user_submit example; the worked
example it refers to reads a lasso selection from a registered tool
instead.
The doc said isolate() just avoided creating a reactive dependency; it's
actually required to avoid a RuntimeError, since the tool runs inside
shinychat's reactive.extended_task. Points at the ai-exploration guide's
Pitfalls section instead of re-explaining the mechanism here.
@netlify

netlify Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for shinywidgets canceled.

Name Link
🔨 Latest commit 3a8dd9e
🔍 Latest deploy log https://app.netlify.com/projects/shinywidgets/deploys/6b104962f48105f6c3820c14

@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

@github-actions
github-actions Bot temporarily deployed to pull request December 2, 2026 00:28 Inactive
Example 2 only ever reads plot.selected_rows(df), never plot.selected()
directly, so follow #520's convention and declare the opt-in with the
name actually called.
@github-actions
github-actions Bot temporarily deployed to pull request December 2, 2026 16:11 Inactive
@cpsievert
cpsievert requested a balanced review from Copilot December 2, 2026 16:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The guide still incorrectly attributes reactive feedback loops to client tool calls and claims an extra websocket round trip for selection state already stored on the server.

2 open issues
What changed in this PR

Updates the AI exploration guide to demonstrate how an LLM adds world knowledge to selected widget data.

Changes:

  • Replaces manual chat handling with a registered selection tool and automatic streaming.
  • Uses selected country rows and a logarithmic GDP axis for the worked example.
  • Revises reactive-isolation guidance and related cross-references.
File Description
website/​guide/​reacting-to-widgets/​index.md Updates the chat example cross-reference.
website/​guide/​ai-exploration/​index.md Reworks the selection example and tool guidance.
website/​guide/​ai-exploration/​examples/​02_selection_to_chat_context.py Implements tool-based selection context.
website/​gallery/​showcases/​map-chart-chat/​index.md Clarifies why tool reads require isolation.

Review effort: Balanced

Comment thread website/guide/ai-exploration/index.md Resolved
Comment thread website/guide/ai-exploration/index.md Resolved
- Reactive loops: tool reads are isolated and tools only run when the
  model calls them, so a tool cannot self-subscribe into a feedback
  loop; attribute that hazard to reactive effects, where it applies.
- Latency: the widget syncs the lasso to the server as it is drawn, so
  get_selection() reads server-side state; drop the incorrect extra
  websocket round-trip claim.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the guide and chat-example updates.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@github-actions
github-actions Bot temporarily deployed to pull request December 2, 2026 16:40 Inactive

This branch was previously deployed

1 inactive deployment
pull request — 3a8dd9e1 Deployed Dec 2, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants