2024-01-21 15:21:42 +00:00
|
|
|
package openai
|
|
|
|
|
|
|
|
import (
|
2024-02-17 16:15:31 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-01-21 15:21:42 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-02-17 16:15:31 +00:00
|
|
|
"github.com/songquanpeng/one-api/common"
|
|
|
|
"github.com/songquanpeng/one-api/relay/channel"
|
2024-02-18 08:17:19 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/channel/ai360"
|
2024-03-01 16:55:48 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/channel/baichuan"
|
2024-03-01 17:24:28 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/channel/minimax"
|
2024-02-18 08:17:19 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/channel/moonshot"
|
2024-02-17 16:15:31 +00:00
|
|
|
"github.com/songquanpeng/one-api/relay/model"
|
|
|
|
"github.com/songquanpeng/one-api/relay/util"
|
|
|
|
"io"
|
2024-01-21 15:21:42 +00:00
|
|
|
"net/http"
|
2024-02-17 16:15:31 +00:00
|
|
|
"strings"
|
2024-01-21 15:21:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Adaptor struct {
|
2024-02-18 08:17:19 +00:00
|
|
|
ChannelType int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) Init(meta *util.RelayMeta) {
|
|
|
|
a.ChannelType = meta.ChannelType
|
2024-01-21 15:21:42 +00:00
|
|
|
}
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
func (a *Adaptor) GetRequestURL(meta *util.RelayMeta) (string, error) {
|
2024-03-01 17:24:28 +00:00
|
|
|
switch meta.ChannelType {
|
|
|
|
case common.ChannelTypeAzure:
|
2024-02-17 16:15:31 +00:00
|
|
|
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/chatgpt-quickstart?pivots=rest-api&tabs=command-line#rest-api
|
|
|
|
requestURL := strings.Split(meta.RequestURLPath, "?")[0]
|
|
|
|
requestURL = fmt.Sprintf("%s?api-version=%s", requestURL, meta.APIVersion)
|
|
|
|
task := strings.TrimPrefix(requestURL, "/v1/")
|
|
|
|
model_ := meta.ActualModelName
|
|
|
|
model_ = strings.Replace(model_, ".", "", -1)
|
|
|
|
// https://github.com/songquanpeng/one-api/issues/67
|
|
|
|
model_ = strings.TrimSuffix(model_, "-0301")
|
|
|
|
model_ = strings.TrimSuffix(model_, "-0314")
|
|
|
|
model_ = strings.TrimSuffix(model_, "-0613")
|
|
|
|
|
|
|
|
requestURL = fmt.Sprintf("/openai/deployments/%s/%s", model_, task)
|
|
|
|
return util.GetFullRequestURL(meta.BaseURL, requestURL, meta.ChannelType), nil
|
2024-03-01 17:24:28 +00:00
|
|
|
case common.ChannelTypeMinimax:
|
|
|
|
return minimax.GetRequestURL(meta)
|
|
|
|
default:
|
|
|
|
return util.GetFullRequestURL(meta.BaseURL, meta.RequestURLPath, meta.ChannelType), nil
|
2024-02-17 16:15:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *util.RelayMeta) error {
|
|
|
|
channel.SetupCommonRequestHeader(c, req, meta)
|
|
|
|
if meta.ChannelType == common.ChannelTypeAzure {
|
|
|
|
req.Header.Set("api-key", meta.APIKey)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+meta.APIKey)
|
|
|
|
if meta.ChannelType == common.ChannelTypeOpenRouter {
|
|
|
|
req.Header.Set("HTTP-Referer", "https://github.com/songquanpeng/one-api")
|
|
|
|
req.Header.Set("X-Title", "One API")
|
|
|
|
}
|
2024-01-21 15:21:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, errors.New("request is nil")
|
|
|
|
}
|
|
|
|
return 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 {
|
|
|
|
var responseText string
|
2024-03-01 19:05:25 +00:00
|
|
|
err, responseText, _ = StreamHandler(c, resp, meta.Mode)
|
2024-02-17 16:15:31 +00:00
|
|
|
usage = ResponseText2Usage(responseText, meta.ActualModelName, meta.PromptTokens)
|
|
|
|
} else {
|
|
|
|
err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) GetModelList() []string {
|
2024-02-18 08:17:19 +00:00
|
|
|
switch a.ChannelType {
|
|
|
|
case common.ChannelType360:
|
|
|
|
return ai360.ModelList
|
|
|
|
case common.ChannelTypeMoonshot:
|
|
|
|
return moonshot.ModelList
|
2024-03-01 16:55:48 +00:00
|
|
|
case common.ChannelTypeBaichuan:
|
|
|
|
return baichuan.ModelList
|
2024-03-01 17:24:28 +00:00
|
|
|
case common.ChannelTypeMinimax:
|
|
|
|
return minimax.ModelList
|
2024-02-18 08:17:19 +00:00
|
|
|
default:
|
|
|
|
return ModelList
|
|
|
|
}
|
2024-01-21 15:21:42 +00:00
|
|
|
}
|
|
|
|
|
2024-02-17 16:15:31 +00:00
|
|
|
func (a *Adaptor) GetChannelName() string {
|
2024-02-18 08:17:19 +00:00
|
|
|
switch a.ChannelType {
|
|
|
|
case common.ChannelTypeAzure:
|
|
|
|
return "azure"
|
|
|
|
case common.ChannelType360:
|
|
|
|
return "360"
|
|
|
|
case common.ChannelTypeMoonshot:
|
|
|
|
return "moonshot"
|
2024-03-01 16:55:48 +00:00
|
|
|
case common.ChannelTypeBaichuan:
|
|
|
|
return "baichuan"
|
2024-03-01 17:24:28 +00:00
|
|
|
case common.ChannelTypeMinimax:
|
|
|
|
return "minimax"
|
2024-02-18 08:17:19 +00:00
|
|
|
default:
|
|
|
|
return "openai"
|
|
|
|
}
|
2024-01-21 15:21:42 +00:00
|
|
|
}
|