feat(oa): 优化 AI服务并添加缓存支持
- 添加 Redis 缓存支持,优化 AI 配置和对话历史的获取性能 - 实现 AI回复缓存,减少重复请求的处理时间 - 新增缓存清除接口,支持对话缓存和 AI 配置缓存的清除 - 重构 AI 服务调用流程,增加缓存检查和更新逻辑
This commit is contained in:
@@ -200,9 +200,29 @@ public class SysOaAiController extends BaseController {
|
||||
*/
|
||||
@PostMapping("/conversation/{conversationId}/end")
|
||||
public R<Void> endConversation(@PathVariable Long conversationId) {
|
||||
// 清除对话缓存
|
||||
aiServiceUtil.clearConversationCache(conversationId);
|
||||
return toAjax(conversationService.endConversation(conversationId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除对话缓存
|
||||
*/
|
||||
@DeleteMapping("/conversation/{conversationId}/cache")
|
||||
public R<Void> clearConversationCache(@PathVariable Long conversationId) {
|
||||
aiServiceUtil.clearConversationCache(conversationId);
|
||||
return R.ok("缓存已清除");
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除AI配置缓存
|
||||
*/
|
||||
@DeleteMapping("/cache/config")
|
||||
public R<Void> clearAiConfigCache() {
|
||||
aiServiceUtil.clearAiConfigCache();
|
||||
return R.ok("AI配置缓存已清除");
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息给AI
|
||||
*/
|
||||
@@ -212,8 +232,14 @@ public class SysOaAiController extends BaseController {
|
||||
// 1. 保存用户消息
|
||||
messageService.addUserMessage(conversationId, message);
|
||||
|
||||
// 2. 获取对话历史
|
||||
List<SysOaAiMessageVo> conversationHistory = messageService.queryByConversationId(conversationId);
|
||||
// 2. 获取对话历史(优先从Redis缓存获取)
|
||||
List<SysOaAiMessageVo> conversationHistory = aiServiceUtil.getCachedConversationHistory(conversationId);
|
||||
if (conversationHistory == null) {
|
||||
// 缓存中没有,从数据库获取
|
||||
conversationHistory = messageService.queryByConversationId(conversationId);
|
||||
// 缓存对话历史
|
||||
aiServiceUtil.cacheConversationHistory(conversationId, conversationHistory);
|
||||
}
|
||||
|
||||
// 3. 调用AI服务获取回复(传递对话历史)
|
||||
String aiResponse = callAiServiceWithHistory(message, conversationHistory);
|
||||
@@ -221,6 +247,10 @@ public class SysOaAiController extends BaseController {
|
||||
// 4. 保存AI回复
|
||||
messageService.addAiMessage(conversationId, aiResponse, 0, java.math.BigDecimal.ZERO);
|
||||
|
||||
// 5. 更新对话历史缓存
|
||||
List<SysOaAiMessageVo> updatedHistory = messageService.queryByConversationId(conversationId);
|
||||
aiServiceUtil.cacheConversationHistory(conversationId, updatedHistory);
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("conversationId", conversationId);
|
||||
result.put("response", aiResponse);
|
||||
|
||||
Reference in New Issue
Block a user