-
Notifications
You must be signed in to change notification settings - Fork 291
Handle missing chat template and propagate other errors #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle missing chat template and propagate other errors #288
Conversation
// but that is not public so just fall back to text | ||
// It might be TokenizerError.chatTemplate("No chat template was specified"), | ||
// but that is not public, so just fall back to text | ||
print("Error while applying chat template: \(error.localizedDescription)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This catch was added for the case where the tokenizer didn't have a chat template at all and to give it something to fall back on. At the time TokenizerError
wasn't public so we couldn't catch it, but now it is and we want this catch to only catch this case:
guard let selectedChatTemplate else {
throw TokenizerError.chatTemplate("This tokenizer does not have a chat template, and no template was passed.")
}
What do you think about doing this?
} catch TokenizerError.chatTemplate(let message) {
if message == "This tokenizer does not have a chat template, and no template was passed." {
// fallback case
let prompt =
messages
.compactMap { $0["content"] as? String }
.joined(separator: ". ")
let promptTokens = tokenizer.encode(text: prompt)
return LMInput(tokens: MLXArray(promptTokens))
} else {
throw TokenizerError.chatTemplate(message)
}
}
That way real errors get propagated as error and this fixes #150
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, although it would probably be better to have a dedicated error case for this type of error in swift-transformers and check for that case rather than the exact error message. I'll open a PR for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, a dedicated error case would be ideal.
f52211e
to
f068b4a
Compare
f068b4a
to
4e62ca4
Compare
This is ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great -- thank you for chasing down the dependencies!
The silent error made debugging more difficult.