langchain4j实现情感分析
枚举
@RequiredArgsConstructor
@Getter
public enum Sentiment {
POSITIVE("积极"),
NEUTRAL("中性"),
NEGATIVE("消极");
private final String message;
}
AiService
@AiService(
wiringMode = AiServiceWiringMode.EXPLICIT,
chatModel = "qwenChatModel"
)
public interface SentimentService {
@UserMessage("分析情绪:{{it}}")
Sentiment analyzeSentimentOf(String text);
@UserMessage("{{it}} 是积极的情绪吗?")
boolean isPositive(String text);
}
controller
@RestController
@RequestMapping("sentiment")
public class SentimentController {
@Autowired
private SentimentService sentimentService;
@GetMapping("analyze/{message}")
public String analyze(@PathVariable String message) {
Sentiment sentiment = sentimentService.analyzeSentimentOf(message);
return sentiment.getMessage();
}
@GetMapping("positive/{message}")
public Boolean positive(@PathVariable String message) {
return sentimentService.isPositive(message);
}
}