🎨 ai调用时新增详细日志
This commit is contained in:
@@ -56,6 +56,49 @@ func (s *AiProxyService) ProcessChatCompletion(ctx context.Context, req *request
|
|||||||
if regexLogs.TotalMatches > 0 || len(regexLogs.InputScripts) > 0 || len(regexLogs.OutputScripts) > 0 {
|
if regexLogs.TotalMatches > 0 || len(regexLogs.InputScripts) > 0 || len(regexLogs.OutputScripts) > 0 {
|
||||||
resp.RegexLogs = regexLogs
|
resp.RegexLogs = regexLogs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 记录匹配到正则时的脚本名称
|
||||||
|
if regexLogs.TotalMatches > 0 {
|
||||||
|
matchedScripts := make([]string, 0)
|
||||||
|
for _, scriptLog := range regexLogs.InputScripts {
|
||||||
|
if scriptLog.MatchCount > 0 {
|
||||||
|
matchedScripts = append(matchedScripts, scriptLog.ScriptName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, scriptLog := range regexLogs.OutputScripts {
|
||||||
|
if scriptLog.MatchCount > 0 {
|
||||||
|
matchedScripts = append(matchedScripts, scriptLog.ScriptName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录接口调用参数和 AI 输出内容
|
||||||
|
aiOutput := resp.Choices[0].Message.Content
|
||||||
|
global.GVA_LOG.Info("AI 请求完成",
|
||||||
|
zap.Any("request", req),
|
||||||
|
zap.String("ai_output", aiOutput),
|
||||||
|
zap.Strings("matched_regex_scripts", matchedScripts),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
// 未匹配到正则时仅记录请求和输出
|
||||||
|
aiOutput := resp.Choices[0].Message.Content
|
||||||
|
global.GVA_LOG.Info("AI 请求完成(无正则匹配)",
|
||||||
|
zap.Any("request", req),
|
||||||
|
zap.String("ai_output", aiOutput),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 无预设或无注入器时也记录基础请求与输出
|
||||||
|
if len(resp.Choices) > 0 {
|
||||||
|
aiOutput := resp.Choices[0].Message.Content
|
||||||
|
global.GVA_LOG.Info("AI 请求完成(无预设/注入器)",
|
||||||
|
zap.Any("request", req),
|
||||||
|
zap.String("ai_output", aiOutput),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
global.GVA_LOG.Info("AI 请求完成(无输出)",
|
||||||
|
zap.Any("request", req),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
@@ -200,6 +243,9 @@ func (s *AiProxyService) forwardStreamRequest(c *gin.Context, provider *app.AiPr
|
|||||||
return fmt.Errorf("不支持流式响应")
|
return fmt.Errorf("不支持流式响应")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 聚合 AI 输出内容用于日志
|
||||||
|
var fullContent bytes.Buffer
|
||||||
|
|
||||||
for {
|
for {
|
||||||
line, err := reader.ReadBytes('\n')
|
line, err := reader.ReadBytes('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -234,6 +280,8 @@ func (s *AiProxyService) forwardStreamRequest(c *gin.Context, provider *app.AiPr
|
|||||||
|
|
||||||
// 应用输出正则处理
|
// 应用输出正则处理
|
||||||
if injector != nil && len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != "" {
|
if injector != nil && 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)
|
chunk.Choices[0].Delta.Content = injector.ProcessResponse(chunk.Choices[0].Delta.Content)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,6 +294,40 @@ func (s *AiProxyService) forwardStreamRequest(c *gin.Context, provider *app.AiPr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 流式请求结束后记录日志
|
||||||
|
if injector != nil {
|
||||||
|
regexLogs := injector.GetRegexLogs()
|
||||||
|
|
||||||
|
if regexLogs != nil && (regexLogs.TotalMatches > 0 || len(regexLogs.InputScripts) > 0 || len(regexLogs.OutputScripts) > 0) {
|
||||||
|
matchedScripts := make([]string, 0)
|
||||||
|
for _, scriptLog := range regexLogs.InputScripts {
|
||||||
|
if scriptLog.MatchCount > 0 {
|
||||||
|
matchedScripts = append(matchedScripts, scriptLog.ScriptName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, scriptLog := range regexLogs.OutputScripts {
|
||||||
|
if scriptLog.MatchCount > 0 {
|
||||||
|
matchedScripts = append(matchedScripts, scriptLog.ScriptName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
global.GVA_LOG.Info("AI 流式请求完成",
|
||||||
|
zap.Any("request", req),
|
||||||
|
zap.String("ai_output", fullContent.String()),
|
||||||
|
zap.Strings("matched_regex_scripts", matchedScripts),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
global.GVA_LOG.Info("AI 流式请求完成(无正则匹配)",
|
||||||
|
zap.Any("request", req),
|
||||||
|
zap.String("ai_output", fullContent.String()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
global.GVA_LOG.Info("AI 流式请求完成(无预设/注入器)",
|
||||||
|
zap.Any("request", req),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user