|
| 1 | +package org.beehive.gpullama3.api.controller; |
| 2 | + |
| 3 | +import org.beehive.gpullama3.api.service.LLMService; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.http.MediaType; |
| 6 | +import org.springframework.web.bind.annotation.GetMapping; |
| 7 | +import org.springframework.web.bind.annotation.PostMapping; |
| 8 | +import org.springframework.web.bind.annotation.RequestBody; |
| 9 | +import org.springframework.web.bind.annotation.RestController; |
| 10 | +import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
| 11 | + |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +@RestController |
| 15 | +public class ChatController { |
| 16 | + |
| 17 | + @Autowired |
| 18 | + private LLMService llmService; |
| 19 | + |
| 20 | + @PostMapping("/chat") |
| 21 | + public Map<String, String> chat(@RequestBody ChatRequest request) { |
| 22 | + logRequest("NON_STREAMING", request, 150, 0.7, 0.9); |
| 23 | + |
| 24 | + if (request.getMessage() == null || request.getMessage().trim().isEmpty()) { |
| 25 | + throw new IllegalArgumentException("Message cannot be empty"); |
| 26 | + } |
| 27 | + |
| 28 | + String response = llmService.generateResponse(request.getMessage(), request.getSystemMessage()); |
| 29 | + |
| 30 | + return Map.of("response", response); |
| 31 | + } |
| 32 | + |
| 33 | + @PostMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
| 34 | + public SseEmitter streamChat(@RequestBody ChatRequest request) { |
| 35 | + logRequest("STREAMING", request, 150, 0.7, 0.9); |
| 36 | + |
| 37 | + if (request.getMessage() == null || request.getMessage().trim().isEmpty()) { |
| 38 | + throw new IllegalArgumentException("Message cannot be empty"); |
| 39 | + } |
| 40 | + |
| 41 | + SseEmitter emitter = new SseEmitter(Long.MAX_VALUE); |
| 42 | + llmService.generateStreamingResponse(request.getMessage(), request.getSystemMessage(), emitter); |
| 43 | + |
| 44 | + return emitter; |
| 45 | + } |
| 46 | + |
| 47 | + @GetMapping("/health") |
| 48 | + public Map<String, String> health() { |
| 49 | + return Map.of("status", "healthy", "timestamp", String.valueOf(System.currentTimeMillis())); |
| 50 | + } |
| 51 | + |
| 52 | + private void logRequest(String type, ChatRequest request, int maxTokens, double temperature, double topP) { |
| 53 | + System.out.printf("REQUEST [%s] user='%s' system='%s' maxTokens=%d temp=%.2f topP=%.2f%n", |
| 54 | + type, |
| 55 | + truncate(request.getMessage(), 100), |
| 56 | + request.getSystemMessage() != null ? truncate(request.getSystemMessage(), 40) : "none", |
| 57 | + maxTokens, |
| 58 | + temperature, |
| 59 | + topP |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + private String truncate(String text, int maxLength) { |
| 64 | + if (text == null) return "null"; |
| 65 | + return text.length() > maxLength ? text.substring(0, maxLength) + "..." : text; |
| 66 | + } |
| 67 | + |
| 68 | + // Simple request class for custom parameters |
| 69 | + public static class ChatRequest { |
| 70 | + private String message; |
| 71 | + private String systemMessage; |
| 72 | + |
| 73 | + public String getMessage() { return message; } |
| 74 | + public void setMessage(String message) { this.message = message; } |
| 75 | + |
| 76 | + public String getSystemMessage() { return systemMessage; } |
| 77 | + public void setSystemMessage(String systemMessage) { this.systemMessage = systemMessage; } |
| 78 | + } |
| 79 | +} |
0 commit comments