feat: support Anthropic chat provider
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
const AnthropicBaseURL = "https://api.anthropic.com/v1"
|
||||
|
||||
// AnthropicProvider implements native Anthropic Messages API metadata.
|
||||
type AnthropicProvider struct{}
|
||||
|
||||
func init() {
|
||||
Register(&AnthropicProvider{})
|
||||
}
|
||||
|
||||
func (p *AnthropicProvider) Info() ProviderInfo {
|
||||
return ProviderInfo{
|
||||
Name: ProviderAnthropic,
|
||||
DisplayName: "Anthropic",
|
||||
Description: "Claude models via native Anthropic Messages API",
|
||||
DefaultURLs: map[types.ModelType]string{
|
||||
types.ModelTypeKnowledgeQA: AnthropicBaseURL,
|
||||
},
|
||||
ModelTypes: []types.ModelType{
|
||||
types.ModelTypeKnowledgeQA,
|
||||
},
|
||||
RequiresAuth: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *AnthropicProvider) ValidateConfig(config *Config) error {
|
||||
if config.APIKey == "" {
|
||||
return fmt.Errorf("API key is required for Anthropic provider")
|
||||
}
|
||||
if config.ModelName == "" {
|
||||
return fmt.Errorf("model name is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -15,6 +15,8 @@ type ProviderName string
|
||||
const (
|
||||
// OpenAI
|
||||
ProviderOpenAI ProviderName = "openai"
|
||||
// Anthropic Claude
|
||||
ProviderAnthropic ProviderName = "anthropic"
|
||||
// 阿里云 DashScope
|
||||
ProviderAliyun ProviderName = "aliyun"
|
||||
// 智谱AI (GLM 系列)
|
||||
@@ -78,6 +80,7 @@ func AllProviders() []ProviderName {
|
||||
ProviderQianfan,
|
||||
ProviderQiniu,
|
||||
ProviderOpenAI,
|
||||
ProviderAnthropic,
|
||||
ProviderGemini,
|
||||
ProviderOpenRouter,
|
||||
ProviderJina,
|
||||
@@ -229,6 +232,8 @@ func DetectProvider(baseURL string) ProviderName {
|
||||
return ProviderAzureOpenAI
|
||||
case containsAny(baseURL, "api.openai.com"):
|
||||
return ProviderOpenAI
|
||||
case containsAny(baseURL, "api.anthropic.com"):
|
||||
return ProviderAnthropic
|
||||
case containsAny(baseURL, "api.deepseek.com"):
|
||||
return ProviderDeepSeek
|
||||
case containsAny(baseURL, "generativelanguage.googleapis.com"):
|
||||
|
||||
@@ -36,6 +36,7 @@ func TestDetectProvider(t *testing.T) {
|
||||
expected ProviderName
|
||||
}{
|
||||
{"https://api.openai.com/v1", ProviderOpenAI},
|
||||
{"https://api.anthropic.com/v1", ProviderAnthropic},
|
||||
{"https://openrouter.ai/api/v1", ProviderOpenRouter},
|
||||
{"https://dashscope.aliyuncs.com/compatible-mode/v1", ProviderAliyun},
|
||||
{"https://open.bigmodel.cn/api/paas/v4", ProviderZhipu},
|
||||
@@ -60,6 +61,36 @@ func TestDetectProvider(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnthropicProviderValidation(t *testing.T) {
|
||||
p := &AnthropicProvider{}
|
||||
|
||||
t.Run("valid config", func(t *testing.T) {
|
||||
config := &Config{
|
||||
APIKey: "sk-ant-test",
|
||||
ModelName: "claude-sonnet-4-5",
|
||||
}
|
||||
err := p.ValidateConfig(config)
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("missing API key", func(t *testing.T) {
|
||||
config := &Config{
|
||||
ModelName: "claude-sonnet-4-5",
|
||||
}
|
||||
err := p.ValidateConfig(config)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "API key")
|
||||
})
|
||||
|
||||
t.Run("info", func(t *testing.T) {
|
||||
info := p.Info()
|
||||
assert.Equal(t, ProviderAnthropic, info.Name)
|
||||
assert.Equal(t, AnthropicBaseURL, info.GetDefaultURL(types.ModelTypeKnowledgeQA))
|
||||
assert.Contains(t, info.ModelTypes, types.ModelTypeKnowledgeQA)
|
||||
assert.True(t, info.RequiresAuth)
|
||||
})
|
||||
}
|
||||
|
||||
func TestOpenAIProviderValidation(t *testing.T) {
|
||||
p := &OpenAIProvider{}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user