Add conversation retrieval endpoint to ChatService

Implemented a new method in ChatService and ChatServiceImpl to fetch conversation history. Added a corresponding endpoint in AppController to handle GET requests for conversation dialogues based on requestId.
This commit is contained in:
ljhbt 2024-09-17 17:21:03 +02:00
parent 9511986fdd
commit 929dcb9885
3 changed files with 21 additions and 4 deletions

View File

@ -1,13 +1,11 @@
package de.hbt.routing.controller
import de.hbt.routing.service.ChatService
import de.hbt.routing.service.helper.ConversationCache
import mu.KotlinLogging
import org.springframework.http.HttpStatus
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 org.springframework.web.bind.annotation.*
private val logger = KotlinLogging.logger {}
@ -17,6 +15,7 @@ class AppController(private val chatService: ChatService) {
data class ChatRequest(val requestId: String, val prompt: String)
data class ChatResponse(val requestId: String, val answer: String)
data class ConversationResponse(val requestId: String, val dialogues: List<ConversationCache.PromptAndAnswer>)
@PostMapping("/send")
fun sendPrompt(@RequestBody request: ChatRequest): ResponseEntity<ChatResponse> {
@ -28,4 +27,15 @@ class AppController(private val chatService: ChatService) {
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ChatResponse(request.requestId, "Error: ${e.message}"))
}
}
@GetMapping("/conversation/{requestId}")
fun getConversation(@PathVariable requestId: String): ResponseEntity<ConversationResponse> {
return try {
val dialogues = chatService.getConversation(requestId)
ResponseEntity.ok(ConversationResponse(requestId, dialogues))
} catch (e: Exception) {
logger.error(e) { "Exception occurred while retrieving conversation for requestId: $requestId" }
ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ConversationResponse(requestId, emptyList()))
}
}
}

View File

@ -1,6 +1,9 @@
package de.hbt.routing.service
import de.hbt.routing.service.helper.ConversationCache
interface ChatService {
data class Response(val requestId: String, val response: String)
fun processPrompt(requestId: String, prompt: String): Response
fun getConversation(requestId: String): List<ConversationCache.PromptAndAnswer>
}

View File

@ -16,4 +16,8 @@ class ChatServiceImpl(private val conversationCache: ConversationCache) : ChatSe
return response
}
override fun getConversation(requestId: String): List<ConversationCache.PromptAndAnswer> {
return conversationCache.getConversation(requestId)
}
}