|
@@ -3,16 +3,17 @@ package ali
|
|
|
import (
|
|
import (
|
|
|
"errors"
|
|
"errors"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
"io"
|
|
"io"
|
|
|
"net/http"
|
|
"net/http"
|
|
|
"one-api/dto"
|
|
"one-api/dto"
|
|
|
"one-api/relay/channel"
|
|
"one-api/relay/channel"
|
|
|
|
|
+ "one-api/relay/channel/claude"
|
|
|
"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/relay/constant"
|
|
|
"one-api/types"
|
|
"one-api/types"
|
|
|
-
|
|
|
|
|
- "github.com/gin-gonic/gin"
|
|
|
|
|
|
|
+ "strings"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type Adaptor struct {
|
|
type Adaptor struct {
|
|
@@ -23,10 +24,8 @@ 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) {
|
|
|
|
|
- //TODO implement me
|
|
|
|
|
- panic("implement me")
|
|
|
|
|
- return nil, nil
|
|
|
|
|
|
|
+func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
|
|
|
|
|
+ return req, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
|
|
func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
|
|
@@ -34,18 +33,24 @@ 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) {
|
|
|
var fullRequestURL string
|
|
var fullRequestURL string
|
|
|
- switch info.RelayMode {
|
|
|
|
|
- case constant.RelayModeEmbeddings:
|
|
|
|
|
- fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/embeddings", info.BaseUrl)
|
|
|
|
|
- case constant.RelayModeRerank:
|
|
|
|
|
- fullRequestURL = fmt.Sprintf("%s/api/v1/services/rerank/text-rerank/text-rerank", info.BaseUrl)
|
|
|
|
|
- case constant.RelayModeImagesGenerations:
|
|
|
|
|
- fullRequestURL = fmt.Sprintf("%s/api/v1/services/aigc/text2image/image-synthesis", info.BaseUrl)
|
|
|
|
|
- case constant.RelayModeCompletions:
|
|
|
|
|
- fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/completions", info.BaseUrl)
|
|
|
|
|
|
|
+ switch info.RelayFormat {
|
|
|
|
|
+ case relaycommon.RelayFormatClaude:
|
|
|
|
|
+ fullRequestURL = fmt.Sprintf("%s/api/v2/apps/claude-code-proxy/v1/messages", info.BaseUrl)
|
|
|
default:
|
|
default:
|
|
|
- fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/chat/completions", info.BaseUrl)
|
|
|
|
|
|
|
+ switch info.RelayMode {
|
|
|
|
|
+ case constant.RelayModeEmbeddings:
|
|
|
|
|
+ fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/embeddings", info.BaseUrl)
|
|
|
|
|
+ case constant.RelayModeRerank:
|
|
|
|
|
+ fullRequestURL = fmt.Sprintf("%s/api/v1/services/rerank/text-rerank/text-rerank", info.BaseUrl)
|
|
|
|
|
+ case constant.RelayModeImagesGenerations:
|
|
|
|
|
+ fullRequestURL = fmt.Sprintf("%s/api/v1/services/aigc/text2image/image-synthesis", info.BaseUrl)
|
|
|
|
|
+ case constant.RelayModeCompletions:
|
|
|
|
|
+ fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/completions", info.BaseUrl)
|
|
|
|
|
+ default:
|
|
|
|
|
+ fullRequestURL = fmt.Sprintf("%s/compatible-mode/v1/chat/completions", info.BaseUrl)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
return fullRequestURL, nil
|
|
return fullRequestURL, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -65,7 +70,13 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn
|
|
|
if request == nil {
|
|
if request == nil {
|
|
|
return nil, errors.New("request is nil")
|
|
return nil, errors.New("request is nil")
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ // docs: https://bailian.console.aliyun.com/?tab=api#/api/?type=model&url=2712216
|
|
|
|
|
+ // fix: InternalError.Algo.InvalidParameter: The value of the enable_thinking parameter is restricted to True.
|
|
|
|
|
+ if strings.Contains(request.Model, "thinking") {
|
|
|
|
|
+ request.EnableThinking = true
|
|
|
|
|
+ request.Stream = true
|
|
|
|
|
+ info.IsStream = true
|
|
|
|
|
+ }
|
|
|
// fix: ali parameter.enable_thinking must be set to false for non-streaming calls
|
|
// fix: ali parameter.enable_thinking must be set to false for non-streaming calls
|
|
|
if !info.IsStream {
|
|
if !info.IsStream {
|
|
|
request.EnableThinking = false
|
|
request.EnableThinking = false
|
|
@@ -106,18 +117,27 @@ 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 {
|
|
|
|
|
- case constant.RelayModeImagesGenerations:
|
|
|
|
|
- err, usage = aliImageHandler(c, resp, info)
|
|
|
|
|
- case constant.RelayModeEmbeddings:
|
|
|
|
|
- err, usage = aliEmbeddingHandler(c, resp)
|
|
|
|
|
- case constant.RelayModeRerank:
|
|
|
|
|
- err, usage = RerankHandler(c, resp, info)
|
|
|
|
|
- default:
|
|
|
|
|
|
|
+ switch info.RelayFormat {
|
|
|
|
|
+ case relaycommon.RelayFormatClaude:
|
|
|
if info.IsStream {
|
|
if info.IsStream {
|
|
|
- usage, err = openai.OaiStreamHandler(c, info, resp)
|
|
|
|
|
|
|
+ err, usage = claude.ClaudeStreamHandler(c, resp, info, claude.RequestModeMessage)
|
|
|
} else {
|
|
} else {
|
|
|
- usage, err = openai.OpenaiHandler(c, info, resp)
|
|
|
|
|
|
|
+ err, usage = claude.ClaudeHandler(c, resp, info, claude.RequestModeMessage)
|
|
|
|
|
+ }
|
|
|
|
|
+ default:
|
|
|
|
|
+ switch info.RelayMode {
|
|
|
|
|
+ case constant.RelayModeImagesGenerations:
|
|
|
|
|
+ err, usage = aliImageHandler(c, resp, info)
|
|
|
|
|
+ case constant.RelayModeEmbeddings:
|
|
|
|
|
+ err, usage = aliEmbeddingHandler(c, resp)
|
|
|
|
|
+ case constant.RelayModeRerank:
|
|
|
|
|
+ err, usage = RerankHandler(c, resp, info)
|
|
|
|
|
+ default:
|
|
|
|
|
+ if info.IsStream {
|
|
|
|
|
+ usage, err = openai.OaiStreamHandler(c, info, resp)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ usage, err = openai.OpenaiHandler(c, info, resp)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return
|
|
return
|