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 DB_NAME=llm
# JWT配置 # JWT配置
......
...@@ -114,19 +114,24 @@ async def add_message(conversation_id: str, user_id: str, content: str) -> Dict[ ...@@ -114,19 +114,24 @@ async def add_message(conversation_id: str, user_id: str, content: str) -> Dict[
# 详细敏感词信息(用于响应与审计) # 详细敏感词信息(用于响应与审计)
detailed_words: List[Dict[str, Any]] = [] detailed_words: List[Dict[str, Any]] = []
if contains_sensitive and sensitive_words: if contains_sensitive and sensitive_words:
cursor = db.db.sensitive_words.find({"word": {"$in": sensitive_words}}) # 新版过滤器已返回结构化信息,直接使用;兼容旧版字符串列表
async for doc in cursor: if isinstance(sensitive_words[0], dict):
detailed_words.append({ detailed_words = sensitive_words
"word": doc.get("word"), else:
"category": doc.get("category"), words_list = [w for w in sensitive_words if isinstance(w, str)]
"subcategory": doc.get("subcategory"), if words_list:
"severity": doc.get("severity", 1), cursor = db.db.sensitive_words.find({"word": {"$in": words_list}})
}) async for doc in cursor:
# 对未命中库的词提供兜底 detailed_words.append({
known = set(dw["word"] for dw in detailed_words) "word": doc.get("word"),
for w in sensitive_words: "category": doc.get("category"),
if w not in known: "subcategory": doc.get("subcategory"),
detailed_words.append({"word": w, "category": None, "subcategory": None, "severity": 1}) "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 = { user_message = {
...@@ -297,4 +302,4 @@ async def delete_conversation(conversation_id: str, user_id: str) -> bool: ...@@ -297,4 +302,4 @@ async def delete_conversation(conversation_id: str, user_id: str) -> bool:
"conversation_id": ObjectId(conversation_id), "conversation_id": ObjectId(conversation_id),
"user_id": ObjectId(user_id) "user_id": ObjectId(user_id)
}) })
return True return True
\ No newline at end of file
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