diff options
| author | Paul Buetow <paul@buetow.org> | 2026-01-29 20:23:41 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-01-29 20:23:41 +0200 |
| commit | c6bb463837ec8c41261604e416aeab023663ba09 (patch) | |
| tree | ea81ab84b698c3a98fda93a9051d21f2c79708a6 /internal/llm/provider.go | |
| parent | d088267f55c45a7ffd90a056d56e02da61b525fc (diff) | |
feat: add native Anthropic API provider support
- Implement new anthropicClient with full Client interface
- Add Streamer interface for token-by-token streaming via SSE
- Add Anthropic Messages API v1 integration with proper headers
- Support claude-3-5-sonnet-20241022 as default model
- Add configuration via [anthropic] TOML section
- Add environment variable overrides (HEXAI_ANTHROPIC_*)
- Support both HEXAI_ANTHROPIC_API_KEY and ANTHROPIC_API_KEY fallback
- Integrate Anthropic key handling in LSP, CLI, and llmutils
- Update provider factory to support 'anthropic' provider name
- Add 11 comprehensive unit tests for Anthropic client
- Update config.toml.example with [anthropic] section
- Update NewFromConfig() signature to accept anthropicAPIKey parameter
- All 51 internal LLM tests pass (11 new Anthropic tests + 40 existing)
Anthropic models can be accessed via:
[anthropic]
model = "claude-3-5-sonnet-20241022"
base_url = "https://api.anthropic.com/v1"
temperature = 0.2
or environment:
export HEXAI_PROVIDER="anthropic"
export HEXAI_ANTHROPIC_API_KEY="your-key"
Amp-Thread-ID: https://ampcode.com/threads/T-019c0af1-f215-72cf-9940-b014b1a9576b
Co-authored-by: Amp <amp@ampcode.com>
Diffstat (limited to 'internal/llm/provider.go')
| -rw-r--r-- | internal/llm/provider.go | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/internal/llm/provider.go b/internal/llm/provider.go index b2c47e4..ae840b0 100644 --- a/internal/llm/provider.go +++ b/internal/llm/provider.go @@ -81,12 +81,16 @@ type Config struct { CopilotBaseURL string CopilotModel string CopilotTemperature *float64 + // Anthropic options + AnthropicBaseURL string + AnthropicModel string + AnthropicTemperature *float64 } // 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, openRouterAPIKey, copilotAPIKey string) (Client, error) { +func NewFromConfig(cfg Config, openAIAPIKey, openRouterAPIKey, copilotAPIKey, anthropicAPIKey string) (Client, error) { p := strings.ToLower(strings.TrimSpace(cfg.Provider)) if p == "" { p = "openai" @@ -140,6 +144,15 @@ func NewFromConfig(cfg Config, openAIAPIKey, openRouterAPIKey, copilotAPIKey str cfg.CopilotTemperature = &t } return newCopilot(cfg.CopilotBaseURL, cfg.CopilotModel, copilotAPIKey, cfg.CopilotTemperature), nil + case "anthropic": + if strings.TrimSpace(anthropicAPIKey) == "" { + return nil, errors.New("missing ANTHROPIC_API_KEY for provider anthropic") + } + if cfg.AnthropicTemperature == nil { + t := 0.2 + cfg.AnthropicTemperature = &t + } + return newAnthropic(cfg.AnthropicBaseURL, cfg.AnthropicModel, anthropicAPIKey, cfg.AnthropicTemperature), nil default: return nil, errors.New("unknown LLM provider: " + p) } |
