diff --git a/docs/config.md b/docs/config.md index 1a2609a..cdad7d1 100644 --- a/docs/config.md +++ b/docs/config.md @@ -21,7 +21,7 @@ config.ASYNC_ENABLED = True | Default value: | `False` | | Env var: | `BANKS_ASYNC_ENABLED` | -Whether or not to use `asyncio` for template rendering and LLM generation. This setting won't speed up your +Whether or not to use `asyncio` for prompt rendering and LLM generation. This setting won't speed up your application, only set it to `True` if you need to integrate Banks into an async codebase. diff --git a/docs/registry.md b/docs/registry.md index cb3666b..14205cb 100644 --- a/docs/registry.md +++ b/docs/registry.md @@ -1,7 +1,8 @@ -## Template registry (BETA) +## Prompt registry (BETA) -Template registry is a storage API for versioned prompts. It allows you to store and retrieve templates from local storage. -Currently, it supports storing templates in a JSON file, but it can be extended to support other storage backends. +Prompt registry is a storage API for versioned prompts. It allows you to store and retrieve prompts from local storage. +Currently, it supports storing templates in a single JSON file or in a file system directory, but it can be extended to +support other storage backends. ### Usage diff --git a/src/banks/prompt.py b/src/banks/prompt.py index 281c60d..f3dcd4b 100644 --- a/src/banks/prompt.py +++ b/src/banks/prompt.py @@ -28,6 +28,9 @@ def __init__( Parameters: text: The template text. + name: The name to identify this prompt. + version: The version string attached to this prompt. + metadata: A key-value set of metadata pairs attached to this prompt. canary_word: The string to use for the `{{canary_word}}` extension. If `None`, a default string will be generated. render_cache: The caching backend to store rendered prompts. If `None`, the default in-memory backend will diff --git a/src/banks/registries/__init__.py b/src/banks/registries/__init__.py index 3ef2703..4c7e152 100644 --- a/src/banks/registries/__init__.py +++ b/src/banks/registries/__init__.py @@ -1,7 +1,7 @@ # SPDX-FileCopyrightText: 2023-present Massimiliano Pippi # # SPDX-License-Identifier: MIT -from .directory import DirectoryTemplateRegistry +from .directory import DirectoryPromptRegistry from .file import FilePromptRegistry -__all__ = ("FilePromptRegistry", "DirectoryTemplateRegistry") +__all__ = ("FilePromptRegistry", "DirectoryPromptRegistry") diff --git a/src/banks/registries/directory.py b/src/banks/registries/directory.py index 6f7f28d..1d4a925 100644 --- a/src/banks/registries/directory.py +++ b/src/banks/registries/directory.py @@ -32,7 +32,7 @@ class PromptFileIndex(BaseModel): files: list[PromptFile] = Field(default=[]) -class DirectoryTemplateRegistry: +class DirectoryPromptRegistry: def __init__(self, directory_path: Path, *, force_reindex: bool = False): if not directory_path.is_dir(): msg = "{directory_path} must be a directory." @@ -93,5 +93,5 @@ def _get_prompt_file(self, *, name: str | None, version: str) -> tuple[int, Prom if pf.name == name and pf.version == version: return i, pf - msg = f"cannot find template with name '{name}' and version '{version}'" + msg = f"cannot find prompt with name '{name}' and version '{version}'" raise PromptNotFoundError(msg) diff --git a/src/banks/registries/file.py b/src/banks/registries/file.py index 8dc0587..70b6da3 100644 --- a/src/banks/registries/file.py +++ b/src/banks/registries/file.py @@ -55,5 +55,5 @@ def _get_prompt_model(self, name: str | None, version: str | None) -> tuple[int, if model.name == name and model.version == version: return i, model - msg = f"cannot find template with name '{name}' and version '{version}'" + msg = f"cannot find prompt with name '{name}' and version '{version}'" raise PromptNotFoundError(msg) diff --git a/tests/test_directory_registry.py b/tests/test_directory_registry.py index ce7e6dc..21d7c75 100644 --- a/tests/test_directory_registry.py +++ b/tests/test_directory_registry.py @@ -6,7 +6,7 @@ from banks.errors import InvalidPromptError, PromptNotFoundError from banks.prompt import Prompt -from banks.registries.directory import DirectoryTemplateRegistry +from banks.registries.directory import DirectoryPromptRegistry @pytest.fixture @@ -16,41 +16,41 @@ def registry(tmp_path: Path): for fp in (Path(__file__).parent / "templates").iterdir(): with open(d / fp.name, "w") as f: f.write(fp.read_text()) - return DirectoryTemplateRegistry(d, force_reindex=True) + return DirectoryPromptRegistry(d, force_reindex=True) -def test_init_from_scratch(registry: DirectoryTemplateRegistry): +def test_init_from_scratch(registry: DirectoryPromptRegistry): p = registry.get(name="blog") assert p.raw.startswith("{# Zero-shot, this is already enough for most topics in english -#}") assert p.metadata == {} -def test_init_from_existing_index(registry: DirectoryTemplateRegistry): +def test_init_from_existing_index(registry: DirectoryPromptRegistry): # at this point, the index has been created assert len(registry._index.files) == 6 -def test_init_from_existing_index_force(registry: DirectoryTemplateRegistry): +def test_init_from_existing_index_force(registry: DirectoryPromptRegistry): # change the directory structure f = registry.path / "blog.jinja" os.remove(f) # force recreation, the renamed file should be updated in the index - r = DirectoryTemplateRegistry(registry.path, force_reindex=True) + r = DirectoryPromptRegistry(registry.path, force_reindex=True) with pytest.raises(PromptNotFoundError): r.get(name="blog") def test_init_invalid_dir(): with pytest.raises(ValueError): - DirectoryTemplateRegistry(Path("does/not/exists")) + DirectoryPromptRegistry(Path("does/not/exists")) -def test_get_not_found(registry: DirectoryTemplateRegistry): +def test_get_not_found(registry: DirectoryPromptRegistry): with pytest.raises(PromptNotFoundError): registry.get(name="FOO") -def test_set_existing_no_overwrite(registry: DirectoryTemplateRegistry): +def test_set_existing_no_overwrite(registry: DirectoryPromptRegistry): new_prompt = Prompt("a new prompt!", name="blog") with pytest.raises( InvalidPromptError, match="Prompt with name 'blog' already exists. Use overwrite=True to overwrite" @@ -58,7 +58,7 @@ def test_set_existing_no_overwrite(registry: DirectoryTemplateRegistry): registry.set(prompt=new_prompt) -def test_set_existing_overwrite(registry: DirectoryTemplateRegistry): +def test_set_existing_overwrite(registry: DirectoryPromptRegistry): new_prompt = Prompt("a new prompt!", name="blog") current_time = time.ctime() registry.set(prompt=new_prompt, overwrite=True) @@ -68,7 +68,7 @@ def test_set_existing_overwrite(registry: DirectoryTemplateRegistry): } # created_at changes because it's overwritten -def test_set_multiple_templates(registry: DirectoryTemplateRegistry): +def test_set_multiple_templates(registry: DirectoryPromptRegistry): current_time = time.ctime() new_prompt = Prompt("a very new prompt!", name="new", version="2") @@ -85,7 +85,7 @@ def test_set_multiple_templates(registry: DirectoryTemplateRegistry): assert p.metadata == {"created_at": current_time} -def test_update_meta(registry: DirectoryTemplateRegistry): +def test_update_meta(registry: DirectoryPromptRegistry): # test metadata for initial set new_prompt = Prompt("a very new prompt!", name="new", version="3", metadata={"accuracy": 91.2}) current_time = time.ctime()