优化调整
This commit is contained in:
@@ -1,76 +1,110 @@
|
||||
package com.emotion.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* AI聊天服务接口
|
||||
*
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-24
|
||||
*/
|
||||
public interface AiChatService {
|
||||
|
||||
/**
|
||||
* 发送聊天消息
|
||||
* 发送聊天消息(保存用户消息和AI回复)
|
||||
* @param conversationId 会话ID
|
||||
* @param message 用户消息内容
|
||||
* @param userId 用户ID
|
||||
* @return AI回复内容
|
||||
*/
|
||||
String sendChatMessage(String conversationId, String message, String userId);
|
||||
|
||||
/**
|
||||
* 发送聊天消息(仅获取AI回复,不保存用户消息)
|
||||
* 用于WebSocket场景,避免重复保存用户消息
|
||||
* WebSocket方式发送聊天消息(只保存AI回复)
|
||||
* @param conversationId 会话ID
|
||||
* @param message 用户消息内容
|
||||
* @param userId 用户ID
|
||||
* @return AI回复内容
|
||||
*/
|
||||
String sendChatMessageForWebSocket(String conversationId, String message, String userId);
|
||||
|
||||
/**
|
||||
* 生成对话总结
|
||||
* @param conversationId 会话ID
|
||||
* @param userId 用户ID
|
||||
* @return 总结内容
|
||||
*/
|
||||
String generateConversationSummary(String conversationId, String userId);
|
||||
|
||||
/**
|
||||
* 检查服务是否可用
|
||||
* 检查AI服务是否可用
|
||||
* @return 可用返回true,否则false
|
||||
*/
|
||||
boolean isServiceAvailable();
|
||||
|
||||
/**
|
||||
* 获取服务状态
|
||||
* 获取AI服务状态
|
||||
* @return "available" 或 "unavailable"
|
||||
*/
|
||||
String getServiceStatus();
|
||||
|
||||
/**
|
||||
* 发送消息到Coze AI
|
||||
* 发送消息到Coze AI(不保存消息,仅AI交互)
|
||||
* @param conversationId 会话ID
|
||||
* @param userMessage 用户消息内容
|
||||
* @param userId 用户ID
|
||||
* @return AI回复内容
|
||||
*/
|
||||
String sendMessage(String conversationId, String userMessage, String userId);
|
||||
|
||||
/**
|
||||
* 访客聊天(不需要登录)
|
||||
* 访客聊天(不登录情况下)
|
||||
* @param message 用户消息内容
|
||||
* @param clientIp 客户端IP
|
||||
* @return 包含AI回复等信息的Map
|
||||
*/
|
||||
Map<String, Object> guestChat(String message, String clientIp);
|
||||
java.util.Map<String, Object> guestChat(String message, String clientIp);
|
||||
|
||||
/**
|
||||
* 创建对话
|
||||
* 创建新对话
|
||||
* @param userId 用户ID
|
||||
* @param title 对话标题
|
||||
* @return 包含对话信息的Map
|
||||
*/
|
||||
Map<String, Object> createConversation(String userId, String title);
|
||||
java.util.Map<String, Object> createConversation(String userId, String title);
|
||||
|
||||
/**
|
||||
* 获取访客用户信息
|
||||
* @param clientIp 客户端IP
|
||||
* @return 包含访客信息的Map
|
||||
*/
|
||||
Map<String, Object> getGuestUserInfo(String clientIp);
|
||||
java.util.Map<String, Object> getGuestUserInfo(String clientIp);
|
||||
|
||||
/**
|
||||
* 流式聊天
|
||||
* 流式聊天(暂时降级为普通聊天)
|
||||
* @param conversationId 会话ID
|
||||
* @param message 用户消息内容
|
||||
* @param userId 用户ID
|
||||
* @return AI回复内容
|
||||
*/
|
||||
String streamChat(String conversationId, String message, String userId);
|
||||
|
||||
/**
|
||||
* 健康检查
|
||||
* @return 健康返回true,否则false
|
||||
*/
|
||||
boolean healthCheck();
|
||||
|
||||
/**
|
||||
* 生成用户当天的情绪记录总结
|
||||
*
|
||||
* 生成用户情绪记录总结
|
||||
* @param userId 用户ID
|
||||
* @return 情绪记录结果
|
||||
* @return 包含情绪总结等信息的Map
|
||||
*/
|
||||
Map<String, Object> generateEmotionSummary(String userId);
|
||||
java.util.Map<String, Object> generateEmotionSummary(String userId);
|
||||
|
||||
/**
|
||||
* 异步生成用户情绪记录总结
|
||||
* @param userId 用户ID
|
||||
* @return 包含情绪总结等信息的CompletableFuture
|
||||
*/
|
||||
java.util.concurrent.CompletableFuture<java.util.Map<String, Object>> generateEmotionSummaryAsync(String userId);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.math.BigDecimal;
|
||||
@@ -34,6 +36,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* AI聊天服务实现类
|
||||
@@ -1017,12 +1020,12 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
String chatHistory = integrateChatHistory(todayMessages);
|
||||
log.info("聊天记录整合完成,总长度: {}", chatHistory.length());
|
||||
|
||||
// 构建情绪分析提示词
|
||||
String emotionPrompt = buildEmotionAnalysisPrompt(chatHistory);
|
||||
// Coze 中已经在工作流设置了提示词,目前不需要构建情绪分析提示词
|
||||
// String emotionPrompt = buildEmotionAnalysisPrompt(chatHistory);
|
||||
|
||||
// 调用Coze API进行情绪分析总结
|
||||
String conversationId = "emotion_summary_" + userId + "_" + today.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
||||
String emotionSummary = sendSummaryMessage(conversationId, emotionPrompt, userId);
|
||||
String emotionSummary = sendSummaryMessage(conversationId, chatHistory, userId);
|
||||
log.info("情绪分析总结生成完成: {}", emotionSummary);
|
||||
|
||||
// 解析AI返回的情绪分析结果
|
||||
@@ -1049,6 +1052,13 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async("taskExecutor")
|
||||
public CompletableFuture<Map<String, Object>> generateEmotionSummaryAsync(String userId) {
|
||||
Map<String, Object> result = generateEmotionSummary(userId);
|
||||
return CompletableFuture.completedFuture(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 整合聊天记录
|
||||
*/
|
||||
@@ -1106,7 +1116,7 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
return EmotionAnalysis.builder()
|
||||
.primaryEmotion(json.getString("primaryEmotion"))
|
||||
.intensity(BigDecimal.valueOf(json.getDoubleValue("intensity")))
|
||||
.keywords(json.getString("triggers"))
|
||||
.keywords(JSON.toJSONString(Arrays.asList(json.getString("triggers"))))
|
||||
.suggestion(json.getString("suggestions"))
|
||||
.text(summary)
|
||||
.polarity(determinePolarity(json.getString("primaryEmotion")))
|
||||
@@ -1122,7 +1132,7 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
return EmotionAnalysis.builder()
|
||||
.primaryEmotion("平静")
|
||||
.intensity(BigDecimal.valueOf(0.5))
|
||||
.keywords("日常对话")
|
||||
.keywords(JSON.toJSONString(Arrays.asList("日常对话")))
|
||||
.suggestion("保持当前的积极状态")
|
||||
.text(summary)
|
||||
.polarity("neutral")
|
||||
@@ -1192,7 +1202,7 @@ public class AiChatServiceImpl implements AiChatService {
|
||||
.triggers(analysisResult.getKeywords())
|
||||
.description(analysisResult.getText())
|
||||
.notes("基于当天聊天记录自动生成的情绪分析")
|
||||
.tags("AI分析,聊天记录,情绪总结")
|
||||
.tags(JSON.toJSONString(Arrays.asList("AI分析", "聊天记录", "情绪总结")))
|
||||
.build();
|
||||
|
||||
emotionRecordService.save(record);
|
||||
|
||||
Reference in New Issue
Block a user