Skip to content

Commit 3a3cb50

Browse files
committed
resolve memory overflow by lazy-loading FF14 prompt
1 parent fe0e7b0 commit 3a3cb50

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

src/main/java/com/phantoms/phantomsbackend/common/LLM/FF14Prompt.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,26 @@
1414
@Slf4j
1515
@Component
1616
public class FF14Prompt {
17-
private static String SYSTEM_PROMPT;
18-
19-
@PostConstruct
20-
public void init() {
21-
try {
22-
ClassPathResource resource = new ClassPathResource("prompts/ff14-translator-prompt.md");
23-
Path path = Paths.get(resource.getURI());
24-
SYSTEM_PROMPT = Files.readString(path, StandardCharsets.UTF_8);
25-
log.info("FF14翻译提示词加载成功,长度: {} 字符", SYSTEM_PROMPT.length());
26-
} catch (IOException e) {
27-
log.error("加载FF14翻译提示词失败", e);
28-
SYSTEM_PROMPT = "你是最终幻想14专用翻译器。";
29-
}
30-
}
17+
private static volatile String SYSTEM_PROMPT;
18+
private static final Object LOCK = new Object();
3119

3220
public static String getSystemPrompt() {
21+
// 懒加载:首次调用时才加载
22+
if (SYSTEM_PROMPT == null) {
23+
synchronized (LOCK) {
24+
if (SYSTEM_PROMPT == null) {
25+
try {
26+
ClassPathResource resource = new ClassPathResource("prompts/ff14-translator-prompt.md");
27+
Path path = Paths.get(resource.getURI());
28+
SYSTEM_PROMPT = Files.readString(path, StandardCharsets.UTF_8);
29+
log.info("FF14翻译提示词加载成功,长度: {} 字符", SYSTEM_PROMPT.length());
30+
} catch (IOException e) {
31+
log.error("加载FF14翻译提示词失败", e);
32+
SYSTEM_PROMPT = "你是最终幻想14专用翻译器。";
33+
}
34+
}
35+
}
36+
}
3337
return SYSTEM_PROMPT;
3438
}
3539
}

0 commit comments

Comments
 (0)