main
old-tom 5 months ago
parent 2a6cd9e8a0
commit 56741d8670

@ -20,7 +20,7 @@ from langchain_core.messages import HumanMessage
from log_conf import log
# debug模式,有更多输出
set_debug(True)
set_debug(False)
set_verbose(False)
# 默认系统提示词
DEFAULT_SYS_PROMPT = ''
@ -139,7 +139,10 @@ class BaseChatAgent(ABC):
llm_with_tools = llm.bind_tools(STRUCT_TOOLS)
# 判断使用哪个工具,需要加提示词让模型判断参数是否符合规则
user_input = PROMPT_TEMPLATE.get('TOOL_CALLER')['template'].format(user_input=user_input)
call_msg = llm_with_tools.invoke(user_input)
# 工具chain加入历史对话
too_chain = self.multi_round_prompt | llm_with_tools
with_message_history = RunnableWithMessageHistory(too_chain, get_session_history, input_messages_key="messages")
call_msg = with_message_history.invoke({'messages': user_input}, config=config)
# 如果参数不满足要求 call_msg 的content会可能会包含参数校验失败信息参数错误分屏数量必须为大于0的整数。请检查指令中的"分屏数量"参数。
# 用模型进行参数校验很不稳定不是每次都能输出错误信息。还是在tool中手动校验靠谱。
messages.append(call_msg)
@ -150,9 +153,10 @@ class BaseChatAgent(ABC):
messages.append(tool_msg)
log.info('【function call】构造输入为{}', messages)
# messages 中包含了 人类指令、AI指令、工具指令, 模型根据历史聊天组装成最后的回答
chain = self.multi_round_prompt | llm_with_tools | parser
chat_chain = self.multi_round_prompt | llm_with_tools | parser
# RunnableWithMessageHistory 会调用历史对话
with_message_history = RunnableWithMessageHistory(chain, get_session_history, input_messages_key="messages")
with_message_history = RunnableWithMessageHistory(chat_chain, get_session_history,
input_messages_key="messages")
response = with_message_history.stream({'messages': messages}, config=config)
for chunk in response:
print(chunk, flush=True, end='')

@ -15,7 +15,7 @@ if __name__ == '__main__':
########## 测试 function call #########
# print(dsr.invoke_with_tool_call('播放南卡口相机'))
## [{'name': 'play_video', 'args': {'camera_name': '北卡口1号道相机'}, 'id': 'call_apnb8fiqdkaz313katcs3tjf', 'type': 'tool_call'}]
# dsr.invoke_with_tool_call('将大屏切换为-1分屏', 1)
dsr.multi_with_tool_call_stream('将大屏切换为-1分屏', 1)
## [{'name': 'split_screen', 'args': {'split_n': 2}, 'id': 'call_2o7c94f591xag8p6lcyice9q', 'type': 'tool_call'}]
# print(dsr.invoke_with_tool('播放北卡口入境1号道录像,从今天到2025-03-16 02:09:31'))
## 由于大模型没有联网,所以无法判断‘今天’
@ -39,5 +39,6 @@ if __name__ == '__main__':
# dsr.multi_with_stream('我的上一个问题是什么?请直接返回问题,不要有多余输出及思考过程', 1)
# dsr.multi_with_stream('我的第一个问题是什么?请直接返回问题,不要有多余输出及思考过程', 1)
dsr.multi_with_tool_call_stream('播放南卡口相机', 1)
dsr.multi_with_tool_call_stream('1', 1)
########## 测试 多轮对话-相机选择 #########
# dsr.multi_with_tool_call_stream('播放南卡口相机', 1)
# dsr.multi_with_tool_call_stream('1', 1)

Loading…
Cancel
Save