🎨 优化正则处理时的日志输出
This commit is contained in:
@@ -54,6 +54,24 @@ func (s *AiProxyService) ProcessChatCompletion(ctx context.Context, req *request
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 获取 AI 输出内容
|
||||
aiOutput := ""
|
||||
if len(resp.Choices) > 0 {
|
||||
aiOutput = resp.Choices[0].Message.Content
|
||||
}
|
||||
|
||||
// 应用预设处理(使用同一个 injector 实例)
|
||||
if injector != nil && len(resp.Choices) > 0 {
|
||||
resp.Choices[0].Message.Content = injector.ProcessResponse(resp.Choices[0].Message.Content)
|
||||
aiOutput = resp.Choices[0].Message.Content
|
||||
|
||||
// 添加正则执行日志到响应
|
||||
regexLogs := injector.GetRegexLogs()
|
||||
if regexLogs.TotalMatches > 0 || len(regexLogs.InputScripts) > 0 || len(regexLogs.OutputScripts) > 0 {
|
||||
resp.RegexLogs = regexLogs
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 处理响应并收集正则日志
|
||||
if resp != nil && resp.Usage != nil {
|
||||
// 统一填充 standard_usage,方便上游使用统一格式解析
|
||||
@@ -66,24 +84,6 @@ func (s *AiProxyService) ProcessChatCompletion(ctx context.Context, req *request
|
||||
}
|
||||
}
|
||||
|
||||
// 获取 AI 输出内容
|
||||
aiOutput := ""
|
||||
if len(resp.Choices) > 0 {
|
||||
aiOutput = resp.Choices[0].Message.Content
|
||||
}
|
||||
|
||||
// 应用预设处理
|
||||
if preset != nil && injector != nil && len(resp.Choices) > 0 {
|
||||
resp.Choices[0].Message.Content = injector.ProcessResponse(resp.Choices[0].Message.Content)
|
||||
aiOutput = resp.Choices[0].Message.Content
|
||||
|
||||
// 添加正则执行日志到响应
|
||||
regexLogs := injector.GetRegexLogs()
|
||||
if regexLogs.TotalMatches > 0 || len(regexLogs.InputScripts) > 0 || len(regexLogs.OutputScripts) > 0 {
|
||||
resp.RegexLogs = regexLogs
|
||||
}
|
||||
}
|
||||
|
||||
// 记录响应内容(统一日志输出)
|
||||
logFields := []zap.Field{
|
||||
zap.String("ai_output", aiOutput),
|
||||
@@ -273,7 +273,7 @@ func (s *AiProxyService) forwardStreamRequest(c *gin.Context, provider *app.AiPr
|
||||
return fmt.Errorf("不支持流式响应")
|
||||
}
|
||||
|
||||
// 聚合 AI 输出内容用于日志
|
||||
// 聚合 AI 输出内容用于日志和正则处理
|
||||
var fullContent bytes.Buffer
|
||||
|
||||
for {
|
||||
@@ -308,25 +308,29 @@ func (s *AiProxyService) forwardStreamRequest(c *gin.Context, provider *app.AiPr
|
||||
continue
|
||||
}
|
||||
|
||||
// 应用输出正则处理
|
||||
if injector != nil && len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != "" {
|
||||
// 先记录原始内容到日志汇总
|
||||
// 收集原始内容(不在流式传输时应用正则,避免重复处理)
|
||||
if len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != "" {
|
||||
fullContent.WriteString(chunk.Choices[0].Delta.Content)
|
||||
chunk.Choices[0].Delta.Content = injector.ProcessResponse(chunk.Choices[0].Delta.Content)
|
||||
}
|
||||
|
||||
// 重新序列化并发送
|
||||
processedData, _ := json.Marshal(chunk)
|
||||
// 直接转发原始响应
|
||||
c.Writer.Write([]byte("data: "))
|
||||
c.Writer.Write(processedData)
|
||||
c.Writer.Write(data)
|
||||
c.Writer.Write([]byte("\n\n"))
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
// 流式结束后,对完整内容应用输出正则处理(仅用于日志记录)
|
||||
processedContent := fullContent.String()
|
||||
if injector != nil && processedContent != "" {
|
||||
processedContent = injector.ProcessResponse(processedContent)
|
||||
}
|
||||
|
||||
// 流式请求结束后记录日志
|
||||
logFields := []zap.Field{
|
||||
zap.String("ai_output", fullContent.String()),
|
||||
zap.String("ai_output_original", fullContent.String()),
|
||||
zap.String("ai_output_processed", processedContent),
|
||||
}
|
||||
|
||||
// 添加正则脚本执行日志
|
||||
|
||||
Reference in New Issue
Block a user