| 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														from models.model import Conversation, Message | 
														 | 
														 | 
														from models.model import Conversation, Message | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														logger = logging.getLogger(__name__) | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														class AssistantCotApplicationRunner(BaseAssistantApplicationRunner): | 
														 | 
														 | 
														class AssistantCotApplicationRunner(BaseAssistantApplicationRunner): | 
													
													
												
													
														 | 
														 | 
														    def run(self, model_instance: ModelInstance, | 
														 | 
														 | 
														    def run(self, model_instance: ModelInstance, | 
													
													
												
													
														 | 
														 | 
														        conversation: Conversation, | 
														 | 
														 | 
														        conversation: Conversation, | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														                prompt_messages_tools = [] | 
														 | 
														 | 
														                prompt_messages_tools = [] | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														            message_file_ids = [] | 
														 | 
														 | 
														            message_file_ids = [] | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														            agent_thought = self.create_agent_thought( | 
														 | 
														 | 
														            agent_thought = self.create_agent_thought( | 
													
													
												
													
														 | 
														 | 
														                message_id=message.id, | 
														 | 
														 | 
														                message_id=message.id, | 
													
													
												
													
														 | 
														 | 
														                message='', | 
														 | 
														 | 
														                message='', | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														                tool_input='', | 
														 | 
														 | 
														                tool_input='', | 
													
													
												
													
														 | 
														 | 
														                messages_ids=message_file_ids | 
														 | 
														 | 
														                messages_ids=message_file_ids | 
													
													
												
													
														 | 
														 | 
														            ) | 
														 | 
														 | 
														            ) | 
													
													
												
													
														 | 
														 | 
														            self.queue_manager.publish_agent_thought(agent_thought, PublishFrom.APPLICATION_MANAGER) | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														            if iteration_step > 1: | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														                self.queue_manager.publish_agent_thought(agent_thought, PublishFrom.APPLICATION_MANAGER) | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														            # update prompt messages | 
														 | 
														 | 
														            # update prompt messages | 
													
													
												
													
														 | 
														 | 
														            prompt_messages = self._originze_cot_prompt_messages( | 
														 | 
														 | 
														            prompt_messages = self._originze_cot_prompt_messages( | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														            # get llm usage | 
														 | 
														 | 
														            # get llm usage | 
													
													
												
													
														 | 
														 | 
														            if llm_result.usage: | 
														 | 
														 | 
														            if llm_result.usage: | 
													
													
												
													
														 | 
														 | 
														                increse_usage(llm_usage, llm_result.usage) | 
														 | 
														 | 
														                increse_usage(llm_usage, llm_result.usage) | 
													
													
												
													
														 | 
														 | 
														                 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														             | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														            # publish agent thought if it's first iteration | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														            if iteration_step == 1: | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														                self.queue_manager.publish_agent_thought(agent_thought, PublishFrom.APPLICATION_MANAGER) | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														            self.save_agent_thought(agent_thought=agent_thought, | 
														 | 
														 | 
														            self.save_agent_thought(agent_thought=agent_thought, | 
													
													
												
													
														 | 
														 | 
														                                    tool_name=scratchpad.action.action_name if scratchpad.action else '', | 
														 | 
														 | 
														                                    tool_name=scratchpad.action.action_name if scratchpad.action else '', | 
													
													
												
													
														 | 
														 | 
														                                    tool_input=scratchpad.action.action_input if scratchpad.action else '', | 
														 | 
														 | 
														                                    tool_input=scratchpad.action.action_input if scratchpad.action else '', | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														                    tool_call_args = scratchpad.action.action_input | 
														 | 
														 | 
														                    tool_call_args = scratchpad.action.action_input | 
													
													
												
													
														 | 
														 | 
														                    tool_instance = tool_instances.get(tool_call_name) | 
														 | 
														 | 
														                    tool_instance = tool_instances.get(tool_call_name) | 
													
													
												
													
														 | 
														 | 
														                    if not tool_instance: | 
														 | 
														 | 
														                    if not tool_instance: | 
													
													
												
													
														 | 
														 | 
														                        logger.error(f"failed to find tool instance: {tool_call_name}") | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														                        answer = f"there is not a tool named {tool_call_name}" | 
														 | 
														 | 
														                        answer = f"there is not a tool named {tool_call_name}" | 
													
													
												
													
														 | 
														 | 
														                        self.save_agent_thought(agent_thought=agent_thought,  | 
														 | 
														 | 
														                        self.save_agent_thought(agent_thought=agent_thought,  | 
													
													
												
													
														 | 
														 | 
														                                                tool_name='', | 
														 | 
														 | 
														                                                tool_name='', | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														                        if error_response: | 
														 | 
														 | 
														                        if error_response: | 
													
													
												
													
														 | 
														 | 
														                            observation = error_response | 
														 | 
														 | 
														                            observation = error_response | 
													
													
												
													
														 | 
														 | 
														                            logger.error(error_response) | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														                        else: | 
														 | 
														 | 
														                        else: | 
													
													
												
													
														 | 
														 | 
														                            observation = self._convert_tool_response_to_str(tool_response) | 
														 | 
														 | 
														                            observation = self._convert_tool_response_to_str(tool_response) | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
											
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														            # add assistant message | 
														 | 
														 | 
														            # add assistant message | 
													
													
												
													
														 | 
														 | 
														            if len(agent_scratchpad) > 0: | 
														 | 
														 | 
														            if len(agent_scratchpad) > 0: | 
													
													
												
													
														 | 
														 | 
														                prompt_messages.append(AssistantPromptMessage( | 
														 | 
														 | 
														                prompt_messages.append(AssistantPromptMessage( | 
													
													
												
													
														 | 
														 | 
														                    content=(agent_scratchpad[-1].thought or '') + "\n" + (agent_scratchpad[-1].observation or '') | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														                    content=(agent_scratchpad[-1].thought or '') | 
													
													
												
													
														 | 
														 | 
														                )) | 
														 | 
														 | 
														                )) | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														             | 
													
													
												
													
														 | 
														 | 
														            # add user message | 
														 | 
														 | 
														            # add user message | 
													
													
												
													
														 | 
														 | 
														            if len(agent_scratchpad) > 0: | 
														 | 
														 | 
														            if len(agent_scratchpad) > 0: | 
													
													
												
													
														 | 
														 | 
														                prompt_messages.append(UserPromptMessage( | 
														 | 
														 | 
														                prompt_messages.append(UserPromptMessage( | 
													
													
												
													
														 | 
														 | 
														                    content=input, | 
														 | 
														 | 
														 | 
													
													
												
													
														 | 
														 | 
														 | 
														 | 
														 | 
														                    content=(agent_scratchpad[-1].observation or ''), | 
													
													
												
													
														 | 
														 | 
														                )) | 
														 | 
														 | 
														                )) | 
													
													
												
													
														 | 
														 | 
														
  | 
														 | 
														 | 
														
  | 
													
													
												
													
														 | 
														 | 
														            return prompt_messages | 
														 | 
														 | 
														            return prompt_messages |