fix list chat
This commit is contained in:
@@ -269,6 +269,7 @@ deploy-all:
|
|||||||
rules:
|
rules:
|
||||||
- if: $CI_COMMIT_BRANCH == "main"
|
- if: $CI_COMMIT_BRANCH == "main"
|
||||||
when: manual
|
when: manual
|
||||||
|
allow_failure: true
|
||||||
needs: []
|
needs: []
|
||||||
script:
|
script:
|
||||||
- |
|
- |
|
||||||
|
|||||||
@@ -22,24 +22,24 @@ public class ChatController {
|
|||||||
|
|
||||||
private final ChatService chatService;
|
private final ChatService chatService;
|
||||||
private final EventPublisher eventPublisher;
|
private final EventPublisher eventPublisher;
|
||||||
|
private final ApiUtils apiUtils;
|
||||||
|
|
||||||
@GetMapping("")
|
@GetMapping("")
|
||||||
public ResponseEntity<List<Chat>> mainPage() {
|
public ResponseEntity<List<Chat>> mainPage() {
|
||||||
log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName());
|
Long ownerId = apiUtils.getUserIdFromAuthentication().longValue();
|
||||||
List<Chat> response = chatService.getAllChats();
|
List<Chat> response = chatService.getAllChatsByOwner(ownerId);
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{chatId}")
|
@GetMapping("/{chatId}")
|
||||||
public ResponseEntity<Chat> showChat(@PathVariable Long chatId) {
|
public ResponseEntity<Chat> showChat(@PathVariable Long chatId) {
|
||||||
log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName());
|
Long ownerId = apiUtils.getUserIdFromAuthentication().longValue();
|
||||||
Chat response = chatService.getChat(chatId);
|
Chat response = chatService.getChat(chatId, ownerId);
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/new")
|
@PostMapping("/new")
|
||||||
public ResponseEntity<Chat> newChat(@RequestParam String title) {
|
public ResponseEntity<Chat> newChat(@RequestParam String title) {
|
||||||
log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName());
|
|
||||||
Chat chat = chatService.createNewChat(title);
|
Chat chat = chatService.createNewChat(title);
|
||||||
|
|
||||||
eventPublisher.publishChatCreated(
|
eventPublisher.publishChatCreated(
|
||||||
@@ -51,9 +51,9 @@ public class ChatController {
|
|||||||
|
|
||||||
@DeleteMapping("/{chatId}")
|
@DeleteMapping("/{chatId}")
|
||||||
public ResponseEntity<Void> deleteChat(@PathVariable Long chatId) {
|
public ResponseEntity<Void> deleteChat(@PathVariable Long chatId) {
|
||||||
log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName());
|
Long ownerId = apiUtils.getUserIdFromAuthentication().longValue();
|
||||||
Chat chat = chatService.getChat(chatId);
|
Chat chat = chatService.getChat(chatId, ownerId);
|
||||||
chatService.deleteChat(chatId);
|
chatService.deleteChat(chatId, ownerId);
|
||||||
|
|
||||||
eventPublisher.publishChatDeleted(
|
eventPublisher.publishChatDeleted(
|
||||||
chat.getIdOwner().toString(),
|
chat.getIdOwner().toString(),
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ public class ChatEntryController {
|
|||||||
private final ChatService chatService;
|
private final ChatService chatService;
|
||||||
private final RagDefaultsProperties ragDefaults;
|
private final RagDefaultsProperties ragDefaults;
|
||||||
private final EventPublisher eventPublisher;
|
private final EventPublisher eventPublisher;
|
||||||
|
private final ApiUtils apiUtils;
|
||||||
|
|
||||||
@PostMapping("/{chatId}")
|
@PostMapping("/{chatId}")
|
||||||
public ResponseEntity<ChatEntry> addUserEntry(
|
public ResponseEntity<ChatEntry> addUserEntry(
|
||||||
@@ -33,10 +34,12 @@ public class ChatEntryController {
|
|||||||
@RequestBody UserEntryRequest request) {
|
@RequestBody UserEntryRequest request) {
|
||||||
log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName());
|
log.trace(ApiLogMessage.NAME_OF_CURRENT_METHOD.getValue(), ApiUtils.getMethodName());
|
||||||
|
|
||||||
|
Long ownerId = apiUtils.getUserIdFromAuthentication().longValue();
|
||||||
|
|
||||||
boolean onlyContext = request.onlyContext() != null ? request.onlyContext() : ragDefaults.onlyContext();
|
boolean onlyContext = request.onlyContext() != null ? request.onlyContext() : ragDefaults.onlyContext();
|
||||||
double topP = request.topP() != null ? request.topP() : ragDefaults.topP();
|
double topP = request.topP() != null ? request.topP() : ragDefaults.topP();
|
||||||
|
|
||||||
Chat chat = chatService.getChat(chatId);
|
Chat chat = chatService.getChat(chatId, ownerId);
|
||||||
ChatEntry entry = chatEntryService.addUserEntry(chatId, request.content(), onlyContext, topP, chat.getIdOwner());
|
ChatEntry entry = chatEntryService.addUserEntry(chatId, request.content(), onlyContext, topP, chat.getIdOwner());
|
||||||
|
|
||||||
eventPublisher.publishQuerySent(
|
eventPublisher.publishQuerySent(
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package com.balex.rag.repo;
|
|||||||
import com.balex.rag.model.entity.Chat;
|
import com.balex.rag.model.entity.Chat;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface ChatRepository extends JpaRepository<Chat, Long> {
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface ChatRepository extends JpaRepository<Chat, Long> {
|
||||||
|
List<Chat> findByIdOwnerOrderByCreatedAtDesc(Long idOwner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,12 +9,11 @@ public interface ChatService {
|
|||||||
|
|
||||||
Chat createNewChat(String title);
|
Chat createNewChat(String title);
|
||||||
|
|
||||||
List<Chat> getAllChats();
|
List<Chat> getAllChatsByOwner(Long ownerId);
|
||||||
|
|
||||||
Chat getChat(Long chatId);
|
Chat getChat(Long chatId, Long ownerId);
|
||||||
|
|
||||||
void deleteChat(Long chatId);
|
void deleteChat(Long chatId, Long ownerId);
|
||||||
|
|
||||||
SseEmitter proceedInteractionWithStreaming(Long chatId, String userPrompt);
|
SseEmitter proceedInteractionWithStreaming(Long chatId, String userPrompt);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import org.springframework.ai.chat.client.ChatClient;
|
|||||||
import org.springframework.ai.chat.memory.ChatMemory;
|
import org.springframework.ai.chat.memory.ChatMemory;
|
||||||
import org.springframework.ai.chat.model.ChatResponse;
|
import org.springframework.ai.chat.model.ChatResponse;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -20,13 +22,11 @@ import java.util.List;
|
|||||||
public class ChatServiceImpl implements ChatService {
|
public class ChatServiceImpl implements ChatService {
|
||||||
|
|
||||||
private final ChatRepository chatRepo;
|
private final ChatRepository chatRepo;
|
||||||
|
|
||||||
private final ChatClient chatClient;
|
private final ChatClient chatClient;
|
||||||
|
|
||||||
private final ApiUtils apiUtils;
|
private final ApiUtils apiUtils;
|
||||||
|
|
||||||
public List<Chat> getAllChats() {
|
public List<Chat> getAllChatsByOwner(Long ownerId) {
|
||||||
return chatRepo.findAll(Sort.by(Sort.Direction.DESC, "createdAt"));
|
return chatRepo.findByIdOwnerOrderByCreatedAtDesc(ownerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Chat createNewChat(String title) {
|
public Chat createNewChat(String title) {
|
||||||
@@ -39,17 +39,21 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
return chat;
|
return chat;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Chat getChat(Long chatId) {
|
public Chat getChat(Long chatId, Long ownerId) {
|
||||||
return chatRepo.findById(chatId).orElseThrow();
|
Chat chat = chatRepo.findById(chatId).orElseThrow(
|
||||||
|
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Chat not found"));
|
||||||
|
if (!chat.getIdOwner().equals(ownerId)) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Access denied");
|
||||||
|
}
|
||||||
|
return chat;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteChat(Long chatId) {
|
public void deleteChat(Long chatId, Long ownerId) {
|
||||||
chatRepo.deleteById(chatId);
|
Chat chat = getChat(chatId, ownerId);
|
||||||
|
chatRepo.deleteById(chat.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public SseEmitter proceedInteractionWithStreaming(Long chatId, String userPrompt) {
|
public SseEmitter proceedInteractionWithStreaming(Long chatId, String userPrompt) {
|
||||||
|
|
||||||
SseEmitter sseEmitter = new SseEmitter(0L);
|
SseEmitter sseEmitter = new SseEmitter(0L);
|
||||||
final StringBuilder answer = new StringBuilder();
|
final StringBuilder answer = new StringBuilder();
|
||||||
|
|
||||||
@@ -65,12 +69,10 @@ public class ChatServiceImpl implements ChatService {
|
|||||||
return sseEmitter;
|
return sseEmitter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
private static void processToken(ChatResponse response, SseEmitter emitter, StringBuilder answer) {
|
private static void processToken(ChatResponse response, SseEmitter emitter, StringBuilder answer) {
|
||||||
var token = response.getResult().getOutput();
|
var token = response.getResult().getOutput();
|
||||||
emitter.send(token);
|
emitter.send(token);
|
||||||
answer.append(token.getText());
|
answer.append(token.getText());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user