Skip to content

Commit 14cd6f5

Browse files
committed
Update indentation parameters
1 parent e4c17c9 commit 14cd6f5

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

Sources/MLXStructured/Generate.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public func generate<Content: Decodable>(
3434
context: ModelContext,
3535
schema: JSONSchema,
3636
generating: Content.Type,
37+
indent: Int? = nil,
3738
didGenerate: ([Int]) -> GenerateDisposition = { _ in .more }
3839
) async throws -> (GenerateResult, Content) {
39-
let grammar = try Grammar.schema(schema)
40+
let grammar = try Grammar.schema(schema, indent: indent)
4041
let sampler = parameters.sampler()
4142
let processor = try await GrammarMaskedLogitProcessor.from(configuration: context.configuration, grammar: grammar)
4243
let iterator = try TokenIterator(input: input, model: context.model, processor: processor, sampler: sampler)
@@ -52,10 +53,11 @@ public func generate<Content: Generable>(
5253
parameters: GenerateParameters = GenerateParameters(),
5354
context: ModelContext,
5455
generating: Content.Type,
56+
indent: Int? = nil,
5557
didGenerate: ([Int]) -> GenerateDisposition = { _ in .more }
5658
) async throws -> (GenerateResult, Content) {
5759
let sampler = parameters.sampler()
58-
let grammar = try Grammar.generable(Content.self)
60+
let grammar = try Grammar.generable(Content.self, indent: indent)
5961
let processor = try await GrammarMaskedLogitProcessor.from(configuration: context.configuration, grammar: grammar)
6062
let iterator = try TokenIterator(input: input, model: context.model, processor: processor, sampler: sampler)
6163
let result = generate(input: input, context: context, iterator: iterator, didGenerate: didGenerate)
@@ -68,10 +70,11 @@ public func generate<Content: Generable>(
6870
input: LMInput,
6971
parameters: GenerateParameters = GenerateParameters(),
7072
context: ModelContext,
71-
generating: Content.Type
73+
generating: Content.Type,
74+
indent: Int? = nil
7275
) async throws -> AsyncStream<Content.PartiallyGenerated> {
7376
let sampler = parameters.sampler()
74-
let grammar = try Grammar.generable(Content.self)
77+
let grammar = try Grammar.generable(Content.self, indent: indent)
7578
let processor = try await GrammarMaskedLogitProcessor.from(configuration: context.configuration, grammar: grammar)
7679
let iterator = try TokenIterator(input: input, model: context.model, processor: processor, sampler: sampler)
7780
let stream = generate(input: input, context: context, iterator: iterator)

Sources/MLXStructuredCLI/Commands/CodableExample.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct CodableExample: AsyncParsableCommand {
6161
let context = try await model.modelContext()
6262
let prompt = MovieRecord.instruction + "\n" + MovieRecord.sample
6363
let input = try await context.processor.prepare(input: UserInput(prompt: prompt))
64-
let (result, model) = try await MLXStructured.generate(input: input, context: context, schema: MovieRecord.schema, generating: MovieRecord.self)
64+
let (result, model) = try await MLXStructured.generate(input: input, context: context, schema: MovieRecord.schema, generating: MovieRecord.self, indent: 2)
6565
print("Generation result:", result.output)
6666
print("Generated model:", model)
6767
}

Sources/MLXStructuredCLI/Commands/GenerableExample.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct GenerableExample: AsyncParsableCommand {
6161
let context = try await model.modelContext()
6262
let prompt = MovieRecord.instruction + "\n" + MovieRecord.sample
6363
let input = try await context.processor.prepare(input: UserInput(prompt: prompt))
64-
let (result, model) = try await MLXStructured.generate(input: input, context: context, generating: MovieRecord.self)
64+
let (result, model) = try await MLXStructured.generate(input: input, context: context, generating: MovieRecord.self, indent: 2)
6565
print("Generation result:", result.output)
6666
print("Generated model:", model)
6767
}

0 commit comments

Comments
 (0)