This commit is contained in:
YILING0013
2025-02-04 16:37:29 +08:00
parent 251d40f8a3
commit b60ac8c7e0
2 changed files with 1 additions and 25 deletions
-3
View File
@@ -27,11 +27,8 @@ def get_chapter_info_from_directory(novel_directory_content: str, chapter_number
if match:
chap_num = int(match.group(1))
if chap_num == chapter_number:
# group(2) 可能是标题及简述的混合
full_title = match.group(2).strip()
# 这里假设用 '-' 进一步区分“标题 - 简述”,也可能用户没写“ - ”
if ' - ' in full_title:
# 根据你的目录格式自由处理
parts = full_title.split(' - ', 1)
return {
"chapter_title": parts[0].strip(),
+1 -22
View File
@@ -123,10 +123,8 @@ def create_embeddings_object(
# ============ 向量库相关操作 ============
def clear_vector_store(filepath: str) -> bool:
"""
通过 Chroma API 移除集合数据后尝试删除整个 vectorstore 目录。
返回值表示是否成功清空向量库。
"""
from chromadb import Client
import shutil
store_dir = get_vectorstore_dir(filepath)
@@ -135,19 +133,6 @@ def clear_vector_store(filepath: str) -> bool:
return False
try:
client = Client(settings=Settings(
persist_directory=store_dir,
allow_reset=True # 允许重置操作
))
collections = client.list_collections()
if collections:
client.delete_collection(name="novel_collection")
logging.info("Collection 'novel_collection' deleted via API.")
client.reset()
logging.info("Client reset successfully.")
# 直接删除整个 vectorstore 目录
if os.path.exists(store_dir):
shutil.rmtree(store_dir)
logging.info(f"Vector store directory '{store_dir}' removed.")
@@ -156,13 +141,6 @@ def clear_vector_store(filepath: str) -> bool:
logging.error(f"程序正在运行,无法删除,请在程序关闭后手动前往 {store_dir} 删除目录。\n {str(e)}")
traceback.print_exc()
return False
finally:
if 'client' in locals():
try:
del client
except AttributeError:
logging.warning("Client object not found to delete.")
def init_vector_store(
api_key: str,
@@ -245,6 +223,7 @@ def split_text_for_vectorstore(chapter_text: str,
return []
nltk.download('punkt', quiet=True)
nltk.download('punkt_tab', quiet=True)
sentences = nltk.sent_tokenize(chapter_text)
if not sentences:
return []