Coverage for presidio_analyzer / llm_utils / examples_loader.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-29 09:03 +0000

1"""Examples loading utilities for LLM recognizers.""" 

2import logging 

3from typing import Dict, List 

4 

5from .config_loader import load_yaml_file, resolve_config_path, validate_config_fields 

6from .langextract_helper import check_langextract_available, lx 

7 

8logger = logging.getLogger("presidio-analyzer") 

9 

10__all__ = [ 

11 "load_yaml_examples", 

12 "convert_to_langextract_format", 

13] 

14 

15 

16def load_yaml_examples( 

17 examples_file: str, conf_subdir: str = "conf" 

18) -> List[Dict]: 

19 """Load and validate examples from YAML configuration file. 

20 

21 :param examples_file: Path to YAML file with examples (repo-root-relative). 

22 :param conf_subdir: Configuration subdirectory (deprecated, kept for compatibility). 

23 :return: List of example dictionaries. 

24 :raises ValueError: If 'examples' field is missing. 

25 """ 

26 filepath = resolve_config_path(examples_file) 

27 data = load_yaml_file(filepath) 

28 validate_config_fields(data, ["examples"], "Examples file") 

29 return data["examples"] 

30 

31 

32def convert_to_langextract_format(examples_data: List[Dict]) -> List: 

33 """Convert example dictionaries to LangExtract Example objects. 

34 

35 :param examples_data: List of example dictionaries with text and extractions. 

36 :return: List of LangExtract Example objects. 

37 :raises ImportError: If langextract is not installed. 

38 """ 

39 check_langextract_available() 

40 

41 langextract_examples = [] 

42 for example in examples_data: 

43 extractions = [ 

44 lx.data.Extraction( 

45 extraction_class=ext["extraction_class"], 

46 extraction_text=ext["extraction_text"], 

47 attributes=ext.get("attributes", {}), 

48 ) 

49 for ext in example.get("extractions", []) 

50 ] 

51 

52 langextract_examples.append( 

53 lx.data.ExampleData(text=example["text"], extractions=extractions) 

54 ) 

55 

56 return langextract_examples