Refactor vector search components to use updated vector store model
This commit is contained in:
parent
33af41d328
commit
cff80d65b3
17 changed files with 19 additions and 19 deletions
83
src/backend/base/langflow/base/vectorstores/model.py
Normal file
83
src/backend/base/langflow/base/vectorstores/model.py
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
from typing import List, Union
|
||||
|
||||
from langchain_core.documents import Document
|
||||
|
||||
from langflow.custom import Component
|
||||
from langflow.field_typing import Text
|
||||
from langflow.helpers.data import docs_to_data
|
||||
from langflow.schema import Data
|
||||
from langflow.template import Output
|
||||
from langflow.field_typing import BaseRetriever, VectorStore
|
||||
|
||||
|
||||
class LCVectorStoreComponent(Component):
|
||||
outputs = [
|
||||
Output(
|
||||
display_name="Retriever",
|
||||
name="base_retriever",
|
||||
method="build_base_retriever",
|
||||
),
|
||||
Output(
|
||||
display_name="Search Results",
|
||||
name="search_results",
|
||||
method="search_documents",
|
||||
),
|
||||
]
|
||||
|
||||
def _validate_outputs(self):
|
||||
# At least these three outputs must be defined
|
||||
required_output_methods = ["build_vector_store", "build_base_retriever", "search_documents"]
|
||||
output_names = [output.name for output in self.outputs]
|
||||
for method_name in required_output_methods:
|
||||
if method_name not in output_names:
|
||||
raise ValueError(f"Output with name '{method_name}' must be defined.")
|
||||
elif not hasattr(self, method_name):
|
||||
raise ValueError(f"Method '{method_name}' must be defined.")
|
||||
|
||||
def search_with_vector_store(
|
||||
self,
|
||||
input_value: Text,
|
||||
search_type: str,
|
||||
vector_store: Union[VectorStore, BaseRetriever],
|
||||
k=10,
|
||||
**kwargs,
|
||||
) -> List[Data]:
|
||||
"""
|
||||
Search for data in the vector store based on the input value and search type.
|
||||
|
||||
Args:
|
||||
input_value (Text): The input value to search for.
|
||||
search_type (str): The type of search to perform.
|
||||
vector_store (VectorStore): The vector store to search in.
|
||||
|
||||
Returns:
|
||||
List[Data]: A list of data matching the search criteria.
|
||||
|
||||
Raises:
|
||||
ValueError: If invalid inputs are provided.
|
||||
"""
|
||||
|
||||
docs: List[Document] = []
|
||||
if input_value and isinstance(input_value, str) and hasattr(vector_store, "search"):
|
||||
docs = vector_store.search(query=input_value, search_type=search_type.lower(), k=k, **kwargs)
|
||||
else:
|
||||
raise ValueError("Invalid inputs provided.")
|
||||
data = docs_to_data(docs)
|
||||
self.status = data
|
||||
return data
|
||||
|
||||
def build_vector_store(self) -> VectorStore:
|
||||
"""
|
||||
Builds the Vector Store object.
|
||||
"""
|
||||
raise NotImplementedError("build_vector_store method must be implemented.")
|
||||
|
||||
def build_base_retriever(self) -> BaseRetriever:
|
||||
"""
|
||||
Builds the BaseRetriever object.
|
||||
"""
|
||||
vector_store = self.build_vector_store()
|
||||
if hasattr(vector_store, "as_retriever"):
|
||||
return vector_store.as_retriever()
|
||||
else:
|
||||
raise ValueError(f"Vector Store {vector_store.__class__.__name__} does not have an as_retriever method.")
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
from typing import List, Optional
|
||||
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.components.vectorstores.AstraDB import AstraVectorStoreComponent
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.field_typing import Embeddings, Text
|
||||
from langflow.schema import Data
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from typing import Any, List, Optional, Tuple
|
|||
|
||||
from langchain_community.utilities.cassandra import SetupMode
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.components.vectorstores.Cassandra import CassandraVectorStoreComponent
|
||||
from langflow.field_typing import Embeddings, Text
|
||||
from langflow.schema import Data
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ from typing import List, Optional
|
|||
import chromadb
|
||||
from chromadb.config import Settings
|
||||
from langchain_chroma import Chroma
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
|
||||
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.field_typing import Embeddings, Text
|
||||
from langflow.schema import Data
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from typing import List
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.components.vectorstores.Couchbase import CouchbaseVectorStoreComponent
|
||||
from langflow.field_typing import Embeddings, Text
|
||||
from langflow.schema import Data
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from typing import List
|
|||
|
||||
from langchain_community.vectorstores.faiss import FAISS
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.field_typing import Embeddings, Text
|
||||
from langflow.schema import Data
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from typing import List, Optional
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.components.vectorstores.MongoDBAtlasVector import MongoVectorStoreComponent
|
||||
from langflow.field_typing import Embeddings, NestedDict, Text
|
||||
from langflow.schema import Data
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from typing import List, Optional
|
|||
|
||||
from langchain_pinecone._utilities import DistanceStrategy
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.components.vectorstores.Pinecone import PineconeVectorStoreComponent
|
||||
from langflow.field_typing import Embeddings, Text
|
||||
from langflow.field_typing.constants import NestedDict
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from typing import List, Optional
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.components.vectorstores.Qdrant import QdrantVectorStoreComponent
|
||||
from langflow.field_typing import Embeddings, NestedDict, Text
|
||||
from langflow.schema import Data
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from typing import List, Optional
|
|||
|
||||
from langchain_core.embeddings import Embeddings
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.components.vectorstores.Redis import RedisVectorStoreComponent
|
||||
from langflow.field_typing import Text
|
||||
from langflow.schema import Data
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from typing import List
|
|||
from langchain_community.vectorstores.supabase import SupabaseVectorStore
|
||||
from supabase.client import Client, create_client
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.field_typing import Embeddings, Text
|
||||
from langflow.schema import Data
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from typing import List, Optional
|
|||
|
||||
from langchain_core.embeddings import Embeddings
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.components.vectorstores.Upstash import UpstashVectorStoreComponent
|
||||
from langflow.field_typing import Text
|
||||
from langflow.schema import Data
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from typing import List
|
|||
|
||||
from langchain_community.vectorstores.vectara import Vectara
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.components.vectorstores.Vectara import VectaraVectorStoreComponent
|
||||
from langflow.field_typing import Text
|
||||
from langflow.schema import Data
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from typing import List, Optional
|
|||
|
||||
from langchain_core.embeddings import Embeddings
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.components.vectorstores.Weaviate import WeaviateVectorStoreComponent
|
||||
from langflow.field_typing import Text
|
||||
from langflow.schema import Data
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from typing import List
|
|||
|
||||
from langchain_core.embeddings import Embeddings
|
||||
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.components.vectorstores.pgvector import PGVectorStoreComponent
|
||||
from langflow.field_typing import Text
|
||||
from langflow.schema import Data
|
||||
|
|
|
|||
|
|
@ -5,9 +5,10 @@ from chromadb.config import Settings
|
|||
from langchain_chroma.vectorstores import Chroma
|
||||
from loguru import logger
|
||||
|
||||
from langflow.base.vectorstores.model import LCVectorStoreComponent
|
||||
from langflow.base.vectorstores.utils import chroma_collection_to_data
|
||||
from langflow.components.vectorstores.base.model import LCVectorStoreComponent
|
||||
from langflow.inputs import BoolInput, DropdownInput, HandleInput, IntInput, StrInput, MessageInput, DataInput
|
||||
from langflow.inputs import BoolInput, DataInput, DropdownInput, HandleInput, IntInput, MessageInput, StrInput
|
||||
IntInput, MessageInput, StrInput)
|
||||
from langflow.schema import Data
|
||||
|
||||
if TYPE_CHECKING:
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
from .model import LCVectorStoreComponent
|
||||
|
||||
__all__ = ["LCVectorStoreComponent"]
|
||||
Loading…
Reference in a new issue