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
Methods:¶
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
Methods:¶
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 ¶
Build Responses API pattern definitions from HPO experiment results.
Functions:¶
build_pattern_json ¶
Update pattern information with responses template.
Parameters:
-
pattern(dict) –A single evaluation result object carrying
indexing_params,rag_params,pattern_name,collection, etc. -
indexing_pipeline_params(dict | None, default:None) –Set of parameter for the indexing pipeline build. If not set, indexing pipeline object is not set.
Notes
pattern["settings"]["generation"] must include model_id, temperature, max_completion_tokens, system_message_text, and user_message_text (as produced by the experiment payload).
Returns:
-
dict–Pattern definition suitable for JSON serialisation.
Source code in ai4rag/components/assets_generator/pattern_builder.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |