Skip to content

Load personas dynamically from .jupyter dir #1380

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

Merged
merged 13 commits into from
Jun 27, 2025

Conversation

fperez
Copy link
Contributor

@fperez fperez commented Jun 24, 2025

Draft PR to get a first look at the approach, will refine based on early feedback before proper review.

Implements a local persona loader that targets pure python files with persona in the name, and loads all personas in those files that subclass BasePersona.

This is meant to be called by the main PersonaManager class safely.

Follow-up issues

Copy link
Contributor

@ellisonbg ellisonbg left a comment

Choose a reason for hiding this comment

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

So nice to review your PR @fperez, truly a pleasure :-) Wonderful to see you have the bandwidth to contribute again. I know this is a WIP but left a few comments.

@@ -135,6 +139,10 @@ def _init_persona_classes(self) -> None:
"ERROR: Jupyter AI has no AI personas available. "
+ "Please verify your server configuration and open a new issue on our GitHub repo if this warning persists."
)

# TODO: check whether in the end we need a full class for this, or if we can just use a simple function that returns a list of persona classes from the local filesystem.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, my sense so far is that a simple function will be simpler and cleaner. The only thing that I could imagine needing state for is for caching, but that can be done with a decorator.

@@ -135,6 +139,10 @@ def _init_persona_classes(self) -> None:
"ERROR: Jupyter AI has no AI personas available. "
+ "Please verify your server configuration and open a new issue on our GitHub repo if this warning persists."
)

# TODO: check whether in the end we need a full class for this, or if we can just use a simple function that returns a list of persona classes from the local filesystem.
persona_classes.extend(LocalPersonaLoader(self.root_dir).load_persona_classes())
Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to wrap this in a try/except once we get a sense of what exceptions might get triggered.

Copy link
Contributor

Choose a reason for hiding this comment

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

One thing we will need to think about is if a user moves the chat file while it is open (which instantiates the PersonaManager) such that a new .jupyter directory is relevant. That and we also want to reload the modules.

directory.
"""

def __init__(
Copy link
Contributor

Choose a reason for hiding this comment

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

This will all go away, but we usually avoid using an init method with LoggingConfigurable. If we need to keep the class we can share examples, but my sense is to switch to a function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, gone to function now (though I later added back the logger, just not making a new one, simply passing it as an arg).

def __init__(
self,
*args,
root_dir: str | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

I will soon have #1376 ready with the right logic for the dir to pass here.

return persona_classes

# Find all .py files in the root directory that contain "persona" in the name
all_py_files = glob(os.path.join(self.root_dir, "*.py"))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this could trigger a permission error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wrapped in try/except now.

for py_file in py_files:
try:
# Get module name from file path
module_name = Path(py_file).stem
Copy link
Contributor

Choose a reason for hiding this comment

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

Might want a private function that takes a py_file and returns the persona class or None. When we start to cache and enable reloading, we may want that encapsulation (this can wait until we really need it).

module_name = Path(py_file).stem

# Skip if module name starts with underscore (private modules)
if module_name.startswith("_"):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also include leading . in this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in commit 2517286, thx!

):
module_persona_classes.append(obj)

if module_persona_classes:
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice that we can support multiple per file :-)

@dlqqq dlqqq added the enhancement New feature or request label Jun 27, 2025
@dlqqq dlqqq changed the title WIP - Local personas loading Load personas dynamically from .jupyter dir Jun 27, 2025
@dlqqq dlqqq marked this pull request as ready for review June 27, 2025 18:13
Copy link
Member

@dlqqq dlqqq left a comment

Choose a reason for hiding this comment

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

Thank you Fernando, this is awesome! I pushed a commit fixing the mypy errors and opened a follow-up issue (#1395) that may help mitigate the performance impact of this PR. @ellisonbg @3coins Feel free to merge once you feel this is ready.

@dlqqq dlqqq merged commit 484fd20 into jupyterlab:main Jun 27, 2025
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants