跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客GitHub 精选镜像工具UI配色美学隐私政策关于联系
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
JavaAIjava

基于腾讯元器智能体构建专属 AI 聊天工具

基于腾讯元器平台创建自定义 AI 智能体,通过工作流管理配置参数提取、大模型及知识问答节点。结合 Java Spring Boot 实现第三方调用,使用 RestTemplate 发送 HTTP 请求至元器 API,并通过 Redis 缓存交互数据。支持自定义提示词与模型参数,实现聊天机器人功能。

SecGuard发布于 2026/4/11更新于 2026/5/2217 浏览
基于腾讯元器智能体构建专属 AI 聊天工具

一、进入腾讯元器平台

访问腾讯元器官网。

文章配图

二、登录并选择智能体模板

登录后在智能体模板中选择所需类型,例如博物馆导览文案生成智能体。

文章配图

文章配图

点击左上角复制智能体,即可在'我的智能体'页面查看。或者点击新建智能体来创建。

文章配图

文章配图

进入编辑页面,可在应用设置中自定义模型设置、工作流、欢迎语等。

文章配图

点击工作流管理进入核心流程配置界面。

文章配图

文章配图

三、主要构建工作流的工具

(1)参数提取器

当用户配置好智能体调用的 APIKEY 和 URL 后,通过与用户进行单轮或多轮对话收集参数,缺失或模糊时可自动反问澄清。

文章配图

文章配图

(2)选项卡

提供可点击的选项,适合选项明确且数量少的情况。支持手动输入固定选项(如售前、售后咨询)或动态拉取变量值。

文章配图

文章配图

(3)文件收集

设置引导语和要求,收集用户上传的文件(文档、音频等)。

文章配图

文章配图

文章配图

(4)文本收集

在问答场景中向用户提问,原样输出用户回答。适用场景:收集信息后传递给后续节点处理、沉淀意见反馈原文、合规留存等。特点:不做判断、不追问,一次性收集。

文章配图

文章配图

文章配图

(5)大模型

使用大模型处理自然语言要求,如回答问题。

文章配图

文章配图

(6)大模型意图识别

大模型根据待识别内容识别意图,如是、否。

文章配图

文章配图

文章配图

文章配图

(7)大模型知识问答

从限定范围的知识库中检索内容,并通过大模型润色回复。

文章配图

文章配图

文章配图

(8)工具

调用自定义 HTTP 接口,集成外部数据和服务。多个工作流重复调用时,建议创建插件。

文章配图

文章配图

文章配图

更多工具使用方法可参考官方文档。

文章配图

四、使用工作流创建简单聊天工具

(1)新建智能体

新建一个智能体并点击对话智能体。

文章配图

(2)填写基本信息

填写智能体的名称和简介,可点击 AI 生成头像。

文章配图

(3)应用设置

进入智能体内部的应用设置并根据需求填写。

文章配图

(4)创建工作流

点击顶部菜单的工作流管理并点击新建,选择手动录入。

文章配图

(5)配置工作流描述

填写工作流名称及描述,可使用模板一键优化,确认进入编辑页面。

文章配图

文章配图

(6)配置参数提取

编辑工作流,第一步使用参数提取工具将工具拖拉进画布。注意:不允许在节点开始后与其他信息收集类节点并行。用于提取用户发来的聊天内容。

文章配图

点击编辑,填写参数名称、参数类型及参数描述。

文章配图

(7)连接大模型知识问答

第二步选择大模型知识问答工具,连接到参数提取器后面。变量名称需与代码使用的变量名一致,数据源引用参数提取器的输出内容赋值给 message。提示词用于让 AI 知道任务目标,使用 {{message}} 格式引用输入变量。

文章配图

文章配图

(8)连接工具节点

在大模型知识问答后面连接工具,接受第三方发起的工作流调用并启动工作流。接口连接必须是公网 IP(本地电脑可使用内网穿透)。

文章配图

API 参数需与第三方平台接口设定保持一致。

文章配图

第三方平台的接口如果有输出变量可写在下方。

文章配图

调试通过后发布,在应用发布中找到体验链接内的 appid 和 appkey 填入第三方平台即可使用。

文章配图

五、编写第三方平台的聊天 AI 实现代码

