Repeatedly calls a function until its output matches one of the given patterns or max attempts is reached. Args: function (Callable): Function returning (answer: str, cb_log). pattern_list (Tuple[str]): Patterns to match in the answer. num_attempts (int): Max number
(
function: Callable, pattern_list: Tuple[str], num_attempts: int = 10
)
| 142 | |
| 143 | |
| 144 | def loop_until_match( |
| 145 | function: Callable, pattern_list: Tuple[str], num_attempts: int = 10 |
| 146 | ): |
| 147 | """ |
| 148 | Repeatedly calls a function until its output matches one of the given patterns or max attempts is reached. |
| 149 | |
| 150 | Args: |
| 151 | function (Callable): Function returning (answer: str, cb_log). |
| 152 | pattern_list (Tuple[str]): Patterns to match in the answer. |
| 153 | num_attempts (int): Max number of attempts (default: 10). |
| 154 | |
| 155 | Returns: |
| 156 | Tuple[str, Any]: The matching answer and its corresponding log object. |
| 157 | """ |
| 158 | correct_format = False |
| 159 | for _ in range(num_attempts): |
| 160 | answer, cb_log = function() |
| 161 | |
| 162 | for pattern in pattern_list: |
| 163 | if pattern in answer: |
| 164 | correct_format = True |
| 165 | |
| 166 | if correct_format: |
| 167 | break |
| 168 | |
| 169 | logger.info("Wrong output formatting, retrying...") |
| 170 | |
| 171 | return answer, cb_log |
| 172 | |
| 173 | |
| 174 | def longcepo_init( |
no outgoing calls
no test coverage detected