Selectors
Define selector abstractions and concrete selection strategies for retrieval results.
This module provides selector components used in the terminology-mapping pipeline to choose the best candidate from a set of retrieved expressions. It includes a simple rule-based selector that picks the first result under a distance threshold, as well as LLM-based selectors backed by OpenAI and Gemini models.
Selectors are designed to operate as pipeline stages that consume
RetrieverResults and return structured SelectorResults objects.
BaseSelector
Bases: PipelineBaseClass, ABC
Define the abstract interface for selector pipeline components.
This base class establishes the contract for selectors that choose a final
candidate from retrieval results. Subclasses must implement select(),
while the base __call__() method provides pipeline-compatible
invocation.
Source code in aatm\selectors.py
select(results)
abstractmethod
Select the best candidate result for each query.
Subclasses must implement this method to examine retrieval results and return a structured selection output for each query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
RetrieverResults
|
Retrieval results containing the original queries and their candidate matches. |
required |
Returns:
| Type | Description |
|---|---|
SelectorResults
|
A |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the subclass does not override this method. |
Source code in aatm\selectors.py
__call__(results)
Invoke the selector on retrieval results.
This method makes selector instances directly callable and delegates to
select().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
RetrieverResults
|
Retrieval results to process. |
required |
Returns:
| Type | Description |
|---|---|
SelectorResults
|
A |
Source code in aatm\selectors.py
FirstResultSelector
Bases: BaseSelector
Select the first retrieved result that satisfies a distance threshold.
This selector implements a simple heuristic strategy: it chooses the first candidate in each result list if its distance is below the configured threshold. If no candidate is available or the first candidate does not satisfy the threshold, an empty selection is returned.
Source code in aatm\selectors.py
__init__(selection_threshold=float('inf'), *args, **kwargs)
Initialize the first-result selector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selection_threshold
|
float
|
Maximum allowed distance for accepting the first retrieved result. If the first result has a larger distance, no selection is made. |
float('inf')
|
*args
|
Any
|
Additional positional arguments reserved for compatibility. |
()
|
**kwargs
|
Any
|
Additional keyword arguments reserved for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in aatm\selectors.py
select(results)
Select the first valid result for each query.
This method inspects the first candidate in each query result list and returns it as the selected expression when its distance is below the configured threshold. If no candidates are available or the threshold is not met, an empty selection is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
RetrieverResults
|
Retrieval results containing candidate expressions for each query. |
required |
Returns:
| Type | Description |
|---|---|
SelectorResults
|
A |
Source code in aatm\selectors.py
OpenAILLMModels
Bases: Enum
Enumerate the supported OpenAI model identifiers for selection.
This enumeration defines the OpenAI model names that can be used by the OpenAI-based selector implementation.
Source code in aatm\selectors.py
OpenAILLMSelector
Bases: BaseSelector
Select results using an OpenAI language model.
This selector formats each query and its retrieved candidate expressions into a prompt, asks an OpenAI model to choose the semantically closest expression, and validates the structured JSON response against the expected selection schema.
If the model returns no selection or selects an expression that is not present in the candidate list, the selector produces an empty selection.
Source code in aatm\selectors.py
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 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 | |
__init__(model_id, prompt_template=None, *args, **kwargs)
Initialize the OpenAI-based selector.
This constructor resolves the configured model identifier, creates an OpenAI client, and sets the prompt template used to ask the model to choose the best candidate expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
Name of the supported OpenAI model variant to use for selection. |
required |
prompt_template
|
List[Dict[str, str]]
|
Optional structured prompt template. If not provided, a default system-and-user prompt is used. |
None
|
*args
|
Any
|
Additional positional arguments reserved for compatibility. |
()
|
**kwargs
|
Any
|
Additional keyword arguments reserved for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in aatm\selectors.py
select(results)
Select the best candidate for each query using an OpenAI model.
This method formats a prompt for each query, sends it to the configured OpenAI model for structured response parsing, validates the selected expression identifier, and returns either the matched candidate or an empty selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
RetrieverResults
|
Retrieval results containing candidate expressions for each query. |
required |
Returns:
| Type | Description |
|---|---|
SelectorResults
|
A |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the parsed response is not a |
Source code in aatm\selectors.py
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 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 | |
GeminiLLMModels
Bases: Enum
Enumerate the supported Gemini model identifiers for selection.
This enumeration defines the Gemini model names that can be used by the Gemini-based selector implementation.
Source code in aatm\selectors.py
GeminiLLMSelector
Bases: BaseSelector
Select results using a Gemini language model.
This selector formats each query and its retrieved candidate expressions into a prompt, sends the prompt to a Gemini model configured for JSON output, and validates the returned structured selection against the expected schema.
If the model returns no selection or selects an expression that is not present in the candidate list, the selector produces an empty selection.
Source code in aatm\selectors.py
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |
__init__(model_id, prompt_template=None, *args, **kwargs)
Initialize the Gemini-based selector.
This constructor resolves the configured Gemini model identifier, creates a Gemini client, and sets the prompt template used to ask the model to choose the best candidate expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
Name of the supported Gemini model variant to use for selection. |
required |
prompt_template
|
List[Dict[str, str]]
|
Optional structured prompt template. If not provided, a default system-and-user prompt is used. |
None
|
*args
|
Any
|
Additional positional arguments reserved for compatibility. |
()
|
**kwargs
|
Any
|
Additional keyword arguments reserved for compatibility. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in aatm\selectors.py
select(results)
Select the best candidate for each query using a Gemini model.
This method formats a prompt for each query, converts it into Gemini content objects, requests a JSON response constrained by the selection schema, validates the returned expression identifier, and returns either the matched candidate or an empty selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
RetrieverResults
|
Retrieval results containing candidate expressions for each query. |
required |
Returns:
| Type | Description |
|---|---|
SelectorResults
|
A |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the parsed response is not a |
Source code in aatm\selectors.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |