From 3847149ef95e67b2ef0ce50f9200e893e62dcdb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D0=B8=D0=BB=20=D0=92=D0=B8=D0=BA?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BE=D0=B2?= Date: Thu, 2 Jul 2026 12:49:02 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20load=5Fdocs.py=20=E2=80=94=20=D0=90?= =?UTF-8?q?=D0=B3=D0=B5=D0=BD=D1=82=20=D1=81=20RAG-=D0=BF=D0=B0=D0=BC?= =?UTF-8?q?=D1=8F=D1=82=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- load_docs.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/load_docs.py b/load_docs.py index d690a62..2d631bf 100644 --- a/load_docs.py +++ b/load_docs.py @@ -1,19 +1,21 @@ import sys from pathlib import Path +from rag_agent import add_to_knowledge_base -from utils import splitter, vector_store -from langchain_core.documents import Document - -def load_documents(dir_path: str): - for file_path in Path(dir_path).rglob("*.txt"): - content = file_path.read_text(encoding="utf-8") - chunks = splitter.split_text(content) - docs = [Document(page_content=chunk, metadata={"title": file_path.name}) for chunk in chunks] - vector_store.add_documents(docs) - print(f"Added {len(docs)} chunks from {file_path}") +def load_docs_from_dir(directory: str): + for file_path in Path(directory).rglob("*"): + if file_path.is_file() and file_path.suffix.lower() in {".txt", ".md", ".pdf"}: + try: + content = file_path.read_text(encoding="utf-8", errors="ignore") + except Exception as e: + print(f"Failed to read {file_path}: {e}") + continue + title = file_path.stem + result = add_to_knowledge_base(content, title) + print(f"Loaded {file_path}: {result}") if __name__ == "__main__": - if len(sys.argv) != 2: + if len(sys.argv) < 2: print("Usage: python load_docs.py ") sys.exit(1) - load_documents(sys.argv[1]) \ No newline at end of file + load_docs_from_dir(sys.argv[1]) \ No newline at end of file