Skip to content

Commit ed44ce6

Browse files
committed
Format
1 parent 2859d92 commit ed44ce6

3 files changed

Lines changed: 27 additions & 26 deletions

File tree

Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let package = Package(
1717
.library(name: "TokenizersCore", targets: ["TokenizersCore"]),
1818
// ^ Basic tokenizers without chat template support
1919
.library(name: "Tokenizers", targets: ["Tokenizers"]),
20-
// ^ Full tokenizers with chat template support
20+
// ^ Full tokenizers with chat template support
2121
.library(name: "Transformers", targets: ["Tokenizers", "Generation", "Models"]),
2222
// ^ Everything, including Core ML inference
2323
.executable(name: "transformers", targets: ["TransformersCLI"]),
@@ -30,7 +30,8 @@ let package = Package(
3030
targets: [
3131
.executableTarget(
3232
name: "TransformersCLI",
33-
dependencies: [ "Models", .product(name: "ArgumentParser", package: "swift-argument-parser")]),
33+
dependencies: ["Models", .product(name: "ArgumentParser", package: "swift-argument-parser")]
34+
),
3435
.executableTarget(name: "HubCLI", dependencies: ["Hub", .product(name: "ArgumentParser", package: "swift-argument-parser")]),
3536
.target(name: "Hub", resources: [.process("FallbackConfigs")], swiftSettings: swiftSettings),
3637
.target(name: "TokenizersCore", dependencies: ["Hub"], path: "Sources/Tokenizers"),

Sources/Tokenizers/Tokenizer.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,16 @@ extension Tokenizer {
232232
}
233233
}
234234

235-
extension Tokenizer {
236-
public func applyChatTemplate(messages: [Message]) throws -> [Int] {
235+
public extension Tokenizer {
236+
func applyChatTemplate(messages: [Message]) throws -> [Int] {
237237
try applyChatTemplate(messages: messages, chatTemplate: nil, addGenerationPrompt: true, truncation: false, maxLength: nil, tools: nil)
238238
}
239239

240-
public func applyChatTemplate(messages: [Message], chatTemplate: ChatTemplateArgument) throws -> [Int] {
240+
func applyChatTemplate(messages: [Message], chatTemplate: ChatTemplateArgument) throws -> [Int] {
241241
try applyChatTemplate(messages: messages, chatTemplate: chatTemplate, addGenerationPrompt: true, truncation: false, maxLength: nil, tools: nil)
242242
}
243243

244-
public func applyChatTemplate(messages: [Message], chatTemplate: String) throws -> [Int] {
244+
func applyChatTemplate(messages: [Message], chatTemplate: String) throws -> [Int] {
245245
try applyChatTemplate(messages: messages, chatTemplate: .literal(chatTemplate), addGenerationPrompt: true, truncation: false, maxLength: nil, tools: nil)
246246
}
247247
}
@@ -264,7 +264,7 @@ public extension Tokenizer {
264264
}
265265
}
266266

267-
// open because we have to subclass from `TokenizersTemplates`
267+
/// open because we have to subclass from `TokenizersTemplates`
268268
open class PreTrainedTokenizer: Tokenizer {
269269
let model: TokenizingModel
270270

Sources/TokenizersTemplates/TokenizersTemplates.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import Foundation
12
import Hub
2-
@_exported import TokenizersCore
33
import Jinja
4-
import Foundation
4+
@_exported import TokenizersCore
55

66
let specialTokenAttributes: [String] = [
77
"bos_token",
@@ -11,24 +11,24 @@ let specialTokenAttributes: [String] = [
1111
"pad_token",
1212
"cls_token",
1313
"mask_token",
14-
"additional_special_tokens"
14+
"additional_special_tokens",
1515
]
1616

17-
open class PreTrainedTokenizerWithTemplates : PreTrainedTokenizer {
18-
// I don't know why these need to be here. They are implemented in the protocol, **and** in the superclass.
19-
public override func applyChatTemplate(messages: [Message]) throws -> [Int] {
17+
open class PreTrainedTokenizerWithTemplates: PreTrainedTokenizer {
18+
/// I don't know why these need to be here. They are implemented in the protocol, **and** in the superclass.
19+
override public func applyChatTemplate(messages: [Message]) throws -> [Int] {
2020
try applyChatTemplate(messages: messages, addGenerationPrompt: true)
2121
}
2222

23-
public override func applyChatTemplate(messages: [Message], chatTemplate: ChatTemplateArgument) throws -> [Int] {
23+
override public func applyChatTemplate(messages: [Message], chatTemplate: ChatTemplateArgument) throws -> [Int] {
2424
try applyChatTemplate(messages: messages, chatTemplate: chatTemplate, addGenerationPrompt: true)
2525
}
2626

27-
public override func applyChatTemplate(messages: [Message], chatTemplate: String) throws -> [Int] {
27+
override public func applyChatTemplate(messages: [Message], chatTemplate: String) throws -> [Int] {
2828
try applyChatTemplate(messages: messages, chatTemplate: .literal(chatTemplate), addGenerationPrompt: true)
2929
}
3030

31-
public override func applyChatTemplate(
31+
override public func applyChatTemplate(
3232
messages: [Message],
3333
chatTemplate: ChatTemplateArgument? = nil,
3434
addGenerationPrompt: Bool = false,
@@ -42,18 +42,18 @@ open class PreTrainedTokenizerWithTemplates : PreTrainedTokenizer {
4242
)
4343
}
4444

45-
public override func applyChatTemplate(
45+
override public func applyChatTemplate(
4646
messages: [Message],
4747
chatTemplate: ChatTemplateArgument? = nil,
4848
addGenerationPrompt: Bool = false,
4949
truncation: Bool = false,
5050
maxLength: Int? = nil,
51-
/// A list of tools (callable functions) that will be accessible to the model. If the template does not
52-
/// support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema,
53-
/// giving the name, description and argument types for the tool. See the
54-
/// [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use)
55-
/// for more information.
56-
/// Note: tool calling is not supported yet, it will be available in a future update.
51+
// A list of tools (callable functions) that will be accessible to the model. If the template does not
52+
// support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema,
53+
// giving the name, description and argument types for the tool. See the
54+
// [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use)
55+
// for more information.
56+
// Note: tool calling is not supported yet, it will be available in a future update.
5757
tools: [[String: Any]]? = nil,
5858
additionalContext: [String: Any]? = nil
5959
) throws -> [Int] {
@@ -143,7 +143,7 @@ open class PreTrainedTokenizerWithTemplates : PreTrainedTokenizer {
143143
}
144144
}
145145

146-
// Template-enabled tokenizer classes
146+
/// Template-enabled tokenizer classes
147147
/// See https://github.com/xenova/transformers.js/blob/1a9964fb09b8f54fcbeac46dc6aae8d76795809d/src/tokenizers.js#L3203 for these exceptions
148148
class LlamaPreTrainedTokenizerWithTemplates: PreTrainedTokenizerWithTemplates {
149149
let isLegacy: Bool
@@ -174,8 +174,8 @@ struct PreTrainedTokenizerTemplateClasses {
174174
]
175175
}
176176

177-
// Override AutoTokenizer to use template-enabled tokenizers
178-
// See Sources/Tokenizers/Tokenizer.swift
177+
/// Override AutoTokenizer to use template-enabled tokenizers
178+
/// See Sources/Tokenizers/Tokenizer.swift
179179
public extension AutoTokenizer {
180180
private static func tokenizerClassWithTemplates(for tokenizerConfig: Config) -> PreTrainedTokenizerWithTemplates.Type {
181181
guard let tokenizerClassName = tokenizerConfig.tokenizerClass?.string() else {

0 commit comments

Comments
 (0)