|
|
|
@ -3,17 +3,35 @@ package com.sztzjy.marketing.service.impl;/**
|
|
|
|
|
* @date 2024-06-03 15:26
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.sztzjy.marketing.entity.dto.ReqChatMessage;
|
|
|
|
|
import com.sztzjy.marketing.entity.dto.StuCreateArticleDTO;
|
|
|
|
|
import com.sztzjy.marketing.entity.dto.StuCreateImgDTO;
|
|
|
|
|
import com.sztzjy.marketing.qianfan.Qianfan;
|
|
|
|
|
import com.sztzjy.marketing.qianfan.model.chat.ChatResponse;
|
|
|
|
|
import com.sztzjy.marketing.qianfan.model.chat.Message;
|
|
|
|
|
import com.sztzjy.marketing.qianfan.model.image.Text2ImageResponse;
|
|
|
|
|
import com.sztzjy.marketing.qianfan.util.http.HttpClient;
|
|
|
|
|
import com.sztzjy.marketing.service.QianFanBigModuleService;
|
|
|
|
|
import com.sztzjy.marketing.util.HttpUtils;
|
|
|
|
|
import com.sztzjy.marketing.util.ResultEntity;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import okhttp3.*;
|
|
|
|
|
import okio.BufferedSource;
|
|
|
|
|
import okio.Okio;
|
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class QianFanBigModuleServiceImpl implements QianFanBigModuleService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -25,7 +43,19 @@ public class QianFanBigModuleServiceImpl implements QianFanBigModuleService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ResultEntity createImgByAi(StuCreateImgDTO stuCreateImgDTO) {
|
|
|
|
|
public ResultEntity createImgByAi(StuCreateImgDTO stuCreateImgDTO) {
|
|
|
|
|
|
|
|
|
|
String accesstoken = null;
|
|
|
|
|
try {
|
|
|
|
|
accesstoken = getAccesstoken();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chat(accesstoken);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Qianfan qianfan = new Qianfan("OAuth", accessKey, secretKey);
|
|
|
|
|
|
|
|
|
@ -42,6 +72,17 @@ public class QianFanBigModuleServiceImpl implements QianFanBigModuleService {
|
|
|
|
|
@Override
|
|
|
|
|
public ResultEntity createArticleByAi(StuCreateArticleDTO stuCreateArticleDTO) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String accesstoken = null;
|
|
|
|
|
try {
|
|
|
|
|
accesstoken = getAccesstoken();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chat(accesstoken);
|
|
|
|
|
|
|
|
|
|
ChatResponse response = new Qianfan("OAuth",accessKey, secretKey).chatCompletion()
|
|
|
|
|
.model("ERNIE-3.5-8K") // 使用model指定预置模型
|
|
|
|
|
//.endpoint("ERNIE-Bot") // 也可以使用endpoint指定任意模型 (二选一)
|
|
|
|
@ -53,7 +94,138 @@ public class QianFanBigModuleServiceImpl implements QianFanBigModuleService {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取accessToken
|
|
|
|
|
public String getAccesstoken() throws IOException {
|
|
|
|
|
|
|
|
|
|
String requestBody = "grant_type=" + URLEncoder.encode("client_credentials", "UTF8") +
|
|
|
|
|
"&client_id=" + URLEncoder.encode(accessKey, "UTF8")+
|
|
|
|
|
"&client_secret=" + URLEncoder.encode(secretKey, "UTF8");
|
|
|
|
|
|
|
|
|
|
cn.hutool.json.JSONObject object = HttpUtils.sendPost(
|
|
|
|
|
"https://aip.baidubce.com/oauth/2.0/token",
|
|
|
|
|
requestBody,
|
|
|
|
|
null,
|
|
|
|
|
null);
|
|
|
|
|
|
|
|
|
|
if (object == null) {
|
|
|
|
|
throw new IllegalArgumentException("登录失败");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cn.hutool.json.JSONObject jsonObject = object.getJSONObject("respString");
|
|
|
|
|
String accessToken = jsonObject.getStr("access_token");
|
|
|
|
|
return accessToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void chat(String accesstoken) {
|
|
|
|
|
try {
|
|
|
|
|
//获取RequestBody
|
|
|
|
|
RequestBody funcCallReqBody = getRequestBody();
|
|
|
|
|
|
|
|
|
|
//发送请求
|
|
|
|
|
sendRequest(funcCallReqBody,accesstoken);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("chat执行失败", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取RequestBody
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
protected static RequestBody getRequestBody() {
|
|
|
|
|
List<Message> messages = new ArrayList<>();
|
|
|
|
|
// Message chatMsg = new Message("user", "content");
|
|
|
|
|
Message chatMsg = new Message();
|
|
|
|
|
chatMsg.setRole("user");
|
|
|
|
|
chatMsg.setContent("您好");
|
|
|
|
|
messages.add(chatMsg);
|
|
|
|
|
|
|
|
|
|
//获取组装好的请求
|
|
|
|
|
ReqChatMessage reqMessage = new ReqChatMessage();
|
|
|
|
|
reqMessage.setMessages(messages);
|
|
|
|
|
reqMessage.setStream(true);
|
|
|
|
|
reqMessage.setSystem("systemInfo");
|
|
|
|
|
reqMessage.setUser_id("userID");
|
|
|
|
|
|
|
|
|
|
String content = JSONObject.toJSONString(reqMessage);
|
|
|
|
|
MediaType mediaType = MediaType.parse("application/json");
|
|
|
|
|
return RequestBody.create(mediaType, content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送请求
|
|
|
|
|
*
|
|
|
|
|
* @param body
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @returnline
|
|
|
|
|
*/
|
|
|
|
|
protected static String sendRequest(RequestBody body, String accessToken) {
|
|
|
|
|
String respResult = "";
|
|
|
|
|
Request request = new Request.Builder().url("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-8k-0329?access_token=" +
|
|
|
|
|
accessToken).method("POST", body).addHeader("Content-Type", "application/json").build();
|
|
|
|
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient();
|
|
|
|
|
|
|
|
|
|
try (Response response = client.newCall(request).execute()) {
|
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
|
throw new IOException("Unexpected code " + response);
|
|
|
|
|
}
|
|
|
|
|
BufferedSource source = Okio.buffer(response.body().source());
|
|
|
|
|
String line = "";
|
|
|
|
|
while ((line = source.readUtf8Line()) != null) {
|
|
|
|
|
//流式打印出来
|
|
|
|
|
System.out.println(line);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("sendRequest请求执行失败", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return respResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public static void main(String[] args) throws IOException {
|
|
|
|
|
//
|
|
|
|
|
// String accessKey = "4zXteUiZO56bkIXfcIypUVsS";
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// String secretKey = "OrY5ZaSdv3mKtqzfh83x5DShCfIF4gi1";
|
|
|
|
|
// String requestBody = "grant_type=" + URLEncoder.encode("client_credentials", "UTF8") +
|
|
|
|
|
// "&client_id=" + URLEncoder.encode(accessKey, "UTF8")+
|
|
|
|
|
// "&client_secret=" + URLEncoder.encode(secretKey, "UTF8");
|
|
|
|
|
//
|
|
|
|
|
// cn.hutool.json.JSONObject object = HttpUtils.sendPost(
|
|
|
|
|
// "https://aip.baidubce.com/oauth/2.0/token",
|
|
|
|
|
// requestBody,
|
|
|
|
|
// null,
|
|
|
|
|
// null);
|
|
|
|
|
//
|
|
|
|
|
// if (object == null) {
|
|
|
|
|
// throw new IllegalArgumentException("登录失败");
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// cn.hutool.json.JSONObject jsonObject = object.getJSONObject("respString");
|
|
|
|
|
// String accessToken = jsonObject.getStr("access_token");
|
|
|
|
|
// System.out.println(accessToken);
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// //获取RequestBody
|
|
|
|
|
// RequestBody funcCallReqBody = getRequestBody();
|
|
|
|
|
//
|
|
|
|
|
// //发送请求
|
|
|
|
|
// sendRequest(funcCallReqBody,accessToken);
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|