🎨 优化正则处理时的日志输出
This commit is contained in:
@@ -212,7 +212,7 @@ func (s *AiProxyService) forwardClaudeStreamRequest(c *gin.Context, provider *ap
|
|||||||
reader := bufio.NewReader(httpResp.Body)
|
reader := bufio.NewReader(httpResp.Body)
|
||||||
flusher, _ := c.Writer.(http.Flusher)
|
flusher, _ := c.Writer.(http.Flusher)
|
||||||
|
|
||||||
// 聚合完整输出用于日志
|
// 聚合完整输出用于日志和正则处理
|
||||||
var fullContent bytes.Buffer
|
var fullContent bytes.Buffer
|
||||||
var totalInputTokens, totalOutputTokens int
|
var totalInputTokens, totalOutputTokens int
|
||||||
|
|
||||||
@@ -235,26 +235,30 @@ func (s *AiProxyService) forwardClaudeStreamRequest(c *gin.Context, provider *ap
|
|||||||
totalOutputTokens = chunk.Usage.OutputTokens
|
totalOutputTokens = chunk.Usage.OutputTokens
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理文本内容
|
// 收集文本内容(不在流式传输时应用正则,避免重复处理)
|
||||||
if chunk.Delta != nil && chunk.Delta.Text != "" {
|
if chunk.Delta != nil && chunk.Delta.Text != "" {
|
||||||
fullContent.WriteString(chunk.Delta.Text)
|
fullContent.WriteString(chunk.Delta.Text)
|
||||||
if injector != nil {
|
|
||||||
chunk.Delta.Text = injector.ProcessResponse(chunk.Delta.Text)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
processedData, _ := json.Marshal(chunk)
|
// 直接转发原始响应
|
||||||
c.Writer.Write([]byte("data: "))
|
c.Writer.Write([]byte("data: "))
|
||||||
c.Writer.Write(processedData)
|
c.Writer.Write(data)
|
||||||
c.Writer.Write([]byte("\n\n"))
|
c.Writer.Write([]byte("\n\n"))
|
||||||
flusher.Flush()
|
flusher.Flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 流式结束后,对完整内容应用输出正则处理(仅用于日志记录)
|
||||||
|
processedContent := fullContent.String()
|
||||||
|
if injector != nil && processedContent != "" {
|
||||||
|
processedContent = injector.ProcessResponse(processedContent)
|
||||||
|
}
|
||||||
|
|
||||||
// 记录完整的流式响应日志
|
// 记录完整的流式响应日志
|
||||||
logFields := []zap.Field{
|
logFields := []zap.Field{
|
||||||
zap.String("ai_output", fullContent.String()),
|
zap.String("ai_output_original", fullContent.String()),
|
||||||
|
zap.String("ai_output_processed", processedContent),
|
||||||
zap.Int("input_tokens", totalInputTokens),
|
zap.Int("input_tokens", totalInputTokens),
|
||||||
zap.Int("output_tokens", totalOutputTokens),
|
zap.Int("output_tokens", totalOutputTokens),
|
||||||
zap.Int("total_tokens", totalInputTokens+totalOutputTokens),
|
zap.Int("total_tokens", totalInputTokens+totalOutputTokens),
|
||||||
|
|||||||
Reference in New Issue
Block a user