Generate results given an input. Args: input (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: T
(
self,
input: PromptType,
max_out_len: int = 512,
)
| 81 | return results |
| 82 | |
| 83 | def _generate( |
| 84 | self, |
| 85 | input: PromptType, |
| 86 | max_out_len: int = 512, |
| 87 | ) -> str: |
| 88 | """Generate results given an input. |
| 89 | |
| 90 | Args: |
| 91 | input (PromptType): A string or PromptDict. |
| 92 | The PromptDict should be organized in OpenCompass' |
| 93 | API format. |
| 94 | max_out_len (int): The maximum length of the output. |
| 95 | |
| 96 | Returns: |
| 97 | str: The generated string. |
| 98 | """ |
| 99 | assert isinstance(input, (str, PromptList)) |
| 100 | |
| 101 | if isinstance(input, str): |
| 102 | messages = [{'role': 'user', 'content': input}] |
| 103 | else: |
| 104 | messages = [] |
| 105 | msg_buffer, last_role = [], None |
| 106 | for item in input: |
| 107 | item['role'] = 'assistant' if item['role'] == 'BOT' else 'user' |
| 108 | if item['role'] != last_role and last_role is not None: |
| 109 | messages.append({ |
| 110 | 'content': '\n'.join(msg_buffer), |
| 111 | 'role': last_role |
| 112 | }) |
| 113 | msg_buffer = [] |
| 114 | msg_buffer.append(item['prompt']) |
| 115 | last_role = item['role'] |
| 116 | messages.append({ |
| 117 | 'content': '\n'.join(msg_buffer), |
| 118 | 'role': last_role |
| 119 | }) |
| 120 | |
| 121 | data = { |
| 122 | 'model': self.model, |
| 123 | 'messages': messages, |
| 124 | } |
| 125 | data.update(self.generation_kwargs) |
| 126 | |
| 127 | max_num_retries = 0 |
| 128 | while max_num_retries < self.retry: |
| 129 | self.acquire() |
| 130 | try: |
| 131 | raw_response = requests.request('POST', |
| 132 | url=self.url, |
| 133 | headers=self.headers, |
| 134 | json=data) |
| 135 | except Exception as err: |
| 136 | print('Request Error:{}'.format(err)) |
| 137 | time.sleep(2) |
| 138 | continue |
| 139 | |
| 140 | response = raw_response.json() |