-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat & bugfix: Added metadata features and fixed naming bug #11
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fd7e220
Added metadata features and fixed naming bug
mayankjobanputra 6ccd242
Added prompt exists check before setting meta
mayankjobanputra bf43977
fix lint issues
mayankjobanputra 2612da1
fix lint issues
mayankjobanputra 17b370c
fix test lint issues
mayankjobanputra 54681ab
fix more lint issues
mayankjobanputra 20da747
fix fmt issues
masci 36cf5e9
remove blanket
masci d46309f
fixed overwrite and add tests
mayankjobanputra d72b42f
Merge branch 'feat_meta' of github.com:masci/banks into feat_meta
mayankjobanputra 8f2829d
fixed lint locally
mayankjobanputra 3946341
make the linters happy
masci d578e65
piggyback on existing index infra, braking changes
mayankjobanputra 09f18c2
fix lint issues
mayankjobanputra 8ac0b66
unified prompt details and meta into single index json
mayankjobanputra ea7fb82
fixed tests from other test files
mayankjobanputra 19b33f6
addressed all comments
mayankjobanputra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
noice!