Intermidate prompt template parser, specifically for API models. Args: meta_template (Dict): The meta template for the model.
| 162 | |
| 163 | |
| 164 | class APITemplateParser: |
| 165 | """Intermidate prompt template parser, specifically for API models. |
| 166 | |
| 167 | Args: |
| 168 | meta_template (Dict): The meta template for the model. |
| 169 | """ |
| 170 | |
| 171 | def __init__(self, meta_template: Optional[Dict] = None): |
| 172 | self.meta_template = meta_template |
| 173 | # Check meta template |
| 174 | if meta_template: |
| 175 | assert 'round' in meta_template, 'round is required in meta' \ |
| 176 | ' template' |
| 177 | assert isinstance(meta_template['round'], list) |
| 178 | keys_to_check = ['round'] |
| 179 | |
| 180 | if 'reserved_roles' in meta_template: |
| 181 | assert isinstance(meta_template['reserved_roles'], list) |
| 182 | keys_to_check.append('reserved_roles') |
| 183 | |
| 184 | self.roles: Dict[str, dict] = dict() # maps role name to config |
| 185 | for meta_key in keys_to_check: |
| 186 | for item in meta_template[meta_key]: |
| 187 | assert isinstance(item, (str, dict)) |
| 188 | if isinstance(item, dict): |
| 189 | assert item['role'] not in self.roles, \ |
| 190 | 'role in meta prompt must be unique!' |
| 191 | self.roles[item['role']] = item.copy() |
| 192 | |
| 193 | def parse_template(self, prompt_template: PromptType, |
| 194 | mode: str) -> PromptType: |
| 195 | """Parse the intermidate prompt template, and wrap it with meta |
| 196 | template if applicable. When the meta template is set and the input is |
| 197 | a PromptList, the return value will be a PromptList containing the full |
| 198 | conversation history. Each item looks like: |
| 199 | |
| 200 | .. code-block:: python |
| 201 | |
| 202 | {'role': 'user', 'prompt': '...'}). |
| 203 | |
| 204 | Args: |
| 205 | prompt_template (List[PromptType]): An intermidate prompt |
| 206 | template (potentially before being wrapped by meta template). |
| 207 | mode (str): Parsing mode. Choices are 'ppl' and 'gen'. |
| 208 | |
| 209 | Returns: |
| 210 | List[PromptType]: The finalized prompt or a conversation. |
| 211 | """ |
| 212 | assert isinstance(prompt_template, (str, list, PromptList, tuple)) |
| 213 | |
| 214 | if not isinstance(prompt_template, (str, PromptList)): |
| 215 | return [self.parse_template(p, mode=mode) for p in prompt_template] |
| 216 | |
| 217 | assert mode in ['ppl', 'gen'] |
| 218 | if isinstance(prompt_template, str): |
| 219 | return prompt_template |
| 220 | if self.meta_template: |
| 221 |
no outgoing calls