Update src/txagent/txagent.py
Browse files43 unmodified lines4445464748495015 unmodified lines666768697071726 unmodified lines798081828384858619 unmodified lines10610710810911011111251 unmodified lines1641651661671681691701711721731741751761771781791801812 unmodified lines18418518618718818919013 unmodified lines20420520620720820921059 unmodified lines270271272273274275276175 unmodified lines452453454455456457458202 unmodified lines6616626636646656666676 unmodified lines67467567667767867968043 unmodified linesself.rag_model_name = rag_model_nameself.tool_files_dict = tool_files_dictself.model = Noneself.rag_model = ToolRAGModel(rag_model_name) if enable_rag else None # MODIFIED: Skip RAG model if disabledself.tooluniverse = Noneself.prompt_multi_step = "You are a helpful assistant that will solve problems through detailed, step-by-step reasoning."self.self_prompt = "Strictly follow the instruction."15 unmodified linesdef init_model(self):self.load_models()if self.enable_rag: # MODIFIED: Only load tools if RAG enabledself.load_tooluniverse()self.load_tool_desc_embedding()6 unmodified linesif model_name == self.model_name:return f"The model {model_name} is already loaded."self.model_name = model_nameself.model = LLM(model=self.model_name, enforce_eager=True) # MODIFIED: Force no torch.compileself.chat_template = Template(self.model.get_tokenizer().chat_template)self.tokenizer = self.model.get_tokenizer()return f"Model {model_name} loaded successfully."19 unmodified linesdef initialize_tools_prompt(self, call_agent, call_agent_level, message):picked_tools_prompt = []if not self.enable_rag: # MODIFIED: No tools if RAG disabledreturn picked_tools_prompt, call_agent_levelpicked_tools_prompt = self.add_special_tools(picked_tools_prompt, call_agent=call_agent)51 unmodified linesreturn picked_tools_promptdef add_special_tools(self, tools, call_agent=False):if not self.enable_rag and not self.enable_finish: # MODIFIED: No tools if RAG disabledreturn toolsif self.enable_finish:tools.append(self.tooluniverse.get_one_tool_by_one_name('Finish', return_prompt=True))logger.info("Finish tool is added")if call_agent:tools.append(self.tooluniverse.get_one_tool_by_one_name('CallAgent', return_prompt=True))logger.info("CallAgent tool is added")elif self.enable_rag:tools.append(self.tooluniverse.get_one_tool_by_one_name('Tool_RAG', return_prompt=True))logger.info("Tool_RAG tool is added")if self.additional_default_tools is not None:for each_tool_name in self.additional_default_tools:tool_prompt = self.tooluniverse.get_one_tool_by_one_name(each_tool_name, return_prompt=True)if tool_prompt is not None:2 unmodified linesreturn toolsdef add_finish_tools(self, tools):if not self.enable_finish:return toolstools.append(self.tooluniverse.get_one_tool_by_one_name('Finish', return_prompt=True))logger.info("Finish tool is added")13 unmodified linescall_agent=False,call_agent_level=None,temperature=None):if not self.enable_rag: # MODIFIED: Skip function calls if RAG disabledreturn [{"role": "assistant", "content": fcall_str.strip()}], existing_tools_prompt or [], ''function_call_json, message = self.tooluniverse.extract_function_call_json(fcall_str, return_message=return_message, verbose=False)call_results = []59 unmodified linescall_agent_level=None,temperature=None,return_gradio_history=True):if not self.enable_rag: # MODIFIED: Skip function calls if RAG disabledgradio_history = [] if return_gradio_history else Nonereturn [{"role": "assistant", "content": fcall_str.strip()}], existing_tools_prompt or [], '', gradio_historyfunction_call_json, message = self.tooluniverse.extract_function_call_json(fcall_str, return_message=return_message, verbose=False)175 unmodified linesif len(assistant_messages) == 2:breakforbidden_ids = [tokenizer.encode(msg, add_special_tokens=False) for msg in assistant_messages]return [NoRepeatSentenceProcessor(forbidden_ids, 3)] # MODIFIED: Stricter penaltyreturn Nonedef llm_infer(self, messages, temperature=0.1, tools=None,202 unmodified linescall_agent, call_agent_level, message)conversation = self.initialize_conversation(message, conversation=conversation, history=history)history = []next_round = Truefunction_call_messages = []current_round = 06 unmodified lineswhile next_round and current_round < max_round:current_round += 1logger.debug(f"Round {current_round}, conversation length: {len(conversation)}")if last_outputs and self.enable_rag: # MODIFIED: Skip function calls if RAG disabledfunction_call_messages, picked_tools_prompt, special_tool_call, current_gradio_history = yield from self.run_function_call_stream(last_outputs, return_message=True,existing_tools_prompt=picked_tools_prompt,43 unmodified lines4445464748495015 unmodified lines666768697071726 unmodified lines7980818283848519 unmodified lines10510610710810911011151 unmodified lines1631641651661671681691701711721731741751761771781791802 unmodified lines18318418518618718818913 unmodified lines20320420520620720820959 unmodified lines269270271272273274275175 unmodified lines451452453454455456457202 unmodified lines6606616626636646656666 unmodified lines67367467567667767867943 unmodified linesself.rag_model_name = rag_model_nameself.tool_files_dict = tool_files_dictself.model = Noneself.rag_model = ToolRAGModel(rag_model_name) if enable_rag else Noneself.tooluniverse = Noneself.prompt_multi_step = "You are a helpful assistant that will solve problems through detailed, step-by-step reasoning."self.self_prompt = "Strictly follow the instruction."15 unmodified linesdef init_model(self):self.load_models()if self.enable_rag:self.load_tooluniverse()self.load_tool_desc_embedding()6 unmodified linesif model_name == self.model_name:return f"The model {model_name} is already loaded."self.model_name = model_nameself.model = LLM(model=self.model_name, enforce_eager=True)self.chat_template = Template(self.model.get_tokenizer().chat_template)self.tokenizer = self.model.get_tokenizer()return f"Model {model_name} loaded successfully."19 unmodified linesdef initialize_tools_prompt(self, call_agent, call_agent_level, message):picked_tools_prompt = []if not self.enable_rag:return picked_tools_prompt, call_agent_levelpicked_tools_prompt = self.add_special_tools(picked_tools_prompt, call_agent=call_agent)51 unmodified linesreturn picked_tools_promptdef add_special_tools(self, tools, call_agent=False):if not self.enable_rag and not self.enable_finish:return toolsif self.enable_finish and self.tooluniverse: # MODIFIED: Check tooluniversetools.append(self.tooluniverse.get_one_tool_by_one_name('Finish', return_prompt=True))logger.info("Finish tool is added")if call_agent and self.tooluniverse: # MODIFIED: Check tooluniversetools.append(self.tooluniverse.get_one_tool_by_one_name('CallAgent', return_prompt=True))logger.info("CallAgent tool is added")elif self.enable_rag and self.tooluniverse: # MODIFIED: Check tooluniversetools.append(self.tooluniverse.get_one_tool_by_one_name('Tool_RAG', return_prompt=True))logger.info("Tool_RAG tool is added")if self.additional_default_tools is not None and self.tooluniverse: # MODIFIED: Check tooluniversefor each_tool_name in self.additional_default_tools:tool_prompt = self.tooluniverse.get_one_tool_by_one_name(each_tool_name, return_prompt=True)if tool_prompt is not None:2 unmodified linesreturn toolsdef add_finish_tools(self, tools):if not self.enable_finish or not self.tooluniverse: # MODIFIED: Check tooluniversereturn toolstools.append(self.tooluniverse.get_one_tool_by_one_name('Finish', return_prompt=True))logger.info("Finish tool is added")13 unmodified linescall_agent=False,call_agent_level=None,temperature=None):if not self.enable_rag:return [{"role": "assistant", "content": fcall_str.strip()}], existing_tools_prompt or [], ''function_call_json, message = self.tooluniverse.extract_function_call_json(fcall_str, return_message=return_message, verbose=False)call_results = []59 unmodified linescall_agent_level=None,temperature=None,return_gradio_history=True):if not self.enable_rag:gradio_history = [] if return_gradio_history else Nonereturn [{"role": "assistant", "content": fcall_str.strip()}], existing_tools_prompt or [], '', gradio_historyfunction_call_json, message = self.tooluniverse.extract_function_call_json(fcall_str, return_message=return_message, verbose=False)175 unmodified linesif len(assistant_messages) == 2:breakforbidden_ids = [tokenizer.encode(msg, add_special_tokens=False) for msg in assistant_messages]return [NoRepeatSentenceProcessor(forbidden_ids, 3)]return Nonedef llm_infer(self, messages, temperature=0.1, tools=None,202 unmodified linescall_agent, call_agent_level, message)conversation = self.initialize_conversation(message, conversation=conversation, history=history)history = [] if not history else history # MODIFIED: Simplify historynext_round = Truefunction_call_messages = []current_round = 06 unmodified lineswhile next_round and current_round < max_round:current_round += 1logger.debug(f"Round {current_round}, conversation length: {len(conversation)}")if last_outputs and self.enable_rag:function_call_messages, picked_tools_prompt, special_tool_call, current_gradio_history = yield from self.run_function_call_stream(last_outputs, return_message=True,existing_tools_prompt=picked_tools_prompt,
src/txagent/txagent.py+15-16