Support OpenAI-compatible inference endpoints

This commit is contained in:
Mark Gregory
2026-11-16 13:58:43 +00:00
parent afeba270f4
commit caa0a8733b
9 changed files with 309 additions and 30 deletions
+84 -12
View File
@@ -49,6 +49,9 @@ struct ChatView: View {
@AppStorage("thinking") private var thinking = Thinking.automatic
@AppStorage("failover") private var failover = true
@AppStorage("mlxOnly") private var mlxOnly = false
@AppStorage("compatibleEnabled") private var compatibleEnabled = false
@AppStorage("compatibleEndpoint") private var compatibleEndpoint = "http://127.0.0.1:1234/v1"
@AppStorage("compatibleModel") private var compatibleModel = ""
@AppStorage("recall") private var recall = true
@AppStorage("globalPromptMode") private var promptMode = PromptMode.standard
@AppStorage("globalCustomPrompt") private var customPrompt = ""
@@ -60,6 +63,10 @@ struct ChatView: View {
@State private var renaming = false
@State private var newTitle = ""
private var backendName: String { compatibleEnabled ? "Compatible API" : "Ollama" }
private var activeEndpoint: String { compatibleEnabled ? compatibleEndpoint : endpoint }
private var activeModel: String { compatibleEnabled ? compatibleModel : model }
private var visibleChats: [Conversation] {
search.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
? studio.conversations : studio.searchConversations(search)
@@ -153,14 +160,14 @@ struct ChatView: View {
private var composer: some View {
VStack(alignment: .leading, spacing: 8) {
HStack {
Label(mlxOnly ? "MLX on this Mac" : "Ollama · \(model.isEmpty ? "Choose a model" : model)",
Label(mlxOnly ? "MLX on this Mac" : "\(backendName) · \(activeModel.isEmpty ? "Choose a model" : activeModel)",
systemImage: mlxOnly ? "cpu" : "server.rack")
.font(.caption)
if !mlxOnly {
Text(endpoint).font(.caption).foregroundStyle(.secondary).lineLimit(1)
Text(activeEndpoint).font(.caption).foregroundStyle(.secondary).lineLimit(1)
}
Spacer()
if !mlxOnly || MLXThinking.supports(mlxPath) {
if (!mlxOnly && !compatibleEnabled) || (mlxOnly && MLXThinking.supports(mlxPath)) {
let modes: [Thinking] = mlxOnly ? [.automatic, .off, .on] : Thinking.allCases
Picker("Thinking", selection: Binding(
get: { modes.contains(thinking) ? thinking : .automatic },
@@ -174,14 +181,14 @@ struct ChatView: View {
}
if !studio.documents.isEmpty {
Toggle(mlxOnly ? "Include matching document excerpts" :
"Send matching document excerpts to Ollama", isOn: $useDocuments)
"Send matching document excerpts to \(backendName)", isOn: $useDocuments)
.font(.caption)
.disabled(studio.running)
}
if let attachment = studio.toolAttachment {
HStack {
Label("Attach \(attachment.name) result to next message", systemImage: "paperclip")
if !mlxOnly { Text("Sent to Ollama").foregroundStyle(.secondary) }
if !mlxOnly { Text("Sent to \(backendName)").foregroundStyle(.secondary) }
Spacer()
Button("Remove", systemImage: "xmark.circle") { studio.removeToolAttachment() }
}
@@ -201,8 +208,8 @@ struct ChatView: View {
Button("Stop", systemImage: "stop.fill") { studio.stop() }
} else {
Button("Send", systemImage: "arrow.up") {
studio.send(endpoint: endpoint, model: model, mlxPath: mlxPath, thinking: thinking,
failover: failover, mlxOnly: mlxOnly, recall: recall, useDocuments: useDocuments,
studio.send(endpoint: activeEndpoint, model: activeModel, mlxPath: mlxPath, thinking: thinking,
failover: failover, mlxOnly: mlxOnly, compatible: compatibleEnabled, recall: recall, useDocuments: useDocuments,
instructions: promptMode.instructions(custom: customPrompt),
settings: GenerationSettings.read(generationSettings))
}
@@ -266,6 +273,9 @@ struct BackendSettings: View {
@AppStorage("mlxPath") private var mlxPath = ""
@AppStorage("failover") private var failover = true
@AppStorage("mlxOnly") private var mlxOnly = false
@AppStorage("compatibleEnabled") private var compatibleEnabled = false
@AppStorage("compatibleEndpoint") private var compatibleEndpoint = "http://127.0.0.1:1234/v1"
@AppStorage("compatibleModel") private var compatibleModel = ""
@AppStorage("recall") private var recall = true
@State private var tag = ""
@State private var pickingFolder = false
@@ -276,11 +286,18 @@ struct BackendSettings: View {
var body: some View {
Form {
Section("Inference") {
Picker("Backend", selection: $mlxOnly) {
Text("Prefer Ollama").tag(false)
Text("MLX only").tag(true)
Picker("Backend", selection: Binding(
get: { mlxOnly ? "mlx" : (compatibleEnabled ? "compatible" : "ollama") },
set: { mode in
mlxOnly = mode == "mlx"
compatibleEnabled = mode == "compatible"
}
)) {
Text("Prefer Ollama").tag("ollama")
Text("OpenAI-compatible").tag("compatible")
Text("MLX only").tag("mlx")
}
Toggle("Fall back to MLX when Ollama fails before output", isOn: $failover)
Toggle("Fall back to MLX before output begins", isOn: $failover)
.disabled(mlxOnly)
Toggle("Recall older matching messages in this conversation", isOn: $recall)
}
@@ -326,6 +343,7 @@ struct BackendSettings: View {
}
Text(studio.modelStatus).foregroundStyle(.secondary).textSelection(.enabled)
}
CompatibleEndpointSettings(studio: studio, endpoint: $compatibleEndpoint, model: $compatibleModel)
Section("MLX") {
LabeledContent("Model folder", value: mlxPath.isEmpty ? "None selected" : mlxPath)
HStack {
@@ -680,9 +698,11 @@ struct GenerationSettingsView: View {
field("Top K", $draft.topK)
field("Min P", $draft.minP)
field("Seed", $draft.seed)
field("Keep alive (e.g. 5m or 0)", $draft.keepAlive)
}
Section("Ollama and OpenAI-compatible") {
field("Frequency penalty", $draft.frequencyPenalty)
field("Presence penalty", $draft.presencePenalty)
field("Keep alive (e.g. 5m or 0)", $draft.keepAlive)
Toggle("JSON output", isOn: $draft.json)
}
Section("MLX") {
@@ -872,3 +892,55 @@ struct LocalToolsView: View {
}
}
}
struct CompatibleEndpointSettings: View {
@ObservedObject var studio: StudioModel
@Binding var endpoint: String
@Binding var model: String
@State private var models: [String] = []
@State private var connecting = false
@State private var status = ""
var body: some View {
Section("OpenAI-compatible endpoint") {
TextField("Base URL", text: $endpoint, prompt: Text("http://127.0.0.1:1234/v1"))
.disabled(connecting)
SecureField("Bearer token (session only)", text: Binding(
get: { studio.compatibleToken },
set: { studio.setCompatibleToken($0, endpoint: endpoint) }
))
.disabled(connecting)
TextField("Model ID", text: $model)
HStack {
Button("Refresh Models") { Task { await refresh() } }
.disabled(connecting)
if connecting { ProgressView().controlSize(.small) }
if !models.isEmpty {
Menu("Available Models") {
ForEach(models, id: \.self) { name in Button(name) { model = name } }
}
}
}
if !status.isEmpty { Text(status).foregroundStyle(.secondary).textSelection(.enabled) }
Text("Use the API base path, such as /v1 for LM Studio. Model downloads and loading are managed by the server.")
.foregroundStyle(.secondary)
}
.onChange(of: endpoint) { _, value in
studio.setCompatibleToken("", endpoint: value)
models = []
status = ""
}
}
private func refresh() async {
guard !connecting else { return }
connecting = true
models = []
status = ""
defer { connecting = false }
do {
models = try await studio.compatibleClient(endpoint: endpoint).models()
status = models.isEmpty ? "The server returned no models." : "\(models.count) model(s) available."
} catch { status = error.localizedDescription }
}
}