refactor: replace print() with structured logging across backend

Replace all print()/stderr logging with Python logging module using
logger = logging.getLogger(__name__) pattern for consistent log levels
and formatting. Extract score conversion to shared score_utils module.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-28 15:52:44 +08:00
parent 4bb50ae9c1
commit 37c0364e7e
19 changed files with 257 additions and 216 deletions
+7 -4
View File
@@ -2,6 +2,7 @@
LangChain 1.0 向量存储封装
"""
import os
import logging
import json
import hashlib
from typing import List, Dict, Any, Optional
@@ -12,6 +13,8 @@ from langchain_core.documents import Document as LangChainDocument
from ..core.config import get_settings
from .embeddings import get_embedding_model
logger = logging.getLogger(__name__)
settings = get_settings()
@@ -40,7 +43,7 @@ class VectorStore:
self.vectorstore.add_documents(documents)
return True
except Exception as e:
print(f"添加文档失败: {str(e)}")
logger.error(f"添加文档失败: {str(e)}")
return False
def as_retriever(self, **kwargs):
@@ -54,7 +57,7 @@ class VectorStore:
filter: Optional[Dict] = None
):
"""相似度搜索(带分数)"""
print(f"[DEBUG-VectorStore] 查询参数 - k: {k}, filter: {filter}")
logger.debug(f"查询参数 - k: {k}, filter: {filter}")
result = self.vectorstore.similarity_search_with_score(
query=query,
@@ -62,7 +65,7 @@ class VectorStore:
filter=filter
)
print(f"[DEBUG-VectorStore] 返回结果数量: {len(result)}")
logger.debug(f"返回结果数量: {len(result)}")
return result
def delete_by_document_id(self, document_id: int) -> bool:
@@ -73,7 +76,7 @@ class VectorStore:
)
return True
except Exception as e:
print(f"删除向量数据失败: {str(e)}")
logger.error(f"删除向量数据失败: {str(e)}")
return False
def max_marginal_relevance_search(