66 lines
2.4 KiB
Go
66 lines
2.4 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// Preset describes a known OpenAI-compatible provider. All presets use
|
|
// "Authorization: Bearer <api_key>" against <base_url>/chat/completions.
|
|
type Preset struct {
|
|
Name string
|
|
BaseURL string
|
|
DefaultModel string
|
|
// KeyEnv is an additional env var read for the API key if none is configured.
|
|
KeyEnv string
|
|
Source string
|
|
}
|
|
|
|
// Presets: values verified against provider docs (see Source) on 2026-10-25.
|
|
var Presets = map[string]Preset{
|
|
"openai": {Name: "openai", BaseURL: "https://api.openai.com/v1", DefaultModel: "gpt-4o-mini", KeyEnv: "OPENAI_API_KEY", Source: "https://platform.openai.com/docs/api-reference"},
|
|
"openrouter": {Name: "openrouter", BaseURL: "https://openrouter.ai/api/v1", DefaultModel: "openai/gpt-4o-mini", KeyEnv: "OPENROUTER_API_KEY", Source: "https://openrouter.ai/docs"},
|
|
"ollama": {Name: "ollama", BaseURL: "http://localhost:11434/v1", DefaultModel: "llama3.1", Source: "https://github.com/ollama/ollama/blob/main/docs/openai.md"},
|
|
// Dahl: OpenAI-compatible inference on the Gonka network.
|
|
"dahl": {Name: "dahl", BaseURL: "https://inference.dahl.global/v1", DefaultModel: "MiniMaxAI/MiniMax-M2.7", KeyEnv: "DAHL_API_KEY", Source: "https://docs.dahl.global/"},
|
|
// GonkaRouter: OpenAI-/Anthropic-compatible router on the Gonka network.
|
|
"gonkarouter": {Name: "gonkarouter", BaseURL: "https://api.gonkarouter.io/v1", DefaultModel: "MiniMaxAI/MiniMax-M2.7", KeyEnv: "GONKAROUTER_API_KEY", Source: "https://gonkarouter.io/docs"},
|
|
}
|
|
|
|
// PresetNames returns known provider names plus "custom".
|
|
func PresetNames() []string {
|
|
out := []string{"custom"}
|
|
for k := range Presets {
|
|
out = append(out, k)
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
// applyPreset fills empty base_url/model/api_key from the selected provider preset.
|
|
// Explicit config values always win. "custom" requires base_url and model.
|
|
func applyPreset(l *LLM, get func(string) string) error {
|
|
name := strings.ToLower(strings.TrimSpace(l.Provider))
|
|
if name == "" {
|
|
name = "openai"
|
|
}
|
|
l.Provider = name
|
|
if name == "custom" {
|
|
return nil
|
|
}
|
|
p, ok := Presets[name]
|
|
if !ok {
|
|
return fmt.Errorf("unknown llm.provider %q (known: %s)", name, strings.Join(PresetNames(), ", "))
|
|
}
|
|
if l.BaseURL == "" {
|
|
l.BaseURL = p.BaseURL
|
|
}
|
|
if l.Model == "" {
|
|
l.Model = p.DefaultModel
|
|
}
|
|
if l.APIKey == "" && p.KeyEnv != "" {
|
|
l.APIKey = get(p.KeyEnv)
|
|
}
|
|
return nil
|
|
}
|