|
|
@ -2,18 +2,16 @@ package cn.teammodel.ai.deepseek;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.teammodel.common.ErrorCode;
|
|
|
|
import cn.teammodel.common.ErrorCode;
|
|
|
|
import cn.teammodel.config.exception.ServiceException;
|
|
|
|
import cn.teammodel.config.exception.ServiceException;
|
|
|
|
import cn.teammodel.model.dto.ai.deepseek.ChatRequestDto;
|
|
|
|
import cn.teammodel.model.dto.ai.deepseek.ChatRequestOKHttpDto;
|
|
|
|
import cn.teammodel.model.dto.ai.deepseek.ChatResponseDto;
|
|
|
|
import cn.teammodel.model.dto.ai.deepseek.ChatResponseDto;
|
|
|
|
import cn.teammodel.model.dto.ai.deepseek.MessageDto;
|
|
|
|
import cn.teammodel.model.dto.ai.deepseek.ChatReqDto;
|
|
|
|
import cn.teammodel.utils.JsonUtil;
|
|
|
|
import cn.teammodel.utils.JsonUtil;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import com.fasterxml.jackson.databind.type.TypeFactory;
|
|
|
|
import com.fasterxml.jackson.databind.type.TypeFactory;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import okhttp3.*;
|
|
|
|
import okhttp3.*;
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
|
|
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
@ -21,13 +19,16 @@ import org.apache.http.entity.StringEntity;
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
public class DeepSeekClient {
|
|
|
|
public class DeepSeekClient {
|
|
|
|
private static final String API_Key;
|
|
|
|
private static final String API_Key;
|
|
|
|
private static final String API_Url;
|
|
|
|
private static final String API_Url;
|
|
|
@ -52,15 +53,15 @@ public class DeepSeekClient {
|
|
|
|
* 提问题 测试使用
|
|
|
|
* 提问题 测试使用
|
|
|
|
* @param mssage
|
|
|
|
* @param mssage
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private static Map<String, Object> ask(MessageDto mssage)
|
|
|
|
private static Map<String, Object> ask(ChatReqDto mssage)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Map<String, Object> mapper = new HashMap<>();
|
|
|
|
Map<String, Object> mapper = new HashMap<>();
|
|
|
|
//创建消息列表
|
|
|
|
//创建消息列表
|
|
|
|
List<MessageDto> msg = new ArrayList<>();
|
|
|
|
List<ChatReqDto> msg = new ArrayList<>();
|
|
|
|
msg.add(mssage);
|
|
|
|
msg.add(mssage);
|
|
|
|
|
|
|
|
|
|
|
|
//构建请求头
|
|
|
|
//构建请求头
|
|
|
|
ChatRequestDto requestBody = new ChatRequestDto();
|
|
|
|
ChatRequestOKHttpDto requestBody = new ChatRequestOKHttpDto();
|
|
|
|
requestBody.setModel(API_Model);
|
|
|
|
requestBody.setModel(API_Model);
|
|
|
|
requestBody.setMessages(msg);
|
|
|
|
requestBody.setMessages(msg);
|
|
|
|
requestBody.setTemperature(0);
|
|
|
|
requestBody.setTemperature(0);
|
|
|
@ -84,10 +85,13 @@ public class DeepSeekClient {
|
|
|
|
* @param requestBody
|
|
|
|
* @param requestBody
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static ChatResponseDto SendRequests(ChatRequestDto requestBody)
|
|
|
|
public static ChatResponseDto SendRequests(ChatRequestOKHttpDto requestBody)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
ChatResponseDto chatResponse = new ChatResponseDto();
|
|
|
|
ChatResponseDto chatResponse = new ChatResponseDto();
|
|
|
|
OkHttpClient client = new OkHttpClient().newBuilder().build();
|
|
|
|
//OkHttpClient client = new OkHttpClient().newBuilder().connectTimeout(60, TimeUnit.SECONDS).build();//设置连接超时时间 1分钟
|
|
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient().newBuilder().build();//设置连接超时时间 1分钟
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MediaType mediaType = MediaType.parse("application/json");
|
|
|
|
MediaType mediaType = MediaType.parse("application/json");
|
|
|
|
//String content = "{\n \"messages\": [\n {\n \"content\": \"You are a helpful assistant\",\n \"role\": \"system\"\n },\n {\n \"content\": \"Hi\",\n \"role\": \"user\"\n }\n ],\n \"model\": \"deepseek-chat\",\n \"frequency_penalty\": 0,\n \"max_tokens\": 2048,\n \"presence_penalty\": 0,\n \"response_format\": {\n \"type\": \"text\"\n },\n \"stop\": null,\n \"stream\": false,\n \"stream_options\": null,\n \"temperature\": 1,\n \"top_p\": 1,\n \"tools\": null,\n \"tool_choice\": \"none\",\n \"logprobs\": false,\n \"top_logprobs\": null\n}";
|
|
|
|
//String content = "{\n \"messages\": [\n {\n \"content\": \"You are a helpful assistant\",\n \"role\": \"system\"\n },\n {\n \"content\": \"Hi\",\n \"role\": \"user\"\n }\n ],\n \"model\": \"deepseek-chat\",\n \"frequency_penalty\": 0,\n \"max_tokens\": 2048,\n \"presence_penalty\": 0,\n \"response_format\": {\n \"type\": \"text\"\n },\n \"stop\": null,\n \"stream\": false,\n \"stream_options\": null,\n \"temperature\": 1,\n \"top_p\": 1,\n \"tools\": null,\n \"tool_choice\": \"none\",\n \"logprobs\": false,\n \"top_logprobs\": null\n}";
|
|
|
|
String content = JsonUtil.convertToJson(requestBody);
|
|
|
|
String content = JsonUtil.convertToJson(requestBody);
|
|
|
@ -133,7 +137,7 @@ public class DeepSeekClient {
|
|
|
|
* @param requestBody
|
|
|
|
* @param requestBody
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static Map<String, Object> SendRequest(ChatRequestDto requestBody) {
|
|
|
|
public static Map<String, Object> SendRequest(ChatRequestOKHttpDto requestBody) {
|
|
|
|
Map<String, Object> mapper = new HashMap<>();
|
|
|
|
Map<String, Object> mapper = new HashMap<>();
|
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
|
// 创建HttpPost对象
|
|
|
|
// 创建HttpPost对象
|
|
|
|