125 lines
5.4 KiB
Python
125 lines
5.4 KiB
Python
"""
|
|
backend/app/services/agent_registry/content_profiles.py
|
|
|
|
用途:Agent 聊天内容 profile 目录(默认欢迎语/推荐问题模板 + Admin 校验白名单)。
|
|
依赖:无。
|
|
逻辑:Admin 保存 content_profile、backfill 回填默认文案、前端模板应用均读取本模块。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from copy import deepcopy
|
|
from typing import Any
|
|
|
|
VALID_CONTENT_PROFILES = frozenset({'generic_entrance', 'hr_policy', 'training'})
|
|
|
|
CONTENT_PROFILE_CATALOG: dict[str, dict[str, Any]] = {
|
|
'generic_entrance': {
|
|
'key': 'generic_entrance',
|
|
'label': '通用入口',
|
|
'description': 'HR + PLM 双 Intent 通用入口 Agent 的默认欢迎语与推荐问题',
|
|
'welcome_message': (
|
|
'我是你的7x24小时专属AI数字人。无需手动检索文档,只需用日常说话的方式提问,'
|
|
'我能从公司最新、最全的人事规章制度中,为你精准提取并总结答案。'
|
|
),
|
|
'suggested_questions_style': 'plain',
|
|
'suggested_questions': [
|
|
{'question': '新员工入职须知', 'group': ''},
|
|
{'question': '查询我有多少假期?', 'group': ''},
|
|
{'question': '如何进行请假?', 'group': ''},
|
|
{'question': '查公司法定节假日?', 'group': ''},
|
|
{'question': '查公司薪酬及福利待遇怎么样?', 'group': ''},
|
|
],
|
|
},
|
|
'hr_policy': {
|
|
'key': 'hr_policy',
|
|
'label': '人事规章专岗',
|
|
'description': '仅 HR SQL Intent 的人事规章 Agent 默认欢迎语与推荐问题',
|
|
'welcome_message': (
|
|
'我是你的7x24小时专属HR政策助手。无需手动检索文档,只需用日常说话的方式提问,'
|
|
'我能从公司最新、最全的规章制度中,为你精准提取并总结答案。'
|
|
),
|
|
'suggested_questions_style': 'styled',
|
|
'suggested_questions': [
|
|
{'question': '查询我有多少假期?', 'group': '考勤与休假'},
|
|
{'question': '如何进行请假?', 'group': '考勤与休假'},
|
|
{'question': '查公司法定节假日?', 'group': '考勤与休假'},
|
|
{'question': '新员工入职须知', 'group': '规章与日常'},
|
|
{'question': '怎么开具在职证明?', 'group': '规章与日常'},
|
|
{'question': '查公司薪酬及福利待遇怎么样?', 'group': '规章与日常'},
|
|
],
|
|
},
|
|
'training': {
|
|
'key': 'training',
|
|
'label': '员工培训',
|
|
'description': '培训工作区 Agent;详细题库仍存 extra_config.training_*',
|
|
'welcome_message': (
|
|
'我是你的新人培训助手,会在同一个聊天页面内带你完成视频学习、知识点复盘、逐题考试和结果复盘。'
|
|
),
|
|
'suggested_questions_style': 'styled',
|
|
'suggested_questions': [
|
|
{'question': '开始新人培训', 'group': '培训流程'},
|
|
{'question': '我已看完视频,请总结知识点', 'group': '培训流程'},
|
|
{'question': '开始考试', 'group': '培训流程'},
|
|
{'question': '导出本月培训成绩', 'group': 'HR 管理'},
|
|
],
|
|
},
|
|
}
|
|
|
|
|
|
def list_content_profiles(*, include_training: bool = True) -> list[dict[str, Any]]:
|
|
"""返回 Admin 可选 content_profile 列表(含默认文案预览字段)。"""
|
|
items = []
|
|
for key in ('generic_entrance', 'hr_policy', 'training'):
|
|
if key == 'training' and not include_training:
|
|
continue
|
|
entry = CONTENT_PROFILE_CATALOG.get(key)
|
|
if entry:
|
|
items.append(deepcopy(entry))
|
|
return items
|
|
|
|
|
|
def get_content_profile_definition(key: str) -> dict[str, Any] | None:
|
|
"""按 key 读取 profile 定义;未知 key 返回 None。"""
|
|
normalized = str(key or '').strip()
|
|
entry = CONTENT_PROFILE_CATALOG.get(normalized)
|
|
return deepcopy(entry) if entry else None
|
|
|
|
|
|
def infer_content_profile_from_intent_routes(routes: list[str] | None) -> str | None:
|
|
"""
|
|
根据 intent_routes 推断 content_profile(与 intent_route_registry 聚合逻辑一致)。
|
|
|
|
Returns:
|
|
generic_entrance | hr_policy | None
|
|
"""
|
|
if not routes:
|
|
return None
|
|
route_set = {str(item or '').strip() for item in routes if str(item or '').strip()}
|
|
has_hr = 'hr_leave_sql' in route_set
|
|
has_plm = 'plm_oracle' in route_set
|
|
if has_hr and has_plm:
|
|
return 'generic_entrance'
|
|
if has_hr and not has_plm:
|
|
return 'hr_policy'
|
|
return None
|
|
|
|
|
|
def merge_profile_defaults_into_fields(fields: dict[str, Any], profile_key: str) -> None:
|
|
"""
|
|
将 profile 默认 welcome_message / suggested_questions 写入回填字段 dict(仅填空)。
|
|
|
|
Args:
|
|
fields: infer_registry_fields_from_legacy 等构建的 ORM 更新 dict(原地修改)。
|
|
profile_key: content_profile key。
|
|
"""
|
|
definition = get_content_profile_definition(profile_key)
|
|
if not definition:
|
|
return
|
|
if not str(fields.get('welcome_message') or '').strip():
|
|
fields['welcome_message'] = definition['welcome_message']
|
|
if not fields.get('suggested_questions'):
|
|
fields['suggested_questions'] = deepcopy(definition['suggested_questions'])
|
|
if not str(fields.get('suggested_questions_style') or '').strip():
|
|
fields['suggested_questions_style'] = definition['suggested_questions_style']
|