微信公众号接入ai大模型实现智能回复
一、先看效果
我使用大模型是讯飞星火,想要实现这个效果,需要部署一个服务来运行调用大模型的API。我是使用Java语言搭建了一个简易的springboot来实现的
二、实现过程
接下来我说一下具体实现过程
1、注册账号,领取免费使用额度
以星火大模型为例,先去官网注册账号,领取免费调用套餐:讯飞星火大模型
圈中的信息就是调用大模型所需的appid等信息,需要配置到项目中
2、下载调用示例
下载调用api示例demo,我使用的Java语言,下载Java调用示例
讯飞api调用demo下载页面
下载的示例稍作改动作为工具类放在utils下
具体代码如下:星火大模型调用api工具类
package com.xbwang.utils.xfmodel;
import cn.hutool.core.lang.Dict;
import cn.hutool.setting.yaml.YamlUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import okhttp3.*;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
public class BigModelNew extends WebSocketListener {
// 地址与鉴权信息 https://spark-api.xf-yun.com/v1.1/chat 1.5地址 domain参数为general
// 地址与鉴权信息 https://spark-api.xf-yun.com/v2.1/chat 2.0地址 domain参数为generalv2
public static final String hostUrl = "https://spark-api.xf-yun.com/v2.1/chat";
public static String appid;
public static String apiSecret;
public static String apiKey;
static {
Dict dict = YamlUtil.loadByPath("application.yml");
Map<String,String> xfmodel = (Map<String, String>) dict.get("xfmodel");
appid = xfmodel.get("appid");
apiSecret = xfmodel.get("apiSecret");
apiKey = xfmodel.get("apiKey");
}
public static List<RoleContent> historyList=new ArrayList<>(); // 对话历史存储集合
public static String totalAnswer=""; // 大模型的答案汇总
// 环境治理的重要性 环保 人口老龄化 我爱我的祖国
public static String NewQuestion = "";
public static final Gson gson = new Gson();
// 个性化参数
private String userId;
private Boolean wsCloseFlag;
private static Boolean totalFlag=true; // 控制提示用户是否输入
private static Boolean resultFlag=false;
// 构造函数
public BigModelNew(String userId, Boolean wsCloseFlag) {
this.userId = userId;
this.wsCloseFlag = wsCloseFlag;
}
// 主函数
public static void main(String[] args) throws Exception {
// 个性化参数入口,如果是并发使用,可以在这里模拟
while (true){
if(totalFlag){
Scanner scanner=new Scanner(System.in);
System.out.print("我:");
totalFlag=false;
NewQuestion=scanner.nextLine();
// 构建鉴权url
String authUrl = getAuthUrl(hostUrl, apiKey, apiSecret);
OkHttpClient client = new OkHttpClient.Builder().build();
String url = authUrl.toString().replace("http://", "ws://").replace("https://", "wss://");
Request request = new Request.Builder().url(url).build();
for (int i = 0; i < 1; i++) {
totalAnswer="";
WebSocket webSocket = client.newWebSocket(request, new BigModelNew(i + "",
false));
}
}else{
Thread.sleep(200);
}
}
}
public static String answer(String problem) throws Exception {
while (true){
if(totalFlag){
System.out.print("我:");
totalFlag=false;
NewQuestion=problem;
System.out.println(problem);
// 构建鉴权url
String authUrl = getAuthUrl(hostUrl, apiKey, apiSecret);
OkHttpClient client = new OkHttpClient.Builder().build();
String url = authUrl.toString().replace("http://", "ws://").replace("https://", "wss://");
Request request = new Request.Builder().url(url).build();
for (int i = 0; i < 1; i++) {
totalAnswer="";
WebSocket webSocket = client.newWebSocket(request, new BigModelNew(i + "",
false));
}
}else{
Thread.sleep(200);
}
if (resultFlag){
System.out.println("最终结果:"+totalAnswer);
resultFlag = false;
break;
}
}
return totalAnswer;
}
public static boolean canAddHistory(){ // 由于历史记录最大上线1.2W左右,需要判断是能能加入历史
int history_length=0;
for(RoleContent temp:historyList){
history_length=history_length+temp.content.length();
}
if(history_length>12000){
historyList.remove(0);
historyList.remove(1);
historyList.remove(2);
historyList.remove(3);
historyList.remove(4);
return false;
}else{
return true;
}
}
// 线程来发送音频与参数
class MyThread extends Thread {
private WebSocket webSocket;
public MyThread(WebSocket webSocket) {
this.webSocket = webSocket;
}
public void run() {
try {
JSONObject requestJson=new JSONObject();
JSONObject header=new JSONObject(); // header参数
header.put("app_id",appid);
header.put("uid",UUID.randomUUID().toString().substring(0, 10));
JSONObject parameter=new JSONObject(); // parameter参数
JSONObject chat=new JSONObject();
chat.put("domain","generalv2");
chat.put("temperature",0.5);
chat.put("max_tokens",4096);
parameter.put("chat",chat);
JSONObject payload=new JSONObject(); // payload参数
JSONObject message=new JSONObject();
JSONArray text=new JSONArray();
// 历史问题获取
if(historyList.size()>0){
for(RoleContent tempRoleContent:historyList){
text.add(JSON.toJSON(tempRoleContent));
}
}
// 最新问题
RoleContent roleContent=new RoleContent();
roleContent.role="user";
roleContent.content=NewQuestion;
text.add(JSON.toJSON(roleContent));
historyList.add(roleContent);
message.put("text",text);
payload.put("message",message);
requestJson.put("header",header);
requestJson.put("parameter",parameter);
requestJson.put("payload",payload);
// System.err.println(requestJson); // 可以打印看每次的传参明细
webSocket.send(requestJson.toString());
// 等待服务端返回完毕后关闭
while (true) {
// System.err.println(wsCloseFlag + "---");
Thread.sleep(200);
if (wsCloseFlag) {
break;
}
}
webSocket.close(1000, "");
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
System.out.print("大模型:");
MyThread myThread = new MyThread(webSocket);
myThread.start();
}
@Override
public void onMessage(WebSocket webSocket, String text) {
// System.out.println(userId + "用来区分那个用户的结果" + text);
JsonParse myJsonParse = gson.fromJson(text, JsonParse.class);
if (myJsonParse.header.code != 0) {
System.out.println("发生错误,错误码为:" + myJsonParse.header.code);
System.out.println("本次请求的sid为:" + myJsonParse.header.sid);
webSocket.close(1000, "");
}
List<Text> textList = myJsonParse.payload.choices.text;
for (Text temp : textList) {
System.out.print(temp.content);
totalAnswer=totalAnswer+temp.content;
}
if (myJsonParse.header.status == 2) {
resultFlag = true;
// 可以关闭连接,释放资源
System.out.println();
System.out.println("*************************************************************************************");
if(canAddHistory()){
RoleContent roleContent=new RoleContent();
roleContent.setRole("assistant");
roleContent.setContent(totalAnswer);
historyList.add(roleContent);
}else{
historyList.remove(0);
RoleContent roleContent=new RoleContent();
roleContent.setRole("assistant");
roleContent.setContent(totalAnswer);
historyList.add(roleContent);
}
wsCloseFlag = true;
totalFlag=true;
}
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
super.onFailure(webSocket, t, response);
try {
if (null != response) {
int code = response.code();
System.out.println("onFailure code:" + code);
System.out.println("onFailure body:" + response.body().string());
if (101 != code) {
System.out.println("connection failed");
System.exit(0);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 鉴权方法
public static String getAuthUrl(String hostUrl, String apiKey, String apiSecret) throws Exception {
URL url = new URL(hostUrl);
// 时间
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = format.format(new Date());
// 拼接
String preStr = "host: " + url.getHost() + "\n" +
"date: " + date + "\n" +
"GET " + url.getPath() + " HTTP/1.1";
// System.err.println(preStr);
// SHA256加密
Mac mac = Mac.getInstance("hmacsha256");
SecretKeySpec spec = new SecretKeySpec(apiSecret.getBytes(StandardCharsets.UTF_8), "hmacsha256");
mac.init(spec);
byte[] hexDigits = mac.doFinal(preStr.getBytes(StandardCharsets.UTF_8));
// Base64加密
String sha = Base64.getEncoder().encodeToString(hexDigits);
// System.err.println(sha);
// 拼接
String authorization = String.format("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", apiKey, "hmac-sha256", "host date request-line", sha);
// 拼接地址
HttpUrl httpUrl = Objects.requireNonNull(HttpUrl.parse("https://" + url.getHost() + url.getPath())).newBuilder().//
addQueryParameter("authorization", Base64.getEncoder().encodeToString(authorization.getBytes(StandardCharsets.UTF_8))).//
addQueryParameter("date", date).//
addQueryParameter("host", url.getHost()).//
build();
// System.err.println(httpUrl.toString());
return httpUrl.toString();
}
//返回的json结果拆解
class JsonParse {
Header header;
Payload payload;
}
class Header {
int code;
int status;
String sid;
}
class Payload {
Choices choices;
}
class Choices {
List<Text> text;
}
class Text {
String role;
String content;
}
class RoleContent{
String role;
String content;
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
}
3、搭建项目框架
将微信公众号信息,星火大模型信息配置写入yml配置文件中
server:
port: 8090
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
logging:
level:
com:
xbwang: debug
# 微信公众号配置
wxToken: xxx
appId: xxx
appsecret: xxxx
### 域名
DNS_URl: http://xxxxxx.xxx
#科大讯飞大模型
xfmodel:
appid: xxx
apiSecret: xxxxxxxxxx
apiKey: xxxxxxxxxxx
关于如何集成微信公众号可以参考我之前写的文章:微信公众号集成
然后整个框架结构如图:
这是一个很纯净的只为微信公众号自动回复功能服务的项目,完整的项目我发布在gitee,地址如下:
gitee项目地址
注意:项目下载下来后要把yml配置文件里信息改成自己的
4、项目部署
这个项目部署就跟普通的springboot项目一样,不再赘述
部署成功后,向公众号发送消息,可以查看到日志输出信息
至此项目搭建完成
三、衍生项目
微信公众号智能回复实现了,我还做了一个微信上智能回复助手,可以单人聊天智能回复,也可以在群聊里智能回复,先看效果
大家慎用
这个项目介绍我放在了我的公众号上,大家感兴趣可以去我公众号上查看,也求关注求点赞