-
-
Notifications
You must be signed in to change notification settings - Fork 395
Separate BaseProvider
for faster import
#1338
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
base: main
Are you sure you want to change the base?
Separate BaseProvider
for faster import
#1338
Conversation
@krassowski Thank you for opening this PR! Really appreciate you taking the lead on debugging the module import latency & for providing objective data. I remember you had mentioned in another thread that LangChain recently improved their module imports to be faster; do you know if that is as simple as upgrading our version floor on I've been tasked with a 2-week coding sprint to work on another project right now. Sorry if I'm slow to respond for the next week. I'll be back on Monday to give this a full review. |
"QianfanProvider", | ||
"TogetherAIProvider", | ||
] | ||
assert set(_dynamic_imports_map.keys()) - set(__all__) == set() |
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.
This static assertion is great, but it won't run until the module is imported. Can we add a unit test for this to guarantee that CI will block release if this assertion excepts?
def test_top_level_module():
from jupyter_ai_magics import * as exports
assert hasattr(exports, 'BaseProvider')
... # etc
I know there are other tests that import from jupyter_ai_magics
, but having an explicit one reduces the chance that this causes a runtime error for users even if we disable/skip other tests in the future.
from langchain_core._import_utils import import_attr | ||
|
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.
This looks like a private module, which means it may break in patch releases. Can we re-implement this in jupyter_ai_magics
to prevent that in advance?
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.
Yes, that's the plan! I took a shortcut for PoC.
_dynamic_imports = { | ||
# expose embedding model providers on the package root | ||
"embedding_providers": [ | ||
"BaseEmbeddingsProvider", | ||
"GPT4AllEmbeddingsProvider", | ||
"HfHubEmbeddingsProvider", | ||
"QianfanEmbeddingsEndpointProvider", | ||
], | ||
"exception": ["store_exception"], | ||
"magics": ["AiMagics"], | ||
# expose JupyternautPersona on the package root | ||
# required by `jupyter-ai`. | ||
"models.persona": ["JupyternautPersona", "Persona"], | ||
# expose model providers on the package root | ||
"providers": [ | ||
"AI21Provider", | ||
"BaseProvider", | ||
"GPT4AllProvider", | ||
"HfHubProvider", | ||
"QianfanProvider", | ||
"TogetherAIProvider", | ||
], | ||
} |
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.
When consumers import from jupyter_ai_magics
, do they still get type inference for mypy
& IDE intellisense? I would test this myself but I'm short on time this week.
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.
It depends on the editor. The explicit __all__
helps, but yes not all editros will autocomplete. I think adding typing stubs could help but I am not sure if it is worth the effort.
I have not checked mypy yet.
We could recommend importing from specific modules instead of the top level which will in 100% preserve completions and mypy checks (and in case of base provider be faster too).
Yup, this was langchain-core, and the timings in this PR are based on the latest version including the fix. In fact this PR in the current state directly imports the lazy import machinery from Separately of this PR if you want to ensure all users get the faster imports then yes adding floor level pin should be enough as it was released in a minor version. Have a good coding spring! I saw your status which is why I did not ping - I really appreciate a quick look from you! |
BTW, do you have a good reference on how Python module imports work? I'd definitely like to review those resources before I review your PR fully. I'm surprised that you & LangChain maintainers had to add manual code changes to get dynamic fast imports. |
The relevant bits are:
|
References
BaseProvider
to a separate python module #1337User-facing changes
IPython users who define a custom model by inheriting from
BaseProvider
will be able to start IPython about 33% faster; this is significant both in relative and absolute terms (while for me it is only 0.4 faster, I run the benchmarks on a very fast CPU which is not necessarily representative of the general user base).from jupyter_ai_magics import BaseProvider
from jupyter_ai_magics.providers import BaseProvider
from jupyter_ai_magics.base_provider import BaseProvider
Before
After
jupyter-ai-magics
users who use%load_ext jupyter_ai_magics
at startup will not see a speedup yet becauseself.providers = get_lm_providers()
inAiMagics
init causes all providers to be loaded. They would only see a speedup if they disabled some of the providers (which previously would not have been the case as all providers would be imported anyways).To be done
import_attr
instead of importing it fromlangchain_core._import_utils
to make sure it does not break in a point release oflangchain
(preserving the attribution to langchain team)fix typingactually,mypy
is failing on other PRs too..