refactor: Lazy import builders, embedders, (chat)generators, retrievers #8655
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.
Related Issues
Proposed Changes:
How did you test it?
Notes for the reviewer
I just added the lazy import for a small selection of components in this draft. All components used in our "first RAG pipeline" tutorial are covered, which comes down to:
Tests are failing with TypeErrors. We also need to check whether type hints or auto completion in IDEs are affected in a bad way by the lazy import. For example, when add an
![Screenshot 2024-12-17 at 17 41 05](https://private-user-images.githubusercontent.com/4181769/396597515-7f0f248d-2a6c-46bd-b3ce-eb850bfde6c7.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5Nzc4NTMsIm5iZiI6MTczODk3NzU1MywicGF0aCI6Ii80MTgxNzY5LzM5NjU5NzUxNS03ZjBmMjQ4ZC0yYTZjLTQ2YmQtYjNjZS1lYjg1MGJmZGU2YzcucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDhUMDExOTEzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YmE1NWZiYmE1YjdhNTk4YWU5ODRlNzYxMGU2YzcwNWY4ZTY1NDA3ZDFjMWZjN2MyM2U4MWVhZTdmNTBjOTZjMiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.CXv5ZBEgjk8W9uZAsk_t2q4irbjdmgUKOZtqjDFx1hY)
AnswerBuilder
, I get two suggestions about importing either the class or the functionAnswerBuilder
Here is a comparison first without and then with lazy imports. Created with
python -X importtime 27_first_rag_pipeline.py 2> lazy-import.log tuna lazy-import.log
without lazy imports
![Screenshot 2024-12-17 at 18 16 02](https://private-user-images.githubusercontent.com/4181769/396598127-c8754761-ea9e-4003-b01f-8afd3959bbfa.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5Nzc4NTMsIm5iZiI6MTczODk3NzU1MywicGF0aCI6Ii80MTgxNzY5LzM5NjU5ODEyNy1jODc1NDc2MS1lYTllLTQwMDMtYjAxZi04YWZkMzk1OWJiZmEucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDhUMDExOTEzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NWE2ZThkNGM4MDM5YTMxOTM3YmM0M2QyYzc4M2MxZTkyZWNkYmQ1Nzc5NDE3NWU4YzZmYzg1OWFmN2UyNzllOSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.VwlM9WF4qC30JTDv9T7H97N0EDC4ExXcR1Mdew0W4vQ)
![Screenshot 2024-12-17 at 18 16 13](https://private-user-images.githubusercontent.com/4181769/396598136-2416db72-30d4-4d95-a41d-e3bc46a36bed.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg5Nzc4NTMsIm5iZiI6MTczODk3NzU1MywicGF0aCI6Ii80MTgxNzY5LzM5NjU5ODEzNi0yNDE2ZGI3Mi0zMGQ0LTRkOTUtYTQxZC1lM2JjNDZhMzZiZWQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIwOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMDhUMDExOTEzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YWJiMjRmNzUzNGZlNjI3NzA4YjU5YTRlMzBjNGRlYTJlZDU3OGZlNzQwNDc0YmNiOWMyN2ZiYzA5ZGE3MzBiNCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.EwkDpQ1mZ1TWxvPoYrle6uFKTENAa3bOCnNVhjqTjmI)
with lazy imports
An alternative to lazy imports would be to require users to know and always use the full path of the component they want to import.
from haystack.components.generators.hugging_face_local import HuggingFaceLocalGenerator
for example, instead of justfrom haystack.components.generators import HuggingFaceLocalGenerator
Checklist
fix:
,feat:
,build:
,chore:
,ci:
,docs:
,style:
,refactor:
,perf:
,test:
and added!
in case the PR includes breaking changes.