Support OpenAI-compatible inference endpoints
This commit is contained in:
@@ -12,6 +12,7 @@ final class StudioModel: ObservableObject {
|
||||
@Published private(set) var libraryStatus = ""
|
||||
@Published private(set) var toolAttachment: (name: String, output: String)?
|
||||
@Published private(set) var ollamaToken = ""
|
||||
@Published private(set) var compatibleToken = ""
|
||||
@Published private(set) var models: [Ollama.Model] = []
|
||||
@Published private(set) var status = ""
|
||||
@Published private(set) var modelStatus = ""
|
||||
@@ -25,6 +26,7 @@ final class StudioModel: ObservableObject {
|
||||
|
||||
private var store: Store?
|
||||
private var tokenEndpoint = ""
|
||||
private var compatibleTokenEndpoint = ""
|
||||
private let mlx = MLXProvider()
|
||||
private var generation: Task<Void, Never>?
|
||||
private var management: Task<Void, Never>?
|
||||
@@ -73,17 +75,28 @@ final class StudioModel: ObservableObject {
|
||||
func stop() { generation?.cancel() }
|
||||
|
||||
func send(endpoint: String, model: String, mlxPath: String, thinking: Thinking, failover: Bool,
|
||||
mlxOnly: Bool, recall: Bool, useDocuments: Bool, instructions: String, settings: GenerationSettings) {
|
||||
mlxOnly: Bool, compatible: Bool, recall: Bool, useDocuments: Bool, instructions: String, settings: GenerationSettings) {
|
||||
let text = prompt.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let tag = model.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !running, !text.isEmpty else { return }
|
||||
guard mlxOnly ? !mlxPath.isEmpty : !tag.isEmpty else {
|
||||
status = mlxOnly ? "Choose an MLX model folder in Settings." : "Choose an Ollama model in Settings."
|
||||
status = mlxOnly ? "Choose an MLX model folder in Settings." : "Choose a model in Settings."
|
||||
return
|
||||
}
|
||||
do {
|
||||
let ollama = mlxOnly ? nil : try ollamaClient(endpoint: endpoint)
|
||||
let parameters = try settings.parameters(mlxOnly: mlxOnly)
|
||||
let provider: (any InferenceProvider)?
|
||||
let name: String
|
||||
if mlxOnly {
|
||||
provider = nil
|
||||
name = "MLX"
|
||||
} else if compatible {
|
||||
provider = try compatibleClient(endpoint: endpoint)
|
||||
name = "Compatible API"
|
||||
} else {
|
||||
provider = try ollamaClient(endpoint: endpoint)
|
||||
name = "Ollama"
|
||||
}
|
||||
let parameters = try settings.parameters(mlxOnly: mlxOnly, compatible: compatible)
|
||||
let store = try database()
|
||||
let chat = try selected ?? create(String(text.prefix(48)))
|
||||
let user = Message(role: .user, content: text)
|
||||
@@ -133,14 +146,15 @@ final class StudioModel: ObservableObject {
|
||||
let local: Router.Producer = { [mlx] in mlx.generate(localRequest) }
|
||||
let start = ProcessInfo.processInfo.systemUptime
|
||||
let stream: AsyncThrowingStream<Delta, Error>
|
||||
if let ollama {
|
||||
if let provider {
|
||||
let request = InferenceRequest(model: tag, messages: context, thinking: thinking, parameters: parameters)
|
||||
stream = Router.stream(primary: { ollama.generate(request) },
|
||||
fallback: failover && !mlxPath.isEmpty ? local : nil)
|
||||
stream = Router.stream(primary: { provider.generate(request) },
|
||||
fallback: failover && !mlxPath.isEmpty ? local : nil,
|
||||
primaryName: name)
|
||||
} else {
|
||||
stream = local()
|
||||
}
|
||||
generation = Task { await consume(stream, in: chat, start: start, requested: mlxOnly ? "MLX" : "Ollama") }
|
||||
generation = Task { await consume(stream, in: chat, start: start, requested: name) }
|
||||
} catch { status = error.localizedDescription }
|
||||
}
|
||||
|
||||
@@ -156,6 +170,15 @@ final class StudioModel: ObservableObject {
|
||||
ollamaToken = token
|
||||
}
|
||||
|
||||
func setCompatibleToken(_ token: String, endpoint: String) {
|
||||
compatibleTokenEndpoint = endpoint
|
||||
compatibleToken = token
|
||||
}
|
||||
|
||||
func compatibleClient(endpoint: String) throws -> CompatibleEndpoint {
|
||||
try CompatibleEndpoint(endpoint: endpoint, token: endpoint == compatibleTokenEndpoint ? compatibleToken : "")
|
||||
}
|
||||
|
||||
private func ollamaClient(endpoint: String) throws -> Ollama {
|
||||
try Ollama(endpoint: endpoint, token: endpoint == tokenEndpoint ? ollamaToken : "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user