ForcePilot/backend/package/yuxi/channels/adapters/wechat/agent_prompt.py

46 lines
1.7 KiB
Python
Raw Normal View History

from __future__ import annotations
from typing import Any
PROMPT_PREFIXES = {
"wecom": "你是一个企业微信 AI 助手,通过企业微信与用户沟通。请保持专业、简洁,使用中文回复。",
"mp": "你是一个微信公众号 AI 助手,通过公众号消息与用户沟通。请保持友好、专业,使用中文回复。",
"personal": "你是一个微信 AI 助手,通过个人微信与用户沟通。请保持自然、亲切,使用中文回复。",
}
class WeChatAgentPromptAdapter:
def __init__(self):
self._custom_prefix: str | None = None
self._enabled: bool = True
def configure(self, config: dict[str, Any], mode: str = "wecom") -> None:
self._enabled = config.get("agent_prompt", {}).get("enabled", True)
self._custom_prefix = config.get("agent_prompt", {}).get("prefix")
if not self._custom_prefix:
self._custom_prefix = PROMPT_PREFIXES.get(mode, "")
def get_channel_prefix(self, config: dict[str, Any], mode: str = "wecom") -> str:
if not self._enabled:
return ""
if self._custom_prefix:
return self._custom_prefix
return PROMPT_PREFIXES.get(mode, "")
@staticmethod
def inject_prompt(base_prompt: str, channel_prefix: str) -> str:
if not channel_prefix:
return base_prompt
return f"{channel_prefix}\n\n{base_prompt}"
@staticmethod
def strip_channel_prefix(prompt: str, mode: str = "wecom") -> str:
prefix = PROMPT_PREFIXES.get(mode, "")
if prefix and prompt.startswith(prefix):
return prompt[len(prefix) :].strip()
return prompt
def is_enabled(self) -> bool:
return self._enabled