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