|
@@ -2,6 +2,10 @@ package com.goafanti.common.utils;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.goafanti.baiduAI.bo.InputSendChat;
|
|
|
|
|
+import com.goafanti.baiduAI.bo.Messages;
|
|
|
|
|
+import com.goafanti.common.error.BusinessException;
|
|
|
|
|
+import com.plexpt.chatgpt.listener.SseStreamListener;
|
|
|
import okhttp3.*;
|
|
import okhttp3.*;
|
|
|
import org.apache.http.HttpResponse;
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.HttpStatus;
|
|
import org.apache.http.HttpStatus;
|
|
@@ -14,24 +18,33 @@ import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
|
import org.apache.ibatis.annotations.Param;
|
|
import org.apache.ibatis.annotations.Param;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
|
|
import javax.crypto.SecretKey;
|
|
import javax.crypto.SecretKey;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
+@Component
|
|
|
public class HttpUtils {
|
|
public class HttpUtils {
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private RedisUtil redisUtil;
|
|
|
|
|
|
|
|
@Value(value = "${baidu.ApiKey}")
|
|
@Value(value = "${baidu.ApiKey}")
|
|
|
- private static final String baiduApiKey=null;
|
|
|
|
|
|
|
+ private String baiduApiKey=null;
|
|
|
|
|
|
|
|
@Value(value = "${baidu.SecretKey}")
|
|
@Value(value = "${baidu.SecretKey}")
|
|
|
- private static final String baiduSecretKey=null;
|
|
|
|
|
|
|
+ private String baiduSecretKey=null;
|
|
|
|
|
|
|
|
|
|
|
|
|
- static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
|
|
|
|
|
|
|
+ static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().connectTimeout(120000, TimeUnit.MILLISECONDS)
|
|
|
|
|
+ .readTimeout(120000, TimeUnit.MILLISECONDS)
|
|
|
|
|
+ .build();
|
|
|
|
|
|
|
|
|
|
|
|
|
public static JSONObject httpGet(String url) {
|
|
public static JSONObject httpGet(String url) {
|
|
@@ -133,11 +146,11 @@ public class HttpUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- public static String getBaiduAccessToken() throws IOException {
|
|
|
|
|
|
|
+ public String getBaiduAccessToken() throws IOException {
|
|
|
MediaType mediaType = MediaType.parse("application/json");
|
|
MediaType mediaType = MediaType.parse("application/json");
|
|
|
RequestBody body = RequestBody.create(mediaType, "");
|
|
RequestBody body = RequestBody.create(mediaType, "");
|
|
|
- StringBuffer url= new StringBuffer("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=")
|
|
|
|
|
- .append(baiduApiKey).append("&client_secret=").append(baiduSecretKey);
|
|
|
|
|
|
|
+ StringBuffer url= new StringBuffer("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&")
|
|
|
|
|
+ .append("client_id=").append(baiduApiKey).append("&client_secret=").append(baiduSecretKey);
|
|
|
Request request = new Request.Builder()
|
|
Request request = new Request.Builder()
|
|
|
.url(url.toString())
|
|
.url(url.toString())
|
|
|
.method("POST", body)
|
|
.method("POST", body)
|
|
@@ -146,13 +159,16 @@ public class HttpUtils {
|
|
|
.build();
|
|
.build();
|
|
|
Response response = HTTP_CLIENT.newCall(request).execute();
|
|
Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
String result=response.body().string();
|
|
String result=response.body().string();
|
|
|
- System.out.println(result);
|
|
|
|
|
- return result;
|
|
|
|
|
|
|
+ HashMap<String,Object> map = JSON.parseObject(result, HashMap.class);
|
|
|
|
|
+ String accessToken = map.get("access_token").toString();
|
|
|
|
|
+ LoggerUtils.debug(getClass(),"获取accessToken="+accessToken);
|
|
|
|
|
+ return accessToken;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static String sendBaiduAI(String accessToken) throws IOException{
|
|
|
|
|
|
|
+ public String sendBaiduAI(InputSendChat in) throws IOException{
|
|
|
|
|
+ String accessToken = getRedisBaiduAccessToken();
|
|
|
MediaType mediaType = MediaType.parse("application/json");
|
|
MediaType mediaType = MediaType.parse("application/json");
|
|
|
- RequestBody body = RequestBody.create(mediaType, "");
|
|
|
|
|
|
|
+ RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(in));
|
|
|
Request request = new Request.Builder()
|
|
Request request = new Request.Builder()
|
|
|
.url("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + accessToken)
|
|
.url("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=" + accessToken)
|
|
|
.method("POST", body)
|
|
.method("POST", body)
|
|
@@ -160,9 +176,56 @@ public class HttpUtils {
|
|
|
.build();
|
|
.build();
|
|
|
Response response = HTTP_CLIENT.newCall(request).execute();
|
|
Response response = HTTP_CLIENT.newCall(request).execute();
|
|
|
String result=response.body().string();
|
|
String result=response.body().string();
|
|
|
- System.out.println(result);
|
|
|
|
|
|
|
+ SseEmitter sseEmitter=new SseEmitter(0l);
|
|
|
|
|
+ SseStreamListener listener=new SseStreamListener(sseEmitter);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
return result;
|
|
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;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|