2024-04-26 15:05:48 +00:00
|
|
|
package cloudflare
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/songquanpeng/one-api/relay/adaptor"
|
|
|
|
"github.com/songquanpeng/one-api/relay/meta"
|
|
|
|
"github.com/songquanpeng/one-api/relay/model"
|
2024-07-06 05:12:30 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/relaymode"
|
2024-04-26 15:05:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Adaptor struct {
|
|
|
|
meta *meta.Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConvertImageRequest implements adaptor.Adaptor.
|
|
|
|
func (*Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
|
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConvertImageRequest implements adaptor.Adaptor.
|
|
|
|
|
|
|
|
func (a *Adaptor) Init(meta *meta.Meta) {
|
|
|
|
a.meta = meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
2024-07-06 05:12:30 +00:00
|
|
|
switch meta.Mode {
|
|
|
|
case relaymode.ChatCompletions:
|
|
|
|
return fmt.Sprintf("%s/client/v4/accounts/%s/ai/v1/chat/completions", meta.BaseURL, meta.Config.UserID), nil
|
|
|
|
case relaymode.Embeddings:
|
|
|
|
return fmt.Sprintf("%s/client/v4/accounts/%s/ai/v1/embeddings", meta.BaseURL, meta.Config.UserID), nil
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%s/client/v4/accounts/%s/ai/run/%s", meta.BaseURL, meta.Config.UserID, meta.ActualModelName), nil
|
|
|
|
}
|
2024-04-26 15:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
|
|
|
adaptor.SetupCommonRequestHeader(c, req, meta)
|
|
|
|
req.Header.Set("Authorization", "Bearer "+meta.APIKey)
|
|
|
|
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")
|
|
|
|
}
|
2024-07-06 05:12:30 +00:00
|
|
|
switch relayMode {
|
|
|
|
case relaymode.Completions:
|
|
|
|
return ConvertCompletionsRequest(*request), nil
|
|
|
|
case relaymode.ChatCompletions, relaymode.Embeddings:
|
|
|
|
return request, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
}
|
2024-04-26 15:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) {
|
|
|
|
return adaptor.DoRequestHelper(a, c, meta, requestBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
|
|
|
if meta.IsStream {
|
|
|
|
err, usage = StreamHandler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
|
|
|
} else {
|
|
|
|
err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetModelList() []string {
|
|
|
|
return ModelList
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
|
|
return "cloudflare"
|
|
|
|
}
|