Add additionalContext support to MLXLanguageModel#145
Add additionalContext support to MLXLanguageModel#145noorbhatia wants to merge 2 commits intomattt:mainfrom
additionalContext support to MLXLanguageModel#145Conversation
|
Hi @mattt , can you take a look at this. Thanks! |
| let additionalContext: [String: any Sendable]? = options[custom: MLXLanguageModel.self] | ||
| .flatMap { $0.additionalContext } | ||
| .map { $0.mapValues { $0.toSendable() } } |
There was a problem hiding this comment.
This logic for converting from MLXLMCommon.JSONValue to Any values for UserInput is repeated a few times, so it'd probably make sense to move it into CustomGenerationOptions itself.
| public struct CustomGenerationOptions: AnyLanguageModel.CustomGenerationOptions { | ||
| /// Additional key-value pairs injected into the chat template rendering context. | ||
| public var additionalContext: [String: MLXLMCommon.JSONValue]? | ||
|
|
There was a problem hiding this comment.
Reminder to myself that we should add a parameter for UserInput.Processing in a follow-up.
df19229 to
d27310d
Compare
There was a problem hiding this comment.
Pull request overview
Adds support for passing an additionalContext dictionary through GenerationOptions into MLXLMCommon’s chat-template rendering context, enabling callers to inject custom variables for Jinja2-based templates.
Changes:
- Extends
MLXLanguageModel.CustomGenerationOptionswithadditionalContextand threads it intoMLXLMCommon.UserInputconstruction paths. - Adds a basic MLX test that exercises setting
additionalContextviaGenerationOptions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
Sources/AnyLanguageModel/Models/MLXLanguageModel.swift |
Introduces additionalContext custom option and forwards it into MLX user input creation for standard, streaming, and structured generation flows. |
Tests/AnyLanguageModelTests/MLXLanguageModelTests.swift |
Adds a regression test ensuring additionalContext can be set and a response is produced. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /// Additional key-value pairs injected into the chat template rendering context. | ||
| public var additionalContext: [String: MLXLMCommon.JSONValue]? | ||
|
|
||
| var additionalContextForUserInput: [String: any Sendable]? { | ||
| additionalContext?.mapValues { $0.toSendable() } | ||
| } |
There was a problem hiding this comment.
additionalContext is exposed as [String: MLXLMCommon.JSONValue]?, which leaks the MLXLMCommon dependency into AnyLanguageModel’s public API. Other models’ custom options use the package-wide JSONValue (e.g., Anthropic’s extraBody: [String: JSONValue]?), so consider changing this to [String: JSONValue]? (or a typealias) and converting internally before building MLXLMCommon.UserInput.
| case .int(let i): return i | ||
| case .double(let d): return d | ||
| case .bool(let b): return b | ||
| case .null: return NSNull() |
There was a problem hiding this comment.
MLXLMCommon.JSONValue.toSendable() maps .null to NSNull(), while elsewhere in this file nulls are represented as MLXLMCommon.JSONValue.null when converting JSON for Sendable contexts. Mixing null representations can lead to inconsistent template rendering and may also interact poorly with strict Sendable checking; prefer a single null representation (ideally MLXLMCommon.JSONValue.null if additionalContext ultimately expects Sendable values).
| case .null: return NSNull() | |
| case .null: return MLXLMCommon.JSONValue.null |
| let userInput = MLXLMCommon.UserInput( | ||
| chat: chat, | ||
| processing: .init(resize: .init(width: 512, height: 512)), | ||
| tools: toolSpecs | ||
| tools: toolSpecs, | ||
| additionalContext: additionalContext, | ||
| ) |
There was a problem hiding this comment.
makeUserInput(...) now appears unused, and MLXLMCommon.UserInput construction (including processing defaults) is duplicated in respond, streamResponse, and structured generation. To avoid drift and keep future changes (like additionalContext) consistent, consider updating makeUserInput to accept additionalContext and reusing it in these call sites, or removing the helper if you prefer inline construction.
Add
CustomGenerationOptionsstruct toMLXLanguageModelwith anadditionalContext: [String: JSONValue]?property for injecting variables into Jinja2 chat templatesUsage