feat: Add role-based responses and context handling to OpenRouterService

This commit is contained in:
2025-02-16 18:56:18 +03:00
parent 4d912b6bc9
commit dc1ac12213
3 changed files with 65 additions and 12 deletions
@@ -0,0 +1,17 @@
package ru.cathub.telegabot.model;
public enum TextRole {
DEFAULT("Ты помощник в Telegram-боте для книгоманов"),
REVIEWER("Анализируй текст как профессиональный редактор"),
RECOMMENDER("Рекомендуй книги на основе предпочтений");
private final String systemPrompt;
TextRole(String systemPrompt) {
this.systemPrompt = systemPrompt;
}
public String getPrompt() {
return systemPrompt;
}
}
@@ -1,7 +1,12 @@
package ru.cathub.telegabot.service;
import ru.cathub.telegabot.exception.OpenRouterException;
import ru.cathub.telegabot.model.TextRole;
import java.util.List;
import java.util.Map;
public interface OpenRouterService {
String getChatResponse(String message) throws OpenRouterException;
String getChatResponse(String userMessage, TextRole role) throws OpenRouterException;
String getChatResponseWithContext(String userMessage, List<Map<String, String>> context) throws OpenRouterException;
String getSimpleResponse(String message) throws OpenRouterException;
}
@@ -13,6 +13,7 @@ import ru.cathub.telegabot.configuration.OpenRouterConfig;
import ru.cathub.telegabot.exception.OpenRouterException;
import ru.cathub.telegabot.service.OpenRouterService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -26,7 +27,27 @@ public class OpenRouterServiceImpl implements OpenRouterService {
private final RestTemplate restTemplate;
@Override
public String getChatResponse(String message) throws OpenRouterException {
public String getChatResponse(String userMessage, TextRole role) throws OpenRouterException {
List<Map<String, String>> messages = new ArrayList<>();
messages.add(Map.of("role", "system", "content", role.getPrompt()));
messages.add(Map.of("role", "user", "content", userMessage));
return sendChatRequest(messages);
}
@Override
public String getChatResponseWithContext(String userMessage, List<Map<String, String>> context) throws OpenRouterException {
context.add(Map.of("role", "user", "content", userMessage));
return sendChatRequest(context);
}
@Override
public String getSimpleResponse(String message) throws OpenRouterException {
return sendChatRequest(List.of(
Map.of("role", "user", "content", message)
));
}
private String sendChatRequest(List<Map<String, String>> messages) throws OpenRouterException {
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(config.getKey());
headers.add("HTTP-Referer", "https://github.com/your-username/telegabot");
@@ -34,10 +55,7 @@ public class OpenRouterServiceImpl implements OpenRouterService {
Map<String, Object> request = new HashMap<>();
request.put("model", config.getModel());
request.put("messages", List.of(Map.of(
"role", "user",
"content", message
)));
request.put("messages", messages);
try {
ResponseEntity<Map> response = restTemplate.postForEntity(
@@ -55,12 +73,25 @@ public class OpenRouterServiceImpl implements OpenRouterService {
private String extractResponse(Map<String, Object> response) throws OpenRouterException {
try {
List<Map<String, Object>> choices = (List<Map<String, Object>>) response.get("choices");
Map<String, Object> firstChoice = choices.get(0);
Map<String, String> message = (Map<String, String>) firstChoice.get("message");
return message.get("content");
} catch (Exception e) {
throw new OpenRouterException("Error parsing API response");
if (!response.containsKey("choices")) {
throw new OpenRouterException("Invalid API response format");
}
List<?> choices = (List<?>) response.get("choices");
if (choices.isEmpty()) {
throw new OpenRouterException("No choices in API response");
}
Map<?, ?> firstChoice = (Map<?, ?>) choices.get(0);
Map<?, ?> message = (Map<?, ?>) firstChoice.get("message");
if (message == null || !message.containsKey("content")) {
throw new OpenRouterException("Malformed message in API response");
}
return (String) message.get("content");
} catch (ClassCastException e) {
throw new OpenRouterException("Type casting error in API response");
}
}
}