解决大模型流输入问题

master
whb
parent 97b209911b
commit 5d78424afe

@ -273,6 +273,15 @@
<version>1.28</version>
</dependency>
<!-- AI-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version> <!-- 使用最新版本 -->
</dependency>
</dependencies>
<build>

@ -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";

@ -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<Message> messages;
private boolean stream;
private String system;
private String user_id;
}

@ -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"

@ -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);
//
//
//
// }
}

Loading…
Cancel
Save