cot_agent_runner.py 18 KB

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