Skip to content

Commit 4c681c5

Browse files
Handle missing chat template and propagate other errors (#288)
* Update packages * Handle missing chat template
1 parent eb1733c commit 4c681c5

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

Libraries/MLXLLM/LLMModelFactory.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,18 @@ private struct LLMUserInputProcessor: UserInputProcessor {
243243

244244
func prepare(input: UserInput) throws -> LMInput {
245245
let messages = messageGenerator.generate(from: input)
246-
247246
do {
248247
let promptTokens = try tokenizer.applyChatTemplate(
249248
messages: messages, tools: input.tools, additionalContext: input.additionalContext)
250249
return LMInput(tokens: MLXArray(promptTokens))
251-
} catch {
252-
// #150 -- it might be a TokenizerError.chatTemplate("No chat template was specified")
253-
// but that is not public so just fall back to text
250+
} catch TokenizerError.missingChatTemplate {
251+
print(
252+
"No chat template was included or provided, so converting messages to simple text format. This is not optimal for model performance, so applications should provide a chat template if none is included with the model."
253+
)
254254
let prompt =
255255
messages
256256
.compactMap { $0["content"] as? String }
257-
.joined(separator: ". ")
257+
.joined(separator: "\n\n")
258258
let promptTokens = tokenizer.encode(text: prompt)
259259
return LMInput(tokens: MLXArray(promptTokens))
260260
}

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let package = Package(
2929
dependencies: [
3030
.package(url: "https://github.com/ml-explore/mlx-swift", .upToNextMinor(from: "0.21.2")),
3131
.package(
32-
url: "https://github.com/huggingface/swift-transformers", .upToNextMinor(from: "0.1.17")
32+
url: "https://github.com/huggingface/swift-transformers", .upToNextMinor(from: "0.1.20")
3333
),
3434
.package(url: "https://github.com/1024jp/GzipSwift", "6.0.1" ... "6.0.1"), // Only needed by MLXMNIST
3535
],

mlx-swift-examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)