Skip to content

Commit

Permalink
template->prompt where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
masci committed Sep 29, 2024
1 parent 0af7061 commit e554a25
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand Down
7 changes: 4 additions & 3 deletions docs/registry.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/banks/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/banks/registries/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <[email protected]>
#
# SPDX-License-Identifier: MIT
from .directory import DirectoryTemplateRegistry
from .directory import DirectoryPromptRegistry
from .file import FilePromptRegistry

__all__ = ("FilePromptRegistry", "DirectoryTemplateRegistry")
__all__ = ("FilePromptRegistry", "DirectoryPromptRegistry")
4 changes: 2 additions & 2 deletions src/banks/registries/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/banks/registries/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
24 changes: 12 additions & 12 deletions tests/test_directory_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,49 +16,49 @@ 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"
):
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)
Expand All @@ -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")
Expand All @@ -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()
Expand Down

0 comments on commit e554a25

Please sign in to comment.