SPB Git

Repository Public

Routing.swift 9.8 KB 147 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatCore/Sources/Routing.swift3// Description: Task classification, model recommendations per task and the optional router (adaptive inference). Rules are explicit and every decision carries its reason.4import Foundation56public enum TaskCategory: String, Codable, Sendable, CaseIterable, Identifiable {7    case everyday, reasoning, coding, math, multilingual, vision, embeddings, tools8    public var id: String { rawValue }9    public var title: String {10        switch self {11        case .everyday: "Everyday chat"; case .reasoning: "Advanced reasoning"; case .coding: "Coding"; case .math: "Mathematics"12        case .multilingual: "Multilingual"; case .vision: "Vision"; case .embeddings: "Embeddings"; case .tools: "Tool use"13        }14    }15    public var symbol: String {16        switch self {17        case .everyday: "bolt"; case .reasoning: "brain"; case .coding: "chevron.left.forwardslash.chevron.right"; case .math: "function"18        case .multilingual: "globe"; case .vision: "eye"; case .embeddings: "point.3.connected.trianglepath.dotted"; case .tools: "wrench.and.screwdriver"19        }20    }21}2223public struct TaskAnalysis: Sendable, Hashable {24    public var category: TaskCategory25    public var complexity: Double      // 0…126    public var signals: [String]27}2829public enum TaskClassifier {30    static let codeWords = ["```", "func ", "def ", "class ", "import ", "#include", "error:", "exception", "stack trace", "compile", "regex", "sql", "swift", "python", "javascript", "typescript", "rust", "bug", "refactor", "api", "json"]31    static let mathWords = ["prove", "theorem", "integral", "derivative", "equation", "matrix", "probability", "eigen", "solve for", "\\frac", "\\int", "∑", "∫", "démontre", "équation", "calcule"]32    static let reasonWords = ["why", "explain", "compare", "analyze", "analyse", "trade-off", "step by step", "plan", "strategy", "design", "evaluate", "pourquoi", "explique", "compare", "raisonne", "stratégie"]33    static let toolWords = ["search the", "look up", "open the file", "read the file", "run ", "execute", "list files", "calculate", "what time", "today", "cherche", "exécute", "lis le fichier"]3435    public static func analyze(_ prompt: String, hasImages: Bool = false, toolsEnabled: Bool = false) -> TaskAnalysis {36        let text = prompt.lowercased(); var signals: [String] = []37        func hits(_ words: [String]) -> Int { words.reduce(0) { $0 + (text.contains($1) ? 1 : 0) } }38        let code = hits(codeWords), math = hits(mathWords), reason = hits(reasonWords), tool = hits(toolWords)39        let nonLatin = prompt.unicodeScalars.filter { $0.value > 0x24F && $0.properties.isAlphabetic }.count40        var category: TaskCategory = .everyday41        if hasImages { category = .vision; signals.append("image attached") }42        else if code >= 2 || text.contains("```") { category = .coding; signals.append("code vocabulary") }43        else if math >= 1 { category = .math; signals.append("mathematical vocabulary") }44        else if toolsEnabled && tool >= 1 { category = .tools; signals.append("asks for an action") }45        else if reason >= 2 || prompt.count > 900 { category = .reasoning; signals.append(prompt.count > 900 ? "long prompt" : "asks for analysis") }46        else if nonLatin > prompt.count / 4 { category = .multilingual; signals.append("non-Latin script") }47        var complexity = min(1, Double(prompt.count) / 2400) * 0.4548        complexity += min(1, Double(code + math + reason) / 5) * 0.449        if prompt.components(separatedBy: "?").count > 3 { complexity += 0.1; signals.append("several questions") }50        if category == .math || category == .reasoning { complexity += 0.15 }51        return TaskAnalysis(category: category, complexity: min(1, complexity), signals: signals)52    }53}5455public struct ModelRecommendation: Sendable, Hashable, Identifiable {56    public var category: TaskCategory57    public var model: ModelDescriptor58    public var verdict: CompatibilityVerdict59    public var reason: String60    public var id: String { category.rawValue + model.id }61}6263public struct RoutingDecision: Sendable, Hashable {64    public var modelID: String65    public var analysis: TaskAnalysis66    public var reason: String67}6869public struct RoutingPreferences: Codable, Sendable, Hashable {70    public var allowRemote: Bool            // remote nodes are never chosen unless the user allowed it71    public var preferSpeed: Bool72    public var pinned: [String: String]     // TaskCategory.rawValue → model id (user override)73    public init(allowRemote: Bool = false, preferSpeed: Bool = false, pinned: [String: String] = [:]) {74        self.allowRemote = allowRemote; self.preferSpeed = preferSpeed; self.pinned = pinned75    }76}7778public enum ModelRouter {79    /// Suitability of a model for a category from what is known about it (declared capabilities, name, size).80    public static func suitability(_ model: ModelDescriptor, for category: TaskCategory) -> Double {81        let name = (model.name + " " + (model.repoID ?? "")).lowercased()82        let billions = Double(model.parameterCount ?? 0) / 1e983        var score: Double84        switch category {85        case .everyday: score = billions > 0 ? max(0.2, 1 - abs(billions - 4) / 12) : 0.586        case .reasoning: score = min(1, billions / 14) + (model.capabilities.contains(.reasoning) ? 0.5 : 0)87        case .coding: score = min(0.8, billions / 16) + (model.capabilities.contains(.code) || name.contains("coder") || name.contains("code") ? 0.7 : 0)88        case .math: score = min(1, billions / 14) + (model.capabilities.contains(.reasoning) ? 0.4 : 0) + (name.contains("math") ? 0.3 : 0)89        case .multilingual: score = min(0.7, billions / 12) + (["qwen", "gemma", "aya", "mistral", "glm"].contains { name.contains($0) } ? 0.4 : 0)90        case .vision: score = model.capabilities.contains(.vision) ? 1 + min(0.5, billions / 20) : 091        case .embeddings: score = model.capabilities.contains(.embeddings) ? 1 : 092        case .tools: score = (model.capabilities.contains(.tools) ? 0.8 : 0) + min(0.5, billions / 16)93        }94        if category != .embeddings && model.isEmbeddingOnly { score = 0 }95        return score96    }9798    public static func recommendations(models: [ModelDescriptor], device: DeviceProfile, backendAvailable: (BackendKind) -> String?,99                                       measuredSpeed: [String: Double] = [:]) -> [ModelRecommendation] {100        TaskCategory.allCases.compactMap { category in101            let ranked = models.compactMap { model -> (ModelDescriptor, CompatibilityVerdict, Double)? in102                let verdict = device.compatibility(of: model, backendAvailable: backendAvailable)103                guard verdict.fit == .comfortable || verdict.fit == .tight else { return nil }104                var s = suitability(model, for: category)105                guard s > 0 else { return nil }106                if verdict.fit == .tight { s *= 0.8 }107                if category == .everyday, let speed = measuredSpeed[model.id] { s += min(0.3, speed / 300) }108                return (model, verdict, s)109            }.sorted { $0.2 > $1.2 }110            guard let best = ranked.first else { return nil }111            var reason = best.1.fit == .tight ? "Best match that still fits, but memory will be tight." : "Best match that fits this device comfortably."112            if let speed = measuredSpeed[best.0.id] { reason += String(format: " Measured %.0f tokens/s on this device.", speed) }113            return ModelRecommendation(category: category, model: best.0, verdict: best.1, reason: reason)114        }115    }116117    /// Picks a model for one prompt among the candidates the user already has. Returns nil when nothing suitable is available.118    public static func route(prompt: String, hasImages: Bool, toolsEnabled: Bool, candidates: [ModelDescriptor], device: DeviceProfile,119                             preferences: RoutingPreferences, backendAvailable: (BackendKind) -> String?,120                             measuredSpeed: [String: Double] = [:]) -> RoutingDecision? {121        let analysis = TaskClassifier.analyze(prompt, hasImages: hasImages, toolsEnabled: toolsEnabled)122        if let pinned = preferences.pinned[analysis.category.rawValue], candidates.contains(where: { $0.id == pinned }) {123            return RoutingDecision(modelID: pinned, analysis: analysis, reason: "Pinned by you for \(analysis.category.title.lowercased()).")124        }125        let usable = candidates.filter { m in126            if m.backend == .remote { return preferences.allowRemote }127            let fit = device.compatibility(of: m, backendAvailable: backendAvailable).fit128            return m.isLocal && (fit == .comfortable || fit == .tight)129        }130        let scored = usable.compactMap { m -> (ModelDescriptor, Double)? in131            var s = suitability(m, for: analysis.category)132            guard s > 0 else { return nil }133            let billions = Double(m.parameterCount ?? 4_000_000_000) / 1e9134            // simple prompts favour small models; complex prompts favour large ones135            s += analysis.complexity * min(1, billions / 20) - (1 - analysis.complexity) * min(0.6, billions / 40)136            if preferences.preferSpeed { s += min(0.4, (measuredSpeed[m.id] ?? 0) / 250) - min(0.3, billions / 60) }137            if m.backend == .remote { s -= 0.15 }   // stay on device when it is close138            return (m, s)139        }.sorted { $0.1 > $1.1 }140        guard let best = scored.first else { return nil }141        let level = analysis.complexity < 0.33 ? "simple" : analysis.complexity < 0.66 ? "moderate" : "complex"142        let where_ = best.0.backend == .remote ? " on a remote node" : ""143        return RoutingDecision(modelID: best.0.id, analysis: analysis,144                               reason: "\(analysis.category.title), \(level) request → \(best.0.name)\(where_)" + (analysis.signals.isEmpty ? "" : " (\(analysis.signals.joined(separator: ", ")))"))145    }146}147
Files main
Menu

Theme