fix: 1)微信QQ统一优先读消息列表(当前对话) 2)知识库min_score=0.30+短文本过滤

This commit is contained in:
youpaishidifu
2026-07-24 22:39:09 +08:00
parent e0ea7ec80f
commit c8c2f2ccc6
2 changed files with 10 additions and 15 deletions
+8 -3
View File
@@ -68,11 +68,16 @@ class KnowledgeBase:
def is_ready(self) -> bool:
return self._ready and len(self.docs) > 0
def search(self, query: str, top_k: int = 3) -> List[Tuple[dict, float]]:
"""搜索知识库,返回 (doc, score) 列表"""
def search(self, query: str, top_k: int = 3, min_score: float = 0.30) -> List[Tuple[dict, float]]:
"""搜索知识库,返回 (doc, score) 列表。"""
if not self.is_ready or not query.strip():
return []
# 短文本(< 4 个字或 <= 2 个有效词)不做搜索,避免噪音
tokens = [t for t in jieba.cut(query.strip()) if len(t) >= 2]
if len(query.strip()) < 4 or len(tokens) <= 1:
return []
query_vec = self._vectorizer.transform([query])
scores = cosine_similarity(query_vec, self._tfidf_matrix)[0]
top_indices = np.argsort(scores)[::-1][:top_k]
@@ -80,7 +85,7 @@ class KnowledgeBase:
results = []
for idx in top_indices:
score = float(scores[idx])
if score < 0.05: # 太低的相似度跳过
if score < min_score:
continue
results.append((self.docs[idx], score))
return results