Generate results given an input. Args: inputs (PromptType): 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:
(
self,
input: PromptType,
max_out_len: int = 512,
)
| 254 | return results |
| 255 | |
| 256 | def _generate( |
| 257 | self, |
| 258 | input: PromptType, |
| 259 | max_out_len: int = 512, |
| 260 | ) -> str: |
| 261 | """Generate results given an input. |
| 262 | |
| 263 | Args: |
| 264 | inputs (PromptType): A string or PromptDict. |
| 265 | The PromptDict should be organized in OpenCompass' |
| 266 | API format. |
| 267 | max_out_len (int): The maximum length of the output. |
| 268 | |
| 269 | Returns: |
| 270 | str: The generated string. |
| 271 | """ |
| 272 | assert isinstance(input, (str, PromptList)) |
| 273 | |
| 274 | if isinstance(input, str): |
| 275 | messages = [{'role': 'user', 'content': input}] |
| 276 | else: |
| 277 | messages = [] |
| 278 | msg_buffer, last_role = [], None |
| 279 | for item in input: |
| 280 | item['role'] = 'assistant' if item['role'] == 'BOT' else 'user' |
| 281 | if item['role'] != last_role and last_role is not None: |
| 282 | messages.append({ |
| 283 | 'content': '\n'.join(msg_buffer), |
| 284 | 'role': last_role |
| 285 | }) |
| 286 | msg_buffer = [] |
| 287 | msg_buffer.append(item['prompt']) |
| 288 | last_role = item['role'] |
| 289 | messages.append({ |
| 290 | 'content': '\n'.join(msg_buffer), |
| 291 | 'role': last_role |
| 292 | }) |
| 293 | |
| 294 | data = {'messages': messages} |
| 295 | |
| 296 | max_num_retries = 0 |
| 297 | while max_num_retries < self.retry: |
| 298 | self.acquire() |
| 299 | try: |
| 300 | raw_response = requests.request('POST', |
| 301 | url=self.url, |
| 302 | json=data) |
| 303 | except Exception as err: |
| 304 | print('Request Error:{}'.format(err)) |
| 305 | time.sleep(2) |
| 306 | continue |
| 307 | |
| 308 | try: |
| 309 | response = raw_response.json() |
| 310 | except Exception as err: |
| 311 | print('Response Error:{}'.format(err)) |
| 312 | response = None |
| 313 | self.release() |