2
This commit is contained in:
@@ -27,11 +27,8 @@ def get_chapter_info_from_directory(novel_directory_content: str, chapter_number
|
|||||||
if match:
|
if match:
|
||||||
chap_num = int(match.group(1))
|
chap_num = int(match.group(1))
|
||||||
if chap_num == chapter_number:
|
if chap_num == chapter_number:
|
||||||
# group(2) 可能是标题及简述的混合
|
|
||||||
full_title = match.group(2).strip()
|
full_title = match.group(2).strip()
|
||||||
# 这里假设用 '-' 进一步区分“标题 - 简述”,也可能用户没写“ - ”
|
|
||||||
if ' - ' in full_title:
|
if ' - ' in full_title:
|
||||||
# 根据你的目录格式自由处理
|
|
||||||
parts = full_title.split(' - ', 1)
|
parts = full_title.split(' - ', 1)
|
||||||
return {
|
return {
|
||||||
"chapter_title": parts[0].strip(),
|
"chapter_title": parts[0].strip(),
|
||||||
|
|||||||
+1
-22
@@ -123,10 +123,8 @@ def create_embeddings_object(
|
|||||||
# ============ 向量库相关操作 ============
|
# ============ 向量库相关操作 ============
|
||||||
def clear_vector_store(filepath: str) -> bool:
|
def clear_vector_store(filepath: str) -> bool:
|
||||||
"""
|
"""
|
||||||
通过 Chroma API 移除集合数据后尝试删除整个 vectorstore 目录。
|
|
||||||
返回值表示是否成功清空向量库。
|
返回值表示是否成功清空向量库。
|
||||||
"""
|
"""
|
||||||
from chromadb import Client
|
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
store_dir = get_vectorstore_dir(filepath)
|
store_dir = get_vectorstore_dir(filepath)
|
||||||
@@ -135,19 +133,6 @@ def clear_vector_store(filepath: str) -> bool:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
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):
|
if os.path.exists(store_dir):
|
||||||
shutil.rmtree(store_dir)
|
shutil.rmtree(store_dir)
|
||||||
logging.info(f"Vector store directory '{store_dir}' removed.")
|
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)}")
|
logging.error(f"程序正在运行,无法删除,请在程序关闭后手动前往 {store_dir} 删除目录。\n {str(e)}")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return False
|
return False
|
||||||
finally:
|
|
||||||
if 'client' in locals():
|
|
||||||
try:
|
|
||||||
del client
|
|
||||||
except AttributeError:
|
|
||||||
logging.warning("Client object not found to delete.")
|
|
||||||
|
|
||||||
|
|
||||||
def init_vector_store(
|
def init_vector_store(
|
||||||
api_key: str,
|
api_key: str,
|
||||||
@@ -245,6 +223,7 @@ def split_text_for_vectorstore(chapter_text: str,
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
nltk.download('punkt', quiet=True)
|
nltk.download('punkt', quiet=True)
|
||||||
|
nltk.download('punkt_tab', quiet=True)
|
||||||
sentences = nltk.sent_tokenize(chapter_text)
|
sentences = nltk.sent_tokenize(chapter_text)
|
||||||
if not sentences:
|
if not sentences:
|
||||||
return []
|
return []
|
||||||
|
|||||||
Reference in New Issue
Block a user