- Move PhoneClaw/ contents to project root (no more nesting) - Delete PhoneClawMac/ (nested SPM package, duplicated iOS code) - Delete LLM/LocalLLMService.swift (dead MediaPipe implementation) - Delete llm_inference_engine.h + PhoneClaw-Bridging-Header.h (unused) - Delete Sources/ (empty dir), phoneclaw.code-workspace, TECH_PATH_ANALYSIS.md, project.yml - Fix project.pbxproj: update INFOPLIST_FILE, CODE_SIGN_ENTITLEMENTS paths - Fix project.pbxproj: remove SWIFT_OBJC_BRIDGING_HEADER setting - Fix project.pbxproj: remove LocalLLMService.swift file reference - Fix project.pbxproj: PhoneClaw group now uses name= instead of path=
56 lines
1.7 KiB
Swift
56 lines
1.7 KiB
Swift
import Foundation
|
|
import MLXLMCommon
|
|
import Tokenizers
|
|
|
|
/// Bridges `swift-tokenizers` to `MLXLMCommon.Tokenizer`.
|
|
struct MLXTokenizerBridge: MLXLMCommon.Tokenizer {
|
|
private let upstream: any Tokenizers.Tokenizer
|
|
|
|
init(_ upstream: any Tokenizers.Tokenizer) {
|
|
self.upstream = upstream
|
|
}
|
|
|
|
func encode(text: String, addSpecialTokens: Bool) -> [Int] {
|
|
upstream.encode(text: text, addSpecialTokens: addSpecialTokens)
|
|
}
|
|
|
|
func decode(tokenIds: [Int], skipSpecialTokens: Bool) -> String {
|
|
upstream.decode(tokenIds: tokenIds, skipSpecialTokens: skipSpecialTokens)
|
|
}
|
|
|
|
func convertTokenToId(_ token: String) -> Int? {
|
|
upstream.convertTokenToId(token)
|
|
}
|
|
|
|
func convertIdToToken(_ id: Int) -> String? {
|
|
upstream.convertIdToToken(id)
|
|
}
|
|
|
|
var bosToken: String? { upstream.bosToken }
|
|
var eosToken: String? { upstream.eosToken }
|
|
var unknownToken: String? { upstream.unknownToken }
|
|
|
|
func applyChatTemplate(
|
|
messages: [[String: any Sendable]],
|
|
tools: [[String: any Sendable]]?,
|
|
additionalContext: [String: any Sendable]?
|
|
) throws -> [Int] {
|
|
do {
|
|
return try upstream.applyChatTemplate(
|
|
messages: messages,
|
|
tools: tools,
|
|
additionalContext: additionalContext
|
|
)
|
|
} catch let error as Tokenizers.TokenizerError where error == .missingChatTemplate {
|
|
throw MLXLMCommon.TokenizerError.missingChatTemplate
|
|
}
|
|
}
|
|
}
|
|
|
|
struct MLXTokenizersLoader: TokenizerLoader {
|
|
func load(from directory: URL) async throws -> any MLXLMCommon.Tokenizer {
|
|
let upstream = try await AutoTokenizer.from(directory: directory)
|
|
return MLXTokenizerBridge(upstream)
|
|
}
|
|
}
|