Notebooks & Templates¶
Programmatic notebook generation from RAG pattern definitions.
Notebook Builder¶
notebook ¶
Classes¶
Notebook ¶
Notebook(
kernel_name: str = "python3",
kernel_display_name: str = "Python 3",
language: str = "python",
language_version: str = "3.13.11",
cells: list[NotebookCell] | None = None,
)
Builder for programmatically creating and manipulating Jupyter notebooks.
Provides a fluent API for building notebooks by adding cells, formatting content with placeholder substitution, and saving to disk.
Parameters:
-
kernel_name(str, default:"python3") –Kernel name for the notebook.
-
kernel_display_name(str, default:"Python 3") –Display name for the kernel.
-
language(str, default:"python") –Programming language.
-
language_version(str, default:"3.13.11") –Language version.
-
cells(list[NotebookCell] | None, default:None) –Notebook cells to build the notebook from.
Examples:
>>> nb = Notebook(
... cells=[
... NotebookCell(
... cell_type="markdown",
... source="### Hello world!",
... )
... ]
... )
>>> nb.save("output.ipynb")
Source code in ai4rag/components/assets_generator/notebook.py
Functions¶
to_dict ¶
Convert notebook to dictionary format.
Returns:
-
dict–Notebook in Jupyter JSON format.
Source code in ai4rag/components/assets_generator/notebook.py
save ¶
Save notebook to a file.
Parameters:
-
path(str | Path) –Output file path.
-
indent(int, default:2) –JSON indentation level.
Returns:
-
Notebook–Self for method chaining.
Examples:
Source code in ai4rag/components/assets_generator/notebook.py
load classmethod ¶
Load a Jupyter notebook template from bundled package data or a custom directory.
Parameters:
-
notebook_name(str) –Name of the template file (e.g.
"ogx_indexing_template.ipynb"). -
templates_dir(str | Path | None, default:None) –Directory containing the template notebooks. When None, templates are loaded from the
notebook_templates/sub-package bundled withai4rag.assets_generator.
Returns:
-
Notebook–A new Notebook instance populated with the loaded cells and metadata.
Examples:
>>> nb = Notebook.load("ogx_indexing_template.ipynb")
>>> nb = Notebook.load("custom.ipynb", templates_dir="/data/templates")
Source code in ai4rag/components/assets_generator/notebook.py
NotebookCell ¶
NotebookCell(cell_type: Literal['code', 'markdown'], source: str | list[str], metadata: dict | None = None)
Represents a single cell in a Jupyter notebook.
Parameters:
-
cell_type(('code', 'markdown'), default:"code") –The type of cell.
-
source(str | list[str]) –The cell content. Can be a string or list of strings.
-
metadata(dict, default:None) –Cell metadata.
Source code in ai4rag/components/assets_generator/notebook.py
Functions¶
to_dict ¶
Convert cell to notebook JSON format.
Returns:
-
dict–Cell in Jupyter notebook JSON format.
Source code in ai4rag/components/assets_generator/notebook.py
format_source ¶
Format cell source by substituting placeholders.
Performs str.format-style substitution on each line of the cell source. Placeholders not present in placeholders_mapping are replaced with empty strings so that missing keys never raise.
Parameters:
-
placeholders_mapping(dict) –Mapping from placeholder names to replacement values.
Returns:
-
Self–This cell instance (mutated in-place) for method chaining.
Source code in ai4rag/components/assets_generator/notebook.py
Template Rendering¶
templates ¶
Functions¶
generate_notebook_from_template ¶
generate_notebook_from_template(
notebook_template: str,
output_data: dict[str, Any],
output_notebook_path: str | Path,
test_data_key: str = "",
input_data_key: str = "",
ogx_base_url: str = "",
) -> None
Generate a filled notebook from a template and pattern configuration.
Loads the named template, substitutes all placeholders with values extracted from output_data, and writes the result to disk.
Parameters:
-
notebook_template(str) –Template base name without the
_template.ipynbsuffix (e.g."ogx_inference"or"ogx_indexing"). -
output_data(dict[str, Any]) –The parsed
pattern.jsondata. -
output_notebook_path(str | Path) –Path where the generated notebook is saved.
-
test_data_key(str, default:"") –S3 key of the test data file used as input to AI4RAG.
-
input_data_key(str, default:"") –S3 key of the documents directory used as input to AI4RAG.
-
ogx_base_url(str, default:"") –Base URL for the OGX API.
Source code in ai4rag/components/assets_generator/templates.py
create_placeholder_mapping ¶
create_placeholder_mapping(
output_data: dict[str, Any], test_data_key: str = "", input_data_key: str = "", ogx_base_url: str = ""
) -> dict[str, Any]
Create a mapping from placeholder names to their values from a pattern definition.
Extracts values from the pattern.json structure produced by the optimisation pipeline and returns a flat dictionary suitable for NotebookCell.format_source().
Parameters:
-
output_data(dict[str, Any]) –The parsed
pattern.jsondata. -
test_data_key(str, default:"") –S3 key of the test data file used as input to AI4RAG.
-
input_data_key(str, default:"") –S3 key of the documents directory used as input to AI4RAG.
-
ogx_base_url(str, default:"") –Base URL for the OGX API. Falls back to an empty string when not provided, allowing the generated notebook to prompt users for the URL.
Returns:
-
dict[str, Any]–Dictionary mapping placeholder names to their values.
Source code in ai4rag/components/assets_generator/templates.py
Pattern Builder¶
pattern_builder ¶
Functions¶
build_pattern_json ¶
Update pattern information with detected language and responses template.
Parameters:
-
pattern(dict) –A single evaluation result object carrying
indexing_params,rag_params,pattern_name,collection, etc. -
detected_language(dict | None, default:None) –Language detection result (
{"code": "...", "name": "..."}).
Returns:
-
dict–Pattern definition suitable for JSON serialisation.