Skip to content

Faiss integration #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ docs/source/_static/
venv/
.venv/
.vscode/
.conda/

# exclude pdf, mp3
*.pdf
Expand All @@ -38,3 +39,6 @@ lib/
*.html
.idea

# extras
cache/
run_smart_scraper.py
15 changes: 14 additions & 1 deletion scrapegraphai/nodes/rag_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from typing import List, Optional
import os

from langchain.docstore.document import Document
from langchain.retrievers import ContextualCompressionRetriever
Expand Down Expand Up @@ -98,7 +99,19 @@ def execute(self, state: dict) -> dict:
)
embeddings = self.embedder_model

retriever = FAISS.from_documents(chunked_docs, embeddings).as_retriever()
folder_name = "cache"
Copy link
Collaborator

@DiTo97 DiTo97 Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's the only thing I'd change.

as it stands, each RAG node with the cache config would overwrite the very same folder, effectively nullifying the cache, unless a RAG node on the very same input state is invoked subsequently.

I would allow for more flexibility by either 1): allowing the user to specify the cache folder in the config for that specific RAG node or 2) automatically compute a unique identifier for that specific input state (e.g., the filename, the endpoint or a some hashing function on the documents) so that the user may choose what to keep and what not to keep in the cache


if self.node_config.get("cache", False) and not os.path.exists(folder_name):
index = FAISS.from_documents(chunked_docs, embeddings)
os.makedirs(folder_name)

index.save_local(folder_name)
if self.node_config.get("cache", False) and os.path.exists(folder_name):
index = FAISS.load_local(folder_path=folder_name, embeddings=embeddings)
else:
index = FAISS.from_documents(chunked_docs, embeddings)

retriever = index.as_retriever()

redundant_filter = EmbeddingsRedundantFilter(embeddings=embeddings)
# similarity_threshold could be set, now k=20
Expand Down