Skip to content

Commit

Permalink
Added prompt exists check before setting meta
Browse files Browse the repository at this point in the history
  • Loading branch information
mayankjobanputra committed Aug 29, 2024
1 parent fd7e220 commit 6ccd242
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/banks/registries/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def set_meta(self, *, meta: dict, name: str, version: str | None = None, overwri
version = version or DEFAULT_VERSION
if not self._meta_path.exists():
self._meta_path.mkdir()
if Path(self._path / f"{name}.{version}.jinja") not in [pf.path for pf in self._index.files]:
raise ValueError(f"Prompt {name}.{version}.jinja not found in the index. "
f"Cannot set meta for a non-existing prompt.")

if f"{name}:{version}.json" in self._meta_path.glob("*.json"):
if not overwrite:
Expand Down
9 changes: 7 additions & 2 deletions tests/test_directory_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,10 @@ def test_empty_meta(populated_dir):

def test_set_meta(populated_dir):
r = DirectoryTemplateRegistry(populated_dir)
r.set_meta(name="new", version="2", meta={"accuracy": 91.2, "last_updated": time.ctime()})
assert r.get_meta(name="new", version="2") == {"accuracy": 91.2, "last_updated": time.ctime()}
new_prompt = Prompt("a very new prompt!")
r.set(name="new", version="3", prompt=new_prompt)
r.set_meta(name="new", version="3", meta={"accuracy": 91.2, "last_updated": time.ctime()})
assert r.get_meta(name="new", version="3") == {"accuracy": 91.2, "last_updated": time.ctime()}
with pytest.raises(ValueError):
r.set_meta(name="foo", version="bar", meta={"accuracy": 91.2, "last_updated": time.ctime()}, overwrite=False)

0 comments on commit 6ccd242

Please sign in to comment.