Skip to content

src.llm.pattern_detection.pattern_utils.load_patterns(yaml_path)

Load tool call patterns from a YAML file.

Parameters:

Name Type Description Default
yaml_path str

Path to the YAML file containing patterns

required

Returns:

Type Description
Dict[str, str]

Dictionary mapping pattern names to pattern strings

Source code in src/llm/pattern_detection/pattern_utils.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
def load_patterns(yaml_path: str) -> Dict[str, str]:
    """
    Load tool call patterns from a YAML file.

    Args:
        yaml_path: Path to the YAML file containing patterns

    Returns:
        Dictionary mapping pattern names to pattern strings
    """
    with open(yaml_path, 'r') as f:
        config = yaml.safe_load(f)
    return config.get('patterns', {})

src.llm.pattern_detection.pattern_utils.normalize_and_map(text)

Returns a tuple of normalized text with whitespace removed and an index mapping.

Processes the input text by removing all whitespace characters and creating a mapping that tracks the original position of each character.

Parameters:

Name Type Description Default
text str

The input string to be normalized.

required

Returns:

Name Type Description
tuple

A tuple containing two elements:

  • normalized_text (str): The input text with all whitespace removed.
  • index_map (list): A list where index_map[i] is the original index of the i-th character in normalized_text.
Example usage
normalize_and_map("a b c")  # ('abc', [0, 2, 4])
Source code in src/llm/pattern_detection/pattern_utils.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def normalize_and_map(text: str):
    """Returns a tuple of normalized text with whitespace removed and an index mapping.

    Processes the input text by removing all whitespace characters and creating
    a mapping that tracks the original position of each character.

    Args:
        text: The input string to be normalized.

    Returns:
        tuple: A tuple containing two elements:

            - `normalized_text` (str): The input text with all whitespace removed.
            - `index_map` (list): A list where index_map[i] is the original index
              of the i-th character in normalized_text.

    ``` python title="Example usage"
    normalize_and_map("a b c")  # ('abc', [0, 2, 4])
    ```
    """
    normalized_chars = []
    index_map = []
    for idx, ch in enumerate(text):
        if not ch.isspace():
            normalized_chars.append(ch)
            index_map.append(idx)
    return "".join(normalized_chars), index_map