| 43 | |
| 44 | |
| 45 | class BaseTree(BaseModel): |
| 46 | |
| 47 | config: Any |
| 48 | question: str |
| 49 | |
| 50 | ground_truth: Optional[Union[str, List[str]]] = None |
| 51 | |
| 52 | llm_model_id: str = None |
| 53 | llm: Any = None |
| 54 | |
| 55 | root: Optional[Type[BaseNode]] = None |
| 56 | current_node: Optional[Type[BaseNode]] = None |
| 57 | |
| 58 | stop: Optional[List[str]] = None |
| 59 | |
| 60 | node_max_retry: int = 5 |
| 61 | |
| 62 | def __init__(self, **kwargs) -> None: |
| 63 | super().__init__(**kwargs) |
| 64 | |
| 65 | self.llm_model_id = self.config.model_dir |
| 66 | |
| 67 | if self.config.stop: |
| 68 | # omegaconf.listconfig.ListConfig -> list |
| 69 | self.stop = OmegaConf.to_object(self.config.stop) |
| 70 | |
| 71 | self.root = self.create_root() |
| 72 | self.current_node = self.root |
| 73 | |
| 74 | if self.config.verbose and self.question: |
| 75 | print(colored(f"Question: {self.question}\n", QUESTION_COLOR)) |
| 76 | |
| 77 | if self.config.create_local_llm: |
| 78 | self.llm = self.create_llm() |
| 79 | |
| 80 | @field_validator("config") |
| 81 | def validate_config(cls, cfg: Any): |
| 82 | if issubclass(type(cfg), DictConfig): |
| 83 | if not os.path.exists(cfg.model_dir): |
| 84 | raise ValueError(f"Model directory \"{cfg.model_dir}\" cannot be found.") |
| 85 | return cfg |
| 86 | |
| 87 | raise TypeError("Wrong type for `config`, must be subclass of BaseConfig") |
| 88 | |
| 89 | def create_root(self) -> Type[BaseNode]: |
| 90 | root = self.create_node() |
| 91 | root.state["extra_info"] = f"question: {self.question}" |
| 92 | return root |
| 93 | |
| 94 | @abstractmethod |
| 95 | def create_node(self, parent: Optinal[Type[BaseNode]] = None) -> Type[BaseNode]: |
| 96 | """ |
| 97 | subclass must implement |
| 98 | """ |
| 99 | |
| 100 | @abstractmethod |
| 101 | def create_llm(self) -> Callable[[...], List[str]]: |
| 102 | """ |
nothing calls this directly
no outgoing calls
no test coverage detected