From 5d78424afe6f7c99319ba7ee9327a83fc7919343 Mon Sep 17 00:00:00 2001 From: whb <17803890193@163.com> Date: Fri, 21 Jun 2024 16:17:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A4=A7=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E6=B5=81=E8=BE=93=E5=85=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 9 + .../stu/DownloadDataController.java | 8 + .../marketing/entity/dto/ReqChatMessage.java | 24 +++ .../marketing/qianfan/model/chat/Message.java | 5 + .../impl/QianFanBigModuleServiceImpl.java | 174 +++++++++++++++++- 5 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/sztzjy/marketing/entity/dto/ReqChatMessage.java diff --git a/pom.xml b/pom.xml index f0c2dd9..a96b86a 100644 --- a/pom.xml +++ b/pom.xml @@ -273,6 +273,15 @@ 1.28 + + + + com.squareup.okhttp3 + okhttp + 4.9.3 + + + diff --git a/src/main/java/com/sztzjy/marketing/controller/stu/DownloadDataController.java b/src/main/java/com/sztzjy/marketing/controller/stu/DownloadDataController.java index 833ce8a..94b5238 100644 --- a/src/main/java/com/sztzjy/marketing/controller/stu/DownloadDataController.java +++ b/src/main/java/com/sztzjy/marketing/controller/stu/DownloadDataController.java @@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; +import java.io.File; import java.io.IOException; /** @@ -121,6 +122,13 @@ public class DownloadDataController { TagWordCloudService tagWordCloudService = new TagWordCloudService(); + File file1 = new File(filePath + "/word/"); + + if (! file1.exists()) + { + file1.mkdir(); + } + //1成图片路径,2.图云背景图片 String url = filePath+"/word/" + IdUtil.simpleUUID()+"."+"png"; diff --git a/src/main/java/com/sztzjy/marketing/entity/dto/ReqChatMessage.java b/src/main/java/com/sztzjy/marketing/entity/dto/ReqChatMessage.java new file mode 100644 index 0000000..4547f4b --- /dev/null +++ b/src/main/java/com/sztzjy/marketing/entity/dto/ReqChatMessage.java @@ -0,0 +1,24 @@ +package com.sztzjy.marketing.entity.dto; + +import com.sztzjy.marketing.qianfan.model.chat.Message; +import lombok.Data; +import org.geolatte.geom.M; + +import java.util.List; + +/** + * @author 17803 + * @date 2024-06-21 15:51 + */ + +@Data +public class ReqChatMessage { + + private List messages; + + private boolean stream; + + private String system; + + private String user_id; +} diff --git a/src/main/java/com/sztzjy/marketing/qianfan/model/chat/Message.java b/src/main/java/com/sztzjy/marketing/qianfan/model/chat/Message.java index fea3556..a806b66 100644 --- a/src/main/java/com/sztzjy/marketing/qianfan/model/chat/Message.java +++ b/src/main/java/com/sztzjy/marketing/qianfan/model/chat/Message.java @@ -16,6 +16,11 @@ package com.sztzjy.marketing.qianfan.model.chat; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + + public class Message { /** * 角色,可选 "user", "assistant", "function" diff --git a/src/main/java/com/sztzjy/marketing/service/impl/QianFanBigModuleServiceImpl.java b/src/main/java/com/sztzjy/marketing/service/impl/QianFanBigModuleServiceImpl.java index 27f1d12..25c7fac 100644 --- a/src/main/java/com/sztzjy/marketing/service/impl/QianFanBigModuleServiceImpl.java +++ b/src/main/java/com/sztzjy/marketing/service/impl/QianFanBigModuleServiceImpl.java @@ -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 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); +// +// +// +// } }