fix: 修复RAG工具关键方法缺少返回值导致的失败问题

- add_document()方法添加return True,修复添加文档失败的bug
- reset_database()方法添加return True,修复重置失败的bug
- reset_database()重新创建集合时添加embedding_function参数
- 修复get_or_create_collection导致的embedding函数冲突问题
- 优化集合初始化逻辑,先尝试获取已存在集合再创建新集合

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
sladro 2025-10-09 16:29:27 +08:00
parent 9e4f2379fa
commit 2702e5020c

View File

@ -43,11 +43,18 @@ class RAGTool:
self.embedding_function = self._get_embedding_function()
# 获取或创建集合
self.collection = self.client.get_or_create_collection(
name=self.settings.collection_name,
embedding_function=self.embedding_function,
metadata={"description": "BidMaster知识库"}
)
try:
# 尝试获取已存在的集合
self.collection = self.client.get_collection(
name=self.settings.collection_name
)
except Exception:
# 集合不存在,创建新集合
self.collection = self.client.create_collection(
name=self.settings.collection_name,
embedding_function=self.embedding_function,
metadata={"description": "BidMaster知识库"}
)
# 初始化文本分割器
self.text_splitter = RecursiveCharacterTextSplitter(
@ -74,6 +81,8 @@ class RAGTool:
# 添加到向量数据库
self._add_chunks_to_db(chunks, file_path)
return True
def search(self, query: str, k: int = 5) -> list[dict[str, Any]]:
"""搜索相关内容"""
results = self.collection.query(
@ -121,9 +130,12 @@ class RAGTool:
# 重新创建集合
self.collection = self.client.get_or_create_collection(
name=self.settings.collection_name,
embedding_function=self.embedding_function,
metadata={"description": "BidMaster知识库"}
)
return True
def _load_document(self, file_path: Path) -> list[Document]:
"""根据文件类型加载文档"""
suffix = file_path.suffix.lower()