Skip to content

Commit b834891

Browse files
committed
Incorporate feedback from review
1 parent bea4b35 commit b834891

4 files changed

Lines changed: 20 additions & 8 deletions

File tree

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ mlxOptions.kvCache = .init(
502502
)
503503
// Apply a deterministic preprocessing step for image inputs.
504504
mlxOptions.userInputProcessing = .resize(to: CGSize(width: 512, height: 512))
505+
// Inject extra template context consumed by model-specific chat templates.
506+
mlxOptions.additionalContext = [
507+
"user_name": .string("Alice"),
508+
"turn_count": .int(3),
509+
"verbose": .bool(true),
510+
]
505511
options[custom: MLXLanguageModel.self] = mlxOptions
506512
507513
let response = try await session.respond(
@@ -511,11 +517,14 @@ let response = try await session.respond(
511517
```
512518
513519
You can specify `userInputProcessing` to enforce a consistent image
514-
preprocessing step
520+
preprocessing step
515521
(for example, fixed dimensions for predictable latency, memory usage, and vision behavior).
516522
By default, images are passed through without an explicit resize override
517523
(`resize: nil`), so MLX applies its default media processing behavior.
518524
525+
You can also set `additionalContext` to provide extra JSON template variables
526+
for model-specific chat templates.
527+
519528
GPU cache behavior can be configured when creating the model:
520529
521530
```swift

Sources/AnyLanguageModel/Models/MLXLanguageModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ import Foundation
281281
}
282282

283283
/// Additional key-value pairs injected into the chat template rendering context.
284-
public var additionalContext: [String: MLXLMCommon.JSONValue]?
284+
public var additionalContext: [String: AnyLanguageModel.JSONValue]?
285285

286286
var additionalContextForUserInput: [String: any Sendable]? {
287287
additionalContext?.mapValues { $0.toSendable() }
@@ -298,7 +298,7 @@ import Foundation
298298
public init(
299299
kvCache: KVCache,
300300
userInputProcessing: UserInputProcessing?,
301-
additionalContext: [String: MLXLMCommon.JSONValue]?
301+
additionalContext: [String: AnyLanguageModel.JSONValue]?
302302
) {
303303
self.kvCache = kvCache
304304
self.additionalContext = additionalContext
@@ -1882,7 +1882,7 @@ import Foundation
18821882
return sampledToken.item(Int.self)
18831883
}
18841884
}
1885-
extension MLXLMCommon.JSONValue {
1885+
extension AnyLanguageModel.JSONValue {
18861886
/// Recursively converts a `JSONValue` to its primitive Swift equivalent.
18871887
func toSendable() -> any Sendable {
18881888
switch self {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import enum JSONSchema.JSONValue
2+
3+
/// A type-safe representation of JSON values used by AnyLanguageModel APIs.
4+
public typealias JSONValue = JSONSchema.JSONValue

Tests/AnyLanguageModelTests/MLXLanguageModelTests.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import Testing
44
@testable import AnyLanguageModel
55

66
#if MLX
7-
import MLXLMCommon
87
private let shouldRunMLXTests = {
98
// Enable when explicitly requested via environment variable
109
if ProcessInfo.processInfo.environment["ENABLE_MLX_TESTS"] != nil {
@@ -273,9 +272,9 @@ import Testing
273272
)
274273
var custom = MLXLanguageModel.CustomGenerationOptions.default
275274
custom.additionalContext = [
276-
"user_name": MLXLMCommon.JSONValue.string("Alice"),
277-
"turn_count": MLXLMCommon.JSONValue.int(3),
278-
"verbose": MLXLMCommon.JSONValue.bool(true),
275+
"user_name": JSONValue.string("Alice"),
276+
"turn_count": JSONValue.int(3),
277+
"verbose": JSONValue.bool(true),
279278
]
280279
options[custom: MLXLanguageModel.self] = custom
281280

0 commit comments

Comments
 (0)