Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] 메일 생성 ai #69

Merged
merged 1 commit into from
Jul 2, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.Nunbody.domain.Windyflo.dto.res.EmailResDto;
import com.Nunbody.external.ConversationQARes;
import com.Nunbody.external.WindyfloMailClient;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
Expand All @@ -18,41 +19,69 @@
public class WindyfloService {
private final WindyfloMailClient windyfloMailClient;
private final ObjectMapper objectMapper;

public EmailResDto createMail(WindyfloReq windyfloReq) throws IOException {
EmailResDto emailResDto = new EmailResDto("","","");
while(emailResDto.template().isEmpty() || emailResDto.header().isEmpty() || emailResDto.body().isEmpty() ) {
EmailResDto emailResDto = new EmailResDto("", "", "");
while (emailResDto.template().isEmpty() || emailResDto.header().isEmpty() || emailResDto.body().isEmpty()) {
ConversationQARes result = windyfloMailClient.findMailInVectorDB(windyfloReq.prompt());
// emailResDto = extractEmailTemplate(result.getText());
if (result.getText().equals("Hmm, I'm not sure")) {
result = windyfloMailClient.createMail(windyfloReq.prompt());
emailResDto = extractEmailTemplate(windyfloMailClient.createMail(windyfloReq.prompt()));
}
emailResDto = extractEmailTemplate(result.getText());
else
emailResDto = extractEmailTemplate(result.getText());
// System.out.println(result.getText());
}
return emailResDto;
}
public EmailResDto extractEmailTemplate(String input) throws IOException {
// JSON 형식인지 확인
if (input.trim().startsWith("{")) {
// JSON 파싱 로직
JsonNode rootNode = objectMapper.readTree(input);
String data = rootNode.path("data").asText();

String format = extractValue(data, "형식: (.+)");
String subject = extractValue(data, "제목: (.+)");
String body = extractValue(data, "본문: \n```\n([\\s\\S]*?)\n```");

return EmailResDto.of(format, subject, body);
} else {
// 텍스트 파싱 로직
String format = extractValue(input, "형식: (.+)");
String subject = extractValue(input, "제목: (.+)");
String body = extractValue(input, "본문: \n```\n([\\s\\S]*?)\n```");
return EmailResDto.of(format, subject, body);

public EmailResDto extractEmailTemplate(Object input) throws IOException {
String inputString = input instanceof String ? (String) input : input.toString();

try {
JsonNode rootNode = objectMapper.readTree(inputString);

// 'json' 키가 있는지 확인
if (rootNode.has("json")) {
JsonNode jsonNode = rootNode.get("json");

if (jsonNode.isObject() && jsonNode.has("template") && jsonNode.has("header") && jsonNode.has("body")) {
String format = jsonNode.path("template").asText();
String subject = jsonNode.path("header").asText();
String body = jsonNode.path("body").asText();

return EmailResDto.of(format, subject, body);
}
}

// 'json' 키가 없거나 필요한 필드가 없는 경우, 루트 노드에서 직접 확인
if (rootNode.isObject() && rootNode.has("template") && rootNode.has("header") && rootNode.has("body")) {
String format = rootNode.path("template").asText();
String subject = rootNode.path("header").asText();
String body = rootNode.path("body").asText();

return EmailResDto.of(format, subject, body);
}
} catch (JsonProcessingException e) {
// JSON 파싱 실패 시 텍스트 파싱 로직으로 넘어감
}

// JSON 파싱 실패 또는 필요한 필드가 없는 경우 텍스트 파싱 시도
String format = extractValue(inputString, "형식: (.+)");
String subject = extractValue(inputString, "제목: (.+)");
String body = extractValue(inputString, "본문: \n```\n([\\s\\S]*?)\n```");

if (format.isEmpty() && subject.isEmpty() && body.isEmpty()) {
// 텍스트 파싱도 실패한 경우, 전체 입력을 본문으로 사용
return EmailResDto.of("", "", inputString);
}

return EmailResDto.of(format, subject, body);
}

private String extractValue(String data, String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(data);
return matcher.find() ? matcher.group(1) : "";
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/Nunbody/external/WindyfloMailClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.Nunbody.external;

import com.Nunbody.global.config.RestTemplateConfig;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
Expand All @@ -25,13 +26,14 @@ public ConversationQARes findMailInVectorDB(String question) {

return restTemplate.postForObject(FIND_MAIL_URL, request, ConversationQARes.class);
}
public ConversationQARes createMail(String question) {
public JsonNode createMail(String question) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

QuestionRequest requestBody = new QuestionRequest(question);
HttpEntity<QuestionRequest> request = new HttpEntity<>(requestBody, headers);

return restTemplate.postForObject(CREATE_MAIL_URL, request, ConversationQARes.class);
JsonNode ss = restTemplate.postForObject(CREATE_MAIL_URL, request, JsonNode.class);
return ss;
}
}
Loading