-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from SUIN-BUNDANG-LINE/develop
설문이용 v1.0.0 백엔드 배포
- Loading branch information
Showing
143 changed files
with
3,560 additions
and
542 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/kotlin/com/sbl/sulmun2yong/ai/adapter/GenerateAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.sbl.sulmun2yong.ai.adapter | ||
|
||
import com.sbl.sulmun2yong.ai.dto.ChatSessionIdWithSurveyGeneratedByAI | ||
import com.sbl.sulmun2yong.ai.dto.response.AISurveyGenerationResponse | ||
import com.sbl.sulmun2yong.ai.exception.SurveyGenerationByAIFailedException | ||
import com.sbl.sulmun2yong.global.error.PythonServerExceptionMapper | ||
import com.sbl.sulmun2yong.survey.dto.response.SurveyMakeInfoResponse | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.client.HttpClientErrorException | ||
import org.springframework.web.client.RestTemplate | ||
|
||
@Component | ||
class GenerateAdapter( | ||
@Value("\${ai-server.base-url}") | ||
private val aiServerBaseUrl: String, | ||
private val restTemplate: RestTemplate, | ||
) { | ||
fun requestSurveyGenerationWithFileUrl( | ||
job: String, | ||
groupName: String, | ||
fileUrl: String, | ||
userPrompt: String, | ||
): AISurveyGenerationResponse { | ||
val requestUrl = "$aiServerBaseUrl/generate/survey/file-url" | ||
|
||
val requestBody = | ||
mapOf( | ||
"job" to job, | ||
"group_name" to groupName, | ||
"file_url" to fileUrl, | ||
"user_prompt" to userPrompt, | ||
) | ||
|
||
return requestToGenerateSurvey(requestUrl, requestBody) | ||
} | ||
|
||
fun requestSurveyGenerationWithTextDocument( | ||
job: String, | ||
groupName: String, | ||
textDocument: String, | ||
userPrompt: String, | ||
): AISurveyGenerationResponse { | ||
val requestUrl = "$aiServerBaseUrl/generate/survey/text-document" | ||
|
||
val requestBody = | ||
mapOf( | ||
"job" to job, | ||
"group_name" to groupName, | ||
"text_document" to textDocument, | ||
"user_prompt" to userPrompt, | ||
) | ||
|
||
return requestToGenerateSurvey(requestUrl, requestBody) | ||
} | ||
|
||
private fun requestToGenerateSurvey( | ||
requestUrl: String, | ||
requestBody: Map<String, String>, | ||
): AISurveyGenerationResponse { | ||
val chatSessionIdWithSurveyGeneratedByAI = | ||
try { | ||
restTemplate | ||
.postForEntity( | ||
requestUrl, | ||
requestBody, | ||
ChatSessionIdWithSurveyGeneratedByAI::class.java, | ||
).body ?: throw SurveyGenerationByAIFailedException() | ||
} catch (e: HttpClientErrorException) { | ||
throw PythonServerExceptionMapper.mapException(e) | ||
} | ||
|
||
val chatSessionId = chatSessionIdWithSurveyGeneratedByAI.chatSessionId | ||
val survey = chatSessionIdWithSurveyGeneratedByAI.surveyGeneratedByAI.toDomain() | ||
val surveyMakeInfoResponse = SurveyMakeInfoResponse.of(survey) | ||
return AISurveyGenerationResponse.from(chatSessionId, surveyMakeInfoResponse) | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/kotlin/com/sbl/sulmun2yong/ai/controller/AIGenerateController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.sbl.sulmun2yong.ai.controller | ||
|
||
import com.sbl.sulmun2yong.ai.controller.doc.AIGenerateApiDoc | ||
import com.sbl.sulmun2yong.ai.dto.request.SurveyGenerationWithFileUrlRequest | ||
import com.sbl.sulmun2yong.ai.dto.request.SurveyGenerationWithTextDocumentRequest | ||
import com.sbl.sulmun2yong.ai.service.GenerateService | ||
import com.sbl.sulmun2yong.survey.dto.response.SurveyMakeInfoResponse | ||
import jakarta.servlet.http.Cookie | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import java.util.UUID | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/ai/generate") | ||
class AIGenerateController( | ||
private val generateService: GenerateService, | ||
) : AIGenerateApiDoc { | ||
@PostMapping("/survey/file-url") | ||
override fun generateSurveyWithFileUrl( | ||
@RequestBody surveyGenerationWithFileUrlRequest: SurveyGenerationWithFileUrlRequest, | ||
response: HttpServletResponse, | ||
): ResponseEntity<SurveyMakeInfoResponse> { | ||
val aiSurveyGenerationResponse = generateService.generateSurveyWithFileUrl(surveyGenerationWithFileUrlRequest) | ||
setChatSessionIdCookie(response, aiSurveyGenerationResponse.chatSessionId) | ||
return ResponseEntity.ok(aiSurveyGenerationResponse.generatedSurvey) | ||
} | ||
|
||
@PostMapping("/survey/text-document") | ||
override fun generateSurveyWithTextDocument( | ||
@RequestBody surveyGenerationWithTextDocumentRequest: SurveyGenerationWithTextDocumentRequest, | ||
response: HttpServletResponse, | ||
): ResponseEntity<SurveyMakeInfoResponse> { | ||
val aiSurveyGenerationResponse = generateService.generateSurveyWithTextDocument(surveyGenerationWithTextDocumentRequest) | ||
setChatSessionIdCookie(response, aiSurveyGenerationResponse.chatSessionId) | ||
return ResponseEntity.ok(aiSurveyGenerationResponse.generatedSurvey) | ||
} | ||
|
||
private fun setChatSessionIdCookie( | ||
response: HttpServletResponse, | ||
chatSessionId: UUID, | ||
) { | ||
val cookie = | ||
Cookie("chat-session-id", chatSessionId.toString()).apply { | ||
maxAge = 3600 | ||
path = "/" | ||
isHttpOnly = true | ||
} | ||
|
||
response.addCookie(cookie) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/com/sbl/sulmun2yong/ai/controller/doc/AIGenerateApiDoc.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.sbl.sulmun2yong.ai.controller.doc | ||
|
||
import com.sbl.sulmun2yong.ai.dto.request.SurveyGenerationWithFileUrlRequest | ||
import com.sbl.sulmun2yong.ai.dto.request.SurveyGenerationWithTextDocumentRequest | ||
import com.sbl.sulmun2yong.survey.dto.response.SurveyMakeInfoResponse | ||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
|
||
@Tag(name = "AI", description = "AI 기능 관련 API") | ||
interface AIGenerateApiDoc { | ||
@Operation(summary = "파일을 통한 AI 설문 생성") | ||
@PostMapping("/survey/file-url") | ||
fun generateSurveyWithFileUrl( | ||
@RequestBody surveyGenerationWithFileUrlRequest: SurveyGenerationWithFileUrlRequest, | ||
response: HttpServletResponse, | ||
): ResponseEntity<SurveyMakeInfoResponse> | ||
|
||
@Operation(summary = "텍스트 입력을 통한 AI 설문 생성") | ||
@PostMapping("/survey/text-document") | ||
fun generateSurveyWithTextDocument( | ||
@RequestBody surveyGenerationWithTextDocumentRequest: SurveyGenerationWithTextDocumentRequest, | ||
response: HttpServletResponse, | ||
): ResponseEntity<SurveyMakeInfoResponse> | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/sbl/sulmun2yong/ai/dto/ChatSessionIdWithSurveyGeneratedByAI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.sbl.sulmun2yong.ai.dto | ||
|
||
import java.util.UUID | ||
|
||
class ChatSessionIdWithSurveyGeneratedByAI( | ||
val chatSessionId: UUID, | ||
val surveyGeneratedByAI: SurveyGeneratedByAI, | ||
) |
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/com/sbl/sulmun2yong/ai/dto/QuestionGeneratedByAI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.sbl.sulmun2yong.ai.dto | ||
|
||
import com.sbl.sulmun2yong.survey.domain.question.QuestionType | ||
import com.sbl.sulmun2yong.survey.domain.question.choice.Choice | ||
import com.sbl.sulmun2yong.survey.domain.question.choice.Choices | ||
import com.sbl.sulmun2yong.survey.domain.question.impl.StandardMultipleChoiceQuestion | ||
import com.sbl.sulmun2yong.survey.domain.question.impl.StandardSingleChoiceQuestion | ||
import com.sbl.sulmun2yong.survey.domain.question.impl.StandardTextQuestion | ||
import java.util.UUID | ||
|
||
class QuestionGeneratedByAI( | ||
private val questionType: QuestionType, | ||
private val title: String, | ||
private val isRequired: Boolean, | ||
private val choices: List<String>?, | ||
private val isAllowOther: Boolean, | ||
) { | ||
fun toDomain() = | ||
when (questionType) { | ||
QuestionType.SINGLE_CHOICE -> | ||
StandardSingleChoiceQuestion( | ||
id = UUID.randomUUID(), | ||
title = this.title, | ||
description = DEFAULT_DESCRIPTION, | ||
isRequired = this.isRequired, | ||
// TODO: Document를 Domain클래스로 변환 중에 생긴 에러는 여기서 직접 반환하도록 수정 | ||
choices = Choices(this.choices?.map { Choice.Standard(it) } ?: listOf(), isAllowOther), | ||
) | ||
QuestionType.MULTIPLE_CHOICE -> | ||
StandardMultipleChoiceQuestion( | ||
id = UUID.randomUUID(), | ||
title = this.title, | ||
description = DEFAULT_DESCRIPTION, | ||
isRequired = this.isRequired, | ||
// TODO: Document를 Domain클래스로 변환 중에 생긴 에러는 여기서 직접 반환하도록 수정 | ||
choices = Choices(this.choices?.map { Choice.Standard(it) } ?: listOf(), isAllowOther), | ||
) | ||
QuestionType.TEXT_RESPONSE -> | ||
StandardTextQuestion( | ||
id = UUID.randomUUID(), | ||
title = this.title, | ||
description = DEFAULT_DESCRIPTION, | ||
isRequired = this.isRequired, | ||
) | ||
} | ||
|
||
companion object { | ||
private const val DEFAULT_DESCRIPTION = "" | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/com/sbl/sulmun2yong/ai/dto/SectionGeneratedByAI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.sbl.sulmun2yong.ai.dto | ||
|
||
import com.sbl.sulmun2yong.survey.domain.routing.RoutingStrategy | ||
import com.sbl.sulmun2yong.survey.domain.section.Section | ||
import com.sbl.sulmun2yong.survey.domain.section.SectionId | ||
import com.sbl.sulmun2yong.survey.domain.section.SectionIds | ||
|
||
class SectionGeneratedByAI( | ||
val title: String, | ||
val description: String, | ||
val questions: List<QuestionGeneratedByAI>, | ||
) { | ||
private val defaultRoutingStrategy = RoutingStrategy.NumericalOrder | ||
|
||
fun toDomain( | ||
sectionId: SectionId.Standard, | ||
sectionIds: SectionIds, | ||
): Section = | ||
Section( | ||
id = sectionId, | ||
title = title, | ||
description = description, | ||
routingStrategy = defaultRoutingStrategy, | ||
questions = questions.map { it.toDomain() }, | ||
sectionIds = sectionIds, | ||
) | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/kotlin/com/sbl/sulmun2yong/ai/dto/SurveyGeneratedByAI.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.sbl.sulmun2yong.ai.dto | ||
|
||
import com.sbl.sulmun2yong.survey.domain.Survey | ||
import com.sbl.sulmun2yong.survey.domain.section.SectionId | ||
import com.sbl.sulmun2yong.survey.domain.section.SectionIds | ||
import java.util.UUID | ||
|
||
class SurveyGeneratedByAI( | ||
private val title: String, | ||
private val description: String, | ||
private val finishMessage: String, | ||
private val sections: List<SectionGeneratedByAI>, | ||
) { | ||
fun toDomain(): Survey { | ||
val sectionIds = List(sections.size) { SectionId.Standard(UUID.randomUUID()) } | ||
val sectionIdsManger = SectionIds.from(sectionIds) | ||
|
||
val sections = | ||
sections.mapIndexed { index, sectionGeneratedByAI -> | ||
sectionGeneratedByAI.toDomain( | ||
sectionIds[index], | ||
sectionIdsManger, | ||
) | ||
} | ||
|
||
val survey = Survey.create(UUID.randomUUID()) | ||
return survey.updateContent( | ||
title = title, | ||
description = description, | ||
thumbnail = survey.thumbnail, | ||
finishMessage = finishMessage, | ||
rewardSetting = survey.rewardSetting, | ||
isVisible = false, | ||
sections = sections, | ||
) | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/com/sbl/sulmun2yong/ai/dto/request/SurveyGenerationWithFileUrlRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.sbl.sulmun2yong.ai.dto.request | ||
|
||
data class SurveyGenerationWithFileUrlRequest( | ||
val job: String, | ||
val groupName: String, | ||
val fileUrl: String, | ||
val userPrompt: String, | ||
) |
8 changes: 8 additions & 0 deletions
8
...main/kotlin/com/sbl/sulmun2yong/ai/dto/request/SurveyGenerationWithTextDocumentRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.sbl.sulmun2yong.ai.dto.request | ||
|
||
data class SurveyGenerationWithTextDocumentRequest( | ||
val job: String, | ||
val groupName: String, | ||
val textDocument: String, | ||
val userPrompt: String, | ||
) |
Oops, something went wrong.