Skip to content
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
merged 17 commits into from
Sep 7, 2024
Merged

Conversation

mayankjobanputra
Copy link
Collaborator

Allowing users to store metadata for each prompt

First create meta subdir inside the root template dir and then create prompt metadata.

@mayankjobanputra mayankjobanputra requested a review from masci August 29, 2024 21:48
@mayankjobanputra
Copy link
Collaborator Author

@masci should we disable more lint errors? I am not sure if these are valid errors, wdyt?

Copy link
Owner

@masci masci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the formatting errors, it's a matter of style but those were legit in order to stay consistent with the rest of the codebase

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"):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

glob returns a generator so this should be something like

if f"{name}:{version}.json" in list(self._meta_path.glob("*.json"))

is it working?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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. :)
Thanks for catching this one 🙌

@mayankjobanputra
Copy link
Collaborator Author

Thanks a lot @masci 🙏

What do you think of the PR?

Copy link
Owner

@masci masci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a comment about the overall plan of having a separate storage for metadata

if not self._index_path.exists() or force_reindex:
self._scan()
else:
self._load()
self._meta_path = self._path / DEFAULT_META_PATH
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why we want to use a separate index for the metadata? If we extend the PromptFile model to include meta: dict[str, Any] we would piggy back on the existing get/set infraastructure

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this, I would make the changes.

raise FileNotFoundError(msg)
return json.loads(open(meta_path, encoding="utf-8").read())

def set_meta(self, *, meta: dict, name: str, version: str | None = None, overwrite: bool = False):
Copy link
Owner

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)

Copy link
Collaborator Author

@mayankjobanputra mayankjobanputra Sep 2, 2024

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.

Copy link
Owner

@masci masci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments, almost there!!!

@@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noice!

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_prompt(self, *, name: str, version: str | None = None) -> "Prompt":
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why changing the method name? Its counterpart is still set

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because with set we can set both meta and prompt. I thought being explicit might be better?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah now I remember the convo, yeah let's scratch this. But I think get_meta will end up being just an utility method calling get_prompt under the hood? See comment below.

pf = PromptFile(name=name, version=version, path=new_prompt_file, meta=meta)
return pf

def set( # pylint: disable=too-many-arguments
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can relax this rule to 10 args per method

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool will do that.

name: str,
prompt: Prompt,
meta: dict | None = None,
version: str | None = None,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since strings are immutable, why not defaulting here directly? version: str = DEFAULT_VERSION,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, will do it.

for pf in self._index.files:
if pf.name == name and pf.version == version:
return pf.meta
raise MetaNotFoundError
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this, the error will be triggered by the fact the requested pf was not found in the index... Also what do you want to signal with MetaNotFound, the absence of the dict or an emty dict?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was like that before. I changed it to be consistent with get_prompt, it also raises TemplateNotFoundError when the prompt is not found.

The idea was that if someone makes a mistake in the name, they should get the error instead of silently returning the empty dict.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about

        for pf in self._index.files:
            if pf.name == name and pf.version == version:
                if not pf.meta:
                    raise MetaNotFoundError
                return pf
        raise TemplateNotFoundError

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 🙌

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, why not just return self.get_prompt(...).meta?

Copy link
Owner

@masci masci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff, merging!

@masci masci merged commit 373ea6f into main Sep 7, 2024
9 checks passed
@masci masci deleted the feat_meta branch September 7, 2024 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants