mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
update:回复上版提示词,无需再二次识别歌曲名
This commit is contained in:
parent
95c8e43b61
commit
164e36f3ac
4 changed files with 24 additions and 80 deletions
|
|
@ -125,7 +125,7 @@ async def analyze_intent_with_llm(conn, text):
|
||||||
# 对话历史记录
|
# 对话历史记录
|
||||||
dialogue = conn.dialogue
|
dialogue = conn.dialogue
|
||||||
try:
|
try:
|
||||||
intent_result = await conn.intent.detect_intent(conn, dialogue.dialogue, text)
|
intent_result = await conn.intent.detect_intent(dialogue.dialogue, text)
|
||||||
logger.bind(tag=TAG).info(f"意图识别结果: {intent_result}")
|
logger.bind(tag=TAG).info(f"意图识别结果: {intent_result}")
|
||||||
|
|
||||||
# 尝试解析JSON结果
|
# 尝试解析JSON结果
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ class IntentProviderBase(ABC):
|
||||||
logger.bind(tag=TAG).debug("Set LLM for intent provider")
|
logger.bind(tag=TAG).debug("Set LLM for intent provider")
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def detect_intent(self, conn, dialogue_history: List[Dict], text: str) -> str:
|
async def detect_intent(self, dialogue_history: List[Dict], text: str) -> str:
|
||||||
"""
|
"""
|
||||||
检测用户最后一句话的意图
|
检测用户最后一句话的意图
|
||||||
Args:
|
Args:
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,7 @@ class IntentProvider(IntentProviderBase):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
super().__init__(config)
|
super().__init__(config)
|
||||||
self.llm = None
|
self.llm = None
|
||||||
self.prompt1_classfy, self.prompt2_getmusicname = self.get_intent_system_prompt()
|
self.promot = self.get_intent_system_prompt()
|
||||||
|
|
||||||
|
|
||||||
def get_intent_system_prompt(self) -> str:
|
def get_intent_system_prompt(self) -> str:
|
||||||
"""
|
"""
|
||||||
|
|
@ -21,56 +20,24 @@ class IntentProvider(IntentProviderBase):
|
||||||
格式化后的系统提示词
|
格式化后的系统提示词
|
||||||
"""
|
"""
|
||||||
intent_list = []
|
intent_list = []
|
||||||
# for key, value in self.intent_options.items():
|
for key, value in self.intent_options.items():
|
||||||
# if key == "continue_chat":
|
if key == "play_music":
|
||||||
# """
|
intent_list.append(f"{value} [歌名]")
|
||||||
# "continue_chat": "1.继续聊天, 除了播放音乐和结束聊天的时候的选项, 比如日常的聊天和问候, 对话等",
|
else:
|
||||||
# "end_chat": "2.结束聊天, 用户发来如再见之类的表示结束的话, 不想再进行对话的时候",
|
intent_list.append(value)
|
||||||
# "play_music": "3.播放音乐, 用户希望你可以播放音乐, 只用于播放音乐的意图"
|
|
||||||
# """
|
prompt = (
|
||||||
intent_list.append("1.继续聊天, 除了播放音乐和结束聊天的时候的选项, 比如日常的聊天和问候, 对话等")
|
"你是一个意图识别助手。你需要根据和用户的对话记录,重点分析用户的最后一句话,判断用户意图属于以下哪一类:\n"
|
||||||
# elif key == "end_chat":
|
f"{', '.join(intent_list)}\n"
|
||||||
intent_list.append("2.结束聊天, 用户发来如再见之类的表示结束的话, 不想再进行对话的时候")
|
"如果是唱歌、听歌、播放音乐,请指定歌名,格式为'播放音乐 [识别出的歌名]'。\n"
|
||||||
# elif key == "play_music":
|
"如果听不出具体歌名,可以返回'随机播放音乐'。\n"
|
||||||
intent_list.append("3.播放音乐, 用户希望你可以播放音乐, 只用于播放音乐的意图")
|
"只需要返回意图结果的json,不要解释。"
|
||||||
prompt1_classfy = ("你是一个分类助手, 请根据以下的对话记录,判断用户的最后意图属于以下哪一类:\n"
|
"返回格式如下:\n"
|
||||||
"你可以使用的分类如下使用<begin><end>进行标记:\n"
|
"{intent: '用户意图'}"
|
||||||
"<begin>\n"
|
)
|
||||||
f"{', '.join(intent_list)}\n"
|
return prompt
|
||||||
"<end>\n"
|
|
||||||
"请你返回你的判断结果标号, 只使用一个数字作为返回结果, 下面是几个实际的示例\n"
|
|
||||||
"```"
|
|
||||||
"用户: 我想听一首可以让我激情澎湃的歌曲\n"
|
|
||||||
"3\n"
|
|
||||||
"```"
|
|
||||||
"用户: 我有点累了, 想休息一下, 再见\n"
|
|
||||||
"2\n"
|
|
||||||
"```"
|
|
||||||
"返回的结果只有一个数字, 没有其他的附加内容")
|
|
||||||
|
|
||||||
prompt2_getmusicname = ("用户发来消息, 请你根据他的消息分类出他想听的音乐的名字:\n"
|
|
||||||
"如果用户没有提供音乐的名字, 请返回随机播放音乐\n"
|
|
||||||
"请你返回你的判断结果, 只使用歌名作为返回结果, 下面是几个实际的示例\n"
|
|
||||||
"```\n"
|
|
||||||
"用户: 我想听静夜思\n"
|
|
||||||
"静夜思\n"
|
|
||||||
"```\n"
|
|
||||||
"用户: 我想听一首轻松的音乐\n"
|
|
||||||
"随机播放音乐\n"
|
|
||||||
"```\n"
|
|
||||||
"你可以使用的音乐的名字如下使用<begin><end>进行标记:\n")
|
|
||||||
# prompt = (
|
|
||||||
# "你是一个意图识别助手。你需要根据和用户的对话记录,重点分析用户的最后一句话,判断用户意图属于以下哪一类:\n"
|
|
||||||
# f"{', '.join(intent_list)}\n"
|
|
||||||
# "如果是唱歌、听歌、播放音乐,请指定歌名,格式为'播放音乐 [识别出的歌名]'。\n"
|
|
||||||
# "如果听不出具体歌名,可以返回'随机播放音乐'。\n"
|
|
||||||
# "只需要返回意图结果的json,不要解释。"
|
|
||||||
# "返回格式如下:\n"
|
|
||||||
# "{intent: '用户意图'}"
|
|
||||||
# )
|
|
||||||
return prompt1_classfy, prompt2_getmusicname
|
|
||||||
|
|
||||||
async def detect_intent(self, conn, dialogue_history: List[Dict], text:str) -> str:
|
async def detect_intent(self, dialogue_history: List[Dict], text:str) -> str:
|
||||||
if not self.llm:
|
if not self.llm:
|
||||||
raise ValueError("LLM provider not set")
|
raise ValueError("LLM provider not set")
|
||||||
|
|
||||||
|
|
@ -82,35 +49,12 @@ class IntentProvider(IntentProviderBase):
|
||||||
elif msg.role== "assistant":
|
elif msg.role== "assistant":
|
||||||
msgStr += f"Assistant: {msg.content}\n"
|
msgStr += f"Assistant: {msg.content}\n"
|
||||||
msgStr += f"User: {text}\n"
|
msgStr += f"User: {text}\n"
|
||||||
|
|
||||||
user_prompt = f"请分析用户的意图:\n{msgStr}"
|
user_prompt = f"请分析用户的意图:\n{msgStr}"
|
||||||
# 使用LLM进行意图识别
|
# 使用LLM进行意图识别
|
||||||
intent = self.llm.response_no_stream(
|
intent = self.llm.response_no_stream(
|
||||||
system_prompt=self.prompt1_classfy,
|
system_prompt=self.promot,
|
||||||
user_prompt=user_prompt
|
user_prompt=user_prompt
|
||||||
)
|
)
|
||||||
# 判断返回的是不是一个数字
|
|
||||||
intent = intent.strip()
|
|
||||||
logger.bind(tag=TAG).info(f"Detected intent: {intent}")
|
logger.bind(tag=TAG).info(f"Detected intent: {intent}")
|
||||||
if intent.isdigit():
|
return intent.strip()
|
||||||
# 处理不同选项
|
|
||||||
if int(intent) == 1:
|
|
||||||
return '{intent: "继续聊天"}'
|
|
||||||
elif int(intent) == 2:
|
|
||||||
return '{intent: "结束聊天"}'
|
|
||||||
elif int(intent) == 3:
|
|
||||||
|
|
||||||
print(conn.music_handler.music_files)
|
|
||||||
prompt = f"{self.prompt2_getmusicname}\n<begin>{conn.music_handler.music_files}\n<end>"
|
|
||||||
# logger.bind(tag=TAG).info(f"Prompt for getting music name: {prompt}")
|
|
||||||
intent = self.llm.response_no_stream(
|
|
||||||
system_prompt=prompt,
|
|
||||||
user_prompt=user_prompt
|
|
||||||
)
|
|
||||||
logger.bind(tag=TAG).info(f"Detected intent: {intent}")
|
|
||||||
# 播放音乐 [识别出的歌名]
|
|
||||||
ret = '{intent: "播放音乐 ' + intent + '"}'
|
|
||||||
logger.bind(tag=TAG).info(f"Detected intent: {ret}")
|
|
||||||
return ret
|
|
||||||
logger.bind(tag=TAG).info(f"Detected intent: {intent}")
|
|
||||||
return {intent: "继续聊天"}
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ logger = setup_logging()
|
||||||
|
|
||||||
|
|
||||||
class IntentProvider(IntentProviderBase):
|
class IntentProvider(IntentProviderBase):
|
||||||
async def detect_intent(self, conn, dialogue_history: List[Dict], text: str) -> str:
|
async def detect_intent(self, dialogue_history: List[Dict], text: str) -> str:
|
||||||
"""
|
"""
|
||||||
默认的意图识别实现,始终返回继续聊天
|
默认的意图识别实现,始终返回继续聊天
|
||||||
Args:
|
Args:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue