欢迎大家来到IT世界,在知识的湖畔探索吧!
前言
随着企业智能化需求的增长,将AI能力集成到内部办公系统成为趋势。本文详细讲解如何通过企业微信自建应用调用DeepSeek接口,实现智能问答、文本分析等功能。在和DeepSeek官方问答时,会经常出现服务器繁忙,如下图:
欢迎大家来到IT世界,在知识的湖畔探索吧!
DeepSeek开放接口
基于此,我们可以自己对接接口,开发一套属于自己的智能问答机器人,除了官方对外的接口开放平台,腾讯、阿里这些大厂也部署了满血版的DeepSeek,也对外提供接口,本篇文章就基于阿里云提供的接口,来和企微对接。
DeepSeek开放平台网址:
https://api-docs.deepseek.com/zh-cn/
阿里云百炼平台,可以根据平台API接口进行调用,支持Python、Java、HTTP等调用。
企业微信应用
企业微信侧配置
- 创建自建应用
1、登录企业微信管理后台
2、进入「应用管理」→「自建应用」→「创建应用」
3、记录AgentId、Secret和企业CorpId
- 配置可信域名
- 在「我的企业」→「安全与保密」设置API接收消息的域名
- 需完成域名所有权验证(上传验证文件)
阿里云侧:
1、在百炼大模型平台申请key(网址是:
https://bailian.console.aliyun.com/)
2、创建一个key,用于程序调用
代码实现,Talk is cheap,show me the code
1、SpringBoot+Maven项目,在pom.xml文件中引入jar包
com.alibaba
dashscope-sdk-java
2.18.2
欢迎大家来到IT世界,在知识的湖畔探索吧!
2、配置的应用回调地址对应的接口中,获取应用对话的消息:
欢迎大家来到IT世界,在知识的湖畔探索吧!// 阿里云deepseek接口 logger.info("接收到消息=="+map); if (map.containsKey("AgentID")) { String content = map.get("Content").toString(); String fromUserName = map.get("FromUserName").toString(); Text frontText = new Text(); frontText.setContent("请稍后..."); TextMessage frontTextMessage = new TextMessage(); frontTextMessage.setText(frontText); frontTextMessage.setMsgtype("text"); frontTextMessage.setTouser(fromUserName); sendMessageService.sendMessage(frontTextMessage); String result = singleChatService.getResult(content); Text text = new Text(); text.setContent(result); TextMessage textMessage = new TextMessage(); textMessage.setText(text); textMessage.setMsgtype("text"); textMessage.setTouser(fromUserName); sendMessageService.sendMessage(textMessage); return; }
回调的事件如果包含AgentID,就代表有人向企微应用发送消息。
3、调用接口实现智能问答
package com.demo.base.deepseek.service.impl; import com.alibaba.dashscope.aigc.generation.Generation; import com.alibaba.dashscope.aigc.generation.GenerationParam; import com.alibaba.dashscope.aigc.generation.GenerationResult; import com.alibaba.dashscope.common.Message; import com.alibaba.dashscope.common.Role; import com.alibaba.dashscope.exception.ApiException; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import com.demo.base.deepseek.service.SingleChatService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.util.Arrays; @Service public class SingleChatServiceImpl implements SingleChatService { private static final Logger logger = LoggerFactory.getLogger(SingleChatServiceImpl.class); public static GenerationResult callWithMessage(String content) throws ApiException, NoApiKeyException, InputRequiredException { logger.info("开始思考:"); Generation gen = new Generation(); String stringBuilder = content; Message userMsg = Message.builder() .role(Role.USER.getValue()) .content(stringBuilder) .build(); GenerationParam param = GenerationParam.builder() // 若没有配置环境变量,请用百炼API Key将下行替换为:.apiKey("sk-xxx") //.apiKey(System.getenv("DASHSCOPE_API_KEY")) .apiKey("sk-*") .model("deepseek-r1") .messages(Arrays.asList(userMsg)) .resultFormat(GenerationParam.ResultFormat.MESSAGE) .build(); return gen.call(param); } @Override public String getResult(String content) { try { GenerationResult result = callWithMessage(content); logger.info("思考过程:"); logger.info(result.getOutput().getChoices().get(0).getMessage().getReasoningContent()); logger.info("回复内容:"); String reslutContent = result.getOutput().getChoices().get(0).getMessage().getContent(); logger.info(reslutContent); return reslutContent; } catch (NoApiKeyException | InputRequiredException e) { logger.info("生成文本发送错误,{}", e.getMessage()); } return "小助手还在努力训练,争取给出准确的回答!"; } }
完成接口对接,当程序收到企微应用的消息,会把内容对接到deepseek进行分析推理并输出内容。
对话效果
以后这个企微应用,就是你的智能助理!
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/114080.html