fix(provider): use native Anthropic SDK for anthropic protocol with API key

When using the "anthropic" protocol with an API key (non-OAuth), the
factory incorrectly created an OpenAI-compatible HTTPProvider instead of
the native Anthropic SDK provider (ClaudeProvider). This caused requests
to be sent in OpenAI /v1/chat/completions format to the Anthropic API.

Now uses NewClaudeProviderWithBaseURL which sends requests via the native
Anthropic Messages API format.
This commit is contained in:
Crong 2026-04-02 06:11:33 +07:00
parent 415abc8cd4
commit fee238a045

View file

@ -266,22 +266,11 @@ func CreateProviderFromConfig(cfg *config.ModelConfig) (LLMProvider, string, err
} }
return provider, modelID, nil return provider, modelID, nil
} }
// Use API key with HTTP API // Use API key with native Anthropic SDK (not OpenAI-compatible HTTPProvider)
apiBase := cfg.APIBase
if apiBase == "" {
apiBase = "https://api.anthropic.com/v1"
}
if cfg.APIKey() == "" { if cfg.APIKey() == "" {
return nil, "", fmt.Errorf("api_key is required for anthropic protocol (model: %s)", cfg.Model) return nil, "", fmt.Errorf("api_key is required for anthropic protocol (model: %s)", cfg.Model)
} }
return NewHTTPProviderWithMaxTokensFieldAndRequestTimeout( return NewClaudeProviderWithBaseURL(cfg.APIKey(), cfg.APIBase), modelID, nil
cfg.APIKey(),
apiBase,
cfg.Proxy,
cfg.MaxTokensField,
cfg.RequestTimeout,
cfg.ExtraBody,
), modelID, nil
case "anthropic-messages": case "anthropic-messages":
// Anthropic Messages API with native format (HTTP-based, no SDK) // Anthropic Messages API with native format (HTTP-based, no SDK)