Optimization Components¶
Search space preparation and RAG optimization functions for the AutoRAG pipeline.
Language Detection¶
search_space_preparation ¶
Attributes¶
LANGUAGE_MAP module-attribute ¶
LANGUAGE_MAP: dict[str, str] = {
"ja": "Japanese",
"ko": "Korean",
"zh-cn": "Chinese",
"zh-tw": "Chinese",
"en": "English",
"de": "German",
"fr": "French",
"es": "Spanish",
"pt": "Portuguese",
"it": "Italian",
"ru": "Russian",
"ar": "Arabic",
"hi": "Hindi",
"th": "Thai",
"vi": "Vietnamese",
"pl": "Polish",
"nl": "Dutch",
"sv": "Swedish",
"cs": "Czech",
"tr": "Turkish",
}
Search Space Preparation¶
search_space_preparation ¶
Classes¶
SearchSpaceReport dataclass ¶
SearchSpaceReport(
search_space: dict[str, Any], selected_models: dict[str, list], detected_language: dict[str, str] | None
)
Result of the search-space preparation step.
Attributes:
-
search_space(dict[str, Any]) –Verbose representation of the search space, including selected model lists and non-model parameter ranges.
-
selected_models(dict[str, list]) –Foundation and embedding model lists that survived pre-selection.
-
detected_language(dict[str, str] | None) –Detected language code and name, or
Nonewhen English or when detection was not performed.
Functions¶
save_yaml ¶
Serialize the report to a YAML file.
The file is suitable as input for the RAG optimization step.
Parameters:
-
path(str | Path) –Destination file path.
Source code in ai4rag/components/optimization/search_space_preparation.py
Functions¶
prepare_search_space_report ¶
prepare_search_space_report(
test_data_path: str | Path,
extracted_text_path: str | Path,
ogx_client: OgxClient,
embedding_models: list[str] | None = None,
generation_models: list[str] | None = None,
metric: str = _DEFAULT_METRIC,
top_n_generation: int = _DEFAULT_TOP_N_GENERATION,
top_k_embedding: int = _DEFAULT_TOP_K_EMBEDDING,
sample_size: int = _DEFAULT_SAMPLE_SIZE,
random_seed: int = _DEFAULT_SEED,
) -> SearchSpaceReport
Run model pre-selection and prepare a search-space report.
Builds an :class:AI4RAGSearchSpace from the given model lists, runs :class:ModelsPreSelector when the number of models exceeds the configured caps, detects the benchmark language, and returns a structured report.
Parameters:
-
test_data_path(str | Path) –Path to a JSON file containing benchmark questions and expected answers.
-
extracted_text_path(str | Path) –Path to a single DoclingDocument JSON file or a directory of such files.
-
ogx_client(OgxClient) –An authenticated :class:
OgxClientinstance. -
embedding_models(list[str] | None, default:None) –Embedding model identifiers.
Noneuses the server defaults. -
generation_models(list[str] | None, default:None) –Generation model identifiers.
Noneuses the server defaults. -
metric(str, default:_DEFAULT_METRIC) –Quality metric for intermediate pattern evaluation. Must be one of
"faithfulness","answer_correctness", or"context_correctness". -
top_n_generation(int, default:_DEFAULT_TOP_N_GENERATION) –Maximum number of generation models to retain.
-
top_k_embedding(int, default:_DEFAULT_TOP_K_EMBEDDING) –Maximum number of embedding models to retain.
-
sample_size(int, default:_DEFAULT_SAMPLE_SIZE) –Number of benchmark records sampled for model pre-selection.
-
random_seed(int, default:_DEFAULT_SEED) –Seed for reproducible sampling.
Returns:
-
SearchSpaceReport–Structured report containing the verbose search space, selected models, and detected language.
Raises:
-
ValueError–If metric is not one of the supported values.
-
TypeError–If embedding_models or generation_models contain invalid entries.
Source code in ai4rag/components/optimization/search_space_preparation.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
RAG Optimization¶
rag_templates_optimization ¶
Classes¶
OptimizationResult dataclass ¶
Output of a complete RAG optimization run.
Attributes:
-
patterns(list[dict]) –Pattern definitions for each evaluated RAG configuration.
-
evaluations(list) –Raw evaluation result objects from the experiment.
Functions¶
run_rag_optimization ¶
run_rag_optimization(
extracted_text_path: str | Path,
test_data_path: str | Path,
search_space_report_path: str | Path,
output_dir: str | Path,
ogx_client: OgxClient,
vector_io_provider_id: str,
test_data_key: str = "",
input_data_key: str = "",
optimization_settings: dict | None = None,
) -> OptimizationResult
Run a full AI4RAG optimization experiment and generate output artefacts.
Orchestrates the end-to-end workflow: load documents, reconstruct the search space from a YAML report, run the experiment, then generate per-pattern outputs (pattern.json, notebooks, scripts, evaluation results).
Parameters:
-
extracted_text_path(str | Path) –Path to a folder of DoclingDocument JSON files (or a single file).
-
test_data_path(str | Path) –Path to a benchmark JSON file with questions and expected answers.
-
search_space_report_path(str | Path) –Path to the YAML report produced by the search-space preparation step.
-
output_dir(str | Path) –Root directory where per-pattern output folders are written.
-
ogx_client(OgxClient) –An authenticated :class:
OgxClientinstance. -
vector_io_provider_id(str) –Vector I/O provider identifier registered in OGX.
-
test_data_key(str, default:'') –Object-storage key for the test data file, embedded into generated notebooks.
-
input_data_key(str, default:'') –Object-storage key for the documents directory, embedded into generated notebooks.
-
optimization_settings(dict | None, default:None) –Optional dictionary with
"metric"and/or"max_number_of_rag_patterns"overrides.
Returns:
-
OptimizationResult–Contains the list of pattern definitions, raw evaluations, and the total number of parameter combinations explored.
Raises:
-
ValueError–If
test_data_keydoes not point to a JSON file,vector_io_provider_idis empty, or the optimization metric is not supported. -
TypeError–If
optimization_settingshas invalid types.
Source code in ai4rag/components/optimization/rag_templates_optimization.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |