Files
go-llm/v2/constructors.go
T
steve 4522310f5a
CI / Root Module (pull_request) Failing after 3s
CI / Lint (pull_request) Failing after 3s
CI / V2 Module (pull_request) Successful in 1m25s
feat(v2): add Parse() function and extensible Registry for model string resolution
Introduces llm.Parse(spec) backed by an extensible Registry that resolves
model strings like "openai/gpt-4o", aliases like "fast", and named targets
like "m5/qwen3:30b" (via LLM_M5 env var DSNs) into ready-to-use *Model
objects. Extension points: RegisterProvider, RegisterAlias, RegisterResolver.
Adds Foreman constructor and sentinel errors ErrAliasLoop, ErrUnknownProvider,
ErrInvalidDSN.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 22:58:14 -04:00

156 lines
4.4 KiB
Go

package llm
import (
anthProvider "gitea.stevedudenhoeffer.com/steve/go-llm/v2/anthropic"
deepseekProvider "gitea.stevedudenhoeffer.com/steve/go-llm/v2/deepseek"
googleProvider "gitea.stevedudenhoeffer.com/steve/go-llm/v2/google"
groqProvider "gitea.stevedudenhoeffer.com/steve/go-llm/v2/groq"
moonshotProvider "gitea.stevedudenhoeffer.com/steve/go-llm/v2/moonshot"
ollamaProvider "gitea.stevedudenhoeffer.com/steve/go-llm/v2/ollama"
openaiProvider "gitea.stevedudenhoeffer.com/steve/go-llm/v2/openai"
xaiProvider "gitea.stevedudenhoeffer.com/steve/go-llm/v2/xai"
)
// OpenAI creates an OpenAI client.
//
// Example:
//
// model := llm.OpenAI("sk-...").Model("gpt-4o")
func OpenAI(apiKey string, opts ...ClientOption) *Client {
cfg := &clientConfig{}
for _, opt := range opts {
opt(cfg)
}
return NewClient(openaiProvider.New(apiKey, cfg.baseURL))
}
// Anthropic creates an Anthropic client.
//
// Example:
//
// model := llm.Anthropic("sk-ant-...").Model("claude-sonnet-4-20250514")
func Anthropic(apiKey string, opts ...ClientOption) *Client {
cfg := &clientConfig{}
for _, opt := range opts {
opt(cfg)
}
_ = cfg // Anthropic doesn't support custom base URL in the SDK
return NewClient(anthProvider.New(apiKey))
}
// Google creates a Google (Gemini) client.
//
// Example:
//
// model := llm.Google("...").Model("gemini-2.0-flash")
func Google(apiKey string, opts ...ClientOption) *Client {
cfg := &clientConfig{}
for _, opt := range opts {
opt(cfg)
}
_ = cfg // Google doesn't support custom base URL in the SDK
return NewClient(googleProvider.New(apiKey))
}
// DeepSeek creates a DeepSeek client (OpenAI-compatible).
//
// Example:
//
// model := llm.DeepSeek("sk-...").Model("deepseek-chat")
func DeepSeek(apiKey string, opts ...ClientOption) *Client {
cfg := &clientConfig{}
for _, opt := range opts {
opt(cfg)
}
return NewClient(deepseekProvider.New(apiKey, cfg.baseURL))
}
// Moonshot creates a Moonshot AI (Kimi) client (OpenAI-compatible).
//
// Example:
//
// model := llm.Moonshot("sk-...").Model("kimi-k2-0711-preview")
func Moonshot(apiKey string, opts ...ClientOption) *Client {
cfg := &clientConfig{}
for _, opt := range opts {
opt(cfg)
}
return NewClient(moonshotProvider.New(apiKey, cfg.baseURL))
}
// XAI creates an xAI (Grok) client (OpenAI-compatible).
//
// Example:
//
// model := llm.XAI("xai-...").Model("grok-2")
func XAI(apiKey string, opts ...ClientOption) *Client {
cfg := &clientConfig{}
for _, opt := range opts {
opt(cfg)
}
return NewClient(xaiProvider.New(apiKey, cfg.baseURL))
}
// Groq creates a Groq client (OpenAI-compatible).
//
// Example:
//
// model := llm.Groq("gsk-...").Model("llama-3.3-70b-versatile")
func Groq(apiKey string, opts ...ClientOption) *Client {
cfg := &clientConfig{}
for _, opt := range opts {
opt(cfg)
}
return NewClient(groqProvider.New(apiKey, cfg.baseURL))
}
// Ollama creates a client for a local Ollama instance using the native
// /api/chat endpoint. No API key is required. Use WithBaseURL to point at a
// non-default host/port.
//
// Example:
//
// model := llm.Ollama().Model("llama3.2")
func Ollama(opts ...ClientOption) *Client {
cfg := &clientConfig{}
for _, opt := range opts {
opt(cfg)
}
return NewClient(ollamaProvider.New("", cfg.baseURL))
}
// Foreman creates a client targeting a foreman daemon (a private, authenticated
// Ollama endpoint with queuing and observability). The token is sent as a Bearer
// token; pass "" for unauthenticated (network-trusted) deployments. Use
// WithBaseURL to set the foreman host URL.
//
// Example:
//
// model := llm.Foreman("my-token", llm.WithBaseURL("https://foreman.local")).Model("qwen3:30b")
func Foreman(token string, opts ...ClientOption) *Client {
cfg := &clientConfig{}
for _, opt := range opts {
opt(cfg)
}
return NewClient(ollamaProvider.New(token, cfg.baseURL))
}
// OllamaCloud creates a client targeting Ollama Cloud (https://ollama.com).
// The apiKey is required and is sent as `Authorization: Bearer <key>`. Use
// WithBaseURL to point at a private Ollama deployment that requires auth.
//
// Example:
//
// model := llm.OllamaCloud(os.Getenv("OLLAMA_API_KEY")).Model("kimi-k2.5")
func OllamaCloud(apiKey string, opts ...ClientOption) *Client {
cfg := &clientConfig{}
for _, opt := range opts {
opt(cfg)
}
baseURL := cfg.baseURL
if baseURL == "" {
baseURL = ollamaProvider.DefaultCloudBaseURL
}
return NewClient(ollamaProvider.New(apiKey, baseURL))
}