Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ for file_path in sorted(p for p in documents_root.rglob("*") if p.is_file()):

Already keeping your corpus in a bucket? `discover_documents()` and `extract_text()` name each document by
its full S3 object key, so that is what `correct_answer_document_keys` must reference — prefix included.
Discovery can span several locations in one bucket (`prefixes=["manuals/", "reports/"]`) and fails with a
`BenchmarkKeyError` if a benchmark key matches none of the documents it found.


### Prepare `benchmark_data.json`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
{
"cell_type": "markdown",
"metadata": {},
"source": "The data processing pipeline prepares documents for the RAG system in multiple steps. Each step produces outputs stored under `step_outputs/`. \n\n| Step | Function | Purpose |\n|------|----------|---------| \n| 1 | **`discover_documents`** | List documents in the bucket, prioritize benchmark-referenced docs, apply a size cap, and write a JSON manifest (no content download). |\n| 2 | **`extract_text`** | Download the listed documents from S3 and extract text to DoclingDocument JSON files using Docling. |"
"source": "The data processing pipeline prepares documents for the RAG system in multiple steps. Each step produces outputs stored under `step_outputs/`. \n\n| Step | Function | Purpose |\n|------|----------|---------| \n| 1 | **`discover_documents`** | List documents under every selected prefix, prioritize benchmark-referenced docs, apply a size cap, and write a JSON manifest (no content download). |\n| 2 | **`extract_text`** | Download the listed documents from S3 and extract text to DoclingDocument JSON files using Docling. |"
},
{
"cell_type": "code",
Expand All @@ -219,7 +219,7 @@
"\n",
"step_output_dir = Path(\"./step_outputs\")\n",
"input_data_bucket_name = os.environ[\"AWS_S3_BUCKET\"]\n",
"input_data_key = \"{INPUT_DATA_KEY}\"\n",
"input_data_keys = {INPUT_DATA_KEYS}\n",
"step_output_dir.mkdir(parents=True, exist_ok=True)"
]
},
Expand All @@ -229,15 +229,15 @@
"source": [
"### Documents discovery\n",
"\n",
"Lists objects in the S3 input bucket, filters by supported extensions (e.g., `.pdf`, `.docx`, `.pptx`, `.md`, `.html`, `.txt`), and builds a document set. Documents referenced in the benchmark are prioritized, then others are added until a configurable size limit (1 GB by default) is reached. This step does not download document contents but writes a JSON manifest (`documents_descriptor.json`) containing the bucket, prefix, and list of selected object keys and sizes for the next step."
"Lists objects in the S3 input bucket, filters by supported extensions (e.g., `.pdf`, `.docx`, `.pptx`, `.md`, `.html`, `.txt`), and builds a document set. Documents referenced in the benchmark are prioritized, then others are added until a configurable size limit (1 GB by default) is reached. This step does not download document contents but writes a JSON manifest (`documents_descriptor.json`) containing the bucket, prefixes, and list of selected object keys and sizes for the next step."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": "result = discover_documents(\n bucket_name=input_data_bucket_name,\n prefix=input_data_key,\n s3_client=s3_client,\n)\nresult.save(step_output_dir / \"discovered_documents\")\n\nprint(json.dumps(result.to_dict(), indent=4, ensure_ascii=False))"
"source": "result = discover_documents(\n bucket_name=input_data_bucket_name,\n prefixes=input_data_keys,\n s3_client=s3_client,\n)\nresult.save(step_output_dir / \"discovered_documents\")\n\nprint(json.dumps(result.to_dict(), indent=4, ensure_ascii=False))"
},
{
"cell_type": "markdown",
Expand Down
20 changes: 12 additions & 8 deletions ai4rag/assets_generator/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright IBM Corp. 2026
# SPDX-License-Identifier: Apache-2.0
# -----------------------------------------------------------------------------
import json
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -38,7 +39,7 @@ def _format_required_env_vars(provider: str) -> str:
def create_placeholder_mapping(
output_data: dict[str, Any],
test_data_key: str = "",
input_data_key: str = "",
input_data_keys: list[str] | None = None,
) -> dict[str, Any]:
"""Create a mapping from placeholder names to their values from a pattern definition.

Expand All @@ -52,8 +53,9 @@ def create_placeholder_mapping(
The parsed ``pattern.json`` data.
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.
input_data_keys : list[str] | None, default=None
S3 key prefixes of the document locations used as input to AI4RAG.
Rendered into the notebook as a Python list literal.

Returns
-------
Expand Down Expand Up @@ -97,7 +99,9 @@ def create_placeholder_mapping(
mapping["CHUNK_OVERLAP"] = ch.get("chunk_overlap", 50)

mapping["TEST_DATA_KEY"] = test_data_key
mapping["INPUT_DATA_KEY"] = input_data_key
# Rendered by ``str.format`` into a bare expression, so it has to carry its
# own quoting and brackets to become a valid Python list literal.
mapping["INPUT_DATA_KEYS"] = json.dumps(input_data_keys or [])

return mapping

Expand All @@ -107,7 +111,7 @@ def generate_notebook_from_template(
output_data: dict[str, Any],
output_notebook_path: str | Path,
test_data_key: str = "",
input_data_key: str = "",
input_data_keys: list[str] | None = None,
) -> None:
"""Generate a filled notebook from a template and pattern configuration.

Expand All @@ -125,13 +129,13 @@ def generate_notebook_from_template(
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.
input_data_keys : list[str] | None, default=None
S3 key prefixes of the document locations used as input to AI4RAG.
"""
placeholder_mapping = create_placeholder_mapping(
output_data,
test_data_key=test_data_key,
input_data_key=input_data_key,
input_data_keys=input_data_keys,
)
notebook = Notebook.load(
notebook_name=f"{notebook_template}_template.ipynb",
Expand Down
8 changes: 7 additions & 1 deletion ai4rag/utils/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
# Copyright IBM Corp. 2025-2026
# SPDX-License-Identifier: Apache-2.0
# -----------------------------------------------------------------------------
from ai4rag.utils.data.documents_discovery import DiscoveryResult, DocumentDescriptor, discover_documents
from ai4rag.utils.data.documents_discovery import (
BenchmarkKeyError,
DiscoveryResult,
DocumentDescriptor,
discover_documents,
)
from ai4rag.utils.data.test_data_loader import TestDataLoaderError, TestDataResult, load_test_data
from ai4rag.utils.data.text_extraction import DoclingExtractionConfig, ExtractionResult, extract_text

__all__ = [
"discover_documents",
"BenchmarkKeyError",
"DiscoveryResult",
"DocumentDescriptor",
"extract_text",
Expand Down
Loading
Loading