|
| 1 | +package com.jobdri.jobdri_api.domain.jobposting.service; |
| 2 | + |
| 3 | +import com.jobdri.jobdri_api.domain.jobposting.dto.request.JobPostingExtractMultipartRequest; |
| 4 | +import com.jobdri.jobdri_api.domain.jobposting.dto.response.JobPostingExtractResponse; |
| 5 | +import com.jobdri.jobdri_api.global.apiPayload.code.GeneralErrorCode; |
| 6 | +import com.jobdri.jobdri_api.global.apiPayload.exception.GeneralException; |
| 7 | +import com.openai.client.OpenAIClient; |
| 8 | +import com.openai.models.responses.ResponseCreateParams; |
| 9 | +import com.openai.models.responses.ResponseInputContent; |
| 10 | +import com.openai.models.responses.ResponseInputImage; |
| 11 | +import com.openai.models.responses.ResponseInputItem; |
| 12 | +import com.openai.models.responses.StructuredResponse; |
| 13 | +import lombok.RequiredArgsConstructor; |
| 14 | +import lombok.extern.slf4j.Slf4j; |
| 15 | +import org.springframework.beans.factory.annotation.Value; |
| 16 | +import org.springframework.stereotype.Service; |
| 17 | +import org.springframework.web.multipart.MultipartFile; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.Base64; |
| 24 | + |
| 25 | +@Service |
| 26 | +@Slf4j |
| 27 | +@RequiredArgsConstructor |
| 28 | +public class JobPostingAiService { |
| 29 | + |
| 30 | + private final OpenAIClient openAIClient; |
| 31 | + |
| 32 | + @Value("${openai.model.job-posting-extractor:gpt-4o-mini}") |
| 33 | + private String extractionModel; |
| 34 | + |
| 35 | + private static final Set<String> SUPPORTED_IMAGE_TYPES = Set.of( |
| 36 | + "image/png", |
| 37 | + "image/jpeg", |
| 38 | + "image/jpg", |
| 39 | + "image/webp", |
| 40 | + "image/gif" |
| 41 | + ); |
| 42 | + |
| 43 | + public JobPostingExtractResponse extractJobPosting(String rawText) { |
| 44 | + return extractJobPosting(rawText, null, null); |
| 45 | + } |
| 46 | + |
| 47 | + public JobPostingExtractResponse extractJobPosting(JobPostingExtractMultipartRequest request) { |
| 48 | + return extractJobPosting(request.getRawText(), request.getImage(), request.getSourceUrl()); |
| 49 | + } |
| 50 | + |
| 51 | + public JobPostingExtractResponse extractJobPosting(String rawText, MultipartFile imageFile, String sourceUrl) { |
| 52 | + validateInput(rawText, imageFile); |
| 53 | + |
| 54 | + List<ResponseInputContent> contents = new ArrayList<>(); |
| 55 | + contents.add(ResponseInputContent.ofInputText( |
| 56 | + com.openai.models.responses.ResponseInputText.builder() |
| 57 | + .text(buildPrompt(rawText, sourceUrl, imageFile != null)) |
| 58 | + .build() |
| 59 | + )); |
| 60 | + |
| 61 | + if (imageFile != null && !imageFile.isEmpty()) { |
| 62 | + contents.add(ResponseInputContent.ofInputImage(buildImageContent(imageFile))); |
| 63 | + } |
| 64 | + |
| 65 | + var params = ResponseCreateParams.builder() |
| 66 | + .model(extractionModel) |
| 67 | + .inputOfResponse(List.of( |
| 68 | + ResponseInputItem.ofMessage( |
| 69 | + ResponseInputItem.Message.builder() |
| 70 | + .role(ResponseInputItem.Message.Role.USER) |
| 71 | + .content(contents) |
| 72 | + .build() |
| 73 | + ) |
| 74 | + )) |
| 75 | + .temperature(0.1) |
| 76 | + .text(JobPostingExtractResponse.class) |
| 77 | + .build(); |
| 78 | + |
| 79 | + try { |
| 80 | + StructuredResponse<JobPostingExtractResponse> response = openAIClient.responses().create(params); |
| 81 | + JobPostingExtractResponse extracted = extractStructuredContent(response); |
| 82 | + |
| 83 | + normalizeResponse(extracted, rawText); |
| 84 | + return extracted; |
| 85 | + |
| 86 | + } catch (Exception e) { |
| 87 | + log.error("채용 공고 추출 OpenAI API 호출 오류: {}", e.getMessage(), e); |
| 88 | + return createFallbackResponse(rawText); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private String buildPrompt(String rawText, String sourceUrl, boolean hasImage) { |
| 93 | + String normalizedRawText = rawText == null ? "" : rawText; |
| 94 | + String normalizedSourceUrl = sourceUrl == null ? "" : sourceUrl; |
| 95 | + |
| 96 | + return """ |
| 97 | + 이 %s는 채용 공고입니다. |
| 98 | + 회사명, 직무명, 주요 업무, 자격 요건, 우대 사항을 추출해주세요. |
| 99 | +
|
| 100 | + 반드시 아래 JSON 형식으로만 응답해주세요. |
| 101 | + 설명 문장, 마크다운, 코드블럭은 포함하지 마세요. |
| 102 | +
|
| 103 | + { |
| 104 | + "companyName": "string", |
| 105 | + "jobTitle": "string", |
| 106 | + "task": "string", |
| 107 | + "requirements": "string", |
| 108 | + "preferredQualifications": "string", |
| 109 | + "rawText": "string", |
| 110 | + "confidence": number |
| 111 | + } |
| 112 | +
|
| 113 | + 규칙: |
| 114 | + 1. 이미지가 있으면 이미지 안의 채용 공고 문구를 읽어 rawText에 정리해주세요. |
| 115 | + 2. 텍스트가 있으면 rawText에는 입력 원문을 최대한 그대로 넣어주세요. |
| 116 | + 3. 이미지와 텍스트가 둘 다 있으면 둘을 함께 참고해서 가장 정확한 값으로 채워주세요. |
| 117 | + 4. 정보가 없거나 확실하지 않으면 해당 필드는 빈 문자열로 두세요. |
| 118 | + 5. confidence는 추출 결과 전체에 대한 신뢰도를 0~1 사이 실수로 반환하세요. |
| 119 | + 6. JSON 외의 다른 텍스트는 절대 출력하지 마세요. |
| 120 | +
|
| 121 | + [원본 URL] |
| 122 | + %s |
| 123 | +
|
| 124 | + [채용 공고 텍스트] |
| 125 | + %s |
| 126 | + """.formatted(hasImage ? "이미지 또는 텍스트" : "텍스트", normalizedSourceUrl, normalizedRawText); |
| 127 | + } |
| 128 | + |
| 129 | + private ResponseInputImage buildImageContent(MultipartFile imageFile) { |
| 130 | + validateImage(imageFile); |
| 131 | + |
| 132 | + try { |
| 133 | + String contentType = imageFile.getContentType(); |
| 134 | + String base64 = Base64.getEncoder().encodeToString(imageFile.getBytes()); |
| 135 | + String dataUrl = "data:%s;base64,%s".formatted(contentType, base64); |
| 136 | + |
| 137 | + return ResponseInputImage.builder() |
| 138 | + .imageUrl(dataUrl) |
| 139 | + .detail(ResponseInputImage.Detail.HIGH) |
| 140 | + .build(); |
| 141 | + } catch (IOException e) { |
| 142 | + throw new GeneralException(GeneralErrorCode.INVALID_PARAMETER, "이미지 파일을 읽을 수 없습니다."); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private JobPostingExtractResponse extractStructuredContent(StructuredResponse<JobPostingExtractResponse> response) { |
| 147 | + return response.output().stream() |
| 148 | + .filter(item -> item.message().isPresent()) |
| 149 | + .flatMap(item -> item.asMessage().content().stream()) |
| 150 | + .filter(content -> content.outputText().isPresent()) |
| 151 | + .map(content -> content.asOutputText()) |
| 152 | + .findFirst() |
| 153 | + .orElseThrow(() -> new GeneralException( |
| 154 | + GeneralErrorCode.INTERNAL_SERVER_ERROR, |
| 155 | + "AI 응답에서 채용 공고 추출 결과를 찾을 수 없습니다." |
| 156 | + )); |
| 157 | + } |
| 158 | + |
| 159 | + private void validateInput(String rawText, MultipartFile imageFile) { |
| 160 | + boolean hasRawText = rawText != null && !rawText.isBlank(); |
| 161 | + boolean hasImage = imageFile != null && !imageFile.isEmpty(); |
| 162 | + |
| 163 | + if (!hasRawText && !hasImage) { |
| 164 | + throw new GeneralException( |
| 165 | + GeneralErrorCode.INVALID_PARAMETER, |
| 166 | + "rawText 또는 image 중 하나는 반드시 포함되어야 합니다." |
| 167 | + ); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private void validateImage(MultipartFile imageFile) { |
| 172 | + String contentType = imageFile.getContentType(); |
| 173 | + if (contentType == null || !SUPPORTED_IMAGE_TYPES.contains(contentType.toLowerCase())) { |
| 174 | + throw new GeneralException( |
| 175 | + GeneralErrorCode.INVALID_PARAMETER, |
| 176 | + "지원하는 이미지 형식은 png, jpg, jpeg, webp, gif 입니다." |
| 177 | + ); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private void normalizeResponse(JobPostingExtractResponse response, String rawText) { |
| 182 | + if (response == null) { |
| 183 | + throw new GeneralException( |
| 184 | + GeneralErrorCode.INTERNAL_SERVER_ERROR, |
| 185 | + "AI 응답이 비어 있습니다." |
| 186 | + ); |
| 187 | + } |
| 188 | + |
| 189 | + if (response.getCompanyName() == null) { |
| 190 | + response.setCompanyName(""); |
| 191 | + } |
| 192 | + if (response.getJobTitle() == null) { |
| 193 | + response.setJobTitle(""); |
| 194 | + } |
| 195 | + if (response.getTask() == null) { |
| 196 | + response.setTask(""); |
| 197 | + } |
| 198 | + if (response.getRequirements() == null) { |
| 199 | + response.setRequirements(""); |
| 200 | + } |
| 201 | + if (response.getPreferredQualifications() == null) { |
| 202 | + response.setPreferredQualifications(""); |
| 203 | + } |
| 204 | + if (response.getRawText() == null || response.getRawText().isBlank()) { |
| 205 | + response.setRawText(rawText == null ? "" : rawText); |
| 206 | + } |
| 207 | + |
| 208 | + double confidence = response.getConfidence(); |
| 209 | + if (Double.isNaN(confidence) || Double.isInfinite(confidence)) { |
| 210 | + response.setConfidence(0.0); |
| 211 | + } else if (confidence < 0.0) { |
| 212 | + response.setConfidence(0.0); |
| 213 | + } else if (confidence > 1.0) { |
| 214 | + response.setConfidence(1.0); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + private JobPostingExtractResponse createFallbackResponse(String rawText) { |
| 219 | + return new JobPostingExtractResponse( |
| 220 | + "", |
| 221 | + "", |
| 222 | + "", |
| 223 | + "", |
| 224 | + "", |
| 225 | + rawText == null ? "" : rawText, |
| 226 | + 0.0 |
| 227 | + ); |
| 228 | + } |
| 229 | +} |
0 commit comments