Skip to content

symfonic.memory.prompts

prompts

Prompt template loader for bundled prompts.

Loads prompt templates from the package's prompts/ directory using importlib.resources, with optional override from a custom path.

load_prompt

load_prompt(
    name: str, overrides: Path | None = None
) -> str

Load a prompt template by name.

Parameters:

Name Type Description Default
name str

Template name without extension (e.g., 'system', 'router', 'compaction').

required
overrides Path | None

Optional directory path. If provided and contains a file named {name}.txt, that file is used instead of the bundled one.

None

Returns:

Type Description
str

The prompt template as a string.

Raises:

Type Description
FileNotFoundError

If the prompt template is not found in either the overrides directory or the bundled package.

Source code in src/symfonic/memory/prompts/__init__.py
def load_prompt(name: str, overrides: Path | None = None) -> str:
    """Load a prompt template by name.

    Args:
        name: Template name without extension (e.g., 'system', 'router', 'compaction').
        overrides: Optional directory path. If provided and contains a file
            named ``{name}.txt``, that file is used instead of the bundled one.

    Returns:
        The prompt template as a string.

    Raises:
        FileNotFoundError: If the prompt template is not found in either
            the overrides directory or the bundled package.
    """
    filename = f"{name}.txt"

    if overrides is not None:
        override_path = overrides / filename
        if override_path.is_file():
            return override_path.read_text(encoding="utf-8")

    package_files = importlib.resources.files("symfonic.memory.prompts")
    resource = package_files.joinpath(filename)
    try:
        return resource.read_text(encoding="utf-8")
    except (FileNotFoundError, TypeError) as exc:
        raise FileNotFoundError(
            f"Prompt template '{filename}' not found in package or overrides"
        ) from exc