cot_agent_runner.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. import json
  2. from abc import ABC, abstractmethod
  3. from collections.abc import Generator
  4. from typing import Union
  5. from core.agent.base_agent_runner import BaseAgentRunner
  6. from core.agent.entities import AgentScratchpadUnit
  7. from core.agent.output_parser.cot_output_parser import CotAgentOutputParser
  8. from core.app.apps.base_app_queue_manager import PublishFrom
  9. from core.app.entities.queue_entities import QueueAgentThoughtEvent, QueueMessageEndEvent, QueueMessageFileEvent
  10. from core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta, LLMUsage
  11. from core.model_runtime.entities.message_entities import (
  12. AssistantPromptMessage,
  13. PromptMessage,
  14. ToolPromptMessage,
  15. UserPromptMessage,
  16. )
  17. from core.tools.entities.tool_entities import ToolInvokeMeta
  18. from core.tools.tool.tool import Tool
  19. from core.tools.tool_engine import ToolEngine
  20. from models.model import Message
  21. class CotAgentRunner(BaseAgentRunner, ABC):
  22. _is_first_iteration = True
  23. _ignore_observation_providers = ['wenxin']
  24. _historic_prompt_messages: list[PromptMessage] = None
  25. _agent_scratchpad: list[AgentScratchpadUnit] = None
  26. _instruction: str = None
  27. _query: str = None
  28. _prompt_messages_tools: list[PromptMessage] = None
  29. def run(self, message: Message,
  30. query: str,
  31. inputs: dict[str, str],
  32. ) -> Union[Generator, LLMResult]:
  33. """
  34. Run Cot agent application
  35. """
  36. app_generate_entity = self.application_generate_entity
  37. self._repack_app_generate_entity(app_generate_entity)
  38. self._init_react_state(query)
  39. # check model mode
  40. if 'Observation' not in app_generate_entity.model_config.stop:
  41. if app_generate_entity.model_config.provider not in self._ignore_observation_providers:
  42. app_generate_entity.model_config.stop.append('Observation')
  43. app_config = self.app_config
  44. # init instruction
  45. inputs = inputs or {}
  46. instruction = app_config.prompt_template.simple_prompt_template
  47. self._instruction = self._fill_in_inputs_from_external_data_tools(instruction, inputs)
  48. iteration_step = 1
  49. max_iteration_steps = min(app_config.agent.max_iteration, 5) + 1
  50. # convert tools into ModelRuntime Tool format
  51. tool_instances, self._prompt_messages_tools = self._init_prompt_tools()
  52. prompt_messages = self._organize_prompt_messages()
  53. function_call_state = True
  54. llm_usage = {
  55. 'usage': None
  56. }
  57. final_answer = ''
  58. def increase_usage(final_llm_usage_dict: dict[str, LLMUsage], usage: LLMUsage):
  59. if not final_llm_usage_dict['usage']:
  60. final_llm_usage_dict['usage'] = usage
  61. else:
  62. llm_usage = final_llm_usage_dict['usage']
  63. llm_usage.prompt_tokens += usage.prompt_tokens
  64. llm_usage.completion_tokens += usage.completion_tokens
  65. llm_usage.prompt_price += usage.prompt_price
  66. llm_usage.completion_price += usage.completion_price
  67. model_instance = self.model_instance
  68. while function_call_state and iteration_step <= max_iteration_steps:
  69. # continue to run until there is not any tool call
  70. function_call_state = False
  71. if iteration_step == max_iteration_steps:
  72. # the last iteration, remove all tools
  73. self._prompt_messages_tools = []
  74. message_file_ids = []
  75. agent_thought = self.create_agent_thought(
  76. message_id=message.id,
  77. message='',
  78. tool_name='',
  79. tool_input='',
  80. messages_ids=message_file_ids
  81. )
  82. if iteration_step > 1:
  83. self.queue_manager.publish(QueueAgentThoughtEvent(
  84. agent_thought_id=agent_thought.id
  85. ), PublishFrom.APPLICATION_MANAGER)
  86. # recalc llm max tokens
  87. prompt_messages = self._organize_prompt_messages()
  88. self.recalc_llm_max_tokens(self.model_config, prompt_messages)
  89. # invoke model
  90. chunks: Generator[LLMResultChunk, None, None] = model_instance.invoke_llm(
  91. prompt_messages=prompt_messages,
  92. model_parameters=app_generate_entity.model_config.parameters,
  93. tools=[],
  94. stop=app_generate_entity.model_config.stop,
  95. stream=True,
  96. user=self.user_id,
  97. callbacks=[],
  98. )
  99. # check llm result
  100. if not chunks:
  101. raise ValueError("failed to invoke llm")
  102. usage_dict = {}
  103. react_chunks = CotAgentOutputParser.handle_react_stream_output(chunks)
  104. scratchpad = AgentScratchpadUnit(
  105. agent_response='',
  106. thought='',
  107. action_str='',
  108. observation='',
  109. action=None,
  110. )
  111. # publish agent thought if it's first iteration
  112. if iteration_step == 1:
  113. self.queue_manager.publish(QueueAgentThoughtEvent(
  114. agent_thought_id=agent_thought.id
  115. ), PublishFrom.APPLICATION_MANAGER)
  116. for chunk in react_chunks:
  117. if isinstance(chunk, AgentScratchpadUnit.Action):
  118. action = chunk
  119. # detect action
  120. scratchpad.agent_response += json.dumps(chunk.dict())
  121. scratchpad.action_str = json.dumps(chunk.dict())
  122. scratchpad.action = action
  123. else:
  124. scratchpad.agent_response += chunk
  125. scratchpad.thought += chunk
  126. yield LLMResultChunk(
  127. model=self.model_config.model,
  128. prompt_messages=prompt_messages,
  129. system_fingerprint='',
  130. delta=LLMResultChunkDelta(
  131. index=0,
  132. message=AssistantPromptMessage(
  133. content=chunk
  134. ),
  135. usage=None
  136. )
  137. )
  138. scratchpad.thought = scratchpad.thought.strip() or 'I am thinking about how to help you'
  139. self._agent_scratchpad.append(scratchpad)
  140. # get llm usage
  141. if 'usage' in usage_dict:
  142. increase_usage(llm_usage, usage_dict['usage'])
  143. else:
  144. usage_dict['usage'] = LLMUsage.empty_usage()
  145. self.save_agent_thought(
  146. agent_thought=agent_thought,
  147. tool_name=scratchpad.action.action_name if scratchpad.action else '',
  148. tool_input={
  149. scratchpad.action.action_name: scratchpad.action.action_input
  150. } if scratchpad.action else {},
  151. tool_invoke_meta={},
  152. thought=scratchpad.thought,
  153. observation='',
  154. answer=scratchpad.agent_response,
  155. messages_ids=[],
  156. llm_usage=usage_dict['usage']
  157. )
  158. if not scratchpad.is_final():
  159. self.queue_manager.publish(QueueAgentThoughtEvent(
  160. agent_thought_id=agent_thought.id
  161. ), PublishFrom.APPLICATION_MANAGER)
  162. if not scratchpad.action:
  163. # failed to extract action, return final answer directly
  164. final_answer = scratchpad.agent_response or ''
  165. else:
  166. if scratchpad.action.action_name.lower() == "final answer":
  167. # action is final answer, return final answer directly
  168. try:
  169. if isinstance(scratchpad.action.action_input, dict):
  170. final_answer = json.dumps(scratchpad.action.action_input)
  171. elif isinstance(scratchpad.action.action_input, str):
  172. final_answer = scratchpad.action.action_input
  173. else:
  174. final_answer = f'{scratchpad.action.action_input}'
  175. except json.JSONDecodeError:
  176. final_answer = f'{scratchpad.action.action_input}'
  177. else:
  178. function_call_state = True
  179. # action is tool call, invoke tool
  180. tool_invoke_response, tool_invoke_meta = self._handle_invoke_action(
  181. action=scratchpad.action,
  182. tool_instances=tool_instances,
  183. message_file_ids=message_file_ids
  184. )
  185. scratchpad.observation = tool_invoke_response
  186. scratchpad.agent_response = tool_invoke_response
  187. self.save_agent_thought(
  188. agent_thought=agent_thought,
  189. tool_name=scratchpad.action.action_name,
  190. tool_input={scratchpad.action.action_name: scratchpad.action.action_input},
  191. thought=scratchpad.thought,
  192. observation={scratchpad.action.action_name: tool_invoke_response},
  193. tool_invoke_meta={scratchpad.action.action_name: tool_invoke_meta.to_dict()},
  194. answer=scratchpad.agent_response,
  195. messages_ids=message_file_ids,
  196. llm_usage=usage_dict['usage']
  197. )
  198. self.queue_manager.publish(QueueAgentThoughtEvent(
  199. agent_thought_id=agent_thought.id
  200. ), PublishFrom.APPLICATION_MANAGER)
  201. # update prompt tool message
  202. for prompt_tool in self._prompt_messages_tools:
  203. self.update_prompt_message_tool(tool_instances[prompt_tool.name], prompt_tool)
  204. iteration_step += 1
  205. yield LLMResultChunk(
  206. model=model_instance.model,
  207. prompt_messages=prompt_messages,
  208. delta=LLMResultChunkDelta(
  209. index=0,
  210. message=AssistantPromptMessage(
  211. content=final_answer
  212. ),
  213. usage=llm_usage['usage']
  214. ),
  215. system_fingerprint=''
  216. )
  217. # save agent thought
  218. self.save_agent_thought(
  219. agent_thought=agent_thought,
  220. tool_name='',
  221. tool_input={},
  222. tool_invoke_meta={},
  223. thought=final_answer,
  224. observation={},
  225. answer=final_answer,
  226. messages_ids=[]
  227. )
  228. self.update_db_variables(self.variables_pool, self.db_variables_pool)
  229. # publish end event
  230. self.queue_manager.publish(QueueMessageEndEvent(llm_result=LLMResult(
  231. model=model_instance.model,
  232. prompt_messages=prompt_messages,
  233. message=AssistantPromptMessage(
  234. content=final_answer
  235. ),
  236. usage=llm_usage['usage'] if llm_usage['usage'] else LLMUsage.empty_usage(),
  237. system_fingerprint=''
  238. )), PublishFrom.APPLICATION_MANAGER)
  239. def _handle_invoke_action(self, action: AgentScratchpadUnit.Action,
  240. tool_instances: dict[str, Tool],
  241. message_file_ids: list[str]) -> tuple[str, ToolInvokeMeta]:
  242. """
  243. handle invoke action
  244. :param action: action
  245. :param tool_instances: tool instances
  246. :return: observation, meta
  247. """
  248. # action is tool call, invoke tool
  249. tool_call_name = action.action_name
  250. tool_call_args = action.action_input
  251. tool_instance = tool_instances.get(tool_call_name)
  252. if not tool_instance:
  253. answer = f"there is not a tool named {tool_call_name}"
  254. return answer, ToolInvokeMeta.error_instance(answer)
  255. if isinstance(tool_call_args, str):
  256. try:
  257. tool_call_args = json.loads(tool_call_args)
  258. except json.JSONDecodeError:
  259. pass
  260. # invoke tool
  261. tool_invoke_response, message_files, tool_invoke_meta = ToolEngine.agent_invoke(
  262. tool=tool_instance,
  263. tool_parameters=tool_call_args,
  264. user_id=self.user_id,
  265. tenant_id=self.tenant_id,
  266. message=self.message,
  267. invoke_from=self.application_generate_entity.invoke_from,
  268. agent_tool_callback=self.agent_callback
  269. )
  270. # publish files
  271. for message_file, save_as in message_files:
  272. if save_as:
  273. self.variables_pool.set_file(tool_name=tool_call_name, value=message_file.id, name=save_as)
  274. # publish message file
  275. self.queue_manager.publish(QueueMessageFileEvent(
  276. message_file_id=message_file.id
  277. ), PublishFrom.APPLICATION_MANAGER)
  278. # add message file ids
  279. message_file_ids.append(message_file.id)
  280. return tool_invoke_response, tool_invoke_meta
  281. def _convert_dict_to_action(self, action: dict) -> AgentScratchpadUnit.Action:
  282. """
  283. convert dict to action
  284. """
  285. return AgentScratchpadUnit.Action(
  286. action_name=action['action'],
  287. action_input=action['action_input']
  288. )
  289. def _fill_in_inputs_from_external_data_tools(self, instruction: str, inputs: dict) -> str:
  290. """
  291. fill in inputs from external data tools
  292. """
  293. for key, value in inputs.items():
  294. try:
  295. instruction = instruction.replace(f'{{{{{key}}}}}', str(value))
  296. except Exception as e:
  297. continue
  298. return instruction
  299. def _init_react_state(self, query) -> None:
  300. """
  301. init agent scratchpad
  302. """
  303. self._query = query
  304. self._agent_scratchpad = []
  305. self._historic_prompt_messages = self._organize_historic_prompt_messages()
  306. @abstractmethod
  307. def _organize_prompt_messages(self) -> list[PromptMessage]:
  308. """
  309. organize prompt messages
  310. """
  311. def _format_assistant_message(self, agent_scratchpad: list[AgentScratchpadUnit]) -> str:
  312. """
  313. format assistant message
  314. """
  315. message = ''
  316. for scratchpad in agent_scratchpad:
  317. if scratchpad.is_final():
  318. message += f"Final Answer: {scratchpad.agent_response}"
  319. else:
  320. message += f"Thought: {scratchpad.thought}\n\n"
  321. if scratchpad.action_str:
  322. message += f"Action: {scratchpad.action_str}\n\n"
  323. if scratchpad.observation:
  324. message += f"Observation: {scratchpad.observation}\n\n"
  325. return message
  326. def _organize_historic_prompt_messages(self) -> list[PromptMessage]:
  327. """
  328. organize historic prompt messages
  329. """
  330. result: list[PromptMessage] = []
  331. scratchpad: list[AgentScratchpadUnit] = []
  332. current_scratchpad: AgentScratchpadUnit = None
  333. for message in self.history_prompt_messages:
  334. if isinstance(message, AssistantPromptMessage):
  335. current_scratchpad = AgentScratchpadUnit(
  336. agent_response=message.content,
  337. thought=message.content or 'I am thinking about how to help you',
  338. action_str='',
  339. action=None,
  340. observation=None,
  341. )
  342. if message.tool_calls:
  343. try:
  344. current_scratchpad.action = AgentScratchpadUnit.Action(
  345. action_name=message.tool_calls[0].function.name,
  346. action_input=json.loads(message.tool_calls[0].function.arguments)
  347. )
  348. current_scratchpad.action_str = json.dumps(
  349. current_scratchpad.action.to_dict()
  350. )
  351. except:
  352. pass
  353. scratchpad.append(current_scratchpad)
  354. elif isinstance(message, ToolPromptMessage):
  355. if current_scratchpad:
  356. current_scratchpad.observation = message.content
  357. elif isinstance(message, UserPromptMessage):
  358. result.append(message)
  359. if scratchpad:
  360. result.append(AssistantPromptMessage(
  361. content=self._format_assistant_message(scratchpad)
  362. ))
  363. scratchpad = []
  364. if scratchpad:
  365. result.append(AssistantPromptMessage(
  366. content=self._format_assistant_message(scratchpad)
  367. ))
  368. return result