-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLMController.java
More file actions
108 lines (89 loc) · 5.07 KB
/
LLMController.java
File metadata and controls
108 lines (89 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.example.spring.app.llm;
import com.example.spring.app.llm.dto.ChatStreamResponseDTO;
import com.example.spring.app.llm.dto.ConversationDTO;
import com.example.spring.app.llm.dto.MessageDTO;
import com.example.spring.app.llm.springAiChatMemory.SpringAiChatMemoryService;
import com.example.spring.app.llm.userConversation.UserConversationModel;
import com.example.spring.app.llm.userConversation.UserConversationService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import java.time.Instant;
import java.util.List;
import static com.example.spring.app.llm.LLMUtils.wrapUserInputWithConversationContext;
import static com.example.spring.common.utils.JwtUtil.extractUserIdFromHeader;
@CrossOrigin
@RestController
@RequestMapping("/v1/chat")
public class LLMController {
private final ChatClient titleClient;
private final ChatClient chatClient;
private final UserConversationService userConversationService;
private final SpringAiChatMemoryService springAiChatMemoryService;
public LLMController(ChatClient titleClient, ChatClient chatClient, UserConversationService userConversationService, SpringAiChatMemoryService springAiChatMemoryService) {
this.titleClient = titleClient;
this.chatClient = chatClient;
this.userConversationService = userConversationService;
this.springAiChatMemoryService = springAiChatMemoryService;
}
@PostMapping(value = "/{conversationId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ChatStreamResponseDTO> streamGeneration(@PathVariable String conversationId, @RequestBody LLMRequest request) {
String userId = extractUserIdFromHeader();
String userInput = wrapUserInputWithConversationContext(request.userInput());
boolean isNewConversation = conversationId.equals("new");
String conversationTitle = isNewConversation
? titleClient.prompt().user(userInput).call().content()
: null;
UserConversationModel conversation =
isNewConversation
? userConversationService.createNewConversationForUser(userId, conversationTitle)
: userConversationService.getUserConversation(conversationId, userId);
if (conversation == null) {
throw new RuntimeException("Conversation not found for user. Mismatched user or conversation ID.");
}
return chatClient.prompt()
.user(userSpec -> userSpec.text(userInput))
.advisors(advisor -> advisor.param(ChatMemory.CONVERSATION_ID, conversation.getConversationId()))
.stream()
.chatResponse()
.map(chatResponse -> new ChatStreamResponseDTO(conversation.getConversationId(), chatResponse, Instant.now().toEpochMilli()));
}
// TODO: Add pagination to this endpoint
@GetMapping("/history/{conversationId}")
public List<MessageDTO> getConversationHistory(@PathVariable String conversationId) {
String userId = extractUserIdFromHeader();
UserConversationModel conversation = userConversationService.getUserConversation(conversationId, userId);
if (conversation == null) {
throw new RuntimeException("Conversation not found for user. Mismatched user or conversation ID.");
}
return springAiChatMemoryService.findAllByConversationId(conversationId).stream()
.map(chatMemoryModel -> new MessageDTO(chatMemoryModel.getContent(), chatMemoryModel.getType(), chatMemoryModel.getTimestamp()))
.toList();
}
// Issue with open-api generator which generates ENUM, and values are the sames
@DeleteMapping("/delete/{conversationId}")
public void deleteConversation(@PathVariable String conversationId) {
String userId = extractUserIdFromHeader();
springAiChatMemoryService.deleteAllByConversationId(conversationId);
userConversationService.deleteUserConversation(conversationId, userId);
}
@GetMapping("/single/{conversationId}")
public ConversationDTO getSingleConversation(@PathVariable String conversationId) {
String userId = extractUserIdFromHeader();
UserConversationModel conversation = userConversationService.getUserConversation(conversationId, userId);
if (conversation == null) {
throw new RuntimeException("Conversation not found for user. Mismatched user or conversation ID.");
}
return new ConversationDTO(conversation.getConversationId(), conversation.getTitle());
}
@GetMapping("/all")
public List<ConversationDTO> getAllUserConversations() {
String userId = extractUserIdFromHeader();
List<UserConversationModel> conversations = userConversationService.getAllConversationsForUser(userId);
return conversations.stream()
.map(conv -> new ConversationDTO(conv.getConversationId(), conv.getTitle()))
.toList();
}
}