Skip to content

neurotrace.core.utils

neurotrace.core.utils

load_prompt(name)

Load a prompt file from the prompts directory.

Parameters:

Name Type Description Default
name str

Name of the prompt file (without .md)

required

Returns:

Name Type Description
str str

Prompt text

Source code in neurotrace/core/utils.py
def load_prompt(name: str) -> str:
    """
    Load a prompt file from the prompts directory.

    Args:
        name (str): Name of the prompt file (without .md)

    Returns:
        str: Prompt text
    """
    path = os.path.join(PROMPT_DIR, "tools", f"{name}.md")
    if not os.path.exists(path):
        raise FileNotFoundError(f"Prompt file {path} does not exist.")
    with open(path, "r", encoding="utf-8") as f:
        return f.read()

safe_json_loads(json_string, return_type=dict)

Safely load a JSON string, returning an empty dictionary on failure.

Parameters:

Name Type Description Default
json_string str

The JSON string to load.

required

Returns:

Type Description
type

param json_string:

type

param return_type:

Source code in neurotrace/core/utils.py
def safe_json_loads(json_string: str, return_type: type = dict) -> type:
    """
    Safely load a JSON string, returning an empty dictionary on failure.

    Args:
        json_string (str): The JSON string to load.

    Returns:
        :param json_string:
        :param return_type:
    """
    try:
        return json.loads(json_string)
    except json.JSONDecodeError:
        return return_type()

strip_json_code_block(text)

Strip code block formatting from a JSON string.

Parameters:

Name Type Description Default
text str

The input text containing a JSON code block.

required

Returns:

Name Type Description
str str

The cleaned JSON string without code block formatting.

Source code in neurotrace/core/utils.py
def strip_json_code_block(text: str) -> str:
    """
    Strip code block formatting from a JSON string.

    Args:
        text (str): The input text containing a JSON code block.

    Returns:
        str: The cleaned JSON string without code block formatting.
    """
    return text.strip("```json\n").strip("```").strip()