-
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.
Remove
from_template
and add Directory Registry (#9)
* directory registry * move test templates under test folder, stop using MultiLoader * fix simplemma * re-enable tests * linting * fix macro docs * fix readme
- Loading branch information
Showing
23 changed files
with
266 additions
and
152 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
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
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,4 +1,6 @@ | ||
import json | ||
# SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import os | ||
from pathlib import Path | ||
from typing import Any | ||
|
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,16 +1,10 @@ | ||
# SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import os | ||
from pathlib import Path | ||
|
||
from jinja2 import Environment, select_autoescape | ||
from jinja2 import Environment, PackageLoader, select_autoescape | ||
|
||
from .config import config | ||
from .filters import lemmatize | ||
from .loader import MultiLoader | ||
from .registries import FileTemplateRegistry | ||
from .registry import TemplateRegistry | ||
|
||
|
||
def _add_extensions(env): | ||
|
@@ -25,15 +19,9 @@ def _add_extensions(env): | |
env.add_extension(HFInferenceEndpointsExtension) | ||
|
||
|
||
def _add_default_templates(r: TemplateRegistry): | ||
templates_dir = Path(os.path.dirname(__file__)) / "templates" | ||
for tpl_file in templates_dir.glob("*.jinja"): | ||
r.set(name=tpl_file.name, prompt=tpl_file.read_text()) | ||
|
||
|
||
# Init the Jinja env | ||
env = Environment( | ||
loader=MultiLoader(), | ||
loader=PackageLoader("banks", "templates"), | ||
autoescape=select_autoescape( | ||
enabled_extensions=("html", "xml"), | ||
default_for_string=False, | ||
|
@@ -43,11 +31,7 @@ def _add_default_templates(r: TemplateRegistry): | |
enable_async=bool(config.ASYNC_ENABLED), | ||
) | ||
|
||
# Init the Template registry | ||
registry = FileTemplateRegistry(config.USER_DATA_PATH) | ||
|
||
|
||
# Setup custom filters and defaults | ||
env.filters["lemmatize"] = lemmatize | ||
_add_extensions(env) | ||
_add_default_templates(registry) |
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
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,6 +1,7 @@ | ||
# SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
from .directory import DirectoryTemplateRegistry | ||
from .file import FileTemplateRegistry | ||
|
||
__all__ = ("FileTemplateRegistry",) | ||
__all__ = ("FileTemplateRegistry", "DirectoryTemplateRegistry") |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
from pathlib import Path | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from banks import Prompt | ||
from banks.registry import TemplateNotFoundError | ||
|
||
|
||
class PromptFile(BaseModel): | ||
name: str | ||
version: str | ||
path: Path | ||
|
||
|
||
class PromptFileIndex(BaseModel): | ||
files: list[PromptFile] = Field(default=[]) | ||
|
||
|
||
class DirectoryTemplateRegistry: | ||
def __init__(self, directory_path: Path, *, force_reindex: bool = False): | ||
if not directory_path.is_dir(): | ||
msg = "{directory_path} must be a directory." | ||
raise ValueError(msg) | ||
|
||
self._path = directory_path | ||
self._index_path = self._path / "index.json" | ||
if not self._index_path.exists() or force_reindex: | ||
self._scan() | ||
else: | ||
self._load() | ||
|
||
def _load(self): | ||
self._index = PromptFileIndex.model_validate_json(self._index_path.read_text()) | ||
|
||
def _scan(self): | ||
self._index: PromptFileIndex = PromptFileIndex() | ||
for path in self._path.glob("*.jinja*"): | ||
pf = PromptFile(name=path.stem, version="0", path=path) | ||
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" | ||
for pf in self._index.files: | ||
if pf.name == name and pf.version == version and pf.path.exists(): | ||
return Prompt(pf.path.read_text()) | ||
raise TemplateNotFoundError | ||
|
||
def set(self, *, name: str, prompt: Prompt, version: str | None = None, overwrite: bool = False): | ||
version = version or "0" | ||
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" | ||
new_prompt_file.write_text(prompt.raw) | ||
pf = PromptFile(name=name, version=version, path=new_prompt_file) | ||
self._index.files.append(pf) |
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
Oops, something went wrong.