Find and use notebook tools

Run the setup cells to create a live roster marker and locate the saved lesson notebook. Then ask four short AI questions to find a kernel name, list a saved notebook, inspect one saved cell, and insert an unexecuted code draft. The ordinary Markdown declaration below makes only the selected tools available to those questions. Connect ChatGPT or an API provider before running them. The saved answers and tool tables record an earlier worked run. No credentials are stored here.

Find and use notebook tools

Run the setup cells to create a live roster marker and locate the saved lesson notebook. Then ask four short AI questions to find a kernel name, list a saved notebook, inspect one saved cell, and insert an unexecuted code draft. The ordinary Markdown declaration below makes only the selected tools available to those questions. Connect ChatGPT or an API provider before running them. The saved answers and tool tables record an earlier worked run. No credentials are stored here.

The imported functions must be present in the kernel. tools_markdown prints editable & references for a declaration; printing those lines alone does not offer tools to the model.

from pathlib import Path

from nbinlineai.tools import (
    find_notebook_cells,
    insert_markdown,
    insert_code,
    inspect_python,
    list_cells,
    list_notebooks,
    read_cell,
    read_notebook_cell,
    read_url,
    search_kernel_names,
    tools_markdown,
    url_to_note,
)

study_roster_marker: dict[str, int] = {"Asha": 2, "Mina": 3}
fixture_candidates: list[Path] = [
    Path("examples/data/ecosystem-lesson.ipynb"),
    Path("data/ecosystem-lesson.ipynb"),
]
fixture_path: str = ""
for candidate in fixture_candidates:
    if candidate.is_file():
        fixture_path = str(candidate)
        break
if not fixture_path:
    raise FileNotFoundError(
        "Copy the examples/data folder alongside this notebook, or run from the repository root."
    )
fixture_directory: str = str(Path(fixture_path).parent)
print(f"Saved fixture: {fixture_path}")
Saved fixture: data/ecosystem-lesson.ipynb

Choose tool references

The next code cell prints selected saved-notebook tool references and the starter list. Copy only the lines needed for a task into ordinary Markdown above its AI question. The printed output does not itself declare tools, and tools_markdown is a Python helper rather than an AI tool.

print("Selected tools:\n" + tools_markdown(["find_notebook_cells", "read_notebook_cell"]))
print("All tools:\n" + tools_markdown())
Selected tools:
Available tools (delete any line you do not want to offer):
- &`find_notebook_cells` — Find saved notebook cells by case-insensitive literal source text.
- &`read_notebook_cell` — Read numbered saved cell source by ID or zero-based index:N fallback.
All tools:
Available tools (delete any line you do not want to offer):
- &`search_kernel_names` — Find live Python kernel names containing a literal query, with types only.
- &`list_notebooks` — List saved .ipynb paths, skipping hidden and environment directories.
- &`find_notebook_cells` — Find saved notebook cells by case-insensitive literal source text.
- &`read_notebook_cell` — Read numbered saved cell source by ID or zero-based index:N fallback.
- &`inspect_python` — Inspect a live Python name's help, signature, or available source.
- &`read_url` — Read a public web page as bounded, sanitized Markdown with its source URL.
- &`path_info` — Show the kernel working directory and a path's resolved type and size.
- &`list_files` — List visible file and directory paths, optionally matching a name pattern.
- &`view_file` — Read a bounded range of one UTF-8 text file with one-based line numbers.
- &`create_file` — Create a new UTF-8 text file without replacing an existing path.
- &`file_str_replace` — Replace an exact literal only when its occurrence count matches expectation.
- &`file_insert_line` — Insert text after a one-based line; line 0 inserts before the first line.
- &`file_replace_lines` — Replace an explicit inclusive, one-based line range with UTF-8 text.
- &`show_doc` — Show docs for a live name, or import `module` and inspect its public name.
- &`list_cells` — List live notebook cells, including unsaved edits, by ID and position.
- &`read_cell` — Read live notebook cell source, including unsaved edits, by cell ID.
- &`insert_markdown` — Insert a Markdown note into the current live notebook after a cell.
- &`insert_code` — Insert an unexecuted code cell below the AI answer, or after a chosen cell.
- &`url_to_note` — Fetch a public page and insert its bounded Markdown as a notebook note.

Tools offered to lower questions

Live names are not live values

The declaration above offers search_kernel_names to this and later questions. It finds a distinctive name in the running Python kernel. This tool lists matching names; use $ syntax in the current question if you want a named variable’s live value. It does not expose every kernel value automatically.

Use the available search_kernel_names tool with query study_roster_marker. Report the matching Python name only; do not guess its value.

study_roster_marker

Observed live tool calls for the answer above (actual subscription events; private fields shortened).

Tool Result state Submitted arguments Observed result
search_kernel_names completed {“query”:“study_roster_marker”} ‘study_roster_marker (dict)’

List saved notebooks in the fixture directory

The earlier declaration also offers list_notebooks. The tool reads saved .ipynb paths under the directory printed by the setup cell; it does not open an unsaved notebook in the browser. It can discover the fixture that the next question searches.

Use the available list_notebooks tool with path set to the live $fixture_directory. Report the saved notebook filename you find there.

ecosystem-lesson.ipynb

Observed live tool calls for the answer above (actual subscription events; private fields shortened).

Tool Result state Submitted arguments Observed result
list_notebooks completed {“path”:“data”} ‘data/ecosystem-lesson.ipynb’

Inspect source in a saved notebook

The earlier declaration offers find_notebook_cells and read_notebook_cell too. The next question first searches for “lichen indicator,” then reads that returned cell ID. A saved .ipynb path is required. These tools do not read unsaved edits in JupyterLab, rich code outputs, or image bytes. The setup cell prints the path that current-question $ interpolation will supply.

The saved fixture path is $fixture_path. Use the available find_notebook_cells tool on that path with query lichen indicator. Then use read_notebook_cell with the same path and the matching cell ID. Summarize what the saved source actually says, and name the cell ID you read.

Cell habitat-observation says the “lichen indicator” is a teaching clue about air quality. Its five observations near an old wall are invented sample data, not a scientific conclusion.

Observed live tool calls for the answer above (actual subscription events; private fields shortened).

Tool Result state Submitted arguments Observed result
find_notebook_cells completed {“path”:“data/ecosystem-lesson.ipynb”,“query”:“lichen indicator”} ‘Saved source matches in data/ecosystem-lesson.ipynb:\n- index:2 id:habitat-observation (markdown): ## Field note: lichen indicator The lichen indicator is a teaching clue about air quality. In this small invented su’
read_notebook_cell completed {“path”:“data/ecosystem-lesson.ipynb”,“cell_id”:“habitat-observation”} ‘Saved source from data/ecosystem-lesson.ipynb, index:2 id:habitat-observation (markdown):\n1: ## Field note: lichen indicator\n2: \n3: The lichen indicator is a teaching clue about air quality. In this small invented survey, five lichen observations were recorded near the old wall. The number is sample data, not a scientific conclusion.’

Change what lower questions can call

Edit the ordinary Markdown tool declaration above to offer fewer or different imported tools, then ask a new AI question below it. Tools named in previous AI questions above also remain available below; mentions in AI answers, code cells, and raw cells do not declare tools. Notebook file tools are read-only but use the Python kernel’s operating-system file permissions; the kernel working directory is only the base for relative paths, not a security boundary. Avoid exposing paths or source you do not intend to share with your provider.

Ask for an editable code draft

The next declaration offers insert_code to the AI question below. Run the question separately if you want a draft cell. The new ordinary code cell appears after the AI answer, remains unexecuted, and can be edited before you choose to run it. Running the full notebook will call your provider and execute existing code cells, but the newly inserted draft is not in that Run All batch. Save to keep the draft.

  • &insert_code: add an unexecuted, editable code draft below the AI answer.

Use insert_code to add a short Python code cell that prints the names in study_roster_marker in sorted order. Leave the new cell for me to review and run; do not execute it.

Inserted the code cell to print the names in sorted order. It remains unexecuted for your review.

Observed live tool calls for the answer above (actual subscription events; private fields shortened).

Tool Result state Submitted arguments Observed result
insert_code completed {“content”:“for name in sorted(study_roster_marker):\n print(name)”} Inserted unexecuted code cell 66347a4e-ea76-4c68-ac4b-55fd47786813 in the live notebook model (not saved to disk).
for name in sorted(study_roster_marker):
    print(name)

← Examples guide · Download this notebook