Generate results given an input. Args: inputs (str or PromptList): A string or PromptDict. The PromptDict should be organized in OpenCompass' API format. max_out_len (int): The maximum length of the output. Returns: str: The gener
(
self,
sess,
input: Union[str, PromptList],
max_out_len: int,
)
| 158 | return results |
| 159 | |
| 160 | def _generate( |
| 161 | self, |
| 162 | sess, |
| 163 | input: Union[str, PromptList], |
| 164 | max_out_len: int, |
| 165 | ) -> str: |
| 166 | """Generate results given an input. |
| 167 | |
| 168 | Args: |
| 169 | inputs (str or PromptList): A string or PromptDict. |
| 170 | The PromptDict should be organized in OpenCompass' API format. |
| 171 | max_out_len (int): The maximum length of the output. |
| 172 | |
| 173 | Returns: |
| 174 | str: The generated string. |
| 175 | """ |
| 176 | if isinstance(input, str): |
| 177 | messages = [{'role': 'user', 'content': input}] |
| 178 | else: |
| 179 | messages = [] |
| 180 | for item in input: |
| 181 | content = item['prompt'] |
| 182 | if not content: |
| 183 | continue |
| 184 | message = {'content': content} |
| 185 | if item['role'] == 'HUMAN': |
| 186 | message['role'] = 'user' |
| 187 | elif item['role'] == 'BOT': |
| 188 | message['role'] = 'assistant' |
| 189 | elif item['role'] == 'SYSTEM': |
| 190 | message['role'] = 'system' |
| 191 | else: |
| 192 | message['role'] = item['role'] |
| 193 | messages.append(message) |
| 194 | request = { |
| 195 | 'model': self._model, |
| 196 | 'messages': messages, |
| 197 | 'max_tokens': max_out_len, |
| 198 | } |
| 199 | request.update(self.generation_kwargs) |
| 200 | retry_num = 0 |
| 201 | while retry_num < self.retry: |
| 202 | try: |
| 203 | response = self._infer_result(request, sess) |
| 204 | except ConnectionError: |
| 205 | time.sleep(random.randint(10, 30)) |
| 206 | retry_num += 1 # retry |
| 207 | continue |
| 208 | if response.status_code == 200: |
| 209 | break # success |
| 210 | elif response.status_code == 426: |
| 211 | retry_num += 1 # retry |
| 212 | elif response.status_code in [302, 429, 500, 504]: |
| 213 | time.sleep(random.randint(10, 30)) |
| 214 | retry_num += 1 # retry |
| 215 | else: |
| 216 | raise ValueError(f'Status code = {response.status_code}') |
| 217 | else: |
nothing calls this directly
no test coverage detected