(1)Controller 类
@RestController
@RequestMapping("/Ai")
public class AiController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private AiServerImpl aiServer;

    @GetMapping("/AiChat")
    private String AiChat(@RequestParam("message") String Message) {
        return aiServer.GetAiChat(Message);
    }

    @GetMapping("/GetMessage")
    public String getMessage(String message) {
        if (message != null) aiServer.getMessage(message);
        else {
            System.out.println((String) redisTemplate.opsForValue().get("ms1"));
        }
        return (String) redisTemplate.opsForValue().get("ms1");
    }
}
(2)Service 接口
public interface AiServer {
    String GetAiChat(String Message);
    void getMessage(String message);
}
(3)ServiceImpl 类
@Slf4j
@Service
public class AiServerImpl implements AiServer {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private AiMapper aiMapper;

    private static final String DEEPSEEK_BASE_URL = "https://yuanqi.tencent.com/openapi/v1/agent/chat/completions";
    private static final String DEEPSEEK_API_KEY = "智能体密钥";
    private static final String DEFAULT_MODEL = "智能体 id";

    public String GetAiChat(String Message) {
        List<AiModel> aiModels = aiMapper.getAiModel();
        if (aiModels == null || aiModels.isEmpty()) {
            return "(模型配置为空,请检查 AI 模型表)";
        }
        String modelName = aiModels.get(0).getAgent_name();
        System.out.println(modelName);

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        httpHeaders.set("Authorization", "Bearer " + DEEPSEEK_API_KEY);

        OpenAiMessage userMessage = new OpenAiMessage();
        userMessage.setRole("user");
        userMessage.setContent(Message);

        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(buildRequestBody(DEFAULT_MODEL, "1", Message), httpHeaders);
        try {
            ResponseEntity<String> response = restTemplate.exchange(DEEPSEEK_BASE_URL, HttpMethod.POST, entity, String.class);
            if (response.getStatusCode() == HttpStatus.OK) {
                String resp = response.getBody();
                if (resp != null) {
                    log.info(resp);
                    return resp;
                }
            }
            return "(无返回内容)";
        } catch (HttpClientErrorException ex) {
            String respBody = ex.getResponseBodyAsString();
            String errorMsg = String.format("(模型调用失败) %s: %s", ex.getStatusCode(), respBody != null ? respBody : "[no body]");
            System.err.println("API 调用 4xx 错误:" + errorMsg);
            return errorMsg;
        } catch (Exception ex) {
            String errorMsg = String.format("(模型调用异常) %s: %s", ex.getClass().getSimpleName(), ex.getMessage());
            System.err.println("API 调用通用异常:" + errorMsg);
            return errorMsg;
        }
    }

    @Override
    public void getMessage(String message) {
        redisTemplate.opsForValue().set("ms1", message);
    }

    private Map<String, Object> buildRequestBody(String agentId, String userId, String message) {
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("assistant_id", agentId);
        requestBody.put("user_id", userId);
        requestBody.put("stream", false);

        List<Map<String, Object>> messages = new ArrayList<>();
        Map<String, Object> userMsg = new HashMap<>();
        userMsg.put("role", "user");
        List<Map<String, Object>> contentList = new ArrayList<>();
        Map<String, Object> textContent = new HashMap<>();
        textContent.put("type", "text");
        String jsonMessage = buildJsonMessage(message);
        textContent.put("text", jsonMessage);
        contentList.add(textContent);
        userMsg.put("content", contentList);
        messages.add(userMsg);
        requestBody.put("messages", messages);

        Map<String, Object> parameters = new HashMap<>();
        parameters.put("temperature", 0.7);
        parameters.put("max_tokens", 2000);
        requestBody.put("parameters", parameters);
        return requestBody;
    }

    private String buildJsonMessage(String message) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("content", message);
            return new ObjectMapper().writeValueAsString(params);
        } catch (Exception e) {
            log.error("构建 JSON 消息失败", e);
            String escapedContent = message.replace("\"", "\\\"").replace("\n", "\\n");
            return String.format("{\"content\":\"%s\"}", escapedContent);
        }
    }

    @Data
    static class OpenAiMessage {
        private String role;
        private String content;
    }

    @Data
    static class OpenAiChatRequest {
        private String model;
        private List<OpenAiMessage> messages;
        @JsonProperty("temperature")
        private Double temperature = 0.7;
    }

    @Data
    static class OpenAiChatResponse {
        private List<Choice> choices;
        private OpenAiError error;

        @Data
        static class Choice {
            private OpenAiMessage message;
            @JsonProperty("finish_reason")
            private String finishReason;
        }

        @Data
        static class OpenAiError {
            private Integer code;
            private String message;
            private String type;
        }
    }
}
(4)实体类
public class AiModel implements Serializable {
    private long id;
    private long user_id;
    private String agent_id;
    private String agent_name;
    private String api_key;
    private String api_endpoint;

