|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <memory> |
| 12 | +#include <string> |
| 13 | +#include <unordered_map> |
| 14 | +#include <utility> |
| 15 | + |
| 16 | +#include <executorch/extension/llm/runner/jinja_chat_formatter.h> |
| 17 | + |
| 18 | +namespace example { |
| 19 | + |
| 20 | +/** |
| 21 | + * Supported chat template formats for different model families. |
| 22 | + */ |
| 23 | +enum class ChatFormat { |
| 24 | + None, // No formatting (pass-through) |
| 25 | + Llama3, // Llama 3.x Instruct models |
| 26 | + Gemma3, // Gemma 3 Instruct models |
| 27 | + Jinja, // Custom Jinja template from file |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * Abstract base class for chat formatters. |
| 32 | + * Implementations format user prompts into model-specific chat templates. |
| 33 | + */ |
| 34 | +class ChatFormatter { |
| 35 | + public: |
| 36 | + virtual ~ChatFormatter() = default; |
| 37 | + |
| 38 | + /** |
| 39 | + * Format a user prompt into the model's expected chat template. |
| 40 | + * |
| 41 | + * @param prompt The user's input message |
| 42 | + * @param system_prompt Optional system prompt to set model behavior |
| 43 | + * @return Formatted string ready for tokenization |
| 44 | + */ |
| 45 | + virtual std::string format( |
| 46 | + const std::string& prompt, |
| 47 | + const std::string& system_prompt = "") const = 0; |
| 48 | + |
| 49 | + /** |
| 50 | + * Whether this formatter includes BOS token in the template. |
| 51 | + * If true, the runner should not prepend additional BOS tokens. |
| 52 | + */ |
| 53 | + virtual bool includes_bos() const = 0; |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * No formatting (pass-through). |
| 58 | + * Use when the prompt is already formatted or for base models. |
| 59 | + */ |
| 60 | +class NoChatFormatter : public ChatFormatter { |
| 61 | + public: |
| 62 | + std::string format( |
| 63 | + const std::string& prompt, |
| 64 | + const std::string& system_prompt = "") const override { |
| 65 | + (void)system_prompt; // Unused in pass-through mode |
| 66 | + return prompt; |
| 67 | + } |
| 68 | + |
| 69 | + bool includes_bos() const override { |
| 70 | + return false; // User controls BOS token |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +class JinjaChatFormatterAdapter : public ChatFormatter { |
| 75 | + public: |
| 76 | + explicit JinjaChatFormatterAdapter( |
| 77 | + std::unique_ptr<executorch::extension::llm::JinjaChatFormatter> formatter) |
| 78 | + : formatter_(std::move(formatter)) {} |
| 79 | + |
| 80 | + std::string format( |
| 81 | + const std::string& prompt, |
| 82 | + const std::string& system_prompt = "") const override { |
| 83 | + return formatter_->format(prompt, system_prompt); |
| 84 | + } |
| 85 | + |
| 86 | + bool includes_bos() const override { |
| 87 | + return formatter_->includesBos(); |
| 88 | + } |
| 89 | + |
| 90 | + private: |
| 91 | + std::unique_ptr<executorch::extension::llm::JinjaChatFormatter> formatter_; |
| 92 | +}; |
| 93 | + |
| 94 | +/** |
| 95 | + * Parse a chat format string into the corresponding enum value. |
| 96 | + * |
| 97 | + * @param format_str String identifier (e.g., "llama3", "none") |
| 98 | + * @return ChatFormat enum value, defaults to None for unknown formats |
| 99 | + */ |
| 100 | +inline ChatFormat parse_chat_format(const std::string& format_str) { |
| 101 | + static const std::unordered_map<std::string, ChatFormat> format_map = { |
| 102 | + {"none", ChatFormat::None}, |
| 103 | + {"llama3", ChatFormat::Llama3}, |
| 104 | + {"llama3.2", ChatFormat::Llama3}, |
| 105 | + {"llama32", ChatFormat::Llama3}, |
| 106 | + {"llama3_2", ChatFormat::Llama3}, |
| 107 | + {"gemma3", ChatFormat::Gemma3}, |
| 108 | + {"jinja", ChatFormat::Jinja}, |
| 109 | + }; |
| 110 | + auto it = format_map.find(format_str); |
| 111 | + if (it != format_map.end()) { |
| 112 | + return it->second; |
| 113 | + } |
| 114 | + return ChatFormat::None; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Get a human-readable list of supported chat formats. |
| 119 | + */ |
| 120 | +inline std::string get_supported_formats() { |
| 121 | + return "llama3, gemma3, jinja, none"; |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Factory function to create the appropriate ChatFormatter instance. |
| 126 | + * |
| 127 | + * @param format The chat format to use |
| 128 | + * @return Unique pointer to a ChatFormatter instance |
| 129 | + */ |
| 130 | +inline std::unique_ptr<ChatFormatter> create_chat_formatter( |
| 131 | + ChatFormat format, |
| 132 | + const std::string& template_file = "") { |
| 133 | + using executorch::extension::llm::ChatTemplateType; |
| 134 | + using executorch::extension::llm::JinjaChatFormatter; |
| 135 | + |
| 136 | + if (!template_file.empty()) { |
| 137 | + return std::make_unique<JinjaChatFormatterAdapter>( |
| 138 | + JinjaChatFormatter::fromFile(template_file)); |
| 139 | + } |
| 140 | + |
| 141 | + switch (format) { |
| 142 | + case ChatFormat::Llama3: |
| 143 | + return std::make_unique<JinjaChatFormatterAdapter>( |
| 144 | + JinjaChatFormatter::fromTemplate(ChatTemplateType::Llama3)); |
| 145 | + case ChatFormat::Gemma3: |
| 146 | + return std::make_unique<JinjaChatFormatterAdapter>( |
| 147 | + JinjaChatFormatter::fromTemplate(ChatTemplateType::Gemma3)); |
| 148 | + case ChatFormat::Jinja: |
| 149 | + return std::make_unique<NoChatFormatter>(); |
| 150 | + case ChatFormat::None: |
| 151 | + default: |
| 152 | + return std::make_unique<NoChatFormatter>(); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +} // namespace example |
0 commit comments