Skip to content

Commit 870a89b

Browse files
authored
Add support for multiple images and videos in response methods (#393)
* Add support for multiple images and videos in response methods - Updated `respond` and `streamResponse` methods to accept `[UserInput.Image]?` and `[UserInput.Video]?` arrays. - Added overloads to maintain backward compatibility with existing single `image` and `video` parameters. - Simplified internal logic using default empty arrays for nil values, improving code readability and flexibility. * Fix mismatched input types in `respond` method - Changed parameters from `UserInput.Image?` and `UserInput.Video?` to `[UserInput.Image]?` and `[UserInput.Video]?`. - Previously, the method expected arrays internally but accepted single optional inputs, leading to a mismatch. - This fix ensures consistency and prepares for cleaner multi-input support in follow-up refactors. * Fix missing `try await` in `respond` overload - Corrected the call to the main async `respond` method by adding `try await`. - Prevents async runtime errors and ensures the method correctly propagates asynchronous execution and error handling. * Enforce non-optional arrays for media inputs - Changed `images` and `videos` parameters in `respond` and `streamResponse` methods to be non-optional. - Removed internal fallback to empty arrays (`?? []`), shifting responsibility to the caller. - Updated doc comments to reflect that these inputs are now required as arrays. - Clarifies the method contract and simplifies internal logic by eliminating conditional handling.
1 parent 314dbab commit 870a89b

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

Libraries/MLXLMCommon/Streamlined.swift

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,26 @@ public class ChatSession {
179179
self.generator = .init(
180180
model: .context(model), instructions: instructions, processing: processing,
181181
generateParameters: generateParameters)
182-
}
183-
182+
}
183+
184184
/// Produces a response to a prompt.
185185
///
186186
/// - Parameters:
187187
/// - prompt: the prompt
188-
/// - image: optional image (for use with VLMs)
189-
/// - video: optional video (for use with VLMs)
188+
/// - images: list of image (for use with VLMs)
189+
/// - videos: list of video (for use with VLMs)
190190
/// - Returns: response from the model
191191
public func respond(
192-
to prompt: String, image: UserInput.Image? = nil, video: UserInput.Video? = nil
192+
to prompt: String,
193+
images: [UserInput.Image],
194+
videos: [UserInput.Video]
193195
) async throws -> String {
194196
generator.messages = [
195197
.user(
196198
prompt,
197-
images: image.flatMap { [$0] } ?? [],
198-
videos: video.flatMap { [$0] } ?? [])
199+
images: images,
200+
videos: videos
201+
)
199202
]
200203
return try await generator.generate()
201204
}
@@ -204,18 +207,59 @@ public class ChatSession {
204207
///
205208
/// - Parameters:
206209
/// - prompt: the prompt
207-
/// - image: optional image (for use with VLMs)
208-
/// - video: optional video (for use with VLMs)
210+
/// - images: optional image (for use with VLMs)
211+
/// - videos: optional video (for use with VLMs)
212+
/// - Returns: response from the model
213+
public func respond(
214+
to prompt: String,
215+
image: UserInput.Image? = nil,
216+
video: UserInput.Video? = nil
217+
) async throws -> String {
218+
try await respond(
219+
to: prompt,
220+
images: image.flatMap { [$0] } ?? [],
221+
videos: video.flatMap { [$0] } ?? []
222+
)
223+
}
224+
225+
/// Produces a response to a prompt.
226+
///
227+
/// - Parameters:
228+
/// - prompt: the prompt
229+
/// - images: list of image (for use with VLMs)
230+
/// - videos: list of video (for use with VLMs)
209231
/// - Returns: a stream of tokens (as Strings) from the model
210232
public func streamResponse(
211-
to prompt: String, image: UserInput.Image? = nil, video: UserInput.Video? = nil
233+
to prompt: String,
234+
images: [UserInput.Image],
235+
videos: [UserInput.Video]
212236
) -> AsyncThrowingStream<String, Error> {
213237
generator.messages = [
214238
.user(
215239
prompt,
216-
images: image.flatMap { [$0] } ?? [],
217-
videos: video.flatMap { [$0] } ?? [])
240+
images: images,
241+
videos: videos
242+
)
218243
]
219244
return generator.stream()
220245
}
246+
247+
/// Produces a response to a prompt.
248+
///
249+
/// - Parameters:
250+
/// - prompt: the prompt
251+
/// - image: optional image (for use with VLMs)
252+
/// - video: optional video (for use with VLMs)
253+
/// - Returns: a stream of tokens (as Strings) from the model
254+
public func streamResponse(
255+
to prompt: String,
256+
image: UserInput.Image? = nil,
257+
video: UserInput.Video? = nil
258+
) -> AsyncThrowingStream<String, Error> {
259+
streamResponse(
260+
to: prompt,
261+
images: image.flatMap { [$0] } ?? [],
262+
videos: video.flatMap { [$0] } ?? []
263+
)
264+
}
221265
}

0 commit comments

Comments
 (0)