MCPcopy Create free account
hub / github.com/modelscope/modelscope / preprocess

Method preprocess

modelscope/preprocessors/templates/base.py:366–407  ·  view source on GitHub ↗
(self, example)

Source from the content-addressed store, hash-verified

364 example['images'] = images
365
366 def preprocess(self, example):
367 # Duplicate example and create a new one to prepare in-place changes
368 example = example.copy()
369 template_type: Optional[str] = getattr(self, 'template_type', None)
370 tools: Union[List[Any], str] = example.get('tools') or []
371
372 # Template needs to be initialized
373 if not self._is_init:
374 raise ValueError(
375 'Template is not initialized, please use the `get_template` function to obtain the template.')
376
377 messages = example['messages']
378 system_round = [message for message in messages if message['role'] == 'system']
379 messages = [message for message in messages if message['role'] != 'system']
380 # Reset system (by default value and agent tools)
381 system: Optional[str] = system_round[0]['content'] if system_round else ''
382 if not system:
383 if self.use_default_system:
384 system = self.default_system
385 else:
386 assert self.system_prefix is not None, (
387 f'The template does not support `system`, template_type: {template_type}')
388 if tools:
389 if isinstance(tools, str):
390 tools = json.loads(tools)
391 if system is None:
392 system = ''
393 system += get_tools_prompt(tools, self.tools_prompt)
394
395 if system:
396 if not system_round:
397 system_round = [{'role': 'system', 'content': None}]
398 system_round[0]['content'] = system
399
400 if len(messages) > 1:
401 assert self.support_multi_round, (
402 f'The template does not support multi-round chat, template_type: {template_type}')
403 example['messages'] = system_round + messages
404 self._preprocess_media(example)
405 # Check the example that whether matching the very template's rules
406 self.check_example(example)
407 return example
408
409 def encode(self, example: Dict[str, Any], streaming: bool = False, is_training: bool = False, **kwargs) -> Tuple[Dict[str, Any], Dict[str, Any]]:
410 """The entrance method of Template!

Callers 3

encodeMethod · 0.95
__call__Method · 0.45
_encodeMethod · 0.45

Calls 5

_preprocess_mediaMethod · 0.95
check_exampleMethod · 0.95
get_tools_promptFunction · 0.85
copyMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected