MCPcopy Create free account
hub / github.com/InternScience/SciReason / LMTemplateParser

Class LMTemplateParser

opencompass/models/base.py:265–515  ·  view source on GitHub ↗

Intermidate prompt template parser, specifically for language models. Args: meta_template (Dict): The meta template for the model.

Source from the content-addressed store, hash-verified

263
264
265class LMTemplateParser:
266 """Intermidate prompt template parser, specifically for language models.
267
268 Args:
269 meta_template (Dict): The meta template for the model.
270 """
271
272 def __init__(self, meta_template: Optional[Dict] = None):
273 self.meta_template = meta_template
274 if meta_template:
275 assert 'round' in meta_template, 'round is required in meta' \
276 ' template'
277 assert isinstance(meta_template['round'], list)
278 keys_to_check = ['round']
279
280 if 'reserved_roles' in meta_template:
281 assert isinstance(meta_template['reserved_roles'], list)
282 keys_to_check.append('reserved_roles')
283
284 self.roles: Dict[str, dict] = dict() # maps role name to config
285 for meta_key in keys_to_check:
286 for item in meta_template[meta_key]:
287 assert isinstance(item, (str, dict))
288 if isinstance(item, dict):
289 assert item['role'] not in self.roles, \
290 'role in meta prompt must be unique!'
291 self.roles[item['role']] = item.copy()
292 # convert list of string and int into a raw string
293 # for the ease of future prompt processing
294 for key in ['begin', 'end']:
295 value = self.roles[item['role']].get(key, '')
296 if isinstance(value, list):
297 self.roles[item['role']][
298 key] = self._encode_speical_tokens(value)
299
300 def parse_template(self, prompt_template: PromptType, mode: str) -> str:
301 """Parse a prompt template, and wrap it with meta template if
302 applicable.
303
304 Args:
305 prompt_template (List[PromptType]): A prompt
306 template (potentially before being wrapped by meta template).
307 mode (str): Parsing mode. Choices are 'ppl' and 'gen'.
308
309 Returns:
310 str: The final string.
311 """
312 assert isinstance(prompt_template, (str, list, PromptList, tuple))
313 if not isinstance(prompt_template, (str, PromptList)):
314 return [self.parse_template(p, mode=mode) for p in prompt_template]
315
316 assert mode in ['ppl', 'gen']
317 if isinstance(prompt_template, str):
318 return prompt_template
319 if self.meta_template:
320
321 prompt = ''
322 # Whether to keep generating the prompt

Callers 11

_load_model_predMethod · 0.90
_process_predictionsMethod · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
setUpMethod · 0.90
__init__Method · 0.70
__init__Method · 0.70

Calls

no outgoing calls