mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-19 10:06:37 +08:00
- Updated error handling across multiple files with `Laisky/errors/v2` package - Replaced hardcoded error messages with `Laisky/errors` in relay/channel/tencent/adaptor.go - Added a function to check if a request should be retried in relay/controller/relay.go - Removed unused imports and variables, and updated comments in various files - Changed Redis cache handling in model/cache.go - Refactored error handling in relay/channel/tencent/main.go and relay/channel/baidu/main.go - Updated import paths and error handling in model/user.go, model/redemption.go, and controller/github.go - Added import for tiktoken-go package in relay/channel/openai/token.go - Added GetSign and ParseConfig functions in relay/channel/tencent/main.go - Replaced specific error imports with a more general one in relay/channel/ali/adaptor.go - Updated import comments and function calls in relay/channel/ali/adaptor.go - Added checks and custom error messages in model/token.go - Removed unused functions and variables in relay/channel/baidu/adaptor.go - Imported "github.com/Laisky/errors/v2" package in controller/channel-billing.go - Removed unused import packages in [relay/channel/tencent/adaptor.go](http://relay/channel/tencent/adaptor.go) and relay/channel/palm/adaptor.go - Updated go.mod and go.sum files with new dependencies and versions
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package anthropic
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/Laisky/errors/v2"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/songquanpeng/one-api/relay/channel"
|
|
"github.com/songquanpeng/one-api/relay/model"
|
|
"github.com/songquanpeng/one-api/relay/util"
|
|
)
|
|
|
|
type Adaptor struct {
|
|
}
|
|
|
|
func (a *Adaptor) Init(meta *util.RelayMeta) {
|
|
|
|
}
|
|
|
|
func (a *Adaptor) GetRequestURL(meta *util.RelayMeta) (string, error) {
|
|
// https://docs.anthropic.com/claude/reference/messages_post
|
|
// anthopic migrate to Message API
|
|
return fmt.Sprintf("%s/v1/messages", meta.BaseURL), nil
|
|
}
|
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *util.RelayMeta) error {
|
|
channel.SetupCommonRequestHeader(c, req, meta)
|
|
req.Header.Set("x-api-key", meta.APIKey)
|
|
anthropicVersion := c.Request.Header.Get("anthropic-version")
|
|
if anthropicVersion == "" {
|
|
anthropicVersion = "2023-06-01"
|
|
}
|
|
req.Header.Set("anthropic-version", anthropicVersion)
|
|
req.Header.Set("anthropic-beta", "messages-2023-12-15")
|
|
return nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
|
|
if request == nil {
|
|
return nil, errors.New("request is nil")
|
|
}
|
|
|
|
c.Set("claude_model", request.Model)
|
|
return ConvertRequest(*request), nil
|
|
}
|
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, meta *util.RelayMeta, requestBody io.Reader) (*http.Response, error) {
|
|
return channel.DoRequestHelper(a, c, meta, requestBody)
|
|
}
|
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *util.RelayMeta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
|
if meta.IsStream {
|
|
err, usage = StreamHandler(c, resp)
|
|
} else {
|
|
err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (a *Adaptor) GetModelList() []string {
|
|
return ModelList
|
|
}
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
return "anthropic"
|
|
}
|