Commit 7a1720ea authored by uuo00_n's avatar uuo00_n

fix(conversation): 更新敏感词处理逻辑以兼容新旧版本

处理敏感词时添加对新版结构化数据的支持,同时保持对旧版字符串列表的兼容
修改数据库连接配置为本地开发环境
parent adff7ae9
# 数据库配置
# 注意:生产环境请通过安全的环境变量管理传递凭据,避免将敏感信息提交到版本库
MONGODB_URL=mongodb://llm:fSjFMwyShmcH4GdR@datacenter.dldzxx.cn:27017/llm?authSource=llm
# MONGODB_URL=mongodb://llm:fSjFMwyShmcH4GdR@datacenter.dldzxx.cn:27017/llm?authSource=llm
MONGODB_URL=mongodb://localhost:27017/
DB_NAME=llm
# JWT配置
......
......@@ -114,19 +114,24 @@ async def add_message(conversation_id: str, user_id: str, content: str) -> Dict[
# 详细敏感词信息(用于响应与审计)
detailed_words: List[Dict[str, Any]] = []
if contains_sensitive and sensitive_words:
cursor = db.db.sensitive_words.find({"word": {"$in": sensitive_words}})
async for doc in cursor:
detailed_words.append({
"word": doc.get("word"),
"category": doc.get("category"),
"subcategory": doc.get("subcategory"),
"severity": doc.get("severity", 1),
})
# 对未命中库的词提供兜底
known = set(dw["word"] for dw in detailed_words)
for w in sensitive_words:
if w not in known:
detailed_words.append({"word": w, "category": None, "subcategory": None, "severity": 1})
# 新版过滤器已返回结构化信息,直接使用;兼容旧版字符串列表
if isinstance(sensitive_words[0], dict):
detailed_words = sensitive_words
else:
words_list = [w for w in sensitive_words if isinstance(w, str)]
if words_list:
cursor = db.db.sensitive_words.find({"word": {"$in": words_list}})
async for doc in cursor:
detailed_words.append({
"word": doc.get("word"),
"category": doc.get("category"),
"subcategory": doc.get("subcategory"),
"severity": doc.get("severity", 1),
})
known = set(dw["word"] for dw in detailed_words)
for w in words_list:
if w not in known:
detailed_words.append({"word": w, "category": None, "subcategory": None, "severity": 1})
# 创建用户消息(消息中也保存结构化敏感词列表,便于后续展示)
user_message = {
......@@ -297,4 +302,4 @@ async def delete_conversation(conversation_id: str, user_id: str) -> bool:
"conversation_id": ObjectId(conversation_id),
"user_id": ObjectId(user_id)
})
return True
\ No newline at end of file
return True
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment