Configure silent prompts, generation parameters, and local tools
This commit is contained in:
@@ -46,18 +46,31 @@ actor MLXProvider: InferenceProvider {
|
||||
}
|
||||
var thinkingOutput = MLXThinking()
|
||||
let input = try await model.prepare(input: UserInput(chat: history, additionalContext: context))
|
||||
let output = try await model.generate(input: input, parameters: GenerateParameters(maxTokens: 1024))
|
||||
let config = request.parameters
|
||||
let parameters = GenerateParameters(maxTokens: config.maxTokens ?? 1024, maxKVSize: config.mlxCache,
|
||||
temperature: Float(config.temperature ?? 0.6),
|
||||
topP: Float(config.topP ?? 1),
|
||||
repetitionPenalty: config.repeatPenalty.map(Float.init),
|
||||
repetitionContextSize: config.repeatLastN ?? 20,
|
||||
prefillStepSize: config.mlxPrefill ?? 512)
|
||||
let output = try await model.generate(input: input, parameters: parameters)
|
||||
var stopMatcher = StopMatcher(sequences: config.stop)
|
||||
continuation.yield(.backend("MLX · \(URL(fileURLWithPath: directory).lastPathComponent)"))
|
||||
var finished = false
|
||||
for await event in output {
|
||||
generation: for await event in output {
|
||||
try Task.checkCancellation()
|
||||
switch event {
|
||||
case .chunk(let text):
|
||||
case .chunk(let chunk):
|
||||
let text = stopMatcher.append(chunk)
|
||||
if supportsThinking {
|
||||
for delta in thinkingOutput.append(text) { continuation.yield(delta) }
|
||||
} else {
|
||||
} else if !text.isEmpty {
|
||||
continuation.yield(.answer(text))
|
||||
}
|
||||
if stopMatcher.stopped {
|
||||
finished = true
|
||||
break generation
|
||||
}
|
||||
case .info(let info):
|
||||
finished = true
|
||||
continuation.yield(.metrics(ProviderMetrics(tokens: info.generationTokenCount, seconds: info.generateTime)))
|
||||
@@ -68,7 +81,12 @@ actor MLXProvider: InferenceProvider {
|
||||
}
|
||||
try Task.checkCancellation()
|
||||
guard finished else { throw StudioError("MLX generation ended without completion information.") }
|
||||
for delta in thinkingOutput.finish() { continuation.yield(delta) }
|
||||
let remainder = stopMatcher.finish()
|
||||
if supportsThinking {
|
||||
for delta in thinkingOutput.append(remainder) + thinkingOutput.finish() { continuation.yield(delta) }
|
||||
} else if !remainder.isEmpty {
|
||||
continuation.yield(.answer(remainder))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user