new features
This commit is contained in:
+90
-4
@@ -12,6 +12,8 @@ struct ChatModelChoice: Equatable, Codable {
|
||||
enum Backend: String, Codable {
|
||||
case ollama = "Ollama"
|
||||
case mlx = "MLX"
|
||||
case gguf = "GGUF"
|
||||
case coreML = "Core ML"
|
||||
case compatible = "Compatible API"
|
||||
}
|
||||
|
||||
@@ -20,7 +22,8 @@ struct ChatModelChoice: Equatable, Codable {
|
||||
|
||||
var displayName: String {
|
||||
guard !model.isEmpty else { return "Choose Model" }
|
||||
return backend == .mlx ? URL(fileURLWithPath: model).lastPathComponent : model
|
||||
if [.gguf, .coreML].contains(backend) { return URL(fileURLWithPath: model).deletingPathExtension().lastPathComponent }
|
||||
return backend == .mlx ? MLXLibrary.displayName(for: URL(fileURLWithPath: model)) : model
|
||||
}
|
||||
|
||||
static func read(_ data: Data) -> [UUID: Self] {
|
||||
@@ -28,11 +31,21 @@ struct ChatModelChoice: Equatable, Codable {
|
||||
}
|
||||
|
||||
func thinkingMode(_ mode: Thinking) -> Thinking {
|
||||
if backend == .mlx && ![Thinking.automatic, .off, .on].contains(mode) { return .automatic }
|
||||
if [.mlx, .gguf].contains(backend) && ![Thinking.automatic, .off, .on].contains(mode) { return .automatic }
|
||||
return mode
|
||||
}
|
||||
}
|
||||
|
||||
enum ChatThinkingControl {
|
||||
static func options(backend: ChatModelChoice.Backend, supportsLocalThinking: Bool) -> [Thinking] {
|
||||
switch backend {
|
||||
case .ollama: Thinking.allCases
|
||||
case .gguf, .mlx: supportsLocalThinking ? [.automatic, .off, .on] : [.automatic]
|
||||
case .compatible, .coreML: [.automatic]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Role: String, Codable, Sendable { case system, user, assistant }
|
||||
|
||||
struct WebReplySource: Identifiable, Equatable, Codable, Sendable {
|
||||
@@ -59,12 +72,38 @@ struct Message: Identifiable, Equatable, Sendable {
|
||||
var documentNames: [String] = []
|
||||
var documentCitations: [DocumentCitation] = []
|
||||
var imagePNG: Data? = nil
|
||||
var performance: GenerationMeasurement? = nil
|
||||
|
||||
var imageDataURL: String? {
|
||||
imagePNG.map { "data:image/png;base64," + $0.base64EncodedString() }
|
||||
}
|
||||
}
|
||||
|
||||
enum LocalChatHistory {
|
||||
static func prepare(_ messages: [Message]) -> [Message] {
|
||||
var system: [String] = []
|
||||
var earlierReplies: [String] = []
|
||||
var turns: [Message] = []
|
||||
for message in messages {
|
||||
if message.role == .system {
|
||||
system.append(message.inferenceContent)
|
||||
} else if turns.isEmpty && message.role == .assistant {
|
||||
earlierReplies.append(message.inferenceContent)
|
||||
} else if turns.last?.role == message.role, let last = turns.indices.last {
|
||||
turns[last].content += "\n\n" + message.inferenceContent
|
||||
} else {
|
||||
turns.append(.init(role: message.role, content: message.inferenceContent))
|
||||
}
|
||||
}
|
||||
if !earlierReplies.isEmpty, let first = turns.indices.first {
|
||||
let context = earlierReplies.joined(separator: "\n\n")
|
||||
turns[first].content = "Earlier assistant replies:\n" + context + "\n\nUser messages:\n" + turns[first].content
|
||||
}
|
||||
if !system.isEmpty { turns.insert(.init(role: .system, content: system.joined(separator: "\n\n")), at: 0) }
|
||||
return turns
|
||||
}
|
||||
}
|
||||
|
||||
private extension Message {
|
||||
var inferenceContent: String {
|
||||
guard !documentCitations.isEmpty else { return content }
|
||||
@@ -196,18 +235,30 @@ enum Router {
|
||||
continuation.yield(delta)
|
||||
}
|
||||
try Task.checkCancellation()
|
||||
if !committed { throw StudioError("The model returned an empty response.") }
|
||||
} catch {
|
||||
try Task.checkCancellation()
|
||||
guard !committed, !(error is CancellationError), (error as? URLError)?.code != .cancelled,
|
||||
let fallback else { throw error }
|
||||
do {
|
||||
var producedOutput = false
|
||||
for try await delta in fallback() {
|
||||
try Task.checkCancellation()
|
||||
switch delta {
|
||||
case .answer(let text), .thinking(let text):
|
||||
producedOutput = producedOutput || !text.isEmpty
|
||||
case .committed: producedOutput = true
|
||||
case .backend, .metrics: break
|
||||
}
|
||||
continuation.yield(delta)
|
||||
}
|
||||
try Task.checkCancellation()
|
||||
if !producedOutput { throw StudioError("The model returned an empty response.") }
|
||||
} catch let fallbackError {
|
||||
try Task.checkCancellation()
|
||||
if fallbackError is CancellationError { throw fallbackError }
|
||||
if fallbackError is CancellationError || (fallbackError as? URLError)?.code == .cancelled {
|
||||
throw fallbackError
|
||||
}
|
||||
throw StudioError("\(primaryName): \(error.localizedDescription) MLX: \(fallbackError.localizedDescription)")
|
||||
}
|
||||
}
|
||||
@@ -226,7 +277,7 @@ struct ProviderMetrics: Codable, Equatable, Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
struct GenerationMeasurement: Codable, Identifiable {
|
||||
struct GenerationMeasurement: Codable, Identifiable, Equatable, Sendable {
|
||||
var id = UUID()
|
||||
static let key = "generationMeasurements"
|
||||
let backend: String
|
||||
@@ -235,6 +286,13 @@ struct GenerationMeasurement: Codable, Identifiable {
|
||||
let characters: Int
|
||||
let provider: ProviderMetrics?
|
||||
let outcome: String
|
||||
var model: String? = nil
|
||||
var recordedAt: Date? = nil
|
||||
var benchmark: Bool? = nil
|
||||
|
||||
static func history(_ data: Data) -> [Self] {
|
||||
(try? JSONDecoder().decode([Self].self, from: data)) ?? []
|
||||
}
|
||||
|
||||
var charactersPerSecond: Double? {
|
||||
guard let firstOutput, seconds > firstOutput, characters > 0 else { return nil }
|
||||
@@ -243,6 +301,34 @@ struct GenerationMeasurement: Codable, Identifiable {
|
||||
}
|
||||
}
|
||||
|
||||
struct GenerationCapture {
|
||||
let start: Double
|
||||
private(set) var firstOutput: Double?
|
||||
private(set) var characters = 0
|
||||
private var backend: String?
|
||||
private var provider: ProviderMetrics?
|
||||
|
||||
mutating func observe(_ delta: Delta, at time: Double) {
|
||||
switch delta {
|
||||
case .answer(let text), .thinking(let text):
|
||||
if !text.isEmpty {
|
||||
if firstOutput == nil { firstOutput = max(0, time - start) }
|
||||
characters += text.count
|
||||
}
|
||||
case .backend(let name): backend = name
|
||||
case .metrics(let metrics): provider = metrics
|
||||
case .committed: break
|
||||
}
|
||||
}
|
||||
|
||||
func finish(at time: Double, requested: String, model: String?, outcome: String,
|
||||
benchmark: Bool = false) -> GenerationMeasurement {
|
||||
GenerationMeasurement(backend: backend ?? requested, firstOutput: firstOutput,
|
||||
seconds: max(0, time - start), characters: characters, provider: provider, outcome: outcome,
|
||||
model: model, recordedAt: Date(), benchmark: benchmark)
|
||||
}
|
||||
}
|
||||
|
||||
enum PromptMode: String, CaseIterable, Identifiable {
|
||||
case standard = "Default"
|
||||
case concise = "Concise"
|
||||
|
||||
Reference in New Issue
Block a user