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

Class Template

modelscope/preprocessors/templates/base.py:99–1041  ·  view source on GitHub ↗

A template class for all supported models. Args: prefix: Prefix tokens before the first turn's prompt prompt: A list of elements whose types are str and list of integers. The input query part of every turn. chat_sep: The chat separators between every turn. suffix

Source from the content-addressed store, hash-verified

97
98
99class Template:
100 """A template class for all supported models.
101
102 Args:
103 prefix: Prefix tokens before the first turn's prompt
104 prompt: A list of elements whose types are str and list of integers. The input query part of every turn.
105 chat_sep: The chat separators between every turn.
106 suffix: The end tokens after the chat finished.
107 default_system: A default system instruction.
108 system_prefix: The prefix if the `system` is not empty.
109 auto_add_bos: By default, the bos_token is not added. The auto_add_bos option will determine
110 whether to add it based on `tokenizer.encode('')`.
111 tools_prompt: The tools prompt name
112 tool_prompt: The tool prompt, usually useful when there is a tool role
113 padding_side: The padding side
114 infer_media_type: The media type supported by the multi-modals
115 Examples:
116 <start_of_output>system\nYou are a helpful assistant!<end_of_output>\n<bos><start_of_output>Who are you?<end_of_output>\n<start_of_output>assistant:I am a robot<end_of_output>\n<start_of_output>Who are you?<end_of_output>\n<start_of_output>assistant:I am a robot<end_of_output> # noqa
117 ----------system------------ ---query---- --response- -----chatsep----- ---query--- --response- ----suffix-----
118 ----------------------------system_prefix---------------------------- ---------------------------- prompt ------------------------------------- ---------------------------- prompt -------------------------------------
119
120 """
121
122 special_tokens = ['<image>', '<video>', '<audio>', '<bbox>', '<ref-object>']
123 special_keys = ['images', 'videos', 'audios', 'objects']
124 grounding_type = 'norm_1000'
125 image_placeholder = ['<image>']
126 load_medias = True
127 compute_per_round_loss = True # for rlhf
128 output_prompt_answer = False # for encoder-decoder & kto
129
130 def __init__(self,
131 prefix: Prompt,
132 prompt: Prompt,
133 chat_sep: Optional[Prompt],
134 suffix: Prompt,
135 default_system: Optional[str] = None,
136 system_prefix: Optional[Prompt] = None,
137 auto_add_bos: bool = False,
138 tools_prompt: str = 'react_en',
139 tool_prompt: Optional[Prompt] = None,
140 padding_side: Literal['left', 'right'] = 'right',
141 infer_media_type: Literal['interleave', 'dialogue', 'round'] = 'interleave') -> None:
142 # check
143 for x in [prefix, prompt, chat_sep, suffix, system_prefix]:
144 assert x is None or isinstance(x, list)
145
146 if default_system == '':
147 default_system = None
148 if self._has_system(prefix):
149 assert system_prefix is None, 'The prefix already contains {{SYSTEM}}.'
150 system_prefix = prefix
151 prefix = self._replace_system(prefix)
152 self.prefix = prefix
153 self.system_prefix = system_prefix
154 if self.system_prefix is None and not any(['{{SYSTEM}}' in context for context in prompt]):
155 assert default_system is None, 'The template does not support `system`.'
156 self.prompt = prompt

Callers 3

push_modelMethod · 0.85
create_templateMethod · 0.85
template.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…