|
|
@@ -0,0 +1,122 @@
|
|
|
+package com.goafanti.common.utils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.goafanti.baiduAI.bo.InputSendChat;
|
|
|
+import com.goafanti.common.error.BusinessException;
|
|
|
+import okhttp3.*;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+
|
|
|
+public class BaiduChatUtils {
|
|
|
+
|
|
|
+ @Value(value = "${baidu.ApiKey}")
|
|
|
+ private String baiduApiKey=null;
|
|
|
+
|
|
|
+ @Value(value = "${baidu.SecretKey}")
|
|
|
+ private String baiduSecretKey=null;
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+
|
|
|
+ /*文心一言地址*/
|
|
|
+ private static final String BAIDU_CHAT_WXYY_URL="https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=";
|
|
|
+ /*Ernie-Lite地址*/
|
|
|
+ private static final String BAIDU_CHAT_ERNIE_LITE_URL="https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=";
|
|
|
+ /*accessToken获取地址*/
|
|
|
+ private static final String BAIDU_ACCESSTOKEN_URL="https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&";
|
|
|
+
|
|
|
+
|
|
|
+ static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().connectTimeout(120000, TimeUnit.MILLISECONDS)
|
|
|
+ .readTimeout(120000, TimeUnit.MILLISECONDS)
|
|
|
+ .build();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public String getBaiduAccessToken() throws IOException {
|
|
|
+ MediaType mediaType = MediaType.parse("application/json");
|
|
|
+ RequestBody body = RequestBody.create(mediaType, "");
|
|
|
+ StringBuffer url= new StringBuffer(BAIDU_ACCESSTOKEN_URL)
|
|
|
+ .append("client_id=").append(baiduApiKey).append("&client_secret=").append(baiduSecretKey);
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(url.toString())
|
|
|
+ .method("POST", body)
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .addHeader("Accept", "application/json")
|
|
|
+ .build();
|
|
|
+ Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
+ String result=response.body().string();
|
|
|
+ HashMap<String,Object> map = JSON.parseObject(result, HashMap.class);
|
|
|
+ String accessToken = map.get("access_token").toString();
|
|
|
+ LoggerUtils.debug(getClass(),"获取accessToken="+accessToken);
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String sendBaiduAI(InputSendChat in) throws IOException{
|
|
|
+ String accessToken = getRedisBaiduAccessToken();
|
|
|
+ MediaType mediaType = MediaType.parse("application/json");
|
|
|
+ RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(in));
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(BAIDU_CHAT_WXYY_URL + accessToken)
|
|
|
+ .method("POST", body)
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .build();
|
|
|
+ Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
+ String result=response.body().string();
|
|
|
+ SseEmitter sseEmitter=new SseEmitter(0l);
|
|
|
+
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private String getRedisBaiduAccessToken() {
|
|
|
+ String redisAccessToken=null;
|
|
|
+ String redisTime=redisUtil.getString("baiduAccessTime");
|
|
|
+ //没有
|
|
|
+ if (redisTime !=null){
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ Long redisAccessTime=Long.valueOf(redisTime);
|
|
|
+ if (cal.getTimeInMillis()>redisAccessTime){
|
|
|
+ redisUtil.deleteString("baiduAccessToken");
|
|
|
+ redisUtil.deleteString("baiduAccessTime");
|
|
|
+ redisAccessToken=pushRedisBaiduAccessToken();
|
|
|
+ LoggerUtils.debug(getClass(),"accessToken过期,重新获取");
|
|
|
+ }else {
|
|
|
+ redisAccessToken= redisUtil.getString("baiduAccessToken");
|
|
|
+ LoggerUtils.debug(getClass(),"accessToken从redis获取");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ redisAccessToken=pushRedisBaiduAccessToken();
|
|
|
+ LoggerUtils.debug(getClass(),"accessToken不存在,从百度获取");
|
|
|
+ }
|
|
|
+ return redisAccessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String pushRedisBaiduAccessToken() {
|
|
|
+ String baiduAccessToken;
|
|
|
+ try {
|
|
|
+ baiduAccessToken = getBaiduAccessToken();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new BusinessException("baiduAccessToken获取失败");
|
|
|
+ }
|
|
|
+ //获取当前系统时间
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ //将时间增加三十天
|
|
|
+ cal.add(Calendar.DATE, 30);
|
|
|
+ //获取改变后的时间
|
|
|
+ Long baiduAccessTime= cal.getTimeInMillis();
|
|
|
+ redisUtil.setString("baiduAccessToken",baiduAccessToken);
|
|
|
+ redisUtil.setString("baiduAccessTime",baiduAccessTime.toString());
|
|
|
+ return baiduAccessToken;
|
|
|
+ }
|
|
|
+}
|