优化
This commit is contained in:
@@ -6,11 +6,13 @@ import com.emotion.entity.Message;
|
||||
import com.emotion.entity.Conversation;
|
||||
import com.emotion.entity.CozeApiCall;
|
||||
import com.emotion.entity.EmotionRecord;
|
||||
import com.emotion.entity.EmotionAnalysis;
|
||||
import com.emotion.service.AIChatService;
|
||||
import com.emotion.service.MessageService;
|
||||
import com.emotion.service.ConversationService;
|
||||
import com.emotion.service.CozeApiCallService;
|
||||
import com.emotion.service.EmotionRecordService;
|
||||
import com.emotion.service.EmotionAnalysisService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -59,6 +61,9 @@ public class AiChatServiceImpl implements AIChatService {
|
||||
@Autowired
|
||||
private EmotionRecordService emotionRecordService;
|
||||
|
||||
@Autowired
|
||||
private EmotionAnalysisService emotionAnalysisService;
|
||||
|
||||
@Value("${emotion.coze.api.token:}")
|
||||
private String cozeApiToken;
|
||||
|
||||
@@ -941,7 +946,7 @@ public class AiChatServiceImpl implements AIChatService {
|
||||
log.info("情绪分析总结生成完成: {}", emotionSummary);
|
||||
|
||||
// 解析AI返回的情绪分析结果
|
||||
EmotionAnalysisResult analysisResult = parseEmotionSummary(emotionSummary);
|
||||
EmotionAnalysis analysisResult = parseEmotionSummary(emotionSummary);
|
||||
|
||||
// 创建情绪记录
|
||||
EmotionRecord emotionRecord = createEmotionRecord(userId, analysisResult, chatHistory);
|
||||
@@ -1011,37 +1016,39 @@ public class AiChatServiceImpl implements AIChatService {
|
||||
/**
|
||||
* 解析情绪分析总结结果
|
||||
*/
|
||||
private EmotionAnalysisResult parseEmotionSummary(String summary) {
|
||||
private EmotionAnalysis parseEmotionSummary(String summary) {
|
||||
try {
|
||||
// 尝试从AI回复中提取JSON
|
||||
String jsonStr = extractJsonFromSummary(summary);
|
||||
if (jsonStr != null) {
|
||||
JSONObject json = JSON.parseObject(jsonStr);
|
||||
|
||||
EmotionAnalysisResult result = new EmotionAnalysisResult();
|
||||
result.setPrimaryEmotion(json.getString("primaryEmotion"));
|
||||
result.setIntensity(json.getDoubleValue("intensity"));
|
||||
result.setTriggers(json.getString("triggers"));
|
||||
result.setEmotionTrend(json.getString("emotionTrend"));
|
||||
result.setSuggestions(json.getString("suggestions"));
|
||||
result.setSummary(json.getString("summary"));
|
||||
|
||||
return result;
|
||||
return EmotionAnalysis.builder()
|
||||
.primaryEmotion(json.getString("primaryEmotion"))
|
||||
.intensity(BigDecimal.valueOf(json.getDoubleValue("intensity")))
|
||||
.keywords(json.getString("triggers"))
|
||||
.suggestion(json.getString("suggestions"))
|
||||
.text(summary)
|
||||
.polarity(determinePolarity(json.getString("primaryEmotion")))
|
||||
.confidence(BigDecimal.valueOf(0.85))
|
||||
.analysisTime(LocalDateTime.now())
|
||||
.build();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("解析情绪分析结果失败,使用默认值: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// 如果解析失败,返回默认结果
|
||||
EmotionAnalysisResult defaultResult = new EmotionAnalysisResult();
|
||||
defaultResult.setPrimaryEmotion("平静");
|
||||
defaultResult.setIntensity(0.5);
|
||||
defaultResult.setTriggers("日常对话");
|
||||
defaultResult.setEmotionTrend("相对稳定");
|
||||
defaultResult.setSuggestions("保持当前的积极状态");
|
||||
defaultResult.setSummary(summary);
|
||||
|
||||
return defaultResult;
|
||||
return EmotionAnalysis.builder()
|
||||
.primaryEmotion("平静")
|
||||
.intensity(BigDecimal.valueOf(0.5))
|
||||
.keywords("日常对话")
|
||||
.suggestion("保持当前的积极状态")
|
||||
.text(summary)
|
||||
.polarity("neutral")
|
||||
.confidence(BigDecimal.valueOf(0.5))
|
||||
.analysisTime(LocalDateTime.now())
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1063,14 +1070,14 @@ public class AiChatServiceImpl implements AIChatService {
|
||||
/**
|
||||
* 创建情绪记录
|
||||
*/
|
||||
private EmotionRecord createEmotionRecord(String userId, EmotionAnalysisResult analysisResult, String chatHistory) {
|
||||
private EmotionRecord createEmotionRecord(String userId, EmotionAnalysis analysisResult, String chatHistory) {
|
||||
EmotionRecord record = EmotionRecord.builder()
|
||||
.userId(userId)
|
||||
.recordDate(LocalDate.now())
|
||||
.emotionType(analysisResult.getPrimaryEmotion())
|
||||
.intensity(BigDecimal.valueOf(analysisResult.getIntensity()))
|
||||
.triggers(analysisResult.getTriggers())
|
||||
.description(analysisResult.getSummary())
|
||||
.intensity(analysisResult.getIntensity())
|
||||
.triggers(analysisResult.getKeywords())
|
||||
.description(analysisResult.getText())
|
||||
.notes("基于当天聊天记录自动生成的情绪分析")
|
||||
.tags("AI分析,聊天记录,情绪总结")
|
||||
.build();
|
||||
@@ -1078,37 +1085,14 @@ public class AiChatServiceImpl implements AIChatService {
|
||||
emotionRecordService.save(record);
|
||||
log.info("情绪记录创建成功: recordId={}", record.getId());
|
||||
|
||||
// 设置情绪分析记录的关联ID并保存
|
||||
analysisResult.setUserId(userId);
|
||||
analysisResult.setMessageId(record.getId()); // 关联到情绪记录ID
|
||||
|
||||
emotionAnalysisService.save(analysisResult);
|
||||
log.info("情绪分析记录创建成功: analysisId={}", analysisResult.getId());
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
/**
|
||||
* 情绪分析结果内部类
|
||||
*/
|
||||
public static class EmotionAnalysisResult {
|
||||
private String primaryEmotion;
|
||||
private Double intensity;
|
||||
private String triggers;
|
||||
private String emotionTrend;
|
||||
private String suggestions;
|
||||
private String summary;
|
||||
|
||||
// Getters and Setters
|
||||
public String getPrimaryEmotion() { return primaryEmotion; }
|
||||
public void setPrimaryEmotion(String primaryEmotion) { this.primaryEmotion = primaryEmotion; }
|
||||
|
||||
public Double getIntensity() { return intensity; }
|
||||
public void setIntensity(Double intensity) { this.intensity = intensity; }
|
||||
|
||||
public String getTriggers() { return triggers; }
|
||||
public void setTriggers(String triggers) { this.triggers = triggers; }
|
||||
|
||||
public String getEmotionTrend() { return emotionTrend; }
|
||||
public void setEmotionTrend(String emotionTrend) { this.emotionTrend = emotionTrend; }
|
||||
|
||||
public String getSuggestions() { return suggestions; }
|
||||
public void setSuggestions(String suggestions) { this.suggestions = suggestions; }
|
||||
|
||||
public String getSummary() { return summary; }
|
||||
public void setSummary(String summary) { this.summary = summary; }
|
||||
}
|
||||
}
|
||||
@@ -195,4 +195,10 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> impl
|
||||
// 通过conversation表关联查询用户的消息,根据关键词搜索
|
||||
return this.baseMapper.searchByUserIdAndKeyword(userId, keyword, limit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Message> getRecentByUserId(String userId, Integer limit) {
|
||||
// 获取用户最近的消息,按时间倒序
|
||||
return this.baseMapper.getRecentByUserId(userId, limit);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user