2
liusuyi
2026-05-12 9b5c9db9189493fbc6db48f55a7b7311b2a3253c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.ard.agent.service;
 
import com.ard.agent.tool.KnowledgeTools;
import com.ard.agent.tool.MonitorTools;
import com.ard.common.security.utils.SecurityUtils;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
 
@Service
public class AIChatService {
 
    @Resource
    private ChatClient chatClient;
    @Resource
    private MonitorTools monitorTools;  // 注入工具类
    @Resource
    private KnowledgeTools knowledgeTools;
    private static final String SYSTEM_PROMPT = """
        你是摄像头管理助手。请严格遵守以下规则:
        
        1. **工具优先原则**:当用户问题涉及摄像机的实时状态、修改操作、查询具体设备信息时,
           必须调用对应的工具(getCameraDetail/searchCameras/updateCameraInfo),
           绝对不要使用自己的记忆或上下文中的静态文本来回答。
        
        2. **知识库使用场景**:只有当用户询问操作指南、故障排查手册、产品说明等静态知识时,
           才使用知识库中的内容。
        
        3. **冲突处理**:如果知识库中提到了某类操作,但用户要求执行具体操作,
           必须以工具调用的结果为准。
        
        ========== 输出格式 ==========
        使用 Markdown 格式输出:
        - ## 标题
        - - 列表项
        - **加粗** 强调
        - 表格用于数据展示
        """;
    /**
     * 流式对话
     */
    public Flux<String> streamChat(String userMessage) {
        Long userId = SecurityUtils.getUserId();
        return chatClient.prompt()
                .system(SYSTEM_PROMPT)
                .user(userMessage)
                .advisors(advisor -> advisor.param("chat_memory_conversation_id", userId))
                .tools(monitorTools, knowledgeTools)
                .stream()
                .content();
    }
}