Skip to content

utils

Various utiles used from the projects.

get_base_path()

Allows exposing the base path.

Source code in cat/utils.py
def get_base_path():
    """Allows exposing the base path."""
    return "cat/"

get_base_url()

Allows exposing the base url.

Source code in cat/utils.py
def get_base_url():
    """Allows exposing the base url."""
    secure = "s" if get_env("CCAT_CORE_USE_SECURE_PROTOCOLS") in ("true", "1") else ""
    cat_host = get_env("CCAT_CORE_HOST")
    cat_port = get_env("CCAT_CORE_PORT")
    return f"http{secure}://{cat_host}:{cat_port}/"

get_plugins_path()

Allows exposing the plugins' path.

Source code in cat/utils.py
def get_plugins_path():
    """Allows exposing the plugins' path."""
    return os.path.join(get_base_path(), "plugins/")

get_static_path()

Allows exposing the static files' path.

Source code in cat/utils.py
def get_static_path():
    """Allows exposing the static files' path."""
    return os.path.join(get_base_path(), "static/")

get_static_url()

Allows exposing the static server url.

Source code in cat/utils.py
def get_static_url():
    """Allows exposing the static server url."""
    return get_base_url() + "static/"

match_prompt_variables(prompt_variables, prompt_template)

Ensure prompt variables and prompt placeholders map, so there are no issues on mismatches

Source code in cat/utils.py
def match_prompt_variables(
        prompt_variables: Dict,
        prompt_template: str
    ) -> Tuple[Dict, str]:
    """Ensure prompt variables and prompt placeholders map, so there are no issues on mismatches"""

    tmp_prompt = PromptTemplate.from_template(
        template=prompt_template
    )

    # outer set difference
    prompt_mismatches = set(prompt_variables.keys()) ^ set(tmp_prompt.input_variables)

    # clean up
    for m in prompt_mismatches:
        if m in prompt_variables.keys():
            log.warning(f"Prompt variable '{m}' not found in prompt template, removed")
            del prompt_variables[m]
        if m in tmp_prompt.input_variables:
            prompt_template = \
                prompt_template.replace("{" + m + "}", "")
            log.warning(f"Placeholder '{m}' not found in prompt variables, removed")

    return prompt_variables, prompt_template

to_camel_case(text)

Format string to camel case.

Takes a string of words separated by either hyphens or underscores and returns a string of words in camel case.

Parameters:

Name Type Description Default
text str

String of hyphens or underscores separated words.

required

Returns:

Type Description
str

Camel case formatted string.

Source code in cat/utils.py
def to_camel_case(text: str) -> str:
    """Format string to camel case.

    Takes a string of words separated by either hyphens or underscores and returns a string of words in camel case.

    Parameters
    ----------
    text : str
        String of hyphens or underscores separated words.

    Returns
    -------
    str
        Camel case formatted string.
    """
    s = text.replace("-", " ").replace("_", " ").capitalize()
    s = s.split()
    if len(text) == 0:
        return text
    return s[0] + "".join(i.capitalize() for i in s[1:])

verbal_timedelta(td)

Convert a timedelta in human form.

The function takes a timedelta and converts it to a human-readable string format.

Parameters:

Name Type Description Default
td timedelta

Difference between two dates.

required

Returns:

Type Description
str

Human-readable string of time difference.

Notes

This method is used to give the Language Model information time information about the memories retrieved from the vector database.

Examples:

>>> print(verbal_timedelta(timedelta(days=2, weeks=1))
'One week and two days ago'
Source code in cat/utils.py
def verbal_timedelta(td: timedelta) -> str:
    """Convert a timedelta in human form.

    The function takes a timedelta and converts it to a human-readable string format.

    Parameters
    ----------
    td : timedelta
        Difference between two dates.

    Returns
    -------
    str
        Human-readable string of time difference.

    Notes
    -----
    This method is used to give the Language Model information time information about the memories retrieved from
    the vector database.

    Examples
    --------
    >>> print(verbal_timedelta(timedelta(days=2, weeks=1))
    'One week and two days ago'
    """

    if td.days != 0:
        abs_days = abs(td.days)
        if abs_days > 7:
            abs_delta = "{} weeks".format(td.days // 7)
        else:
            abs_delta = "{} days".format(td.days)
    else:
        abs_minutes = abs(td.seconds) // 60
        if abs_minutes > 60:
            abs_delta = "{} hours".format(abs_minutes // 60)
        else:
            abs_delta = "{} minutes".format(abs_minutes)
    if td < timedelta(0):
        return "{} ago".format(abs_delta)
    else:
        return "{} ago".format(abs_delta)