Use this skill when the user states or updates their preferences, e.g. hotel brands, airlines, home location, seat preference. Triggers when user says "我喜欢住汉庭", "我还喜欢如家", "我搬家到上海了", "我常坐东航". This skill uses PreferenceAgent and requires a MemoryManager to persist preferences; when used standalone, apply returned preferences via memory_manager.long_term.save_preference.
识别用户偏好并支持追加(还、也)与覆盖(搬家到、改成)。使用 PreferenceAgent。持久化由 MemoryManager 完成:在协调器流程中由协调器写回;单独调用时需根据返回的 preferences 列表自行调用 memory_manager.long_term.save_preference()。
agents/preference_agent.py)reply() 为 async,需 awaithotel_brands, , , , , 等,支持自定义airlineshome_locationseat_preferencemeal_preferencebudget_levelhome_location=苏州、transportation_preference=高铁、accommodation_preference=安静酒店import 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_brands", "value": "如家", "action": "append"}], "has_preferences": true}
preferences: 列表,每项 { "type", "value", "action": "append"|"replace" }has_preferences: bool【任务说明】 你需要判断用户的意图:
追加(append):用户想在已有偏好基础上增加新的选项
覆盖(replace):用户想更新/替换原有的偏好
首次设置:用户第一次提及某个偏好
【常见偏好类型】
【偏好识别提示】
transportation_preferencefood_preferenceaccommodation_preferenceaccommodation_preferencebudget_levelaccommodation_preference【输出格式】(严格JSON) {{ "preferences": [ {{ "type": "hotel_brands", "value": "汉庭", "action": "append" }}, {{ "type": "home_location", "value": "上海浦东新区", "action": "replace" }} ], "has_preferences": true }}
【重要规则】