Qwen models support ReAct (Reasoning and Acting) prompting, enabling them to use external tools through a thought-action-observation loop. This allows the model to break down complex tasks, call appropriate tools, and reason about the results.
Question: [User's question]Thought: [Model's reasoning about what to do]Action: [Tool to call]Action Input: [Arguments for the tool]Observation: [Result from the tool]... (repeat as needed)Thought: I now know the final answerFinal Answer: [Model's final response]
TOOL_DESC = """{name_for_model}: Call this tool to interact with the {name_for_human} API. What is the {name_for_human} API useful for? {description_for_model} Parameters: {parameters}"""REACT_PROMPT = """Answer the following questions as best you can. You have access to the following tools:{tools_text}Use the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [{tools_name_text}]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can be repeated zero or more times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question: {query}"""
Configure stop words to halt generation at “Observation:“:
def text_completion(input_text: str, stop_words) -> str: """Generate text with stop words.""" im_end = '<|im_end|>' if im_end not in stop_words: stop_words = stop_words + [im_end] stop_words_ids = [tokenizer.encode(w) for w in stop_words] input_ids = torch.tensor([tokenizer.encode(input_text)]).to(model.device) output = model.generate(input_ids, stop_words_ids=stop_words_ids) output = output.tolist()[0] output = tokenizer.decode(output, errors="ignore") # Remove input and special tokens output = output[len(input_text):].replace('<|endoftext|>', '').replace(im_end, '') # Trim at stop words for stop_str in stop_words: idx = output.find(stop_str) if idx != -1: output = output[:idx + len(stop_str)] return output
User: 搜索一下谁是周杰伦Qwen: Thought: 我应该使用Google搜索查找相关信息。Action: google_searchAction Input: {"search_query": "周杰伦"}Observation: Jay Chou is a Taiwanese singer, songwriter, record producer...Thought: I now know the final answer.Final Answer: 周杰伦(Jay Chou)是一位来自台湾的歌手、词曲创作人、音乐制作人...
# Set top-p for balanced outputmodel.generation_config.top_p = 0.5# Or use greedy decoding for maximum accuracymodel.generation_config.do_sample = Falsemodel.generation_config.top_k = 1