From 3172c956f7fc0199e26a0ed1851fbf7e081def97 Mon Sep 17 00:00:00 2001 From: etnAtker Date: Mon, 13 Oct 2025 20:06:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0SiliconFlow?= =?UTF-8?q?=E5=9B=BE=E5=83=8F=E7=94=9F=E6=88=90=E6=8E=A5=E5=8F=A3=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=BD=AC=E6=8D=A2=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 将对SiliconFlow渠道的RelayModeImagesGenerations请求,转发至v1/images/generations端点。 2. SiliconFlow图像生成接口额外参数适配。 --- relay/channel/siliconflow/adaptor.go | 24 ++++++++++++++++++++++-- relay/channel/siliconflow/dto.go | 15 +++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/relay/channel/siliconflow/adaptor.go b/relay/channel/siliconflow/adaptor.go index f2fb1b776..57e13d7e6 100644 --- a/relay/channel/siliconflow/adaptor.go +++ b/relay/channel/siliconflow/adaptor.go @@ -6,6 +6,7 @@ import ( "io" "net/http" + "github.com/QuantumNous/new-api/common" "github.com/QuantumNous/new-api/dto" "github.com/QuantumNous/new-api/relay/channel" "github.com/QuantumNous/new-api/relay/channel/openai" @@ -35,8 +36,25 @@ func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInf } func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) { - adaptor := openai.Adaptor{} - return adaptor.ConvertImageRequest(c, info, request) + // 解析extra到SFImageRequest里,以填入SiliconFlow特殊字段。若失败重建一个空的。 + extra, _ := common.Marshal(request.Extra) + sfRequest := &SFImageRequest{} + err := common.Unmarshal(extra, sfRequest) + if err != nil { + sfRequest = &SFImageRequest{} + } + + sfRequest.Model = request.Model + sfRequest.Prompt = request.Prompt + // 优先使用image_size/batch_size,否则使用OpenAI标准的size/n + if sfRequest.ImageSize == "" { + sfRequest.ImageSize = request.Size + } + if sfRequest.BatchSize == 0 { + sfRequest.BatchSize = request.N + } + + return sfRequest, nil } func (a *Adaptor) Init(info *relaycommon.RelayInfo) { @@ -51,6 +69,8 @@ func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) { return fmt.Sprintf("%s/v1/chat/completions", info.ChannelBaseUrl), nil } else if info.RelayMode == constant.RelayModeCompletions { return fmt.Sprintf("%s/v1/completions", info.ChannelBaseUrl), nil + } else if info.RelayMode == constant.RelayModeImagesGenerations { + return fmt.Sprintf("%s/v1/images/generations", info.ChannelBaseUrl), nil } return fmt.Sprintf("%s/v1/chat/completions", info.ChannelBaseUrl), nil } diff --git a/relay/channel/siliconflow/dto.go b/relay/channel/siliconflow/dto.go index f075542c0..100975107 100644 --- a/relay/channel/siliconflow/dto.go +++ b/relay/channel/siliconflow/dto.go @@ -15,3 +15,18 @@ type SFRerankResponse struct { Results []dto.RerankResponseResult `json:"results"` Meta SFMeta `json:"meta"` } + +type SFImageRequest struct { + Model string `json:"model"` + Prompt string `json:"prompt"` + NegativePrompt string `json:"negative_prompt,omitempty"` + ImageSize string `json:"image_size,omitempty"` + BatchSize uint `json:"batch_size,omitempty"` + Seed uint64 `json:"seed,omitempty"` + NumInferenceSteps uint `json:"num_inference_steps,omitempty"` + GuidanceScale float64 `json:"guidance_scale,omitempty"` + Cfg float64 `json:"cfg,omitempty"` + Image string `json:"image,omitempty"` + Image2 string `json:"image2,omitempty"` + Image3 string `json:"image3,omitempty"` +} From fdbc31eb9ab17d1f630aa3d7ae63b4e5c28be195 Mon Sep 17 00:00:00 2001 From: etnAtker Date: Mon, 13 Oct 2025 20:20:08 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DPR=E7=9A=84?= =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 解析ImageRequest的Extra时,处理err 2. DoResponse方法添加RelayModeImagesGenerations(fallthrough) --- relay/channel/siliconflow/adaptor.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/relay/channel/siliconflow/adaptor.go b/relay/channel/siliconflow/adaptor.go index 57e13d7e6..daffff180 100644 --- a/relay/channel/siliconflow/adaptor.go +++ b/relay/channel/siliconflow/adaptor.go @@ -37,11 +37,13 @@ func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInf func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) { // 解析extra到SFImageRequest里,以填入SiliconFlow特殊字段。若失败重建一个空的。 - extra, _ := common.Marshal(request.Extra) sfRequest := &SFImageRequest{} - err := common.Unmarshal(extra, sfRequest) - if err != nil { - sfRequest = &SFImageRequest{} + extra, err := common.Marshal(request.Extra) + if err == nil { + err = common.Unmarshal(extra, sfRequest) + if err != nil { + sfRequest = &SFImageRequest{} + } } sfRequest.Model = request.Model @@ -122,6 +124,8 @@ func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycom fallthrough case constant.RelayModeChatCompletions: fallthrough + case constant.RelayModeImagesGenerations: + fallthrough default: if info.IsStream { usage, err = openai.OaiStreamHandler(c, info, resp)