Use this skill when the user states or updates employee profile preferences for business travel, e.g. resident city, travel tier, seat preference, hotel brand preference, reimbursement preference.
识别员工差旅画像并支持追加(还、也)与覆盖(改成、现在是)。使用 PreferenceAgent。持久化由 MemoryManager 完成:在协调器流程中由协调器写回;单独调用时需根据返回的 preferences 列表自行调用 memory_manager.long_term.save_preference()。
agents/preference_agent.py)reply() 为 async,需 awaitresident_city, , , , 等,支持自定义travel_tierseat_preferencehotel_brand_preferencereimbursement_preferenceimport asyncio
import json
from agentscope.message import Msg
from agentscope.model import OpenAIChatModel
from config_agentscope import init_agentscope
from config import LLM_CONFIG
from context.memory_manager import MemoryManager
from agents.preference_agent import PreferenceAgent
async def save_preference(user_query: str, user_id: str = "default_user", session_id: str = "default"):
init_agentscope()
model = OpenAIChatModel(
model_name=LLM_CONFIG["model_name"],
api_key=LLM_CONFIG["api_key"],
client_kwargs={"base_url": LLM_CONFIG["base_url"], "timeout": 60},
temperature=LLM_CONFIG.get("temperature", 0.7),
max_tokens=LLM_CONFIG.get("max_tokens", 2000),
)
memory_manager = MemoryManager(user_id=user_id, session_id=session_id, llm_model=model)
agent = PreferenceAgent(name="PreferenceAgent", model=model, memory_manager=memory_manager)
user_msg = Msg(name="user", content=user_query, role="user")
result = await agent.reply(user_msg)
data = json.loads(result.content) if isinstance(result.content, str) else result.content
if data.get("has_preferences") and data.get("preferences"):
for item in data["preferences"]:
pref_type = item.get("type")
value = item.get("value")
action = item.get("action", "replace")
current = memory_manager.long_term.get_preference().get(pref_type)
if action == "append" and isinstance(current, list):
memory_manager.long_term.save_preference(pref_type, current + [value])
else:
memory_manager.long_term.save_preference(pref_type, value)
return data
# 使用
data = asyncio.run(save_preference("我还喜欢如家酒店"))
# data: {"preferences": [{"type": "hotel_brand_preference", "value": "如家", "action": "append"}], "has_preferences": true}
preferences: 列表,每项 { "type", "value", "action": "append"|"replace" }has_preferences: bool【任务说明】 你需要判断用户的意图:
追加(append):用户想在已有偏好基础上增加新的选项
覆盖(replace):用户想更新/替换原有的偏好
首次设置:用户第一次提及某个偏好
【常见偏好类型】
【输出格式】(严格JSON) {{ "preferences": [ {{ "type": "hotel_brand_preference", "value": "汉庭", "action": "append" }}, {{ "type": "resident_city", "value": "上海", "action": "replace" }} ], "has_preferences": true }}
【重要规则】