Skip to content

utils ¤

Functions:

Name Description
can_be_jinja

Checks if a string can be compiled using the jinja2 template engine.

format_prompt_msg

Formats a string using the jinja2 template engine if possible, otherwise uses the python string format.

can_be_jinja ¤

can_be_jinja(string: str) -> bool

Checks if a string can be compiled using the jinja2 template engine.

Source code in src/declarai/operators/utils.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def can_be_jinja(string: str) -> bool:
    """
    Checks if a string can be compiled using the jinja2 template engine.
    """
    if "{{" in string or "{%" in string or "{#" in string:
        try:
            jinja2.Template(string)
            return True
        except jinja2.exceptions.TemplateSyntaxError:
            return False
    else:
        return False

format_prompt_msg ¤

format_prompt_msg(_string: str, **kwargs: str) -> str

Formats a string using the jinja2 template engine if possible, otherwise uses the python string format. Args: _string: The string to format **kwargs: The kwargs to pass to the template

Returns: The formatted string

Source code in src/declarai/operators/utils.py
18
19
20
21
22
23
24
25
26
27
28
29
30
def format_prompt_msg(_string: str, **kwargs) -> str:
    """
    Formats a string using the jinja2 template engine if possible, otherwise uses the python string format.
    Args:
        _string: The string to format
        **kwargs: The kwargs to pass to the template

    Returns: The formatted string
    """
    if can_be_jinja(_string):
        return jinja2.Template(_string).render(**kwargs)
    else:
        return _string.format(**kwargs)