ai-gateway/providers/claude/base.go

51 lines
1.0 KiB
Go
Raw Permalink Normal View History

package claude
2023-11-28 10:32:26 +00:00
import (
"one-api/providers/base"
2023-11-28 10:32:26 +00:00
"github.com/gin-gonic/gin"
)
2023-12-02 10:14:48 +00:00
type ClaudeProviderFactory struct{}
2023-11-28 10:32:26 +00:00
2023-12-02 10:14:48 +00:00
// 创建 ClaudeProvider
func (f ClaudeProviderFactory) Create(c *gin.Context) base.ProviderInterface {
2023-11-28 10:32:26 +00:00
return &ClaudeProvider{
BaseProvider: base.BaseProvider{
2023-11-28 10:32:26 +00:00
BaseURL: "https://api.anthropic.com",
ChatCompletions: "/v1/complete",
Context: c,
},
}
}
2023-12-02 10:14:48 +00:00
type ClaudeProvider struct {
base.BaseProvider
}
2023-11-28 10:32:26 +00:00
// 获取请求头
func (p *ClaudeProvider) GetRequestHeaders() (headers map[string]string) {
headers = make(map[string]string)
p.CommonRequestHeaders(headers)
2023-11-28 10:32:26 +00:00
headers["x-api-key"] = p.Channel.Key
2023-11-28 10:32:26 +00:00
anthropicVersion := p.Context.Request.Header.Get("anthropic-version")
if anthropicVersion == "" {
anthropicVersion = "2023-06-01"
}
headers["anthropic-version"] = anthropicVersion
return headers
}
func stopReasonClaude2OpenAI(reason string) string {
switch reason {
case "stop_sequence":
return "stop"
case "max_tokens":
return "length"
default:
return reason
}
}