情感陪伴 AI 怎么收反馈:先脱敏,再区分情绪与产品问题
情感陪伴产品里的反馈可能包含很私密的生活信息。收集之前先做最小化与脱敏,归因时再区分语气、知识、流程和安全问题;示例代码不能替代隐私评审。
隐私防线与数据闭环治理架构
在情感陪伴产品中,用户反馈不能简单地当作“日志文件”直接存进数据库。我们需要在客户端与大模型训练/评估库之间建立严格的隐私隔离层。
flowchart TD
UserApp[用户交互客户端] — 对话 & 显式点踩(Thumb-down) –> EdgeSanitizer[边缘敏感数据脱敏层]
subgraph 隐私隔离与脱敏管道
EdgeSanitizer — PII 正则/NER 替换 –> SanitizedData[脱敏文本 (除姓名/电话/地址)]
SanitizedData –> DataVault[(安全数据加密仓)]
end
DataVault –> AttributionEngine{异动归因分析引擎}
AttributionEngine — 归因: 语气冷硬/说教 –> PromptOptimizer[Prompt 提示词模板微调]
AttributionEngine — 归因: 知识幻觉/错乱 –> VectorIndexFix[Vector DB 知识库更新]
AttributionEngine — 归因: 安全越界/过度共情 –> PolicyGuard[安全合规 Guardrail 过滤]
PromptOptimizer –> RegressionTest[自动化回归测试集]
RegressionTest — 通过校验 –> ProductionDeployment[发布新版本 Agent 策略]
1. 最小化数据收集与边缘脱敏(Edge PII Sanitization)
在用户的点踩(Thumb-down)或反馈被上报至服务器之前,必须在客户端或边缘节点(Edge Middleware)清洗掉个人可识别信息(PII,如身份证、真实姓名、地理位置、联系方式)。
2. 异动归因(Feedback Attribution)
用户给出一个“不满意”评价,原因可能是多方面的:大模型产生了说教口吻、回答耗时过长、回复暴露了机械占位符,或者触碰了伦理安全边界。归因引擎需要通过语义聚类与逻辑打标,把散乱的反馈转化为具体的工程改进项。
3. 提示词与模型闭环迭代(Closed-Loop Iteration)
将脱敏后的典型负反馈样本加入自动化回归测试集(Evaluation Dataset),在更新系统提示词(System Prompt)或微调模型后自动跑分,确保修复一个问题的同时不会破坏已有的治愈体验。
用户反馈脱敏与归因服务示例
下面使用 Python 实现一套生产级用户反馈处理与归因管道。代码具备 PII 敏感信息脱敏、语义漂移分析、分类归因打标以及安全降级报警功能。
import json
import logging
import re
from dataclasses import asdict, dataclass
from enum import Enum
from typing import Any, Dict, List, Optional
logging.basicConfig(level=logging.INFO, format="%(asctime)s – [%(levelname)s] – %(message)s")
logger = logging.getLogger("PrivacyFeedbackPipeline")
class AttributionCategory(Enum):
PREACHY_TONE = "PREACHY_TONE" # 说教/冷硬语气
HALLUCINATION = "HALLUCINATION" # 幻觉/记忆错乱
PII_RISK = "PII_RISK" # 隐私越界风险
LATENCY_TOO_HIGH = "LATENCY_TOO_HIGH" # 耗时过长
UNCERTAIN = "UNCERTAIN" # 未知原因
@dataclass
class UserFeedbackPayload:
feedback_id: str
session_id: str
user_query: str
ai_response: str
rating: int # -1 代表点踩,1 代表点赞
user_comment: Optional[str] = None
latency_ms: float = 0.0
@dataclass
class ProcessedFeedbackReport:
feedback_id: str
sanitized_query: str
sanitized_response: str
primary_attribution: str
suggested_action: str
requires_manual_audit: bool
class PIISanitizer:
"""敏感隐私数据脱敏器"""
# 正则表达式匹配手机号、身份证、Email
PHONE_REGEX = re.compile(r"1[3-9]\\d{9}")
ID_CARD_REGEX = re.compile(r"[1-9]\\d{5}(18|19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[\\dXx]")
EMAIL_REGEX = re.compile(r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+")
@classmethod
def sanitize(cls, text: str) -> str:
if not text:
return ""
sanitized = cls.PHONE_REGEX.sub("[USER_PHONE]", text)
sanitized = cls.ID_CARD_REGEX.sub("[USER_ID_CARD]", sanitized)
sanitized = cls.EMAIL_REGEX.sub("[USER_EMAIL]", sanitized)
return sanitized
class FeedbackAttributionEngine:
def __init__(self):
# 预设说教/冷硬词库
self.preachy_keywords = ["你应该", "你必须", "我劝你", "错误的做法", "首先其次"]
def analyze_attribution(self, payload: UserFeedbackPayload) -> ProcessedFeedbackReport:
# 1. 执行隐私脱敏
clean_query = PIISanitizer.sanitize(payload.user_query)
clean_response = PIISanitizer.sanitize(payload.ai_response)
attribution = AttributionCategory.UNCERTAIN
action = "归档观察"
manual_audit = False
# 2. 耗时归因
if payload.latency_ms > 3000.0:
attribution = AttributionCategory.LATENCY_TOO_HIGH
action = "排查上游 API 吞吐或优化 RAG 向量检索耗时"
# 3. 语气说教归因
elif any(word in clean_response for word in self.preachy_keywords):
attribution = AttributionCategory.PREACHY_TONE
action = "重构 Prompt 调整同理心与治愈系语气约束"
# 4. 用户显式批注分析
if payload.user_comment:
clean_comment = PIISanitizer.sanitize(payload.user_comment)
if "胡说" in clean_comment or "记错" in clean_comment:
attribution = AttributionCategory.HALLUCINATION
action = "更新 Vector DB 检索 Top-K 参数与重排(Rerank)逻辑"
manual_audit = True
return ProcessedFeedbackReport(
feedback_id=payload.feedback_id,
sanitized_query=clean_query,
sanitized_response=clean_response,
primary_attribution=attribution.value,
suggested_action=action,
requires_manual_audit=manual_audit,
)
class ClosedLoopPipelineService:
def __init__(self, attribution_engine: FeedbackAttributionEngine):
self.engine = attribution_engine
self.audit_queue: List[ProcessedFeedbackReport] = []
def process_incoming_feedback(self, payload: UserFeedbackPayload) -> None:
logger.info(f"收到用户负反馈处理请求 ID: {payload.feedback_id}")
# 仅针对负反馈进行深入归因
if payload.rating < 0:
report = self.engine.analyze_attribution(payload)
logger.warning(
f"⚠️ [负反馈归因完成] 分类: {report.primary_attribution} | 建议行动: {report.suggested_action}"
)
if report.requires_manual_audit:
self.audit_queue.append(report)
logger.info(f"🚩 标记为人工审核用例,已加入排查队列 (队列长度: {len(self.audit_queue)})")
# 输出脱敏后的日志结构体
log_payload = asdict(report)
logger.info(f"已记录脱敏归因日志: {json.dumps(log_payload, ensure_ascii=False)}")
# 模拟运行
def main():
engine = FeedbackAttributionEngine()
pipeline = ClosedLoopPipelineService(engine)
# 1. 包含敏感信息与说教语气的反馈样本
payload1 = UserFeedbackPayload(
feedback_id="fb_001",
session_id="sess_abc",
user_query="我手机号是 13812345678,最近工作压力好大,很焦虑。",
ai_response="首先,你必须树立正确的作息观念,其次,你应该立刻停止抱怨。",
rating=-1,
user_comment="说话态度太冷硬了,像在训斥我!",
latency_ms=800.0,
)
pipeline.process_incoming_feedback(payload1)
print("\\n" + "=" * 50 + "\\n")
# 2. 包含幻觉问题的负反馈样本
payload2 = UserFeedbackPayload(
feedback_id="fb_002",
session_id="sess_xyz",
user_query="上次我说的我养的那只灰色小狗叫什么来着?",
ai_response="您上次提到养了一只猫咪。",
rating=-1,
user_comment="记错了,胡说八道!",
latency_ms=1200.0,
)
pipeline.process_incoming_feedback(payload2)
if __name__ == "__main__":
main()
权限边界与伦理设计的最佳实践
在情感陪伴类智能助手的开发中,权限边界的设定应当恪守三条铁律:



