|
@@ -2,37 +2,47 @@ package com.sxtvs.open.api.chat.service.impl;
|
|
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.sxtvs.open.api.chat.dto.KimiRequest;
|
|
import com.sxtvs.open.api.chat.dto.KimiRequest;
|
|
import com.sxtvs.open.api.chat.dto.KimiStreamResponse;
|
|
import com.sxtvs.open.api.chat.dto.KimiStreamResponse;
|
|
import com.sxtvs.open.api.chat.dto.Message;
|
|
import com.sxtvs.open.api.chat.dto.Message;
|
|
import com.sxtvs.open.api.chat.entity.KimiChat;
|
|
import com.sxtvs.open.api.chat.entity.KimiChat;
|
|
import com.sxtvs.open.api.chat.mapper.KimiChatMapper;
|
|
import com.sxtvs.open.api.chat.mapper.KimiChatMapper;
|
|
import com.sxtvs.open.api.chat.service.IKimiChatService;
|
|
import com.sxtvs.open.api.chat.service.IKimiChatService;
|
|
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
import com.sxtvs.open.api.review.service.impl.SSEService;
|
|
import com.sxtvs.open.api.review.service.impl.SSEService;
|
|
import jakarta.annotation.Resource;
|
|
import jakarta.annotation.Resource;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
-import org.apache.hc.core5.http.io.HttpClientResponseHandler;
|
|
|
|
-import org.apache.http.client.methods.HttpPost;
|
|
|
|
-import org.apache.http.client.methods.HttpUriRequest;
|
|
|
|
-import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
-import org.apache.http.impl.client.HttpClients;
|
|
|
|
-import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
+import org.apache.hc.client5.http.classic.methods.HttpPost;
|
|
|
|
+import org.apache.hc.client5.http.entity.mime.ContentBody;
|
|
|
|
+import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
|
|
|
|
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
|
|
|
+import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
|
|
|
|
+import org.apache.hc.client5.http.impl.classic.HttpClients;
|
|
|
|
+import org.apache.hc.core5.http.ContentType;
|
|
|
|
+import org.apache.hc.core5.http.ParseException;
|
|
|
|
+import org.apache.hc.core5.http.io.entity.EntityUtils;
|
|
import org.apache.http.util.TextUtils;
|
|
import org.apache.http.util.TextUtils;
|
|
-import org.apache.logging.log4j.util.Strings;
|
|
|
|
-import org.springframework.http.*;
|
|
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.io.InputStreamReader;
|
|
|
|
+import java.io.OutputStream;
|
|
import java.net.URI;
|
|
import java.net.URI;
|
|
import java.net.http.HttpClient;
|
|
import java.net.http.HttpClient;
|
|
import java.net.http.HttpRequest;
|
|
import java.net.http.HttpRequest;
|
|
import java.net.http.HttpResponse;
|
|
import java.net.http.HttpResponse;
|
|
|
|
+import java.nio.channels.Channels;
|
|
|
|
+import java.nio.channels.Pipe;
|
|
|
|
+import java.nio.file.Paths;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
-import java.util.*;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Random;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -182,4 +192,48 @@ public class KimiChatServiceImpl extends ServiceImpl<KimiChatMapper, KimiChat> i
|
|
chatCompletions( kimiRequest, kimiChat.getGroupId());
|
|
chatCompletions( kimiRequest, kimiChat.getGroupId());
|
|
return kimiChat;
|
|
return kimiChat;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public void upload(MultipartFile file) throws IOException, InterruptedException, ParseException {
|
|
|
|
+ String boun = "----WebKitFormBoundary7MA4YWxkTrZu0gW";
|
|
|
|
+ org.apache.hc.core5.http.HttpEntity httpEntity = MultipartEntityBuilder
|
|
|
|
+ .create()
|
|
|
|
+ .addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, file.getOriginalFilename())
|
|
|
|
+ .addTextBody("purpose", "file-extract")
|
|
|
|
+ .setBoundary( boun )
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ HttpPost httpPost = new HttpPost("https://api.moonshot.cn/v1/files");
|
|
|
|
+ httpPost.setHeader("Authorization", "Bearer " + token.get(random.nextInt(token.size())));
|
|
|
|
+// httpPost.setHeader("Content-Type", "multipart/form-data; boundary=" + boun);
|
|
|
|
+ httpPost.setEntity(httpEntity);
|
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
|
+ CloseableHttpResponse response = httpClient.execute(httpPost);
|
|
|
|
+ String s = EntityUtils.toString(response.getEntity());
|
|
|
|
+ log.info("上传文件 -- {}", s);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /*Pipe pipe = Pipe.open();
|
|
|
|
+ new Thread(() -> {
|
|
|
|
+ try (OutputStream outputStream = Channels.newOutputStream(pipe.sink())) {
|
|
|
|
+ httpEntity.writeTo(outputStream);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }).start();
|
|
|
|
+
|
|
|
|
+ HttpRequest.BodyPublisher bodyPublisher = HttpRequest.BodyPublishers.ofInputStream(() -> Channels.newInputStream(pipe.source()));
|
|
|
|
+
|
|
|
|
+ HttpClient client = HttpClient.newBuilder().build();
|
|
|
|
+
|
|
|
|
+ HttpRequest request = HttpRequest.newBuilder()
|
|
|
|
+ .uri(URI.create("https://api.moonshot.cn/v1/files"))
|
|
|
|
+ .method("POST", bodyPublisher)
|
|
|
|
+ .header(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=" + boun)
|
|
|
|
+ .headers("Authorization", "Bearer " + token.get(random.nextInt(token.size())))
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ HttpResponse<String> send = client.send(request, HttpResponse.BodyHandlers.ofString());
|
|
|
|
+ log.info("上传文件响应 {}", send.body());*/
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|