2023-11-29 08:07:09 +00:00
|
|
|
package claude
|
2023-11-28 10:32:26 +00:00
|
|
|
|
|
|
|
import (
|
2023-11-29 08:07:09 +00:00
|
|
|
"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{
|
2023-11-29 08:07:09 +00:00
|
|
|
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)
|
2023-11-29 08:07:09 +00:00
|
|
|
p.CommonRequestHeaders(headers)
|
2023-11-28 10:32:26 +00:00
|
|
|
|
2023-12-26 08:40:50 +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
|
|
|
|
}
|
|
|
|
}
|