first commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
"""置信度审计日志:将每次 query 的置信度指标追加写入当月 CSV 文件。"""
|
||||
|
||||
import csv
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
from app.config import settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CSV_FIELDS = [
|
||||
"timestamp", "question", "confidence",
|
||||
"qv_help_id", "qv_score", "threshold_margin",
|
||||
"retrieval_pass", "retrieval_confidence",
|
||||
"self_eval_score",
|
||||
"faq_score",
|
||||
"answer",
|
||||
]
|
||||
|
||||
|
||||
def log_confidence_record(record: dict) -> None:
|
||||
"""将一条置信度记录追加写入当月 CSV 文件。
|
||||
|
||||
文件路径: {confidence_log_dir}/confidence_log_{YYYY-MM}.csv
|
||||
失败时仅记录 warning 日志,不抛异常。
|
||||
"""
|
||||
try:
|
||||
log_dir = settings.confidence_log_dir
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
|
||||
month_str = datetime.now().strftime("%Y-%m")
|
||||
file_path = os.path.join(log_dir, f"confidence_log_{month_str}.csv")
|
||||
|
||||
write_header = not os.path.exists(file_path)
|
||||
|
||||
with open(file_path, "a", newline="", encoding="utf-8") as f:
|
||||
writer = csv.DictWriter(f, fieldnames=CSV_FIELDS)
|
||||
if write_header:
|
||||
writer.writeheader()
|
||||
|
||||
row = {field: record.get(field, "") for field in CSV_FIELDS}
|
||||
row["timestamp"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
writer.writerow(row)
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to write confidence log: {e}")
|
||||
Reference in New Issue
Block a user