chore: Adjust Faiss Vector Store parameters format

This commit is contained in:
joaoguilhermeS 2024-06-22 12:05:56 -03:00 committed by Gabriel Luiz Freitas Almeida
commit e6e0649264

View file

@ -21,26 +21,24 @@ class FaissVectorStoreComponent(LCVectorStoreComponent):
icon = "FAISS"
inputs = [
StrInput(
name="folder_path",
display_name="Folder Path",
info="Path to save the FAISS index. It will be relative to where Langflow is running.",
),
StrInput(
name="index_name",
display_name="Index Name",
value="langflow_index",
),
HandleInput(name="embedding", display_name="Embedding", input_types=["Embeddings"]),
DataInput(
name="vector_store_inputs",
display_name="Vector Store Inputs",
is_list=True,
StrInput(
name="persist_directory",
display_name="Persist Directory",
info="Path to save the FAISS index. It will be relative to where Langflow is running.",
),
BoolInput(
name="add_to_vector_store",
display_name="Add to Vector Store",
info="If true, the Vector Store Inputs will be added to the Vector Store.",
MultilineInput(
name="search_query",
display_name="Search Query",
),
DataInput(
name="ingest_data",
display_name="Ingest Data",
is_list=True,
),
BoolInput(
name="allow_dangerous_deserialization",
@ -49,10 +47,7 @@ class FaissVectorStoreComponent(LCVectorStoreComponent):
advanced=True,
value=True,
),
MultilineInput(
name="search_input",
display_name="Search Input",
),
HandleInput(name="embedding", display_name="Embedding", input_types=["Embeddings"]),
IntInput(
name="number_of_results",
display_name="Number of Results",
@ -66,27 +61,20 @@ class FaissVectorStoreComponent(LCVectorStoreComponent):
"""
Builds the FAISS object.
"""
if not self.folder_path:
if not self.persist_directory:
raise ValueError("Folder path is required to save the FAISS index.")
path = self.resolve_path(self.folder_path)
path = self.resolve_path(self.persist_directory)
if self.add_to_vector_store:
documents = []
for _input in self.vector_store_inputs or []:
if isinstance(_input, Data):
documents.append(_input.to_lc_document())
else:
documents.append(_input)
documents = []
faiss = FAISS.from_documents(documents=documents, embedding=self.embedding)
faiss.save_local(Text(path), self.index_name)
else:
faiss = FAISS.load_local(
folder_path=Text(path),
embeddings=self.embedding,
index_name=self.index_name,
allow_dangerous_deserialization=self.allow_dangerous_deserialization,
)
for _input in self.ingest_data or []:
if isinstance(_input, Data):
documents.append(_input.to_lc_document())
else:
documents.append(_input)
faiss = FAISS.from_documents(documents=documents, embedding=self.embedding)
faiss.save_local(Text(path), self.index_name)
return faiss
@ -94,12 +82,12 @@ class FaissVectorStoreComponent(LCVectorStoreComponent):
"""
Search for documents in the FAISS vector store.
"""
if not self.folder_path:
if not self.persist_directory:
raise ValueError("Folder path is required to load the FAISS index.")
path = self.resolve_path(self.folder_path)
path = self.resolve_path(self.persist_directory)
vector_store = FAISS.load_local(
folder_path=Text(path),
persist_directory=Text(path),
embeddings=self.embedding,
index_name=self.index_name,
allow_dangerous_deserialization=self.allow_dangerous_deserialization,
@ -108,12 +96,12 @@ class FaissVectorStoreComponent(LCVectorStoreComponent):
if not vector_store:
raise ValueError("Failed to load the FAISS index.")
logger.debug(f"Search input: {self.search_input}")
logger.debug(f"Search input: {self.search_query}")
logger.debug(f"Number of results: {self.number_of_results}")
if self.search_input and isinstance(self.search_input, str) and self.search_input.strip():
if self.search_query and isinstance(self.search_query, str) and self.search_query.strip():
docs = vector_store.similarity_search(
query=self.search_input,
query=self.search_query,
k=self.number_of_results,
)