diff options
| author | Paul Buetow <paul@buetow.org> | 2025-08-16 23:16:54 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-08-16 23:16:54 +0300 |
| commit | 765eda955eb811d08d867ff4d3914fc6d60c22dd (patch) | |
| tree | fdc87da6af9d86dbda2ea9ab08244e93fd167188 /internal/llm/provider.go | |
| parent | 1b01e35c34b953cbf51298f4650dc3215c382a4f (diff) | |
refactor(config): drop env-based config (except OPENAI_API_KEY)
- Switch to config-file-only; only OPENAI_API_KEY read from env.\n- llm: replace env autodetect with Config + NewFromConfig; add newOpenAI/newOllama.\n- lsp: NewServer now accepts injected llm.Client.\n- cli: remove env overrides; extend appConfig with provider-specific fields; build client from config + OPENAI_API_KEY.\n- docs: update README (config-only, defaults to OpenAI, minimal example); simplify flags table.\n- add config.json.example.\n- prompts: enforce ;text; (no spaces) and add ;;text; to remove entire line; tests added.
Diffstat (limited to 'internal/llm/provider.go')
| -rw-r--r-- | internal/llm/provider.go | 55 |
1 files changed, 27 insertions, 28 deletions
diff --git a/internal/llm/provider.go b/internal/llm/provider.go index f7dad31..c7367ed 100644 --- a/internal/llm/provider.go +++ b/internal/llm/provider.go @@ -3,7 +3,6 @@ package llm import ( "context" "errors" - "os" "strings" ) @@ -42,34 +41,34 @@ func WithStop(stop ...string) RequestOption { return func(o *Options) { o.Stop = append([]string{}, stop...) } } -// NewDefault returns the default provider using environment configuration. -// Selection order: -// 1) HEXAI_LLM_PROVIDER=openai|ollama -// 2) If OPENAI_API_KEY is set -> OpenAI -// 3) If any OLLAMA_* vars are set -> Ollama -func NewDefault() (Client, error) { - // Explicit provider selection - if p := strings.ToLower(strings.TrimSpace(os.Getenv("HEXAI_LLM_PROVIDER"))); p != "" { - switch p { - case "openai": - apiKey := os.Getenv("OPENAI_API_KEY") - if apiKey == "" { - return nil, errors.New("OPENAI_API_KEY is not set") - } - return newOpenAIFromEnv(apiKey), nil - case "ollama": - return newOllamaFromEnv(), nil - default: - return nil, errors.New("unknown HEXAI_LLM_PROVIDER: " + p) - } - } +// Config defines provider configuration read from the Hexai config file. +type Config struct { + Provider string + // OpenAI options + OpenAIBaseURL string + OpenAIModel string + // Ollama options + OllamaBaseURL string + OllamaModel string +} - // Auto-detect - if apiKey := os.Getenv("OPENAI_API_KEY"); apiKey != "" { - return newOpenAIFromEnv(apiKey), nil +// NewFromConfig creates an LLM client using only the supplied configuration. +// The OpenAI API key is supplied separately and may be read from the environment +// by the caller; other environment-based configuration is not used. +func NewFromConfig(cfg Config, openAIAPIKey string) (Client, error) { + p := strings.ToLower(strings.TrimSpace(cfg.Provider)) + if p == "" { + p = "openai" } - if os.Getenv("OLLAMA_BASE_URL") != "" || os.Getenv("OLLAMA_HOST") != "" || os.Getenv("OLLAMA_MODEL") != "" { - return newOllamaFromEnv(), nil + switch p { + case "openai": + if strings.TrimSpace(openAIAPIKey) == "" { + return nil, errors.New("missing OPENAI_API_KEY for provider openai") + } + return newOpenAI(cfg.OpenAIBaseURL, cfg.OpenAIModel, openAIAPIKey), nil + case "ollama": + return newOllama(cfg.OllamaBaseURL, cfg.OllamaModel), nil + default: + return nil, errors.New("unknown LLM provider: " + p) } - return nil, errors.New("no LLM provider configured (set OPENAI_API_KEY or HEXAI_LLM_PROVIDER/OLLAMA_*)") } |
