Skip to content

Commit 81431bc

Browse files
committed
Prefer chat_template.jinja
1 parent 02b828d commit 81431bc

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Sources/Hub/Hub.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public class LanguageModelConfigurationFromHub {
144144
revision: String,
145145
hubApi: HubApi = .shared
146146
) async throws -> Configurations {
147-
let filesToDownload = ["config.json", "tokenizer_config.json", "chat_template.json", "tokenizer.json"]
147+
let filesToDownload = ["config.json", "tokenizer_config.json", "chat_template.jinja", "chat_template.json", "tokenizer.json"]
148148
let repo = Hub.Repo(id: modelName)
149149

150150
do {
@@ -195,11 +195,22 @@ public class LanguageModelConfigurationFromHub {
195195
}
196196

197197
// Check for chat template and merge if available
198-
let chatTemplateURL = modelFolder.appending(path: "chat_template.json")
199-
if FileManager.default.fileExists(atPath: chatTemplateURL.path),
200-
let chatTemplateConfig = try? hubApi.configuration(fileURL: chatTemplateURL),
201-
let chatTemplate = chatTemplateConfig.chatTemplate.string()
198+
// Prefer .jinja template over .json template
199+
var chatTemplate: String? = nil
200+
let chatTemplateJinjaURL = modelFolder.appending(path: "chat_template.jinja")
201+
let chatTemplateJsonURL = modelFolder.appending(path: "chat_template.json")
202+
203+
if FileManager.default.fileExists(atPath: chatTemplateJinjaURL.path) {
204+
// Try to load .jinja template as plain text
205+
chatTemplate = try? String(contentsOf: chatTemplateJinjaURL, encoding: .utf8)
206+
} else if FileManager.default.fileExists(atPath: chatTemplateJsonURL.path),
207+
let chatTemplateConfig = try? hubApi.configuration(fileURL: chatTemplateJsonURL)
202208
{
209+
// Fall back to .json template
210+
chatTemplate = chatTemplateConfig.chatTemplate.string()
211+
}
212+
213+
if let chatTemplate {
203214
// Create or update tokenizer config with chat template
204215
if var configDict = tokenizerConfig?.dictionary() {
205216
configDict["chat_template"] = .init(chatTemplate)

0 commit comments

Comments
 (0)