|
| 1 | +package kattsyn.dev.rentplace.controllers; |
| 2 | + |
| 3 | +import io.swagger.v3.oas.annotations.Operation; |
| 4 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 5 | +import io.swagger.v3.oas.annotations.responses.ApiResponses; |
| 6 | +import io.swagger.v3.oas.annotations.security.SecurityRequirement; |
| 7 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 8 | +import jakarta.security.auth.message.AuthException; |
| 9 | +import jakarta.validation.Valid; |
| 10 | +import kattsyn.dev.rentplace.dtos.ai.GenerateDescriptionRequest; |
| 11 | +import kattsyn.dev.rentplace.dtos.ai.GenerateDescriptionResponse; |
| 12 | +import kattsyn.dev.rentplace.services.AiService; |
| 13 | +import lombok.RequiredArgsConstructor; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.http.ResponseEntity; |
| 16 | +import org.springframework.web.bind.annotation.*; |
| 17 | + |
| 18 | +@RestController |
| 19 | +@RequestMapping("${api.path}/ai") |
| 20 | +@RequiredArgsConstructor |
| 21 | +@Tag(name = "AI Controller", description = "Интеграция с OpenRouter для работы с LLM моделями") |
| 22 | +public class AiController { |
| 23 | + |
| 24 | + private final AiService aiService; |
| 25 | + |
| 26 | + @Operation( |
| 27 | + summary = "Сгенерировать описание проекта", |
| 28 | + description = "Принимает системный и пользовательский промпты, отсылает их в Open Router AI и возвращает сгенерированный текст." |
| 29 | + ) |
| 30 | + @ApiResponses({ |
| 31 | + @ApiResponse(responseCode = "200", description = "Сгенерированное описание получено"), |
| 32 | + @ApiResponse(responseCode = "400", description = "Неверные данные запроса"), |
| 33 | + @ApiResponse(responseCode = "401", description = "Неавторизованный доступ"), |
| 34 | + @ApiResponse(responseCode = "403", description = "Доступ запрещён"), |
| 35 | + @ApiResponse(responseCode = "404", description = "Пользователь не найден"), |
| 36 | + @ApiResponse(responseCode = "429", description = "Превышен лимит AI-запросов (10 в час)"), |
| 37 | + @ApiResponse(responseCode = "500", description = "Внутренняя ошибка сервера") |
| 38 | + }) |
| 39 | + @PostMapping(path = "/description", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) |
| 40 | + @SecurityRequirement(name = "JWT") |
| 41 | + public ResponseEntity<GenerateDescriptionResponse> generateDescription( |
| 42 | + @Valid @ModelAttribute GenerateDescriptionRequest request |
| 43 | + ) throws AuthException { |
| 44 | + GenerateDescriptionResponse response = new GenerateDescriptionResponse(aiService.generateDescription(request)); |
| 45 | + return ResponseEntity.ok(response); |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments