|
| 1 | +import Foundation |
| 2 | +import struct Hub.Config |
| 3 | +import MLX |
| 4 | +import MLXLLM |
| 5 | +import MLXLMCommon |
| 6 | +import Tokenizers |
| 7 | + |
| 8 | +public final class LLM { |
| 9 | + private let directory: URL |
| 10 | + private let context: ModelContext |
| 11 | + private let configuration: ModelConfiguration |
| 12 | + |
| 13 | + static func cohere(directory: URL) async throws -> LLM { |
| 14 | + try await Self( |
| 15 | + directory: directory, |
| 16 | + modelInit: CohereModel.init |
| 17 | + ) |
| 18 | + } |
| 19 | + |
| 20 | + static func gemma(directory: URL) async throws -> LLM { |
| 21 | + try await Self( |
| 22 | + directory: directory, |
| 23 | + modelInit: GemmaModel.init |
| 24 | + ) |
| 25 | + } |
| 26 | + |
| 27 | + static func gemma2(directory: URL) async throws -> LLM { |
| 28 | + try await Self( |
| 29 | + directory: directory, |
| 30 | + modelInit: Gemma2Model.init |
| 31 | + ) |
| 32 | + } |
| 33 | + |
| 34 | + static func internLM2(directory: URL) async throws -> LLM { |
| 35 | + try await Self( |
| 36 | + directory: directory, |
| 37 | + modelInit: InternLM2Model.init |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + static func llama(directory: URL) async throws -> LLM { |
| 42 | + try await Self( |
| 43 | + directory: directory, |
| 44 | + modelInit: LlamaModel.init |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + static func openELM(directory: URL) async throws -> LLM { |
| 49 | + try await Self( |
| 50 | + directory: directory, |
| 51 | + modelInit: OpenELMModel.init |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + static func phi(directory: URL) async throws -> LLM { |
| 56 | + try await Self( |
| 57 | + directory: directory, |
| 58 | + modelInit: PhiModel.init |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + static func phi3(directory: URL) async throws -> LLM { |
| 63 | + try await Self( |
| 64 | + directory: directory, |
| 65 | + modelInit: Phi3Model.init |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + static func phiMoE(directory: URL) async throws -> LLM { |
| 70 | + try await Self( |
| 71 | + directory: directory, |
| 72 | + modelInit: PhiMoEModel.init |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + static func qwen2(directory: URL) async throws -> LLM { |
| 77 | + try await Self( |
| 78 | + directory: directory, |
| 79 | + modelInit: Qwen2Model.init |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + static func smolLM(directory: URL) async throws -> LLM { |
| 84 | + try await Self( |
| 85 | + directory: directory, |
| 86 | + modelInit: LlamaModel.init |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + static func starcoder2(directory: URL) async throws -> LLM { |
| 91 | + try await Self( |
| 92 | + directory: directory, |
| 93 | + modelInit: Starcoder2Model.init |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + private init<Configuration: Decodable>( |
| 98 | + directory: URL, |
| 99 | + modelInit: (Configuration) -> some LanguageModel |
| 100 | + ) async throws { |
| 101 | + self.directory = directory |
| 102 | + let decoder = JSONDecoder() |
| 103 | + |
| 104 | + let config = try Data( |
| 105 | + contentsOf: directory.appending( |
| 106 | + path: "config.json", |
| 107 | + directoryHint: .notDirectory |
| 108 | + ) |
| 109 | + ) |
| 110 | + |
| 111 | + let baseConfig = try decoder.decode( |
| 112 | + BaseConfiguration.self, |
| 113 | + from: config |
| 114 | + ) |
| 115 | + |
| 116 | + let modelConfig = try decoder.decode( |
| 117 | + Configuration.self, |
| 118 | + from: config |
| 119 | + ) |
| 120 | + let model = modelInit(modelConfig) |
| 121 | + |
| 122 | + try loadWeights( |
| 123 | + modelDirectory: directory, |
| 124 | + model: model, |
| 125 | + quantization: baseConfig.quantization |
| 126 | + ) |
| 127 | + |
| 128 | + guard let tokenizerConfigJSON = try JSONSerialization.jsonObject( |
| 129 | + with: try Data(contentsOf: directory.appending( |
| 130 | + path: "tokenizer_config.json", |
| 131 | + directoryHint: .notDirectory |
| 132 | + )) |
| 133 | + ) as? [NSString: Any] else { |
| 134 | + throw SHLLMError.invalidOrMissingConfig( |
| 135 | + "tokenizer_config.json" |
| 136 | + ) |
| 137 | + } |
| 138 | + |
| 139 | + let tokenizerConfig = Config(tokenizerConfigJSON) |
| 140 | + |
| 141 | + guard let tokenizerDataJSON = try JSONSerialization.jsonObject( |
| 142 | + with: try Data(contentsOf: directory.appending( |
| 143 | + path: "tokenizer.json", |
| 144 | + directoryHint: .notDirectory |
| 145 | + )) |
| 146 | + ) as? [NSString: Any] else { |
| 147 | + throw SHLLMError.invalidOrMissingConfig( |
| 148 | + "tokenizer.json" |
| 149 | + ) |
| 150 | + } |
| 151 | + |
| 152 | + let tokenizerData = Config(tokenizerDataJSON) |
| 153 | + |
| 154 | + let tokenizer = try PreTrainedTokenizer( |
| 155 | + tokenizerConfig: tokenizerConfig, |
| 156 | + tokenizerData: tokenizerData |
| 157 | + ) |
| 158 | + |
| 159 | + configuration = ModelConfiguration( |
| 160 | + directory: directory, |
| 161 | + overrideTokenizer: nil, |
| 162 | + defaultPrompt: "You are a helpful assistant." |
| 163 | + ) |
| 164 | + |
| 165 | + context = ModelContext( |
| 166 | + configuration: configuration, |
| 167 | + model: model, |
| 168 | + processor: LLMUserInputProcessor( |
| 169 | + tokenizer: tokenizer, |
| 170 | + configuration: configuration |
| 171 | + ), |
| 172 | + tokenizer: tokenizer |
| 173 | + ) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +extension LLM { |
| 178 | + func request( |
| 179 | + _ input: UserInput, |
| 180 | + maxTokenCount: Int = 1024 * 1024 |
| 181 | + ) async throws -> String { |
| 182 | + let input = try await context.processor.prepare(input: input) |
| 183 | + |
| 184 | + let result = try MLXLMCommon.generate( |
| 185 | + input: input, |
| 186 | + parameters: .init(), |
| 187 | + context: context |
| 188 | + ) { tokens in |
| 189 | + if tokens.count >= maxTokenCount { |
| 190 | + .stop |
| 191 | + } else { |
| 192 | + .more |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + return result.output |
| 197 | + } |
| 198 | +} |
0 commit comments