feat: support set system prompt for channel (close #1920)

This commit is contained in:
JustSong
2024-11-10 14:53:34 +08:00
parent 92cd46d64f
commit 6eb0770a89
8 changed files with 44 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
package role
const (
System = "system"
Assistant = "assistant"
)

View File

@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/songquanpeng/one-api/relay/constant/role"
"math"
"net/http"
"strings"
@@ -154,3 +155,22 @@ func isErrorHappened(meta *meta.Meta, resp *http.Response) bool {
}
return false
}
func setSystemPrompt(ctx context.Context, request *relaymodel.GeneralOpenAIRequest, prompt string) {
if prompt == "" {
return
}
if len(request.Messages) == 0 {
return
}
if request.Messages[0].Role == role.System {
request.Messages[0].Content = prompt
logger.Infof(ctx, "rewrite system prompt")
return
}
request.Messages = append([]relaymodel.Message{{
Role: role.System,
Content: prompt,
}}, request.Messages...)
logger.Infof(ctx, "add system prompt")
}

View File

@@ -36,6 +36,8 @@ func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode {
meta.OriginModelName = textRequest.Model
textRequest.Model, _ = getMappedModelName(textRequest.Model, meta.ModelMapping)
meta.ActualModelName = textRequest.Model
// set system prompt if not empty
setSystemPrompt(ctx, textRequest, meta.SystemPrompt)
// get model ratio & group ratio
modelRatio := billingratio.GetModelRatio(textRequest.Model, meta.ChannelType)
groupRatio := billingratio.GetGroupRatio(meta.Group)

View File

@@ -30,6 +30,7 @@ type Meta struct {
ActualModelName string
RequestURLPath string
PromptTokens int // only for DoResponse
SystemPrompt string
}
func GetByContext(c *gin.Context) *Meta {
@@ -46,6 +47,7 @@ func GetByContext(c *gin.Context) *Meta {
BaseURL: c.GetString(ctxkey.BaseURL),
APIKey: strings.TrimPrefix(c.Request.Header.Get("Authorization"), "Bearer "),
RequestURLPath: c.Request.URL.String(),
SystemPrompt: c.GetString(ctxkey.SystemPrompt),
}
cfg, ok := c.Get(ctxkey.Config)
if ok {