1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
@Component
public class StructuredOutputInvoker {
private static final String STRICT_JSON_INSTRUCTION = """
请仅返回可被 JSON 解析器直接解析的 JSON 对象,并严格满足字段结构要求:
1) 不要输出 Markdown 代码块(如 ```json)。
2) 不要输出任何解释文字、前后缀、注释。
3) 所有字符串内引号必须正确转义。
""";
private static final String METRIC_INVOCATIONS = "app.ai.structured_output.invocations";
private static final String METRIC_ATTEMPTS = "app.ai.structured_output.attempts";
private static final String METRIC_LATENCY = "app.ai.structured_output.latency";
private final int maxAttempts;
private final boolean includeLastErrorInRetryPrompt;
private final boolean retryUseRepairPrompt;
private final boolean retryAppendStrictJsonInstruction;
private final int errorMessageMaxLength;
private final boolean metricsEnabled;
private final MeterRegistry meterRegistry;
public StructuredOutputInvoker(
StructuredOutputProperties properties,
@Autowired(required = false) MeterRegistry meterRegistry
) {
this.maxAttempts = Math.max(1, properties.getStructuredMaxAttempts());
this.includeLastErrorInRetryPrompt = properties.isStructuredIncludeLastError();
this.retryUseRepairPrompt = properties.isStructuredRetryUseRepairPrompt();
this.retryAppendStrictJsonInstruction = properties.isStructuredRetryAppendStrictJsonInstruction();
this.errorMessageMaxLength = Math.max(20, properties.getStructuredErrorMessageMaxLength());
this.metricsEnabled = properties.isStructuredMetricsEnabled();
this.meterRegistry = meterRegistry;
}
public <T> T invoke(
ChatClient chatClient,
String systemPromptWithFormat,
String userPrompt,
BeanOutputConverter<T> outputConverter,
ErrorCode errorCode,
String errorPrefix,
String logContext,
Logger log
) {
long startNanos = System.nanoTime();
String contextTag = normalizeContextTag(logContext);
Exception lastError = null;
for (int attempt = 1; attempt <= maxAttempts; attempt++) {
// 构建提示词:首次尝试用原始提示词,重试时追加错误信息
String attemptSystemPrompt = attempt == 1
? systemPromptWithFormat
: buildRetrySystemPrompt(systemPromptWithFormat, lastError);
try {
T result = chatClient.prompt()
.system(attemptSystemPrompt)
.user(userPrompt)
.call()
.entity(outputConverter);
recordAttempt(contextTag, STATUS_SUCCESS);
recordInvocation(contextTag, STATUS_SUCCESS, startNanos);
return result;
} catch (Exception e) {
lastError = e;
recordAttempt(contextTag, STATUS_FAILURE);
if (attempt < maxAttempts) {
log.warn("{}结构化解析失败,准备重试: attempt={}/{}, error={}",
logContext, attempt, maxAttempts, e.getMessage());
} else {
log.error("{}结构化解析失败,已达最大重试次数: attempts={}, error={}",
logContext, maxAttempts, e.getMessage());
}
}
}
recordInvocation(contextTag, STATUS_FAILURE, startNanos);
throw new BusinessException(
errorCode,
errorPrefix + (lastError != null ? lastError.getMessage() : "unknown")
);
}
/**
* 构建重试提示词
* 在原始提示词基础上追加:
* 1. 严格的 JSON 格式说明
* 2. 上次失败的原因
*/
private String buildRetrySystemPrompt(String systemPromptWithFormat, Exception lastError) {
if (!retryUseRepairPrompt) {
return systemPromptWithFormat;
}
StringBuilder prompt = new StringBuilder(systemPromptWithFormat)
.append("\n\n");
if (retryAppendStrictJsonInstruction) {
prompt.append(STRICT_JSON_INSTRUCTION).append('\n');
}
prompt.append("上次输出解析失败,请仅返回合法 JSON。");
if (includeLastErrorInRetryPrompt && lastError != null && lastError.getMessage() != null) {
prompt.append("\n上次失败原因:")
.append(sanitizeErrorMessage(lastError.getMessage()));
}
return prompt.toString();
}
private String sanitizeErrorMessage(String message) {
// 错误信息转成单行,并限制长度
String oneLine = message.replace('\n', ' ').replace('\r', ' ').trim();
if (oneLine.length() > errorMessageMaxLength) {
return oneLine.substring(0, errorMessageMaxLength) + "...";
}
return oneLine;
}
}
|