-
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.
- Loading branch information
Showing
13 changed files
with
218 additions
and
12 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
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/me/misik/api/api/response/ParsedOcrResponse.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,10 @@ | ||
package me.misik.api.api.response | ||
|
||
data class ParsedOcrResponse( | ||
val parsed: List<KeyValuePair> | ||
) { | ||
data class KeyValuePair( | ||
val key: String, | ||
val value: String, | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package me.misik.api.core | ||
|
||
data class ErrorResponse( | ||
val message: String, | ||
) { | ||
|
||
companion object { | ||
fun from(exception: Exception): ErrorResponse = | ||
ErrorResponse(exception.message ?: exception.localizedMessage) | ||
} | ||
} |
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,85 @@ | ||
package me.misik.api.core | ||
|
||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.service.annotation.PostExchange | ||
|
||
fun interface OcrParser { | ||
|
||
@PostExchange("/testapp/v1/chat-completions/HCX-003") | ||
fun createParsedOcr(@RequestBody request: Request): Response | ||
|
||
data class Request( | ||
val messages: List<Message>, | ||
val maxTokens: Int = 100, | ||
val includeAiFilters: Boolean = true, | ||
) { | ||
data class Message( | ||
val role: String, | ||
val content: String, | ||
) { | ||
|
||
companion object { | ||
|
||
fun createSystem(content: String) = Message( | ||
role = "system", | ||
content = content, | ||
) | ||
|
||
fun createUser(content: String) = Message( | ||
role = "user", | ||
content = content, | ||
) | ||
} | ||
} | ||
|
||
companion object { | ||
val cachedParsingSystemMessage = Message.createSystem( | ||
""" | ||
리뷰에 쓸만한 정보를 추출해줘. key에는 방문 장소명, 품명 등이 포함될 수 있어. key는 최대 3개만 뽑아줘. | ||
응답 형식은 반드시 다음과 같은 JSON이야. 응답에는 해당 JSON만 있어야해. | ||
{ | ||
"parsed": [ | ||
{ | ||
"key": "품명", | ||
"value": "카야토스트+음료세트" | ||
}, | ||
{ | ||
"key": "가격", | ||
"value": "3000" | ||
}, | ||
... | ||
] | ||
} | ||
응답의 총 길이는 300자를 넘으면 안돼. | ||
""" | ||
) | ||
|
||
fun from(ocrText: String): Request { | ||
return Request( | ||
messages = listOf( | ||
cachedParsingSystemMessage, | ||
Message.createUser(ocrText) | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
data class Response( | ||
val status: Status?, | ||
val result: Result? | ||
) { | ||
data class Status( | ||
val code: String, | ||
val message: String | ||
) | ||
data class Result( | ||
val message: Message? | ||
) { | ||
data class Message( | ||
val role: String, | ||
val content: String | ||
) | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/me/misik/api/core/advice/GlobalExceptionHandler.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 me.misik.api.core.advice | ||
|
||
import me.misik.api.core.ErrorResponse | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.web.bind.annotation.ExceptionHandler | ||
import org.springframework.web.bind.annotation.ResponseStatus | ||
import org.springframework.web.bind.annotation.RestControllerAdvice | ||
|
||
@RestControllerAdvice | ||
class GlobalExceptionHandler { | ||
|
||
private val logger = LoggerFactory.getLogger(this::class.simpleName) | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(IllegalArgumentException::class) | ||
fun handleIllegalArgumentException(exception: IllegalArgumentException): ErrorResponse { | ||
logger.error(exception.message, exception) | ||
return ErrorResponse.from(exception) | ||
} | ||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
@ExceptionHandler(IllegalStateException::class) | ||
fun handleIllegalStateException(exception: IllegalStateException): ErrorResponse { | ||
logger.error(exception.message, exception) | ||
return ErrorResponse.from(exception) | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/me/misik/api/domain/request/OcrTextRequest.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,5 @@ | ||
package me.misik.api.domain.request | ||
|
||
data class OcrTextRequest( | ||
val text: String, | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,16 @@ | ||
INSERT INTO prompt (id, style, command, created_at, modified_at) VALUES | ||
(1, 'PROFESSIONAL', '자연스러운, 전문적인 말투, 존대말, 과거형으로 리뷰를 만들어줘. 응답 형식은 {“review”:”리뷰 내용”}이어야 해. 응답에는 해당 JSON만 있어야해. 또한, 리뷰는 공백을 포함해서 300자가 넘어야 해. 리뷰에는 다음 의견도 자연스럽게 넣어줘.', NOW(), NOW()), | ||
(2, 'FRIENDLY', '~다, ~요를 적절히 섞은 높임말로 리뷰를 만들어줘. 한국 중년처럼 보이도록 가끔 문장 끝에 ..같은 특수문자를 넣어줘. 응답 형식은 {“review”:”리뷰 내용”}이어야 해. 응답에는 해당 JSON만 있어야해. 또한, 리뷰는 공백을 포함해서 100자 이상 200자 이하여야해. 리뷰에는 다음 의견도 자연스럽게 넣어줘.', NOW(), NOW()), | ||
(3, 'CUTE', '~다, ~요를 적절히 섞은 높임말로 리뷰를 만들어줘. 깜찍한 10대처럼 보이도록 가끔 어미 뒤에 랜덤하게 😘, ㅎㅎ 같은 특수문자, "당, 용" 같이 끝나는 어미를 넣어줘. 응답 형식은 {"review":"리뷰 내용"}이어야 해. 응답에는 해당 JSON만 있어야 해. 또한, 리뷰는 공백을 포함해서 100자 이상 200자 이하여야 해. 리뷰에는 다음 의견도 자연스럽게 넣어줘.', NOW(), NOW()); | ||
(3, 'CUTE', '~다, ~요를 적절히 섞은 높임말로 리뷰를 만들어줘. 깜찍한 10대처럼 보이도록 가끔 어미 뒤에 랜덤하게 😘, ㅎㅎ 같은 특수문자, "당, 용" 같이 끝나는 어미를 넣어줘. 응답 형식은 {"review":"리뷰 내용"}이어야 해. 응답에는 해당 JSON만 있어야 해. 또한, 리뷰는 공백을 포함해서 100자 이상 200자 이하여야 해. 리뷰에는 다음 의견도 자연스럽게 넣어줘.', NOW(), NOW()); | ||
|
||
create table if not exists misikapi.prompt | ||
( | ||
created_at datetime(6) null, | ||
id bigint not null | ||
primary key, | ||
modified_at datetime(6) null, | ||
command text not null, | ||
style varchar(20) not null, | ||
constraint UKn1rq77g5p76xoejtkn8xgj207 | ||
unique (style) | ||
); |