A chat formatter that uses jinja2 templates to format the prompt.
(
self,
template: str,
eos_token: str,
bos_token: str,
add_generation_prompt: bool = True,
stop_token_ids: Optional[List[int]] = None,
)
| 203 | return parser.parse_statements(("name:endgeneration",), drop_needle=True) |
| 204 | |
| 205 | def __init__( |
| 206 | self, |
| 207 | template: str, |
| 208 | eos_token: str, |
| 209 | bos_token: str, |
| 210 | add_generation_prompt: bool = True, |
| 211 | stop_token_ids: Optional[List[int]] = None, |
| 212 | ): |
| 213 | """A chat formatter that uses jinja2 templates to format the prompt.""" |
| 214 | self.template = template |
| 215 | self.eos_token = eos_token |
| 216 | self.bos_token = bos_token |
| 217 | self.add_generation_prompt = add_generation_prompt |
| 218 | self.stop_token_ids = ( |
| 219 | set(stop_token_ids) if stop_token_ids is not None else None |
| 220 | ) |
| 221 | |
| 222 | environment = ImmutableSandboxedEnvironment( |
| 223 | loader=jinja2.BaseLoader(), |
| 224 | trim_blocks=True, |
| 225 | lstrip_blocks=True, |
| 226 | # Keep this aligned with Transformers' chat-template Jinja extensions. |
| 227 | # https://github.com/huggingface/transformers/blob/39603d0e5cdb6f00e8d473d7fcbb01032d709181/src/transformers/utils/chat_template_utils.py#L489-L490 |
| 228 | extensions=[ |
| 229 | Jinja2ChatFormatter.IgnoreGenerationTags, |
| 230 | jinja2.ext.loopcontrols, |
| 231 | ], |
| 232 | ) |
| 233 | # Match Transformers' chat-template JSON rendering behavior. |
| 234 | # https://github.com/huggingface/transformers/blob/39603d0e5cdb6f00e8d473d7fcbb01032d709181/src/transformers/utils/chat_template_utils.py#L481-L484 |
| 235 | environment.filters["tojson"] = self.tojson |
| 236 | self._environment = environment.from_string(self.template) |
| 237 | |
| 238 | @staticmethod |
| 239 | def strftime_now(f: str) -> str: |
nothing calls this directly
no test coverage detected