-
Notifications
You must be signed in to change notification settings - Fork 0
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 #109 from Kusitms-29th-ASAP/feature/tranlate/todo
feat: 투두 번역 및 변역 결과 저장 테이블 추가
- Loading branch information
Showing
25 changed files
with
631 additions
and
80 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
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/asap/asapbackend/batch/event/IncompleteEventRepublicationHandler.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,21 @@ | ||
package com.asap.asapbackend.batch.event | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.modulith.events.IncompleteEventPublications | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Component | ||
|
||
private val logger = KotlinLogging.logger { } | ||
|
||
@Component | ||
class IncompleteEventRepublicationHandler( | ||
private val applicationEventMulticaster: IncompleteEventPublications | ||
) { | ||
|
||
// 매 분마다 실행 | ||
@Scheduled(cron = "0 0 * * * ?") | ||
fun resubmitIncompletePublications() { | ||
applicationEventMulticaster.resubmitIncompletePublications { true } | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
src/main/kotlin/com/asap/asapbackend/client/openai/chatgpt/GptClient.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,88 @@ | ||
package com.asap.asapbackend.client.openai.chatgpt | ||
|
||
import com.asap.asapbackend.client.openai.OpenAIProperties | ||
import org.springframework.http.MediaType | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.reactive.function.client.WebClient | ||
|
||
@Component | ||
class GptClient( | ||
private val openAIProperties: OpenAIProperties | ||
) { | ||
|
||
fun getResultForContentWithPolicy( | ||
content: String, | ||
policy: Policy | ||
): String? { | ||
val response = WebClient.builder() | ||
.baseUrl("https://api.openai.com") | ||
.build() | ||
.post() | ||
.uri("/v1/chat/completions") | ||
.header("Authorization", "Bearer ${openAIProperties.apiKey}") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.bodyValue( | ||
mapOf( | ||
"model" to "gpt-4o-mini", | ||
"messages" to listOf( | ||
mapOf( | ||
"role" to "system", | ||
"content" to policy.instruction | ||
), | ||
mapOf( | ||
"role" to "user", | ||
"content" to policy.exampleContent | ||
), | ||
mapOf( | ||
"role" to "assistant", | ||
"content" to policy.exampleResult | ||
), | ||
mapOf( | ||
"role" to "user", | ||
"content" to content | ||
) | ||
) | ||
) | ||
) | ||
.retrieve() | ||
.bodyToMono(GptResponse::class.java) | ||
.block() | ||
return response?.let { | ||
it.getMessage(0).also { message -> | ||
if (policy.isJson) | ||
convertJsonFormat(message) | ||
} | ||
} | ||
} | ||
|
||
private fun convertJsonFormat(content: String): String { | ||
return content.replace("```json", "").replace("```", "") | ||
} | ||
|
||
data class Policy( | ||
val instruction: String, | ||
val exampleContent: String, | ||
val exampleResult: String, | ||
val isJson: Boolean | ||
) | ||
|
||
data class GptResponse( | ||
val id: String, | ||
val model: String, | ||
val choices: List<Choice> | ||
) { | ||
fun getMessage(index: Int): String { | ||
return choices[index].message.content | ||
} | ||
} | ||
|
||
data class Choice( | ||
val index: Int, | ||
val message: Message | ||
) | ||
|
||
data class Message( | ||
val role: String, | ||
val content: String | ||
) | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/com/asap/asapbackend/client/openai/chatgpt/GptTextTranslateClient.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,62 @@ | ||
package com.asap.asapbackend.client.openai.chatgpt | ||
|
||
import com.asap.asapbackend.global.util.TextTranslator | ||
import com.asap.asapbackend.global.vo.Language | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class GptTextTranslateClient( | ||
private val gptClient: GptClient, | ||
private val objectMapper: ObjectMapper | ||
) : TextTranslator { | ||
override fun translate(text: String): TextTranslator.TranslateResponse{ | ||
val policy = GptClient.Policy( | ||
GptTextTranslatePrompt.TRANSLATION_INSTRUCTION, | ||
GptTextTranslatePrompt.EXAMPLE_CONTENT, | ||
GptTextTranslatePrompt.TRANSLATION_RESPONSE, | ||
true | ||
) | ||
val result = gptClient.getResultForContentWithPolicy(text, policy) ?: run { | ||
throw IllegalStateException("Failed to get result from GPT") | ||
} | ||
|
||
return objectMapper.readValue(result) | ||
} | ||
|
||
|
||
private object GptTextTranslatePrompt { | ||
val TRANSLATION_INSTRUCTION = """ | ||
위 문장을 ${Language.getAllLanguageNames()} 언어로 번역해줘, 변역을 완료하면 JSON 형태로 출력해줘. | ||
각 언어를 번역할때는 언어에 맞는 key와 value로 출력해줘. 예시 형태는 다음과 같아. | ||
""" | ||
|
||
val TRANSLATIONS = Language.entries.joinToString(", ") { language -> | ||
"""{ "language" : "${language.name}", "text" : "String" }""" | ||
} | ||
|
||
val TRANSLATION_RESPONSE = """ | ||
{ | ||
"translations" : [ | ||
$TRANSLATIONS | ||
] | ||
} | ||
""" | ||
|
||
val EXAMPLE_CONTENT = """ | ||
{ | ||
"translations" : [ | ||
{ "language" : "KOREAN", "text" : "안녕하세요" }, | ||
{ "language" : "ENGLISH", "text" : "Hello" }, | ||
{ "language" : "CHINESE", "text" : "你好" }, | ||
{ "language" : "JAPANESE", "text" : "こんにちは" }, | ||
{ "language" : "VIETNAMESE", "text" : "Xin chào" }, | ||
{ "language" : "PILIPINO", "text" : "Kamusta" } | ||
] | ||
} | ||
""".trimIndent() | ||
|
||
|
||
} | ||
} |
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
26 changes: 0 additions & 26 deletions
26
src/main/kotlin/com/asap/asapbackend/domain/todo/domain/repository/TodoJdbcRepository.kt
This file was deleted.
Oops, something went wrong.
23 changes: 6 additions & 17 deletions
23
src/main/kotlin/com/asap/asapbackend/domain/todo/domain/repository/TodoRepository.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 |
---|---|---|
@@ -1,27 +1,16 @@ | ||
package com.asap.asapbackend.domain.todo.domain.repository | ||
|
||
import com.asap.asapbackend.domain.todo.domain.model.Todo | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Query | ||
import java.time.LocalDate | ||
|
||
interface TodoRepository : JpaRepository<Todo, Long> { | ||
interface TodoRepository { | ||
fun findAllByChildIdAndDeadlineAfter(childId: Long, deadline: LocalDate): List<Todo> | ||
|
||
@Query(""" | ||
select t | ||
from Todo t | ||
join fetch t.child | ||
join PrimaryChild p on p.childId = t.child.id and p.userId = :userId | ||
""") | ||
fun findAllByUserId(userId: Long): List<Todo> | ||
fun findByUserIdAndTodoId(userId: Long, todoId: Long): Todo? | ||
|
||
fun save(todo: Todo): Todo | ||
|
||
@Query(""" | ||
select t | ||
from Todo t | ||
join t.child c on c.id = t.child.id and c.parent.id = :userId | ||
where t.id = :todoId | ||
""") | ||
fun findByUserIdAndTodoId(userId: Long, todoId: Long): Todo? | ||
fun insertBatch(todos: Set<Todo>) | ||
|
||
fun delete(todo: Todo) | ||
} |
17 changes: 9 additions & 8 deletions
17
src/main/kotlin/com/asap/asapbackend/domain/todo/domain/service/TodoAppender.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 |
---|---|---|
@@ -1,25 +1,26 @@ | ||
package com.asap.asapbackend.domain.todo.domain.service | ||
|
||
import com.asap.asapbackend.domain.todo.domain.model.Todo | ||
import com.asap.asapbackend.domain.todo.domain.repository.TodoJdbcRepository | ||
import com.asap.asapbackend.domain.todo.domain.repository.TodoRepository | ||
import com.asap.asapbackend.domain.todo.event.MultiTodoCreateEvent | ||
import com.asap.asapbackend.domain.todo.event.TodoCreateEvent | ||
import org.springframework.context.ApplicationEventPublisher | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class TodoAppender( | ||
private val todoRepository: TodoRepository, | ||
private val todoJdbcRepository: TodoJdbcRepository | ||
private val applicationEventPublisher: ApplicationEventPublisher | ||
) { | ||
|
||
fun appendAll(todos: Set<Todo>) { | ||
todoRepository.saveAll(todos) | ||
} | ||
|
||
fun appendAllBatch(todos: Set<Todo>) { | ||
todoJdbcRepository.insertBatch(todos) | ||
todoRepository.insertBatch(todos) | ||
applicationEventPublisher.publishEvent(MultiTodoCreateEvent(todos)) | ||
} | ||
|
||
fun appendTodo(todo: Todo): Todo { | ||
return todoRepository.save(todo) | ||
val createdTodo = todoRepository.save(todo) | ||
applicationEventPublisher.publishEvent(TodoCreateEvent(createdTodo)) | ||
return createdTodo | ||
} | ||
} |
Oops, something went wrong.