Register FunAudioChat chat templates with LLaMA-Factory.
()
| 102 | |
| 103 | |
| 104 | def register_templates() -> None: |
| 105 | """ |
| 106 | Register FunAudioChat chat templates with LLaMA-Factory. |
| 107 | """ |
| 108 | try: |
| 109 | from llamafactory.data.template import register_template |
| 110 | from llamafactory.data.formatter import StringFormatter |
| 111 | from llamafactory.data.mm_plugin import get_mm_plugin |
| 112 | |
| 113 | # Check if templates already exist |
| 114 | from llamafactory.data.template import TEMPLATES |
| 115 | |
| 116 | # Register funaudiochat template |
| 117 | if "funaudiochat" not in TEMPLATES: |
| 118 | try: |
| 119 | register_template( |
| 120 | name="funaudiochat", |
| 121 | format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]), |
| 122 | format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]), |
| 123 | format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]), |
| 124 | default_system="You are a helpful assistant.", |
| 125 | stop_words=["<|im_end|>"], |
| 126 | replace_eos=True, |
| 127 | mm_plugin=get_mm_plugin(name="funaudiochat", audio_token="<|AUDIO|>"), |
| 128 | ) |
| 129 | logger.info("Registered template: funaudiochat") |
| 130 | except ValueError as e: |
| 131 | logger.debug(f"Template funaudiochat already registered: {e}") |
| 132 | |
| 133 | logger.info("Successfully registered all FunAudioChat templates") |
| 134 | |
| 135 | except ImportError as e: |
| 136 | logger.error(f"Failed to import template modules: {e}") |
| 137 | logger.error("Make sure LLaMA-Factory is installed correctly") |
| 138 | raise |
| 139 | except Exception as e: |
| 140 | logger.error(f"Failed to register templates: {e}") |
| 141 | raise |
| 142 | |
| 143 | |
| 144 | def register_all() -> None: |