|
@@ -7,12 +7,14 @@ import (
|
|
|
"net/http"
|
|
"net/http"
|
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
+ "github.com/QuantumNous/new-api/common"
|
|
|
"github.com/QuantumNous/new-api/dto"
|
|
"github.com/QuantumNous/new-api/dto"
|
|
|
"github.com/QuantumNous/new-api/relay/channel"
|
|
"github.com/QuantumNous/new-api/relay/channel"
|
|
|
"github.com/QuantumNous/new-api/relay/channel/claude"
|
|
"github.com/QuantumNous/new-api/relay/channel/claude"
|
|
|
"github.com/QuantumNous/new-api/relay/channel/openai"
|
|
"github.com/QuantumNous/new-api/relay/channel/openai"
|
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
|
|
"github.com/QuantumNous/new-api/relay/constant"
|
|
"github.com/QuantumNous/new-api/relay/constant"
|
|
|
|
|
+ "github.com/QuantumNous/new-api/setting/reasoning"
|
|
|
"github.com/QuantumNous/new-api/types"
|
|
"github.com/QuantumNous/new-api/types"
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
|
)
|
|
)
|
|
@@ -27,7 +29,18 @@ func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dt
|
|
|
|
|
|
|
|
func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
|
|
func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
|
|
|
adaptor := claude.Adaptor{}
|
|
adaptor := claude.Adaptor{}
|
|
|
- return adaptor.ConvertClaudeRequest(c, info, req)
|
|
|
|
|
|
|
+ convertedRequest, err := adaptor.ConvertClaudeRequest(c, info, req)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ claudeRequest, ok := convertedRequest.(*dto.ClaudeRequest)
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ return convertedRequest, nil
|
|
|
|
|
+ }
|
|
|
|
|
+ if err := applyDeepSeekV4ClaudeThinkingSuffix(info, claudeRequest); err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ return claudeRequest, 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) {
|
|
@@ -71,9 +84,71 @@ 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")
|
|
|
}
|
|
}
|
|
|
|
|
+ if err := applyDeepSeekV4OpenAIThinkingSuffix(info, request); err != nil {
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return request, nil
|
|
return request, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func applyDeepSeekV4OpenAIThinkingSuffix(info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) error {
|
|
|
|
|
+ modelName := request.Model
|
|
|
|
|
+ if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
|
|
|
|
|
+ modelName = info.UpstreamModelName
|
|
|
|
|
+ }
|
|
|
|
|
+ baseModel, thinkingType, effort, ok := reasoning.ParseDeepSeekV4ThinkingSuffix(modelName)
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ thinking, err := common.Marshal(map[string]string{
|
|
|
|
|
+ "type": thinkingType,
|
|
|
|
|
+ })
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return fmt.Errorf("error marshalling thinking: %w", err)
|
|
|
|
|
+ }
|
|
|
|
|
+ request.Model = baseModel
|
|
|
|
|
+ request.THINKING = thinking
|
|
|
|
|
+ request.ReasoningEffort = effort
|
|
|
|
|
+ if info != nil {
|
|
|
|
|
+ if info.ChannelMeta != nil {
|
|
|
|
|
+ info.UpstreamModelName = baseModel
|
|
|
|
|
+ }
|
|
|
|
|
+ info.ReasoningEffort = effort
|
|
|
|
|
+ }
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func applyDeepSeekV4ClaudeThinkingSuffix(info *relaycommon.RelayInfo, request *dto.ClaudeRequest) error {
|
|
|
|
|
+ modelName := request.Model
|
|
|
|
|
+ if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
|
|
|
|
|
+ modelName = info.UpstreamModelName
|
|
|
|
|
+ }
|
|
|
|
|
+ baseModel, thinkingType, effort, ok := reasoning.ParseDeepSeekV4ThinkingSuffix(modelName)
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ request.Model = baseModel
|
|
|
|
|
+ request.Thinking = &dto.Thinking{Type: thinkingType}
|
|
|
|
|
+ if effort == "" {
|
|
|
|
|
+ request.OutputConfig = nil
|
|
|
|
|
+ } else {
|
|
|
|
|
+ outputConfig, err := common.Marshal(map[string]string{
|
|
|
|
|
+ "effort": effort,
|
|
|
|
|
+ })
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return fmt.Errorf("error marshalling output_config: %w", err)
|
|
|
|
|
+ }
|
|
|
|
|
+ request.OutputConfig = outputConfig
|
|
|
|
|
+ }
|
|
|
|
|
+ if info != nil {
|
|
|
|
|
+ if info.ChannelMeta != nil {
|
|
|
|
|
+ info.UpstreamModelName = baseModel
|
|
|
|
|
+ }
|
|
|
|
|
+ info.ReasoningEffort = effort
|
|
|
|
|
+ }
|
|
|
|
|
+ return nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
|
|
func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
|
|
|
return nil, nil
|
|
return nil, nil
|
|
|
}
|
|
}
|