Skip to content

Commit

Permalink
Merge :: 캣스기 Http 적용 #338 (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
seu1a authored Oct 22, 2024
2 parents df8731b + f5dbe1e commit 7489503
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.seugi.api.domain.ai.presentation

import com.seugi.api.domain.ai.service.AiService
import com.seugi.api.domain.chat.presentation.websocket.dto.ChatMessageDto
import com.seugi.api.global.common.annotation.GetAuthenticatedId
import com.seugi.api.global.response.BaseResponse
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/ai")
class AiController(
private val aiService: AiService,
) {

@GetMapping
fun generatedAiChatbot(
@RequestBody chatMessageDto: ChatMessageDto,
@GetAuthenticatedId userId: Long,
): BaseResponse<String> {
return BaseResponse(
message = "캣스기답변",
data = aiService.handleRequest(chatMessageDto, userId).message
)
}

}
42 changes: 28 additions & 14 deletions src/main/kotlin/com/seugi/api/domain/ai/service/AiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,45 +67,59 @@ class AiService(
val prompt = promptTemplate.create(mapOf("message" to message.message), requestModel)

val result = openAiChatModel.call(prompt).result.output.content
return handleResponse(result, message)
return handleResponse(result, message.chatRoomId, message.userId, message.message)
}

fun handleRequest(
chatMessageDto: ChatMessageDto,
userId: Long,
): ChatMessageDto {
val promptTemplate = PromptTemplate(rolePrompt + beforePrompt)

val prompt = promptTemplate.create(mapOf("message" to chatMessageDto.message), requestModel)

val result = openAiChatModel.call(prompt).result.output.content
return handleResponse(result, chatMessageDto.roomId ?: "", userId, chatMessageDto.message ?: "")
}

private fun handleResponse(
result: String,
message: Message,
chatRoomId: String,
userId: Long,
message: String,
): ChatMessageDto {

val date: Any = when (result) {
"급식" -> {
mealService.getMealByDate(
mealDate = getToday(),
workspaceId = findChatRoomById(message.chatRoomId).workspaceId
workspaceId = findChatRoomById(chatRoomId).workspaceId
)
}

"시간표" -> {
timetableService.getDayTimetableByUserInfo(
workspaceId = findChatRoomById(message.chatRoomId).workspaceId,
userId = message.userId
workspaceId = findChatRoomById(chatRoomId).workspaceId,
userId = userId
)
}

"공지" -> {
notificationService.getNotices(
workspaceId = findChatRoomById(message.chatRoomId).workspaceId,
userId = message.userId,
workspaceId = findChatRoomById(chatRoomId).workspaceId,
userId = userId,
pageable = PageRequest.of(0, 1)
)
}

"사람 뽑기" -> {
findChatRoomById(message.chatRoomId).joinedUserInfo.map {
findChatRoomById(chatRoomId).joinedUserInfo.map {
it.userId
}
}

"팀짜기" -> {
findChatRoomById(message.chatRoomId).joinedUserInfo.map {
findChatRoomById(chatRoomId).joinedUserInfo.map {
it.userId
}
}
Expand All @@ -120,12 +134,12 @@ class AiService(
"급식", "시간표", "공지" -> {
ChatMessageDto(
type = Type.BOT,
roomId = message.chatRoomId,
roomId = chatRoomId,
message = AiResponse(
keyword = result,
data = date
).toJsonString(),
mention = setOf(message.userId)
mention = setOf(userId)
)
}

Expand All @@ -135,20 +149,20 @@ class AiService(
val aiResult = openAiChatModel.call(
promptTemplate.create(
mapOf(
"first" to message.message,
"first" to message,
"keyword" to result,
"data" to date.toJsonString()
), responseModel
)
).result.output.content
return ChatMessageDto(
type = Type.BOT,
roomId = message.chatRoomId,
roomId = chatRoomId,
message = AiResponse(
keyword = result,
data = aiResult
).toJsonString(),
mention = setOf(message.userId)
mention = setOf(userId)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ class ChatRoomServiceImpl(
return chatRoomMapper.toResponse(
room = room,
members = getUserInfo(room),
lastMessage = lastMessageEntity?.message ?: "",
lastMessageTimeStamp = (lastMessageEntity?.timestamp ?: "").toString(),
lastMessage = if (lastMessageEntity?.type == Type.MESSAGE) lastMessageEntity.message else "",
lastMessageTimeStamp = (lastMessageEntity?.timestamp ?: LocalDateTime.now()).toString(),
notReadCnt = messageService.getNotReadMessageCount(room, userId)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class MessageServiceImpl(

@Transactional(readOnly = true)
override fun getLastMessage(roomId: String): MessageEntity? {
return messageRepository.findByChatRoomId(roomId).lastOrNull { it.type == Type.MESSAGE }
return messageRepository.findByChatRoomId(roomId).lastOrNull()
}

@Transactional(readOnly = true)
Expand Down

0 comments on commit 7489503

Please sign in to comment.