🔥 refactor(custom_lists.py): remove unused vectorstores_type_to_cls_dict variable

🔨 refactor(vector_store/base.py): refactor VectorstoreCreator to use import_class from langflow.interface.importing.utils
The `vectorstores_type_to_cls_dict` variable was not being used, so it was removed. The `VectorstoreCreator` class was refactored to use the `import_class` function from `langflow.interface.importing.utils` instead of importing it directly. This improves the code's readability and maintainability.
This commit is contained in:
Gabriel Almeida 2023-05-24 09:27:00 -03:00
commit a66adff004
2 changed files with 15 additions and 31 deletions

View file

@ -60,11 +60,6 @@ embedding_type_to_cls_dict: dict[str, Any] = {
for embedding_name in embeddings.__all__
}
## Vector Stores
vectorstores_type_to_cls_dict: dict[str, Any] = {
vectorstore_name: import_class(f"langchain.vectorstores.{vectorstore_name}")
for vectorstore_name in vectorstores.__all__
}
## Document Loaders
documentloaders_type_to_cls_dict: dict[str, Any] = {

View file

@ -1,11 +1,13 @@
from typing import Dict, List, Optional, Type
from typing import Any, Dict, List, Optional, Type
from langchain import vectorstores
from langflow.interface.base import LangChainTypeCreator
from langflow.interface.custom_lists import vectorstores_type_to_cls_dict
from langflow.interface.importing.utils import import_class
from langflow.settings import settings
from langflow.template.nodes import VectorStoreFrontendNode
from langflow.utils.logger import logger
from langflow.utils.util import build_template_from_class, build_template_from_method
from langflow.utils.util import build_template_from_method
class VectorstoreCreator(LangChainTypeCreator):
@ -17,36 +19,23 @@ class VectorstoreCreator(LangChainTypeCreator):
@property
def type_to_loader_dict(self) -> Dict:
return vectorstores_type_to_cls_dict
if self.type_dict is None:
self.type_dict: dict[str, Any] = {
vectorstore_name: import_class(
f"langchain.vectorstores.{vectorstore_name}"
)
for vectorstore_name in vectorstores.__all__
}
return self.type_dict
def get_signature(self, name: str) -> Optional[Dict]:
"""Get the signature of an embedding."""
try:
signature = build_template_from_method(
return build_template_from_method(
name,
type_to_cls_dict=vectorstores_type_to_cls_dict,
type_to_cls_dict=self.type_to_loader_dict,
method_name="from_texts",
)
# TODO: Use FrontendendNode class to build the signature
# signature["template"] = {
# "documents": {
# "type": "TextSplitter",
# "required": True,
# "show": True,
# "name": "documents",
# "display_name": "Text Splitter",
# },
# "embedding": {
# "type": "Embeddings",
# "required": True,
# "show": True,
# "name": "embedding",
# "display_name": "Embedding",
# },
# }
return signature
except ValueError as exc:
raise ValueError(f"Vector Store {name} not found") from exc
except AttributeError as exc: