From bf45a955c33257e2745130529d06d00b41ab013b Mon Sep 17 00:00:00 2001 From: JustSong Date: Sun, 9 Feb 2025 14:41:42 +0800 Subject: [PATCH] fix: update system prompt handling by renaming field and ensuring proper usage in request processing (close #2069) --- relay/controller/text.go | 8 ++++++-- relay/meta/relay_meta.go | 38 +++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/relay/controller/text.go b/relay/controller/text.go index 6a61884d..f912498a 100644 --- a/relay/controller/text.go +++ b/relay/controller/text.go @@ -38,7 +38,7 @@ func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode { textRequest.Model, _ = getMappedModelName(textRequest.Model, meta.ModelMapping) meta.ActualModelName = textRequest.Model // set system prompt if not empty - systemPromptReset := setSystemPrompt(ctx, textRequest, meta.SystemPrompt) + systemPromptReset := setSystemPrompt(ctx, textRequest, meta.ForcedSystemPrompt) // get model ratio & group ratio modelRatio := billingratio.GetModelRatio(textRequest.Model, meta.ChannelType) groupRatio := billingratio.GetGroupRatio(meta.Group) @@ -88,7 +88,11 @@ func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode { } func getRequestBody(c *gin.Context, meta *meta.Meta, textRequest *model.GeneralOpenAIRequest, adaptor adaptor.Adaptor) (io.Reader, error) { - if !config.EnforceIncludeUsage && meta.APIType == apitype.OpenAI && meta.OriginModelName == meta.ActualModelName && meta.ChannelType != channeltype.Baichuan { + if !config.EnforceIncludeUsage && + meta.APIType == apitype.OpenAI && + meta.OriginModelName == meta.ActualModelName && + meta.ChannelType != channeltype.Baichuan && + meta.ForcedSystemPrompt == "" { // no need to convert request for openai return c.Request.Body, nil } diff --git a/relay/meta/relay_meta.go b/relay/meta/relay_meta.go index 6bf070f3..8c74ef8a 100644 --- a/relay/meta/relay_meta.go +++ b/relay/meta/relay_meta.go @@ -30,29 +30,29 @@ type Meta struct { // OriginModelName is the model name from the raw user request OriginModelName string // ActualModelName is the model name after mapping - ActualModelName string - RequestURLPath string - PromptTokens int // only for DoResponse - SystemPrompt string - StartTime time.Time + ActualModelName string + RequestURLPath string + PromptTokens int // only for DoResponse + ForcedSystemPrompt string + StartTime time.Time } func GetByContext(c *gin.Context) *Meta { meta := Meta{ - Mode: relaymode.GetByPath(c.Request.URL.Path), - ChannelType: c.GetInt(ctxkey.Channel), - ChannelId: c.GetInt(ctxkey.ChannelId), - TokenId: c.GetInt(ctxkey.TokenId), - TokenName: c.GetString(ctxkey.TokenName), - UserId: c.GetInt(ctxkey.Id), - Group: c.GetString(ctxkey.Group), - ModelMapping: c.GetStringMapString(ctxkey.ModelMapping), - OriginModelName: c.GetString(ctxkey.RequestModel), - BaseURL: c.GetString(ctxkey.BaseURL), - APIKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "), - RequestURLPath: c.Request.URL.String(), - SystemPrompt: c.GetString(ctxkey.SystemPrompt), - StartTime: time.Now(), + Mode: relaymode.GetByPath(c.Request.URL.Path), + ChannelType: c.GetInt(ctxkey.Channel), + ChannelId: c.GetInt(ctxkey.ChannelId), + TokenId: c.GetInt(ctxkey.TokenId), + TokenName: c.GetString(ctxkey.TokenName), + UserId: c.GetInt(ctxkey.Id), + Group: c.GetString(ctxkey.Group), + ModelMapping: c.GetStringMapString(ctxkey.ModelMapping), + OriginModelName: c.GetString(ctxkey.RequestModel), + BaseURL: c.GetString(ctxkey.BaseURL), + APIKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "), + RequestURLPath: c.Request.URL.String(), + ForcedSystemPrompt: c.GetString(ctxkey.SystemPrompt), + StartTime: time.Now(), } cfg, ok := c.Get(ctxkey.Config) if ok {