fix: Improve path handling and type annotations in FaissVectorStoreComponent (#6081)
* 📝 (faiss.py): import Path and List modules for better type hinting and file path handling 🐛 (faiss.py): fix issue with building vector store when persist_directory is not provided 🐛 (faiss.py): fix issue with loading FAISS index when index file does not exist 📝 (faiss.py): add type hints for search_documents method parameters and return value 📝 (faiss.py): remove unnecessary logging statements from search_documents method * [autofix.ci] apply automated fixes * 📝 (faiss.py): add 'required' flag to the 'Persist Directory' input field to ensure it is mandatory for the user to provide a value * 🔧 (faiss.py): refactor build_vector_store method to handle persist_directory more efficiently 🔧 (faiss.py): refactor search_documents method to handle persist_directory more efficiently * [autofix.ci] apply automated fixes * 🔧 (faiss.py): refactor get_persist_directory method to return resolved persist directory path or current directory if not set ♻️ (faiss.py): refactor build_vector_store and search_documents methods to use get_persist_directory method for path resolution * ♻️ (faiss.py): refactor resolve_path method to be static and return a string instead of Path object for consistency and clarity --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
d98d37778d
commit
fda2f17a9f
1 changed files with 33 additions and 27 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from langchain_community.vectorstores import FAISS
|
from langchain_community.vectorstores import FAISS
|
||||||
|
|
||||||
from langflow.base.vectorstores.model import LCVectorStoreComponent, check_cached_vector_store
|
from langflow.base.vectorstores.model import LCVectorStoreComponent, check_cached_vector_store
|
||||||
|
|
@ -44,16 +46,30 @@ class FaissVectorStoreComponent(LCVectorStoreComponent):
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def resolve_path(path: str) -> str:
|
||||||
|
"""Resolve the path relative to the Langflow root.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
path: The path to resolve
|
||||||
|
Returns:
|
||||||
|
str: The resolved path as a string
|
||||||
|
"""
|
||||||
|
return str(Path(path).resolve())
|
||||||
|
|
||||||
|
def get_persist_directory(self) -> Path:
|
||||||
|
"""Returns the resolved persist directory path or the current directory if not set."""
|
||||||
|
if self.persist_directory:
|
||||||
|
return Path(self.resolve_path(self.persist_directory))
|
||||||
|
return Path()
|
||||||
|
|
||||||
@check_cached_vector_store
|
@check_cached_vector_store
|
||||||
def build_vector_store(self) -> FAISS:
|
def build_vector_store(self) -> FAISS:
|
||||||
"""Builds the FAISS object."""
|
"""Builds the FAISS object."""
|
||||||
if not self.persist_directory:
|
path = self.get_persist_directory()
|
||||||
msg = "Folder path is required to save the FAISS index."
|
path.mkdir(parents=True, exist_ok=True)
|
||||||
raise ValueError(msg)
|
|
||||||
path = self.resolve_path(self.persist_directory)
|
|
||||||
|
|
||||||
documents = []
|
documents = []
|
||||||
|
|
||||||
for _input in self.ingest_data or []:
|
for _input in self.ingest_data or []:
|
||||||
if isinstance(_input, Data):
|
if isinstance(_input, Data):
|
||||||
documents.append(_input.to_lc_document())
|
documents.append(_input.to_lc_document())
|
||||||
|
|
@ -62,41 +78,31 @@ class FaissVectorStoreComponent(LCVectorStoreComponent):
|
||||||
|
|
||||||
faiss = FAISS.from_documents(documents=documents, embedding=self.embedding)
|
faiss = FAISS.from_documents(documents=documents, embedding=self.embedding)
|
||||||
faiss.save_local(str(path), self.index_name)
|
faiss.save_local(str(path), self.index_name)
|
||||||
|
|
||||||
return faiss
|
return faiss
|
||||||
|
|
||||||
def search_documents(self) -> list[Data]:
|
def search_documents(self) -> list[Data]:
|
||||||
"""Search for documents in the FAISS vector store."""
|
"""Search for documents in the FAISS vector store."""
|
||||||
if not self.persist_directory:
|
path = self.get_persist_directory()
|
||||||
msg = "Folder path is required to load the FAISS index."
|
index_path = path / f"{self.index_name}.faiss"
|
||||||
raise ValueError(msg)
|
|
||||||
path = self.resolve_path(self.persist_directory)
|
|
||||||
|
|
||||||
vector_store = FAISS.load_local(
|
if not index_path.exists():
|
||||||
folder_path=path,
|
vector_store = self.build_vector_store()
|
||||||
embeddings=self.embedding,
|
else:
|
||||||
index_name=self.index_name,
|
vector_store = FAISS.load_local(
|
||||||
allow_dangerous_deserialization=self.allow_dangerous_deserialization,
|
folder_path=str(path),
|
||||||
)
|
embeddings=self.embedding,
|
||||||
|
index_name=self.index_name,
|
||||||
|
allow_dangerous_deserialization=self.allow_dangerous_deserialization,
|
||||||
|
)
|
||||||
|
|
||||||
if not vector_store:
|
if not vector_store:
|
||||||
msg = "Failed to load the FAISS index."
|
msg = "Failed to load the FAISS index."
|
||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
|
|
||||||
self.log(f"Search input: {self.search_query}")
|
|
||||||
self.log(f"Number of results: {self.number_of_results}")
|
|
||||||
|
|
||||||
if self.search_query and isinstance(self.search_query, str) and self.search_query.strip():
|
if self.search_query and isinstance(self.search_query, str) and self.search_query.strip():
|
||||||
docs = vector_store.similarity_search(
|
docs = vector_store.similarity_search(
|
||||||
query=self.search_query,
|
query=self.search_query,
|
||||||
k=self.number_of_results,
|
k=self.number_of_results,
|
||||||
)
|
)
|
||||||
|
return docs_to_data(docs)
|
||||||
self.log(f"Retrieved documents: {len(docs)}")
|
|
||||||
|
|
||||||
data = docs_to_data(docs)
|
|
||||||
self.log(f"Converted documents to data: {len(data)}")
|
|
||||||
self.log(data)
|
|
||||||
return data # Return the search results data
|
|
||||||
self.log("No search input provided. Skipping search.")
|
|
||||||
return []
|
return []
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue