Skip to content

Commit f9c8594

Browse files
committed
Add generate method with tokens argument to LanguageModel
1 parent 9461057 commit f9c8594

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Sources/Models/LanguageModel.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,31 @@ extension LanguageModel: TextGenerationModel {
420420
}
421421
}
422422

423+
@available(macOS 15.0, iOS 18.0, tvOS 18.0, visionOS 2.0, watchOS 11.0, *)
424+
extension LanguageModel {
425+
/// Generates tokens from input tokens.
426+
///
427+
/// - Parameters:
428+
/// - config: The generation configuration specifying parameters like max tokens and sampling
429+
/// - tokens: The input token sequence to use as the generation starting point
430+
/// - callback: Optional callback to receive intermediate generation results
431+
/// - Returns: The generated token sequence
432+
/// - Throws: An error if token generation fails
433+
@discardableResult
434+
public func generate(
435+
config: GenerationConfig,
436+
tokens: InputTokens,
437+
callback: PredictionTokensCallback? = nil
438+
) async throws -> GenerationOutput {
439+
return await generate(
440+
config: config,
441+
tokens: tokens,
442+
model: callAsFunction,
443+
callback: callback
444+
)
445+
}
446+
}
447+
423448
/// Language model implementation with stateful KV Cache.
424449
///
425450
/// Maintains a KV Cache as sequence generation progresses,

0 commit comments

Comments
 (0)