Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/Minari/cheongForDo/domain/term/entity/Term.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ public void update(TermCommandReq termRequestDTO) {
this.termDifficulty = termRequestDTO.getTermDifficulty();
}

// 쉬운용어풀이 저장
@Column(columnDefinition = "TEXT") // 긴 문자열 저장 가능
private String summary;

// // 쉬운용어풀이 업데이트
// public void updateSummary(String summary) {
// this.summary = summary;
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,37 @@ public class TermaryService {

public String summarizeTerm(TermaryRequestDTO termaryRequestDTO) {
String termNm = termaryRequestDTO.getTermNm();

Term term = termRepository.findByTermNm(termNm);
if (term == null) {
throw new IllegalArgumentException("해당 용어를 찾을 수 없습니다: " + termNm);
}

// 이미 요약된 내용이 있으면 그대로 반환
if (term.getSummary() != null && !term.getSummary().isEmpty()) {
return term.getSummary();
}

// 요약이 없으면 OpenAI API 호출
String termExplain = term.getTermExplain();
String prompt = "다음 경제 용어에 대해 설명해 주세요. 설명은 마크다운 형식으로 작성해 주시고, 각 항목은 제목과 내용으로 나누어 주세요.\n\n" +
String prompt = generatePrompt(termExplain);
String summary = callOpenAiApi(prompt);

// DB에 저장
term.setSummary(summary);
termRepository.save(term);

return summary;
}

private String generatePrompt(String termExplain) {
return "다음 경제 용어에 대해 설명해 주세요. 설명은 마크다운 형식으로 작성해 주시고, 각 항목은 제목과 내용으로 나누어 주세요.\n\n" +
"용어: " + termExplain + "\n\n" +
"설명할 때는 다음과 같은 포맷을 따라주세요:\n" +
"- **정의**: 용어의 기본 정의\n" +
"- **중요성**: 용어의 중요성\n" +
"- **실생활 예시**: 실생활에서의 예시\n" +
"- **요약**: 용어의 간단한 요약\n\n" +
"각 항목을 명확히 나누어 주시고, 설명은 청소년이 이해하기 쉬운 언어로 작성해 주세요.";

return callOpenAiApi(prompt);
}

private String callOpenAiApi(String prompt) {
Expand All @@ -64,7 +78,7 @@ private String callOpenAiApi(String prompt) {
messages.add(message);

request.put("messages", messages);
// request.put("max_tokens", 300); // 토큰에 제한을 둬야할 상황이 생길 떄 사용
// request.put("max_tokens", 300); // 응답 길이 제한

HttpEntity<Map<String, Object>> entity = new HttpEntity<>(request, headers);

Expand Down