-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat & bugfix: Added metadata features and fixed naming bug (#11)
* Added metadata features and fixed naming bug * Added prompt exists check before setting meta * fix lint issues * fix lint issues * fix test lint issues * fix more lint issues * fix fmt issues * remove blanket * fixed overwrite and add tests * fixed lint locally * make the linters happy * piggyback on existing index infra, braking changes * fix lint issues * unified prompt details and meta into single index json * fixed tests from other test files * addressed all comments --------- Co-authored-by: Massimiliano Pippi <[email protected]>
- Loading branch information
1 parent
c15f849
commit 373ea6f
Showing
5 changed files
with
115 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,4 +185,5 @@ disable = [ | |
"missing-class-docstring", | ||
"missing-function-docstring", | ||
"cyclic-import", | ||
] | ||
] | ||
max-args = 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
# SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import time | ||
from pathlib import Path | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from banks import Prompt | ||
from banks.registry import TemplateNotFoundError | ||
|
||
# Constants | ||
DEFAULT_VERSION = "0" | ||
DEFAULT_INDEX_NAME = "index.json" | ||
|
||
|
||
class PromptFile(BaseModel): | ||
name: str | ||
version: str | ||
path: Path | ||
meta: dict | ||
|
||
|
||
class PromptFileIndex(BaseModel): | ||
|
@@ -26,7 +32,7 @@ def __init__(self, directory_path: Path, *, force_reindex: bool = False): | |
raise ValueError(msg) | ||
|
||
self._path = directory_path | ||
self._index_path = self._path / "index.json" | ||
self._index_path = self._path / DEFAULT_INDEX_NAME | ||
if not self._index_path.exists() or force_reindex: | ||
self._scan() | ||
else: | ||
|
@@ -38,24 +44,63 @@ def _load(self): | |
def _scan(self): | ||
self._index: PromptFileIndex = PromptFileIndex() | ||
for path in self._path.glob("*.jinja*"): | ||
pf = PromptFile(name=path.stem, version="0", path=path) | ||
name, version = path.stem.rsplit(".", 1) if "." in path.stem else (path.stem, DEFAULT_VERSION) | ||
pf = PromptFile(name=name, version=version, path=path, meta={}) | ||
self._index.files.append(pf) | ||
self._index_path.write_text(self._index.model_dump_json()) | ||
|
||
def get(self, *, name: str, version: str | None = None) -> "Prompt": | ||
version = version or "0" | ||
def get(self, *, name: str, version: str = DEFAULT_VERSION) -> "PromptFile": | ||
for pf in self._index.files: | ||
if pf.name == name and pf.version == version and pf.path.exists(): | ||
return Prompt(pf.path.read_text()) | ||
return pf | ||
raise TemplateNotFoundError | ||
|
||
def set(self, *, name: str, prompt: Prompt, version: str | None = None, overwrite: bool = False): | ||
version = version or "0" | ||
def get_prompt(self, *, name: str, version: str = DEFAULT_VERSION) -> Prompt: | ||
return Prompt(self.get(name=name, version=version).path.read_text()) | ||
|
||
def _get_prompt_file(self, *, name: str, version: str) -> PromptFile | None: | ||
for pf in self._index.files: | ||
if pf.name == name and pf.version == version and overwrite: | ||
pf.path.write_text(prompt.raw) | ||
return | ||
new_prompt_file = self._path / "{name}.{version}.jinja" | ||
if pf.name == name and pf.version == version: | ||
return pf | ||
return None | ||
|
||
def _create_pf(self, *, name: str, prompt: Prompt, version: str, overwrite: bool, meta: dict) -> "PromptFile": | ||
pf = self._get_prompt_file(name=name, version=version) | ||
if pf: | ||
if not overwrite: | ||
msg = f"Prompt {name}.{version}.jinja already exists. Use overwrite=True to overwrite." | ||
raise ValueError(msg) | ||
pf.path.write_text(prompt.raw) | ||
pf.meta = meta | ||
return pf | ||
new_prompt_file = self._path / f"{name}.{version}.jinja" | ||
new_prompt_file.write_text(prompt.raw) | ||
pf = PromptFile(name=name, version=version, path=new_prompt_file) | ||
self._index.files.append(pf) | ||
pf = PromptFile(name=name, version=version, path=new_prompt_file, meta=meta) | ||
return pf | ||
|
||
def set( | ||
self, | ||
*, | ||
name: str, | ||
prompt: Prompt, | ||
meta: dict | None = None, | ||
version: str = DEFAULT_VERSION, | ||
overwrite: bool = False, | ||
): | ||
meta = {**(meta or {}), "created_at": time.ctime()} | ||
|
||
pf = self._create_pf(name=name, prompt=prompt, version=version, overwrite=overwrite, meta=meta) | ||
if pf not in self._index.files: | ||
self._index.files.append(pf) | ||
self._index_path.write_text(self._index.model_dump_json()) | ||
|
||
def get_meta(self, *, name: str, version: str = DEFAULT_VERSION) -> dict: | ||
return self.get(name=name, version=version).meta | ||
|
||
def update_meta(self, *, meta: dict, name: str, version: str = DEFAULT_VERSION): | ||
pf = self._get_prompt_file(name=name, version=version) | ||
if not pf: | ||
unk_err = f"Prompt {name}.{version} not found in the index. Cannot set meta for a non-existing prompt." | ||
raise ValueError(unk_err) | ||
pf.meta = meta | ||
self._index_path.write_text(self._index.model_dump_json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters