Parameters for chat template. Args: model_name: the name of the deployed model. Determine which chat template will be applied. All the chat template names: ``lmdeploy list`` system: begin of the system prompt. meta_instruction: system prompt. eosys: e
| 33 | |
| 34 | @dataclasses.dataclass |
| 35 | class ChatTemplateConfig: |
| 36 | """Parameters for chat template. |
| 37 | |
| 38 | Args: |
| 39 | model_name: the name of the deployed model. Determine which chat template will be applied. |
| 40 | All the chat template names: ``lmdeploy list`` |
| 41 | system: begin of the system prompt. |
| 42 | meta_instruction: system prompt. |
| 43 | eosys: end of the system prompt. |
| 44 | user: begin of the user prompt. |
| 45 | eoh: end of the user prompt. |
| 46 | assistant: begin of the assistant prompt. |
| 47 | eoa: end of the assistant prompt. |
| 48 | tool: begin of the tool prompt. |
| 49 | eotool: end of the tool prompt. |
| 50 | capability: the capability of the model, one of |
| 51 | ``'completion'``, ``'infilling'``, ``'chat'``, ``'python'``. |
| 52 | Default to None. |
| 53 | stop_words: list of stop words. Default to None. |
| 54 | """ |
| 55 | |
| 56 | model_name: str |
| 57 | model_path: str | None = None |
| 58 | system: str | None = None |
| 59 | meta_instruction: str | None = None |
| 60 | eosys: str | None = None |
| 61 | user: str | None = None |
| 62 | eoh: str | None = None |
| 63 | assistant: str | None = None |
| 64 | eoa: str | None = None |
| 65 | tool: str | None = None |
| 66 | eotool: str | None = None |
| 67 | separator: str | None = None |
| 68 | capability: Literal['completion', 'infilling', 'chat', 'python'] | None = None |
| 69 | stop_words: list[str] | None = None |
| 70 | |
| 71 | def chat_template(self, trust_remote_code: bool = False): |
| 72 | attrs = {key: value for key, value in dataclasses.asdict(self).items() if value is not None} |
| 73 | attrs.pop('model_name', None) |
| 74 | if self.model_name in MODELS.module_dict.keys(): |
| 75 | model = MODELS.get(self.model_name)(**attrs, trust_remote_code=trust_remote_code) |
| 76 | else: |
| 77 | logger.warning(f'Could not find {self.model_name} in registered models. ' |
| 78 | f'Register {self.model_name} using the BaseChatTemplate.') |
| 79 | model = BaseChatTemplate(**attrs, trust_remote_code=trust_remote_code) |
| 80 | return model |
| 81 | |
| 82 | def to_json(self, file_path=None): |
| 83 | """Convert the dataclass instance to a JSON formatted string and |
| 84 | optionally save to a file.""" |
| 85 | json_str = json.dumps(dataclasses.asdict(self), ensure_ascii=False, indent=4) |
| 86 | if file_path: |
| 87 | with open(file_path, 'w', encoding='utf-8') as file: |
| 88 | file.write(json_str) |
| 89 | return json_str |
| 90 | |
| 91 | @classmethod |
| 92 | def from_json(cls, file_or_string): |
no outgoing calls
no test coverage detected