-
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
Changes from 7 commits
fd7e220
6ccd242
bf43977
2612da1
17b370c
54681ab
20da747
36cf5e9
d46309f
d72b42f
8f2829d
3946341
d578e65
09f18c2
8ac0b66
ea7fb82
19b33f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
# SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import json | ||
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" | ||
default_meta_path = "meta" | ||
|
||
|
||
class PromptFile(BaseModel): | ||
name: str | ||
|
@@ -26,11 +32,12 @@ 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: | ||
self._load() | ||
self._meta_path = self._path / default_meta_path | ||
|
||
def _load(self): | ||
self._index = PromptFileIndex.model_validate_json(self._index_path.read_text()) | ||
|
@@ -43,19 +50,42 @@ def _scan(self): | |
self._index_path.write_text(self._index.model_dump_json()) | ||
|
||
def get(self, *, name: str, version: str | None = None) -> "Prompt": | ||
version = version or "0" | ||
version = version or default_version | ||
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" | ||
version = version or default_version | ||
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 = 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) | ||
|
||
def get_meta(self, *, name: str, version: str | None = None) -> dict: | ||
version = version or default_version | ||
meta_path = self._meta_path / f"{name}.{version}.json" | ||
if not meta_path.exists(): | ||
msg = f"Meta directory or file for prompt {name}:{version}.jinja not found." | ||
raise FileNotFoundError(msg) | ||
return json.loads(open(meta_path).read()) | ||
|
||
def set_meta(self, *, meta: dict, name: str, version: str | None = None, overwrite: bool = False): | ||
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]: | ||
msg = f"Prompt {name}.{version}.jinja not found in the index. Cannot set meta for a non-existing prompt." | ||
raise ValueError(msg) | ||
|
||
if f"{name}:{version}.json" in self._meta_path.glob("*.json"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
is it working? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, it wasn't. I fixed and added unit tests as well. :) |
||
if not overwrite: | ||
msg = f"Meta file for prompt {name}:{version} already exists. Use overwrite=True to overwrite." | ||
raise ValueError(msg) | ||
meta_path = self._meta_path / f"{name}.{version}.json" | ||
meta_path.write_text(json.dumps(meta)) |
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.
I would try to pass meta to the regular
set()
(see comment above)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.
I would do that but I will also provide
set_meta
separately because there are many use cases (i.e., adding accuracy, adding failed stats) that require setting meta after running the prompt. We also need such things per prompt so I will still keep the different folders for storing the meta and the prompt/index.