mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	feat: add warning in log when system prompt is reset
This commit is contained in:
		@@ -91,7 +91,7 @@ func preConsumeQuota(ctx context.Context, textRequest *relaymodel.GeneralOpenAIR
 | 
			
		||||
	return preConsumedQuota, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func postConsumeQuota(ctx context.Context, usage *relaymodel.Usage, meta *meta.Meta, textRequest *relaymodel.GeneralOpenAIRequest, ratio float64, preConsumedQuota int64, modelRatio float64, groupRatio float64) {
 | 
			
		||||
func postConsumeQuota(ctx context.Context, usage *relaymodel.Usage, meta *meta.Meta, textRequest *relaymodel.GeneralOpenAIRequest, ratio float64, preConsumedQuota int64, modelRatio float64, groupRatio float64, systemPromptReset bool) {
 | 
			
		||||
	if usage == nil {
 | 
			
		||||
		logger.Error(ctx, "usage is nil, which is unexpected")
 | 
			
		||||
		return
 | 
			
		||||
@@ -119,7 +119,11 @@ func postConsumeQuota(ctx context.Context, usage *relaymodel.Usage, meta *meta.M
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logger.Error(ctx, "error update user quota cache: "+err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f,补全倍率 %.2f", modelRatio, groupRatio, completionRatio)
 | 
			
		||||
	var extraLog string
 | 
			
		||||
	if systemPromptReset {
 | 
			
		||||
		extraLog = " (注意系统提示词已被重置)"
 | 
			
		||||
	}
 | 
			
		||||
	logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f,补全倍率 %.2f%s", modelRatio, groupRatio, completionRatio, extraLog)
 | 
			
		||||
	model.RecordConsumeLog(ctx, meta.UserId, meta.ChannelId, promptTokens, completionTokens, textRequest.Model, meta.TokenName, quota, logContent)
 | 
			
		||||
	model.UpdateUserUsedQuotaAndRequestCount(meta.UserId, quota)
 | 
			
		||||
	model.UpdateChannelUsedQuota(meta.ChannelId, quota)
 | 
			
		||||
@@ -156,21 +160,22 @@ func isErrorHappened(meta *meta.Meta, resp *http.Response) bool {
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func setSystemPrompt(ctx context.Context, request *relaymodel.GeneralOpenAIRequest, prompt string) {
 | 
			
		||||
func setSystemPrompt(ctx context.Context, request *relaymodel.GeneralOpenAIRequest, prompt string) (reset bool) {
 | 
			
		||||
	if prompt == "" {
 | 
			
		||||
		return
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	if len(request.Messages) == 0 {
 | 
			
		||||
		return
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	if request.Messages[0].Role == role.System {
 | 
			
		||||
		request.Messages[0].Content = prompt
 | 
			
		||||
		logger.Infof(ctx, "rewrite system prompt")
 | 
			
		||||
		return
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
	request.Messages = append([]relaymodel.Message{{
 | 
			
		||||
		Role:    role.System,
 | 
			
		||||
		Content: prompt,
 | 
			
		||||
	}}, request.Messages...)
 | 
			
		||||
	logger.Infof(ctx, "add system prompt")
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -37,7 +37,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
 | 
			
		||||
	setSystemPrompt(ctx, textRequest, meta.SystemPrompt)
 | 
			
		||||
	systemPromptReset := setSystemPrompt(ctx, textRequest, meta.SystemPrompt)
 | 
			
		||||
	// get model ratio & group ratio
 | 
			
		||||
	modelRatio := billingratio.GetModelRatio(textRequest.Model, meta.ChannelType)
 | 
			
		||||
	groupRatio := billingratio.GetGroupRatio(meta.Group)
 | 
			
		||||
@@ -82,7 +82,7 @@ func RelayTextHelper(c *gin.Context) *model.ErrorWithStatusCode {
 | 
			
		||||
		return respErr
 | 
			
		||||
	}
 | 
			
		||||
	// post-consume quota
 | 
			
		||||
	go postConsumeQuota(ctx, usage, meta, textRequest, ratio, preConsumedQuota, modelRatio, groupRatio)
 | 
			
		||||
	go postConsumeQuota(ctx, usage, meta, textRequest, ratio, preConsumedQuota, modelRatio, groupRatio, systemPromptReset)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user