Files
prompts-core/service/model_output_parser.go
2026-05-12 13:59:15 +08:00

44 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package service
import (
"encoding/json"
"fmt"
"strings"
)
// ============================================
// 消息解析
// ============================================
func parseModelOutput(text string) (map[string]any, error) {
var result map[string]any
if err := json.Unmarshal([]byte(text), &result); err != nil {
return nil, fmt.Errorf("解析模型输出失败: %w", err)
}
return result, nil
}
// cleanJSONString 清理字符串中可能导致JSON解析失败的字符
func cleanJSONString(s string) string {
s = strings.ReplaceAll(s, "\u2018", "'") // 左单引号
s = strings.ReplaceAll(s, "\u2019", "'") // 右单引号
s = strings.ReplaceAll(s, "\u201c", "\"") // 左双引号 “
s = strings.ReplaceAll(s, "\u201d", "\"") // 右双引号 ”
return s
}
func truncateStr(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
return s[:maxLen]
}
// sessionParseModelOutput 解析会话模型输出
func sessionParseModelOutput(text string) (map[string]any, error) {
var result map[string]any
if err := json.Unmarshal([]byte(text), &result); err != nil {
return nil, fmt.Errorf("解析模型输出失败: %w", err)
}
return result, nil
}