feat: enhance Adaptor to support multiple relay modes in request handling
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
"one-api/relay/channel"
|
"one-api/relay/channel"
|
||||||
"one-api/relay/channel/openai"
|
"one-api/relay/channel/openai"
|
||||||
relaycommon "one-api/relay/common"
|
relaycommon "one-api/relay/common"
|
||||||
|
"one-api/relay/constant"
|
||||||
"one-api/types"
|
"one-api/types"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -42,7 +43,20 @@ func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
||||||
return fmt.Sprintf("%s/v2/chat/completions", info.BaseUrl), nil
|
switch info.RelayMode {
|
||||||
|
case constant.RelayModeChatCompletions:
|
||||||
|
return fmt.Sprintf("%s/v2/chat/completions", info.BaseUrl), nil
|
||||||
|
case constant.RelayModeEmbeddings:
|
||||||
|
return fmt.Sprintf("%s/v2/embeddings", info.BaseUrl), nil
|
||||||
|
case constant.RelayModeImagesGenerations:
|
||||||
|
return fmt.Sprintf("%s/v2/images/generations", info.BaseUrl), nil
|
||||||
|
case constant.RelayModeImagesEdits:
|
||||||
|
return fmt.Sprintf("%s/v2/images/edits", info.BaseUrl), nil
|
||||||
|
case constant.RelayModeRerank:
|
||||||
|
return fmt.Sprintf("%s/v2/rerank", info.BaseUrl), nil
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("unsupported relay mode: %d", info.RelayMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
|
||||||
@@ -98,11 +112,8 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
||||||
if info.IsStream {
|
adaptor := openai.Adaptor{}
|
||||||
usage, err = openai.OaiStreamHandler(c, info, resp)
|
usage, err = adaptor.DoResponse(c, resp, info)
|
||||||
} else {
|
|
||||||
usage, err = openai.OpenaiHandler(c, info, resp)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,10 +28,9 @@ func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dt
|
|||||||
return nil, errors.New("not implemented")
|
return nil, errors.New("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertClaudeRequest(*gin.Context, *relaycommon.RelayInfo, *dto.ClaudeRequest) (any, error) {
|
func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
|
||||||
//TODO implement me
|
adaptor := openai.Adaptor{}
|
||||||
panic("implement me")
|
return adaptor.ConvertClaudeRequest(c, info, req)
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
|
||||||
@@ -196,6 +195,10 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
|||||||
return fmt.Sprintf("%s/api/v3/embeddings", info.BaseUrl), nil
|
return fmt.Sprintf("%s/api/v3/embeddings", info.BaseUrl), nil
|
||||||
case constant.RelayModeImagesGenerations:
|
case constant.RelayModeImagesGenerations:
|
||||||
return fmt.Sprintf("%s/api/v3/images/generations", info.BaseUrl), nil
|
return fmt.Sprintf("%s/api/v3/images/generations", info.BaseUrl), nil
|
||||||
|
case constant.RelayModeImagesEdits:
|
||||||
|
return fmt.Sprintf("%s/api/v3/images/edits", info.BaseUrl), nil
|
||||||
|
case constant.RelayModeRerank:
|
||||||
|
return fmt.Sprintf("%s/api/v3/rerank", info.BaseUrl), nil
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("unsupported relay mode: %d", info.RelayMode)
|
return "", fmt.Errorf("unsupported relay mode: %d", info.RelayMode)
|
||||||
@@ -232,18 +235,8 @@ func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, request
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
|
||||||
switch info.RelayMode {
|
adaptor := openai.Adaptor{}
|
||||||
case constant.RelayModeChatCompletions:
|
usage, err = adaptor.DoResponse(c, resp, info)
|
||||||
if info.IsStream {
|
|
||||||
usage, err = openai.OaiStreamHandler(c, info, resp)
|
|
||||||
} else {
|
|
||||||
usage, err = openai.OpenaiHandler(c, info, resp)
|
|
||||||
}
|
|
||||||
case constant.RelayModeEmbeddings:
|
|
||||||
usage, err = openai.OpenaiHandler(c, info, resp)
|
|
||||||
case constant.RelayModeImagesGenerations, constant.RelayModeImagesEdits:
|
|
||||||
usage, err = openai.OpenaiHandlerWithUsage(c, info, resp)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user