refactor(module-llm):优化知识库嵌入请求的超时设置

- 在 OkHttpClient 实例中添加了连接、读取和写入的超时时间设置- 将 knowledgeEmbed 方法中的 OkHttpClient 实例化过程进行了优化
This commit is contained in:
Liuyang 2025-02-21 10:46:09 +08:00
parent 93e4f44bf5
commit 503f031683

View File

@ -38,6 +38,7 @@ import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -310,7 +311,7 @@ public class RagHttpService {
* @param id 知识库ID
* @throws IOException 如果发生I/O错误
*/
public void knowledgeEmbed(KnowledgeRagEmbedReqVO reqVO, Long id) throws IOException {
public void knowledgeEmbed (KnowledgeRagEmbedReqVO reqVO, Long id) throws IOException {
// 获取向量嵌入接口的URL
String ragEmbed = llmBackendProperties.getEmbed();
@ -335,7 +336,14 @@ public class RagHttpService {
byte[] fileBytes = Objects.requireNonNull(getFileByte(fileUrl));
// 创建 OkHttpClient 实例
OkHttpClient client = new OkHttpClient();
OkHttpClient client = new OkHttpClient.Builder()
// 连接超时时间
.connectTimeout(30, TimeUnit.MINUTES)
// 读取超时时间
.readTimeout(30, TimeUnit.MINUTES)
// 写入超时时间
.writeTimeout(30, TimeUnit.MINUTES)
.build();
// 创建 MultipartBody
RequestBody requestBody = new MultipartBody.Builder()
@ -418,7 +426,7 @@ public class RagHttpService {
* @param fileName 文件名
* @return 文件类型
*/
private static String getMediaType(String fileName) {
private static String getMediaType (String fileName) {
String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1);
switch (fileSuffix) {
case "pdf":
@ -531,4 +539,47 @@ public class RagHttpService {
Throwable cause = e.getCause();
return cause instanceof IOException;
}
public static void main (String[] args) {
// 创建 OkHttpClient 实例
OkHttpClient client = new OkHttpClient();
String ragEmbed = "http://36.103.199.248:8123/embed";
String fileId = "778899";
String fileName = "docx23_副本.docx";
String fileUrl = "http://xhllm.xinnuojinzhi.com/admin-api/infra/file/29/get/5533434c4ed6b58415c33db46a73be3abe121b0ab66f25fb1a9050a2a978fda2.docx";
String mediaType = getMediaType(fileName);
byte[] fileBytes = Objects.requireNonNull(getFileByte(fileUrl));
log.info("URL: {}, fileId: {} ,fileName: {}, fileUrl: {}, mediaType: {} ", ragEmbed, fileId, fileName, fileUrl, mediaType);
// 创建文件对象
// File file = new File("/Users/yangliu/Documents/测试上传/测试上传 - new/docx1_副本.docx");
// 创建 MultipartBody
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file_id", fileId)
.addFormDataPart("file", fileName,
RequestBody.create(fileBytes, MediaType.parse(mediaType)))
.build();
// 创建请求
Request request = new Request.Builder()
.url(ragEmbed)
.post(requestBody)
.addHeader("accept", "application/json")
.build();
// 发送请求
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
System.out.println("Request successful: " + response.body().string());
} else {
System.out.println("Request failed: " + response.code() + " " + response.message());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}