🎨 优化正则处理逻辑

This commit is contained in:
2026-03-04 20:01:51 +08:00
parent 354e01992a
commit 65803a846f
2 changed files with 37 additions and 10 deletions

View File

@@ -196,6 +196,13 @@ func (s *AiProxyService) forwardRequest(ctx context.Context, provider *app.AiPro
return nil, fmt.Errorf("序列化请求失败: %w", err)
}
// 上游请求参数
global.GVA_LOG.Info("转发 ChatCompletion 请求到上游",
zap.String("provider", provider.Name),
zap.String("model", req.Model),
zap.Any("messages", req.Messages),
)
url := strings.TrimRight(provider.BaseURL, "/") + "/v1/chat/completions"
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(reqBody))
if err != nil {
@@ -238,6 +245,12 @@ func (s *AiProxyService) forwardStreamRequest(c *gin.Context, provider *app.AiPr
if err != nil {
return err
}
// 上游请求参数
global.GVA_LOG.Info("转发 ChatCompletion 流式请求到上游",
zap.String("provider", provider.Name),
zap.String("model", req.Model),
zap.Any("messages", req.Messages),
)
url := strings.TrimRight(provider.BaseURL, "/") + "/v1/chat/completions"
httpReq, err := http.NewRequestWithContext(c.Request.Context(), "POST", url, bytes.NewReader(reqBody))
@@ -302,14 +315,21 @@ func (s *AiProxyService) forwardStreamRequest(c *gin.Context, provider *app.AiPr
continue
}
// 收集原始内容(不在流式传输时应用正则,避免重复处理
// 收集原始内容并应用正则处理
if len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != "" {
fullContent.WriteString(chunk.Choices[0].Delta.Content)
originalContent := chunk.Choices[0].Delta.Content
fullContent.WriteString(originalContent)
// 应用输出正则处理
if injector != nil {
chunk.Choices[0].Delta.Content = injector.ProcessResponse(originalContent)
}
}
// 直接转发原始响应
// 重新序列化并转发处理后的响应
processedData, _ := json.Marshal(chunk)
c.Writer.Write([]byte("data: "))
c.Writer.Write(data)
c.Writer.Write(processedData)
c.Writer.Write([]byte("\n\n"))
flusher.Flush()
}