cot_agent_runner.py 17 KB

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