From a66adff004b34b1bf852ebb57405297384925468 Mon Sep 17 00:00:00 2001 From: Gabriel Almeida Date: Wed, 24 May 2023 09:27:00 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20refactor(custom=5Flists.py):=20r?= =?UTF-8?q?emove=20unused=20vectorstores=5Ftype=5Fto=5Fcls=5Fdict=20variab?= =?UTF-8?q?le=20=F0=9F=94=A8=20refactor(vector=5Fstore/base.py):=20refacto?= =?UTF-8?q?r=20VectorstoreCreator=20to=20use=20import=5Fclass=20from=20lan?= =?UTF-8?q?gflow.interface.importing.utils=20The=20`vectorstores=5Ftype=5F?= =?UTF-8?q?to=5Fcls=5Fdict`=20variable=20was=20not=20being=20used,=20so=20?= =?UTF-8?q?it=20was=20removed.=20The=20`VectorstoreCreator`=20class=20was?= =?UTF-8?q?=20refactored=20to=20use=20the=20`import=5Fclass`=20function=20?= =?UTF-8?q?from=20`langflow.interface.importing.utils`=20instead=20of=20im?= =?UTF-8?q?porting=20it=20directly.=20This=20improves=20the=20code's=20rea?= =?UTF-8?q?dability=20and=20maintainability.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../langflow/interface/custom_lists.py | 5 --- .../langflow/interface/vector_store/base.py | 41 +++++++------------ 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/src/backend/langflow/interface/custom_lists.py b/src/backend/langflow/interface/custom_lists.py index f07b03f04..af453139b 100644 --- a/src/backend/langflow/interface/custom_lists.py +++ b/src/backend/langflow/interface/custom_lists.py @@ -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] = { diff --git a/src/backend/langflow/interface/vector_store/base.py b/src/backend/langflow/interface/vector_store/base.py index 425fa9559..d30563d7b 100644 --- a/src/backend/langflow/interface/vector_store/base.py +++ b/src/backend/langflow/interface/vector_store/base.py @@ -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: