From 080c80f49374ac0dae0df215681a4836feae9fed Mon Sep 17 00:00:00 2001 From: lonpatovaadelina Date: Thu, 4 Jun 2026 11:53:01 +0000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0:=201.=20=D0=97=D0=B0=D0=B2=D0=B5=D1=80=D1=88=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20vectorstore.py:=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20load=5Fdocuments=20(?= =?UTF-8?q?=D1=87=D0=B0=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- init_db.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/init_db.py b/init_db.py index 1241777..2cc23ee 100644 --- a/init_db.py +++ b/init_db.py @@ -6,13 +6,27 @@ def main(): load_dotenv() # загружаем TAVILY_API_KEY и другие переменные persist_dir = os.getenv("CHROMA_PERSIST_DIR", "./chroma_db") vectorstore = create_vectorstore(persist_directory=persist_dir) - docs_dir = os.path.join(os.path.dirname(__file__), "documents") - if not os.path.isdir(docs_dir): - print(f"Каталог {docs_dir} не найден.") - return - print(f"Загрузка документов из {docs_dir} в ChromaDB ({persist_dir})...") - load_documents(docs_dir, vectorstore) - print("Загрузка завершена.") + + # Проверяем, пуста ли база данных + try: + if hasattr(vectorstore, "_collection"): + doc_count = vectorstore._collection.count() + else: + # запасной способ: пробуем выполнить пустой поиск + doc_count = len(vectorstore.similarity_search("test", k=1)) + except Exception: + doc_count = 0 + + if doc_count == 0: + docs_dir = os.path.join(os.path.dirname(__file__), "documents") + if not os.path.isdir(docs_dir): + print(f"Каталог {docs_dir} не найден.") + return + print(f"Загрузка документов из {docs_dir} в ChromaDB ({persist_dir})...") + load_documents(docs_dir, vectorstore) + print("Загрузка завершена.") + else: + print(f"База данных в {persist_dir} уже содержит {doc_count} документов. Загрузка пропущена.") if __name__ == "__main__": main() \ No newline at end of file