Step-level Beam Search
| 29 | |
| 30 | |
| 31 | class SBSREACT(REACT): |
| 32 | """ |
| 33 | Step-level Beam Search |
| 34 | """ |
| 35 | |
| 36 | current_top_num: int = 1 |
| 37 | current_nodes: List[Type[BaseNode]] = [] |
| 38 | final_answer_nodes: List[Type[BaseNode]] = [] |
| 39 | candidate_nodes: List[Type[BaseNode]] = [] |
| 40 | |
| 41 | def __init__(self, **kwargs) -> None: |
| 42 | super().__init__(**kwargs) |
| 43 | |
| 44 | self.candidate_nodes.append(self.current_node) |
| 45 | self.current_top_num = self.config.step_beam_width |
| 46 | self.select_next_step() |
| 47 | |
| 48 | @field_validator("config") |
| 49 | def validate_config(cls, cfg: Any): |
| 50 | BaseTree.validate_config(cfg) |
| 51 | if not cfg.mode == "sbs": |
| 52 | raise ValueError(f"Wrong value for config mode, must be react") |
| 53 | if not cfg.n_generate_sample >= 1: |
| 54 | raise ValueError(f"Wrong value for config n_generate_sample, must be greater than 1") |
| 55 | if cfg.stop is None: |
| 56 | raise ValueError(f"Wrong value for config stop, cannot be None") |
| 57 | return cfg |
| 58 | |
| 59 | def create_llm(self) -> Callable[[...], List[str]]: |
| 60 | # we only implement the batch inference |
| 61 | pass |
| 62 | |
| 63 | def is_ignored_node(self, node: Type[BaseNode]) -> bool: |
| 64 | return node.is_terminal or node.depth > self.config.max_depth |
| 65 | |
| 66 | def should_generate_next(self) -> bool: |
| 67 | need_generate = False |
| 68 | for step_node in self.current_nodes: |
| 69 | if not self.is_ignored_node(step_node): |
| 70 | need_generate = True |
| 71 | break |
| 72 | return need_generate |
| 73 | |
| 74 | def create_prompt( |
| 75 | self, |
| 76 | is_value_only: bool = False, |
| 77 | ) -> str: |
| 78 | """ |
| 79 | if is_value_only, the prompt is used to produce value estimate. |
| 80 | """ |
| 81 | prompts = [] |
| 82 | current_nodes = self.candidate_nodes if is_value_only else self.current_nodes |
| 83 | for current_node in current_nodes: |
| 84 | if not is_value_only and self.is_ignored_node(current_node): |
| 85 | continue |
| 86 | partial_solution = self.collect_partial_solution(current_node) |
| 87 | prompt = self.prompt_wrap( |
| 88 | self.question, |
nothing calls this directly
no outgoing calls
no test coverage detected