Skip to content

Commit

Permalink
Feat :: AI 대화에 Chat controller 추가
Browse files Browse the repository at this point in the history
AI 대화를 위해 컨트롤러를 추가했습니다.
AiService에서 새로운 요청 핸들러를 구현했습니다.
이제 특정 키워드에 대한 일정, 급식, 공지 등을 제공합니다.
  • Loading branch information
yeseong0412 committed Oct 22, 2024
1 parent d3c456b commit f5dbe1e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 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

0 comments on commit f5dbe1e

Please sign in to comment.