cot_agent_runner.py 18 KB

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