Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package MathCaptain.weakness.domain.Chat.controller;

import MathCaptain.weakness.domain.Chat.dto.request.ChatRequest;
import MathCaptain.weakness.domain.Chat.dto.request.LLMRequest;
import MathCaptain.weakness.domain.Chat.dto.response.ChatResponse;
import MathCaptain.weakness.domain.Chat.service.ChatService;
import MathCaptain.weakness.domain.User.entity.Users;
import MathCaptain.weakness.global.annotation.LoginUser;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
Expand All @@ -26,6 +26,14 @@ public List<ChatResponse> getHistory(@PathVariable Long userId) {
return chatService.getHistory(userId);
}

@PostMapping("/chat/test/{userId}")
public LLMRequest test(
@PathVariable Long userId,
@RequestBody ChatRequest request
) {
return chatService.test(userId, request);
}

@MessageMapping("/chat.send") // 클라이언트 → /app/chat.send
public void handleChat(ChatRequest request) {
// 1) 유저 메시지 저장 + 클라이언트에게 에코
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package MathCaptain.weakness.domain.Chat.dto.request;

import MathCaptain.weakness.domain.Chat.entity.Chat;
import MathCaptain.weakness.domain.User.entity.Users;
import MathCaptain.weakness.domain.Chat.dto.response.ChatResponse;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Getter
@NoArgsConstructor
public class LLMRequest {

private Long userId;
private Prompt prompt;

private String message;
private Map<String, Object> resources;

private List<Chat> history;
private List<Object> tools;

private LLMRequest(Long userId, String message, List<Chat> history) {
this.userId = userId;
this.message = message;
this.history = history;
private LLMRequest(Prompt prompt) {
this.prompt = prompt;
this.resources = new HashMap<>();
this.tools = List.of();
}

public static LLMRequest of(ChatRequest request, List<Chat> history) {
return new LLMRequest(request.getUserId(), request.getMessage(), history);
public static LLMRequest of(ChatRequest request, List<ChatResponse> history) {
Prompt prompt = Prompt.of(request, history);
return new LLMRequest(prompt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package MathCaptain.weakness.domain.Chat.dto.request;

import MathCaptain.weakness.domain.Chat.dto.response.ChatResponse;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Getter
@NoArgsConstructor
public class Prompt {

private String template;

private Map<String, String> inputs;

private List<ChatResponse> history;

private Prompt(Long userId, String message, List<ChatResponse> history) {
this.template = "다음 내용을 참고하여 답변해줘";
this.inputs = Map.of(
"user_id", userId.toString(),
"user_question", message
);
this.history = history;
}

public static Prompt of(ChatRequest request, List<ChatResponse> history) {
return new Prompt(request.getUserId(), request.getMessage(), history);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package MathCaptain.weakness.domain.Chat.service;

import MathCaptain.weakness.domain.Chat.dto.request.ChatRequest;
import MathCaptain.weakness.domain.Chat.dto.request.LLMRequest;
import MathCaptain.weakness.domain.Chat.dto.response.ChatResponse;
import MathCaptain.weakness.domain.Chat.entity.Chat;
import MathCaptain.weakness.domain.Chat.repository.ChatRepository;
import MathCaptain.weakness.domain.User.entity.Users;
import MathCaptain.weakness.domain.common.enums.ChatRole;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -24,15 +23,16 @@ public class ChatService {

@Transactional
public ChatResponse saveUserMessage(ChatRequest request) {
log.info("userid {}", request.getUserId());
log.info("message {}", request.getMessage());
Chat message = Chat.of(request);
Chat saved = chatRepository.save(message);
return ChatResponse.of(saved);
}

public List<ChatResponse> askAI(ChatRequest request) {
List<Chat> history = chatRepository.findAllByUserIdOrderBySendTimeAsc(request.getUserId());
List<ChatResponse> history = chatRepository.findAllByUserIdOrderBySendTimeAsc(request.getUserId()).stream()
.map(ChatResponse::of)
.toList();

List<Chat> aiChats = llm.call(history, request);
return aiChats.stream()
.map(this::storeAndTransform)
Expand All @@ -47,6 +47,15 @@ public List<ChatResponse> getHistory(Long userId) {
.toList();
}

@Transactional
public LLMRequest test(Long userId, ChatRequest request) {
List<ChatResponse> history = chatRepository.findAllByUserIdOrderBySendTimeAsc(userId).stream()
.map(ChatResponse::of)
.toList();

return LLMRequest.of(request, history);
}

private ChatResponse storeAndTransform(Chat message) {
Chat saved = chatRepository.save(message);
return ChatResponse.of(saved);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class LLMClient {
@Value("${llm.server.url}")
private String baseUrl;

public List<Chat> call(List<Chat> history, ChatRequest request) {
public List<Chat> call(List<ChatResponse> history, ChatRequest request) {
try {
LLMRequest llmRequest = LLMRequest.of(request, history);

Expand Down