de2b2f0f28
llama-swap was http-only by DSN, pushing TLS-fronted instances onto the openai:// scheme (which loses the management/image methods). Add a "llama-swaps" scheme that builds an https base URL, alongside "llama-swap" (http, local-first) — mirroring redis/rediss. Both share one factory; llama-swaps is scheme-only (no default built-in). The choice stays explicit because a DSN has no reliable http-vs-https signal. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
136 lines
5.0 KiB
Go
136 lines
5.0 KiB
Go
package majordomo
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"gitea.stevedudenhoeffer.com/steve/majordomo/llm"
|
|
"gitea.stevedudenhoeffer.com/steve/majordomo/provider/anthropic"
|
|
"gitea.stevedudenhoeffer.com/steve/majordomo/provider/google"
|
|
"gitea.stevedudenhoeffer.com/steve/majordomo/provider/llamaswap"
|
|
"gitea.stevedudenhoeffer.com/steve/majordomo/provider/ollama"
|
|
"gitea.stevedudenhoeffer.com/steve/majordomo/provider/openai"
|
|
)
|
|
|
|
// Built-in provider names.
|
|
const (
|
|
ProviderOpenAI = "openai"
|
|
ProviderAnthropic = "anthropic"
|
|
ProviderGoogle = "google"
|
|
ProviderOllama = "ollama"
|
|
ProviderOllamaCloud = "ollama-cloud"
|
|
ProviderForeman = "foreman"
|
|
ProviderLlamaSwap = "llama-swap"
|
|
// ProviderLlamaSwapTLS is the DSN scheme for a TLS-fronted llama-swap
|
|
// (https base URL). It is a scheme only, not a default built-in provider
|
|
// name. Why a separate scheme rather than auto-detecting: a DSN carries no
|
|
// reliable signal for http vs https, so the choice is explicit
|
|
// (llama-swap = http local-first, llama-swaps = https), mirroring rediss.
|
|
ProviderLlamaSwapTLS = "llama-swaps"
|
|
)
|
|
|
|
// registerBuiltins installs the built-in providers and env-DSN scheme
|
|
// factories into a fresh registry. httpClient, when non-nil, is used by
|
|
// every provider and factory the registry itself constructs.
|
|
func registerBuiltins(r *Registry, httpClient *http.Client) {
|
|
ollamaOpts := func(extra ...ollama.Option) []ollama.Option {
|
|
if httpClient != nil {
|
|
extra = append(extra, ollama.WithHTTPClient(httpClient))
|
|
}
|
|
return extra
|
|
}
|
|
|
|
// Native-Ollama family: three names over one client with presets.
|
|
r.providers[ProviderOllama] = ollama.Local(ollamaOpts()...)
|
|
r.providers[ProviderOllamaCloud] = ollama.Cloud(ollamaOpts()...)
|
|
// foreman has no default URL; the no-DSN registration resolves but
|
|
// errors on use with a clear message (use an LLM_* DSN or
|
|
// ollama.Foreman(...) + RegisterProvider).
|
|
r.providers[ProviderForeman] = ollama.New(ollamaOpts(ollama.WithName(ProviderForeman))...)
|
|
|
|
ollamaScheme := func(name string, dsn DSN) (llm.Provider, error) {
|
|
return ollama.New(ollamaOpts(
|
|
ollama.WithName(name),
|
|
ollama.WithBaseURL(dsn.BaseURL()),
|
|
ollama.WithToken(dsn.Token),
|
|
)...), nil
|
|
}
|
|
r.schemes[ProviderOllama] = ollamaScheme
|
|
r.schemes[ProviderOllamaCloud] = ollamaScheme
|
|
r.schemes[ProviderForeman] = ollamaScheme
|
|
|
|
// OpenAI and OpenAI-compatible endpoints.
|
|
openaiOpts := func(extra ...openai.Option) []openai.Option {
|
|
if httpClient != nil {
|
|
extra = append(extra, openai.WithHTTPClient(httpClient))
|
|
}
|
|
return extra
|
|
}
|
|
r.providers[ProviderOpenAI] = openai.New(openaiOpts()...)
|
|
r.schemes[ProviderOpenAI] = func(name string, dsn DSN) (llm.Provider, error) {
|
|
return openai.New(openaiOpts(
|
|
openai.WithName(name),
|
|
openai.WithBaseURL(dsn.BaseURL()),
|
|
openai.WithAPIKey(dsn.Token),
|
|
)...), nil
|
|
}
|
|
|
|
// llama-swap: OpenAI-compatible chat + image generation + management
|
|
// endpoints over a model-swapping proxy. Chat reuses the openai client
|
|
// (provider/llamaswap delegates). Two schemes: "llama-swap" builds an
|
|
// http:// base URL (local-first default), "llama-swaps" builds https://
|
|
// for a TLS-fronted instance (mirrors redis/rediss). The no-DSN built-in
|
|
// errors on use with a clear message, mirroring foreman.
|
|
llamaSwapOpts := func(extra ...llamaswap.Option) []llamaswap.Option {
|
|
if httpClient != nil {
|
|
extra = append(extra, llamaswap.WithHTTPClient(httpClient))
|
|
}
|
|
return extra
|
|
}
|
|
llamaSwapScheme := func(urlScheme string) SchemeFactory {
|
|
return func(name string, dsn DSN) (llm.Provider, error) {
|
|
return llamaswap.New(llamaSwapOpts(
|
|
llamaswap.WithName(name),
|
|
llamaswap.WithBaseURL(urlScheme+"://"+dsn.Host),
|
|
llamaswap.WithToken(dsn.Token),
|
|
)...), nil
|
|
}
|
|
}
|
|
r.providers[ProviderLlamaSwap] = llamaswap.New(llamaSwapOpts(llamaswap.WithName(ProviderLlamaSwap))...)
|
|
r.schemes[ProviderLlamaSwap] = llamaSwapScheme("http")
|
|
r.schemes[ProviderLlamaSwapTLS] = llamaSwapScheme("https")
|
|
|
|
// Anthropic and Anthropic-compatible endpoints.
|
|
anthropicOpts := func(extra ...anthropic.Option) []anthropic.Option {
|
|
if httpClient != nil {
|
|
extra = append(extra, anthropic.WithHTTPClient(httpClient))
|
|
}
|
|
return extra
|
|
}
|
|
r.providers[ProviderAnthropic] = anthropic.New(anthropicOpts()...)
|
|
r.schemes[ProviderAnthropic] = func(name string, dsn DSN) (llm.Provider, error) {
|
|
return anthropic.New(anthropicOpts(
|
|
anthropic.WithName(name),
|
|
anthropic.WithBaseURL(dsn.BaseURL()),
|
|
anthropic.WithAPIKey(dsn.Token),
|
|
)...), nil
|
|
}
|
|
|
|
// Google (Gemini) on the official SDK; "gemini" is an alternate scheme.
|
|
googleOpts := func(extra ...google.Option) []google.Option {
|
|
if httpClient != nil {
|
|
extra = append(extra, google.WithHTTPClient(httpClient))
|
|
}
|
|
return extra
|
|
}
|
|
r.providers[ProviderGoogle] = google.New(googleOpts()...)
|
|
googleScheme := func(name string, dsn DSN) (llm.Provider, error) {
|
|
return google.New(googleOpts(
|
|
google.WithName(name),
|
|
google.WithBaseURL(dsn.BaseURL()),
|
|
google.WithAPIKey(dsn.Token),
|
|
)...), nil
|
|
}
|
|
r.schemes[ProviderGoogle] = googleScheme
|
|
r.schemes["gemini"] = googleScheme
|
|
}
|