assistant_fc_runner.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. import json
  2. import logging
  3. from typing import Union, Generator, Dict, Any, Tuple, List
  4. from core.model_runtime.entities.message_entities import PromptMessage, UserPromptMessage,\
  5. SystemPromptMessage, AssistantPromptMessage, ToolPromptMessage, PromptMessageTool
  6. from core.model_runtime.entities.llm_entities import LLMResultChunk, LLMResult, LLMUsage
  7. from core.model_manager import ModelInstance
  8. from core.application_queue_manager import PublishFrom
  9. from core.tools.errors import ToolInvokeError, ToolNotFoundError, \
  10. ToolNotSupportedError, ToolProviderNotFoundError, ToolParamterValidationError, \
  11. ToolProviderCredentialValidationError
  12. from core.features.assistant_base_runner import BaseAssistantApplicationRunner
  13. from models.model import Conversation, Message, MessageAgentThought
  14. logger = logging.getLogger(__name__)
  15. class AssistantFunctionCallApplicationRunner(BaseAssistantApplicationRunner):
  16. def run(self, model_instance: ModelInstance,
  17. conversation: Conversation,
  18. message: Message,
  19. query: str,
  20. ) -> Generator[LLMResultChunk, None, None]:
  21. """
  22. Run FunctionCall agent application
  23. """
  24. app_orchestration_config = self.app_orchestration_config
  25. prompt_template = self.app_orchestration_config.prompt_template.simple_prompt_template or ''
  26. prompt_messages = self.history_prompt_messages
  27. prompt_messages = self.organize_prompt_messages(
  28. prompt_template=prompt_template,
  29. query=query,
  30. prompt_messages=prompt_messages
  31. )
  32. # convert tools into ModelRuntime Tool format
  33. prompt_messages_tools: List[PromptMessageTool] = []
  34. tool_instances = {}
  35. for tool in self.app_orchestration_config.agent.tools if self.app_orchestration_config.agent else []:
  36. try:
  37. prompt_tool, tool_entity = self._convert_tool_to_prompt_message_tool(tool)
  38. except Exception:
  39. # api tool may be deleted
  40. continue
  41. # save tool entity
  42. tool_instances[tool.tool_name] = tool_entity
  43. # save prompt tool
  44. prompt_messages_tools.append(prompt_tool)
  45. # convert dataset tools into ModelRuntime Tool format
  46. for dataset_tool in self.dataset_tools:
  47. prompt_tool = self._convert_dataset_retriever_tool_to_prompt_message_tool(dataset_tool)
  48. # save prompt tool
  49. prompt_messages_tools.append(prompt_tool)
  50. # save tool entity
  51. tool_instances[dataset_tool.identity.name] = dataset_tool
  52. iteration_step = 1
  53. max_iteration_steps = min(app_orchestration_config.agent.max_iteration, 5) + 1
  54. # continue to run until there is not any tool call
  55. function_call_state = True
  56. agent_thoughts: List[MessageAgentThought] = []
  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. while function_call_state and iteration_step <= max_iteration_steps:
  71. function_call_state = False
  72. if iteration_step == max_iteration_steps:
  73. # the last iteration, remove all tools
  74. prompt_messages_tools = []
  75. message_file_ids = []
  76. agent_thought = self.create_agent_thought(
  77. message_id=message.id,
  78. message='',
  79. tool_name='',
  80. tool_input='',
  81. messages_ids=message_file_ids
  82. )
  83. self.queue_manager.publish_agent_thought(agent_thought, PublishFrom.APPLICATION_MANAGER)
  84. # recale llm max tokens
  85. self.recale_llm_max_tokens(self.model_config, prompt_messages)
  86. # invoke model
  87. chunks: Generator[LLMResultChunk, None, None] = model_instance.invoke_llm(
  88. prompt_messages=prompt_messages,
  89. model_parameters=app_orchestration_config.model_config.parameters,
  90. tools=prompt_messages_tools,
  91. stop=app_orchestration_config.model_config.stop,
  92. stream=True,
  93. user=self.user_id,
  94. callbacks=[],
  95. )
  96. tool_calls: List[Tuple[str, str, Dict[str, Any]]] = []
  97. # save full response
  98. response = ''
  99. # save tool call names and inputs
  100. tool_call_names = ''
  101. tool_call_inputs = ''
  102. current_llm_usage = None
  103. for chunk in chunks:
  104. # check if there is any tool call
  105. if self.check_tool_calls(chunk):
  106. function_call_state = True
  107. tool_calls.extend(self.extract_tool_calls(chunk))
  108. tool_call_names = ';'.join([tool_call[1] for tool_call in tool_calls])
  109. try:
  110. tool_call_inputs = json.dumps({
  111. tool_call[1]: tool_call[2] for tool_call in tool_calls
  112. }, ensure_ascii=False)
  113. except json.JSONDecodeError as e:
  114. # ensure ascii to avoid encoding error
  115. tool_call_inputs = json.dumps({
  116. tool_call[1]: tool_call[2] for tool_call in tool_calls
  117. })
  118. if chunk.delta.message and chunk.delta.message.content:
  119. if isinstance(chunk.delta.message.content, list):
  120. for content in chunk.delta.message.content:
  121. response += content.data
  122. else:
  123. response += chunk.delta.message.content
  124. if chunk.delta.usage:
  125. increase_usage(llm_usage, chunk.delta.usage)
  126. current_llm_usage = chunk.delta.usage
  127. yield chunk
  128. # save thought
  129. self.save_agent_thought(
  130. agent_thought=agent_thought,
  131. tool_name=tool_call_names,
  132. tool_input=tool_call_inputs,
  133. thought=response,
  134. observation=None,
  135. answer=response,
  136. messages_ids=[],
  137. llm_usage=current_llm_usage
  138. )
  139. self.queue_manager.publish_agent_thought(agent_thought, PublishFrom.APPLICATION_MANAGER)
  140. final_answer += response + '\n'
  141. # call tools
  142. tool_responses = []
  143. for tool_call_id, tool_call_name, tool_call_args in tool_calls:
  144. tool_instance = tool_instances.get(tool_call_name)
  145. if not tool_instance:
  146. tool_response = {
  147. "tool_call_id": tool_call_id,
  148. "tool_call_name": tool_call_name,
  149. "tool_response": f"there is not a tool named {tool_call_name}"
  150. }
  151. tool_responses.append(tool_response)
  152. else:
  153. # invoke tool
  154. error_response = None
  155. try:
  156. tool_invoke_message = tool_instance.invoke(
  157. user_id=self.user_id,
  158. tool_paramters=tool_call_args,
  159. )
  160. # transform tool invoke message to get LLM friendly message
  161. tool_invoke_message = self.transform_tool_invoke_messages(tool_invoke_message)
  162. # extract binary data from tool invoke message
  163. binary_files = self.extract_tool_response_binary(tool_invoke_message)
  164. # create message file
  165. message_files = self.create_message_files(binary_files)
  166. # publish files
  167. for message_file, save_as in message_files:
  168. if save_as:
  169. self.variables_pool.set_file(tool_name=tool_call_name, value=message_file.id, name=save_as)
  170. # publish message file
  171. self.queue_manager.publish_message_file(message_file, PublishFrom.APPLICATION_MANAGER)
  172. # add message file ids
  173. message_file_ids.append(message_file.id)
  174. except ToolProviderCredentialValidationError as e:
  175. error_response = f"Plese check your tool provider credentials"
  176. except (
  177. ToolNotFoundError, ToolNotSupportedError, ToolProviderNotFoundError
  178. ) as e:
  179. error_response = f"there is not a tool named {tool_call_name}"
  180. except (
  181. ToolParamterValidationError
  182. ) as e:
  183. error_response = f"tool paramters validation error: {e}, please check your tool paramters"
  184. except ToolInvokeError as e:
  185. error_response = f"tool invoke error: {e}"
  186. except Exception as e:
  187. error_response = f"unknown error: {e}"
  188. if error_response:
  189. observation = error_response
  190. tool_response = {
  191. "tool_call_id": tool_call_id,
  192. "tool_call_name": tool_call_name,
  193. "tool_response": error_response
  194. }
  195. tool_responses.append(tool_response)
  196. else:
  197. observation = self._convert_tool_response_to_str(tool_invoke_message)
  198. tool_response = {
  199. "tool_call_id": tool_call_id,
  200. "tool_call_name": tool_call_name,
  201. "tool_response": observation
  202. }
  203. tool_responses.append(tool_response)
  204. prompt_messages = self.organize_prompt_messages(
  205. prompt_template=prompt_template,
  206. query=None,
  207. tool_call_id=tool_call_id,
  208. tool_call_name=tool_call_name,
  209. tool_response=tool_response['tool_response'],
  210. prompt_messages=prompt_messages,
  211. )
  212. if len(tool_responses) > 0:
  213. # save agent thought
  214. self.save_agent_thought(
  215. agent_thought=agent_thought,
  216. tool_name=None,
  217. tool_input=None,
  218. thought=None,
  219. observation=tool_response['tool_response'],
  220. answer=None,
  221. messages_ids=message_file_ids
  222. )
  223. self.queue_manager.publish_agent_thought(agent_thought, PublishFrom.APPLICATION_MANAGER)
  224. # update prompt messages
  225. if response.strip():
  226. prompt_messages.append(AssistantPromptMessage(
  227. content=response,
  228. ))
  229. # update prompt tool
  230. for prompt_tool in prompt_messages_tools:
  231. self.update_prompt_message_tool(tool_instances[prompt_tool.name], prompt_tool)
  232. iteration_step += 1
  233. self.update_db_variables(self.variables_pool, self.db_variables_pool)
  234. # publish end event
  235. self.queue_manager.publish_message_end(LLMResult(
  236. model=model_instance.model,
  237. prompt_messages=prompt_messages,
  238. message=AssistantPromptMessage(
  239. content=final_answer,
  240. ),
  241. usage=llm_usage['usage'],
  242. system_fingerprint=''
  243. ), PublishFrom.APPLICATION_MANAGER)
  244. def check_tool_calls(self, llm_result_chunk: LLMResultChunk) -> bool:
  245. """
  246. Check if there is any tool call in llm result chunk
  247. """
  248. if llm_result_chunk.delta.message.tool_calls:
  249. return True
  250. return False
  251. def extract_tool_calls(self, llm_result_chunk: LLMResultChunk) -> Union[None, List[Tuple[str, str, Dict[str, Any]]]]:
  252. """
  253. Extract tool calls from llm result chunk
  254. Returns:
  255. List[Tuple[str, str, Dict[str, Any]]]: [(tool_call_id, tool_call_name, tool_call_args)]
  256. """
  257. tool_calls = []
  258. for prompt_message in llm_result_chunk.delta.message.tool_calls:
  259. tool_calls.append((
  260. prompt_message.id,
  261. prompt_message.function.name,
  262. json.loads(prompt_message.function.arguments),
  263. ))
  264. return tool_calls
  265. def organize_prompt_messages(self, prompt_template: str,
  266. query: str = None,
  267. tool_call_id: str = None, tool_call_name: str = None, tool_response: str = None,
  268. prompt_messages: list[PromptMessage] = None
  269. ) -> list[PromptMessage]:
  270. """
  271. Organize prompt messages
  272. """
  273. if not prompt_messages:
  274. prompt_messages = [
  275. SystemPromptMessage(content=prompt_template),
  276. UserPromptMessage(content=query),
  277. ]
  278. else:
  279. if tool_response:
  280. prompt_messages = prompt_messages.copy()
  281. prompt_messages.append(
  282. ToolPromptMessage(
  283. content=tool_response,
  284. tool_call_id=tool_call_id,
  285. name=tool_call_name,
  286. )
  287. )
  288. return prompt_messages