This document describes the sequence of events and the code path taken when a user performs a Custom Analysis using the LLM-powered feature in NcDashboard.
- File:
ncdashboard.py/llm/llm_layout.py - Class:
CustomAnalysisUI - Sequence:
- User clicks the "🤖 Custom Analysis" button in the sidebar (in
ncdashboard.py). CustomAnalysisUI.open_dialog()(inllm/llm_layout.py) creates a PanelCardmodal.- User selects a provider, source data, and enters a request.
- User clicks "🚀 Generate", which triggers the
on_generateinner callback. on_generatecallsself.run_analysis(provider, source_id, request).
- User clicks the "🤖 Custom Analysis" button in the sidebar (in
- File:
llm/llm_layout.py - Method:
CustomAnalysisUI.run_analysis - Sequence:
- Locates the source data (either the
rootdataset or a specific node's data). - Initializes the LLM client using
get_llm_client(provider). - Calls
run_with_retry(llm_client, data, request, max_attempts=3).
- Locates the source data (either the
- File:
llm/code_executor.py - Function:
run_with_retry - Sequence:
- Prompt Building:
PromptBuilder(llm/prompt_builder.py) extracts metadata from thexarrayobject (variables, coordinates, shapes) and formats it into a prompt using templates inllm/prompts/base.py. - LLM Generation: The
llm_clientsends the prompt to the selected provider and returns a raw Python code string. - Execution:
CodeExecutor.execute()runs the code in a "sandbox":- It uses
exec()with a restricted dictionary of globals (onlynp,xr, and thedataobject are allowed). - It strips imports and checks that the code defines an
outputvariable of typexr.DataArray.
- It uses
- Error Correction: If execution fails (e.g., SyntaxError, ValueError), a new prompt is built containing the error message and the failed code. This is sent back to the LLM for a fix (up to 3 attempts).
- Prompt Building:
- File:
model/dashboard.py - Method:
Dashboard.create_figure_from_dataarray - Sequence:
- Receives the
xr.DataArraynamedoutputfrom the LLM execution. - Determines the dimensionality of the data (1D, 2D, 3D, or 4D).
- Creates the appropriate node type (e.g.,
TwoDNode,ThreeDNode). - Wraps the node in a
FigureLayoutobject. - Appends the layout to the
main_area(Panel FlexBox).
- Receives the
- File:
ncdashboard.py/model/dashboard.py - Sequence:
- Because the
main_areais a Panel component already displayed in the browser, adding the new figure layout to it causes a reactive update. - The browser renders the new Bokeh/HoloViews plot immediately.
- Because the
| Component | File | Responsibility |
|---|---|---|
| UI | llm/llm_layout.py |
Dialog management and status updates. |
| Orchestrator | llm/code_executor.py |
Implementation of the run_with_retry loop. |
| Logic | llm/prompt_builder.py |
Metadata extraction and prompt formatting. |
| Sandbox | llm/code_executor.py |
Secure execution of LLM-generated Python code. |
| Client | llm/llm_client.py |
Communication with OpenAI, Gemini, or Ollama. |
| Model | model/dashboard.py |
Turning the resulting xarray object into a dashboard figure. |