    public long getId() { return id; }
    public void setId(long id) { this.id = id; }
    public long getUser_id() { return user_id; }
    public void setUser_id(long user_id) { this.user_id = user_id; }
    public String getAgent_id() { return agent_id; }
    public void setAgent_id(String agent_id) { this.agent_id = agent_id; }
    public String getAgent_name() { return agent_name; }
    public void setAgent_name(String agent_name) { this.agent_name = agent_name; }
    public String getApi_key() { return api_key; }
    public void setApi_key(String api_key) { this.api_key = api_key; }
    public String getApi_endpoint() { return api_endpoint; }
    public void setApi_endpoint(String api_endpoint) { this.api_endpoint = api_endpoint; }
}
(5)Mapper 类
@Mapper
public interface AiMapper {
    @Select("select id,user_id,agent_id,agent_name,api_key,api_endpoint from yuanqi_agent where id=1")
    public List<AiModel> getAiModel();
}
(6)RestTemplate 配置
@Configuration
public class Config {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
(7)Redis 配置
@Configuration
public class RedisAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean(name = {"redisTemplate"})
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(om, Object.class);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        template.setDefaultSerializer(jackson2JsonRedisSerializer);
        return template;
    }

    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
        ObjectMapper om = new ObjectMapper();
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(om, Object.class);
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofDays(1))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
        return RedisCacheManager.builder(factory).cacheDefaults(config).build();
    }
}

六、效果演示

文章配图

文章配图

文章配图

文章配图

文章配图

目录

  1. 一、进入腾讯元器平台
  2. 二、登录并选择智能体模板
  3. 三、主要构建工作流的工具
  4. (1)参数提取器
  5. (2)选项卡
  6. (3)文件收集
  7. (4)文本收集
  8. (5)大模型
  9. (6)大模型意图识别
  10. (7)大模型知识问答
  11. (8)工具
  12. 四、使用工作流创建简单聊天工具
  13. (1)新建智能体
  14. (2)填写基本信息
  15. (3)应用设置
  16. (4)创建工作流
  17. (5)配置工作流描述
  18. (6)配置参数提取
  19. (7)连接大模型知识问答
  20. (8)连接工具节点
  21. 五、编写第三方平台的聊天 AI 实现代码
  22. (1)Controller 类
  23. (2)Service 接口
  24. (3)ServiceImpl 类
  25. (4)实体类
  26. (5)Mapper 类
  27. (6)RestTemplate 配置
  28. (7)Redis 配置
  29. 六、效果演示
  • 💰 8折买阿里云服务器限时8折了解详情
  • Magick API 一键接入全球大模型注册送1000万token查看
  • 🤖 一键搭建Deepseek满血版了解详情
  • 一键打造专属AI 智能体了解详情
极客日志微信公众号二维码

微信扫一扫,关注极客日志

微信公众号「极客日志V2」,在微信中扫描左侧二维码关注。展示文案:极客日志V2 zeeklog

更多推荐文章

查看全部
  • ES6 数组 some 与 every 方法用法
  • 为什么 AI 难以取代软件工程师?
  • Formality 原语(primitive)概念详解
  • PHP 对接 DeepSeek API 实现指南
  • VS Code 中 GitHub 扩展登录报错:尚未完成授权此扩展使用 GitHub 的操作
  • FPGA 实现 CAN 总线原理与 Verilog 代码详解
  • llama.cpp 加载多模态 GGUF 模型方法
  • GitHub OAuth 登录对接配置指南
  • VSCode 连接 GitHub 的 Git 操作流程与分支管理
  • Linux TCP 协议基础与连接管理详解:从三次握手到四次挥手
  • Python+Flask+Vue 在线宠物医疗预约平台的设计与实现
  • 基于 SWIFT 的 VLLM 推理加速与部署实战
  • Vibe Coding:用 AI 自动化生成完整产品代码
  • 线性代数与 AI 的关系
  • 10 种专业渗透测试工具详解
  • 归并排序详解:递归实现与复杂度分析
  • CoRAL:协作检索增强大型语言模型改进长尾推荐
  • C++11 详解:列表初始化与右值引用移动语义
  • Llama-3.2-3B 代码审查:基于 Java 面试题的质量评估体系
  • Python 使用 SQLAlchemy 操作 MySQL 入门与实战

相关免费在线工具

  • Keycode 信息

    查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online

  • Escape 与 Native 编解码

    JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online

  • JavaScript / HTML 格式化

    使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online

  • JavaScript 压缩与混淆

    Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online

  • RSA密钥对生成器

    生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online

  • Mermaid 预览与可视化编辑

    基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online