This commit is contained in:
2025-09-10 20:40:06 +08:00
parent 73063af861
commit 8991a00e17
7 changed files with 101 additions and 96 deletions
+47 -34
View File
@@ -140,8 +140,8 @@ func (p *PetChatService) StreamChat(ctx context.Context, userId uint, req ChatRe
global.GVA_LOG.Error("敏感词检测失败", zap.Error(err))
eventChan <- StreamEvent{
Event: "error",
Data: map[string]interface{}{
"error": "敏感词检测失败",
Data: petResponse.StreamErrorData{
Error: "敏感词检测失败",
},
}
return err
@@ -158,18 +158,17 @@ func (p *PetChatService) StreamChat(ctx context.Context, userId uint, req ChatRe
eventChan <- StreamEvent{
Event: "message",
Data: ChatResponse{
Message: "您的消息包含不当内容,请重新输入。",
SessionId: req.SessionId,
IsSensitive: true,
TokenCount: 0,
ResponseTime: time.Since(startTime).Milliseconds(),
Data: petResponse.StreamMessageData{
Delta: "您的消息包含不当内容,请重新输入。",
},
}
eventChan <- StreamEvent{
Event: "done",
Data: map[string]interface{}{"finished": true},
Data: petResponse.StreamDoneData{
Message: "您的消息包含不当内容,请重新输入。",
SessionId: req.SessionId,
},
}
return nil
}
@@ -186,8 +185,8 @@ func (p *PetChatService) StreamChat(ctx context.Context, userId uint, req ChatRe
global.GVA_LOG.Error("获取对话历史失败", zap.Error(err))
eventChan <- StreamEvent{
Event: "error",
Data: map[string]interface{}{
"error": "获取对话历史失败",
Data: petResponse.StreamErrorData{
Error: "获取对话历史失败",
},
}
return err
@@ -215,8 +214,8 @@ func (p *PetChatService) StreamChat(ctx context.Context, userId uint, req ChatRe
global.GVA_LOG.Error("LLM流式调用失败", zap.Error(err))
eventChan <- StreamEvent{
Event: "error",
Data: map[string]interface{}{
"error": "LLM调用失败",
Data: petResponse.StreamErrorData{
Error: "LLM调用失败",
},
}
}
@@ -246,9 +245,8 @@ func (p *PetChatService) StreamChat(ctx context.Context, userId uint, req ChatRe
// 转发消息给客户端
eventChan <- StreamEvent{
Event: "message",
Data: map[string]interface{}{
"delta": delta,
"sessionId": sessionId,
Data: petResponse.StreamMessageData{
Delta: delta,
},
}
}
@@ -284,13 +282,9 @@ func (p *PetChatService) StreamChat(ctx context.Context, userId uint, req ChatRe
// 发送完成事件
eventChan <- StreamEvent{
Event: "done",
Data: ChatResponse{
Message: fullMessage,
SessionId: sessionId,
IsSensitive: aiHasSensitive,
TokenCount: tokenCount,
ResponseTime: responseTime,
RequestId: llmReq.RequestID,
Data: petResponse.StreamDoneData{
Message: fullMessage,
SessionId: sessionId,
},
}
return nil
@@ -300,8 +294,8 @@ func (p *PetChatService) StreamChat(ctx context.Context, userId uint, req ChatRe
global.GVA_LOG.Error("LLM流式响应错误", zap.Any("error", llmEvent.Data.Error))
eventChan <- StreamEvent{
Event: "error",
Data: map[string]interface{}{
"error": "LLM响应错误",
Data: petResponse.StreamErrorData{
Error: "LLM响应错误",
},
}
return fmt.Errorf("LLM响应错误")
@@ -311,7 +305,7 @@ func (p *PetChatService) StreamChat(ctx context.Context, userId uint, req ChatRe
}
// GetChatHistory 获取对话历史
func (p *PetChatService) GetChatHistory(ctx context.Context, userId uint, sessionId string, limit int) ([]pet.PetAiAssistantConversations, error) {
func (p *PetChatService) GetChatHistory(ctx context.Context, userId uint, sessionId string, limit int) ([]petResponse.ChatHistoryItem, error) {
var conversations []pet.PetAiAssistantConversations
query := global.GVA_DB.WithContext(ctx).Where("user_id = ?", userId)
@@ -331,7 +325,28 @@ func (p *PetChatService) GetChatHistory(ctx context.Context, userId uint, sessio
conversations[i], conversations[j] = conversations[j], conversations[i]
}
return conversations, nil
// 转换为简化结构
items := make([]petResponse.ChatHistoryItem, 0, len(conversations))
for _, conv := range conversations {
if conv.MessageContent != nil && conv.Role != nil {
item := petResponse.ChatHistoryItem{
ID: conv.ID,
Role: *conv.Role,
Message: *conv.MessageContent,
SessionId: "",
CreatedAt: conv.CreatedAt,
}
// 设置会话ID(如果存在)
if conv.SessionId != nil {
item.SessionId = *conv.SessionId
}
items = append(items, item)
}
}
return items, nil
}
// SaveConversation 保存对话记录
@@ -368,7 +383,7 @@ func (p *PetChatService) GetChatSessions(ctx context.Context, userId uint) ([]ma
}
// buildMessages 构建LLM请求消息
func (p *PetChatService) buildMessages(history []pet.PetAiAssistantConversations, userMessage string) []utils.LLMMessage {
func (p *PetChatService) buildMessages(history []petResponse.ChatHistoryItem, userMessage string) []utils.LLMMessage {
messages := []utils.LLMMessage{
{
Role: "system",
@@ -378,12 +393,10 @@ func (p *PetChatService) buildMessages(history []pet.PetAiAssistantConversations
// 添加历史对话
for _, conv := range history {
if conv.MessageContent != nil && conv.Role != nil {
messages = append(messages, utils.LLMMessage{
Role: *conv.Role,
Content: *conv.MessageContent,
})
}
messages = append(messages, utils.LLMMessage{
Role: conv.Role,
Content: conv.Message,
})
}
// 添加当前用户消息