Returns the number of grids to go back. When a child node of a node generates a final answer or give up, it should go back a few more grids In a sense, the larger this value is, the more diverse it is, and it is GreedySearch@n when it is enlarged to infinity.
(self, now_node, single_chain_max_step, tree_beam_size, max_query_count, answer, with_filter=True)
| 118 | return self.DFS(self.tree.root, single_chain_max_step, tree_beam_size, max_query_count, answer, with_filter) |
| 119 | |
| 120 | def DFS(self, now_node, single_chain_max_step, tree_beam_size, max_query_count, answer, with_filter=True): |
| 121 | """Returns the number of grids to go back. When a child node of a node generates a final answer or give up, it should go back a few more grids |
| 122 | In a sense, the larger this value is, the more diverse it is, and it is GreedySearch@n when it is enlarged to infinity. |
| 123 | """ |
| 124 | |
| 125 | # this two value declares the rate to go back, Algo degrades to CoT when the value=Inf |
| 126 | final_answer_back_length = 2 |
| 127 | prune_back_length = 2 |
| 128 | |
| 129 | now_node.expand_num = self.now_expand_num |
| 130 | self.now_expand_num += 1 |
| 131 | if now_node.get_depth() >= single_chain_max_step or now_node.pruned or now_node.is_terminal: |
| 132 | if now_node.is_terminal: # final answer |
| 133 | self.status = 1 |
| 134 | self.terminal_node.append(now_node) |
| 135 | return final_answer_back_length |
| 136 | else: |
| 137 | now_node.pruned = True |
| 138 | if now_node.observation_code == 4: |
| 139 | self.give_up_node.append(now_node) |
| 140 | return prune_back_length |
| 141 | else: |
| 142 | return 1 |
| 143 | |
| 144 | next_tree_split_nodes = [] |
| 145 | for i in range(tree_beam_size): |
| 146 | temp_now_node = now_node |
| 147 | |
| 148 | """If a node have children now, We will prompt the model to generate different nodes than all the existing nodes""" |
| 149 | delete_former_diversity_message = False |
| 150 | diversity_message = None |
| 151 | if len(temp_now_node.children) > 0: |
| 152 | |
| 153 | former_candidates_des = "" |
| 154 | js_list = [] |
| 155 | for k, child in enumerate(temp_now_node.children): |
| 156 | temp_node = child |
| 157 | while not temp_node.is_terminal and temp_node.node_type != "Action Input" and len(temp_node.children) > 0: |
| 158 | temp_node = temp_node.children[0] |
| 159 | if temp_node.node_type == "Action Input": |
| 160 | obj_dict = { |
| 161 | "name": temp_node.father.description, |
| 162 | "arguments": temp_node.description, |
| 163 | "function_output": temp_node.observation, |
| 164 | "mento-carlo-action-value": temp_node.compute_weight(), |
| 165 | } |
| 166 | js_list.append(obj_dict) |
| 167 | |
| 168 | if len(js_list) > 0: |
| 169 | former_candidates_des = former_candidates_des + \ |
| 170 | f"{json.dumps(js_list,indent=2)}\n" |
| 171 | if temp_now_node.observation != "": |
| 172 | former_candidates_des = former_candidates_des + \ |
| 173 | f"again, your former observation: {temp_now_node.observation}\n" |
| 174 | diverse_prompt = DIVERSITY_PROMPT |
| 175 | diverse_prompt = diverse_prompt.replace( |
| 176 | "{previous_candidate}", former_candidates_des) |
| 177 | diversity_message = { |
no test coverage detected