|
|
@@ -1,14 +1,16 @@
|
|
|
package com.njuzr.eaibackend.service;
|
|
|
-
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.google.gson.Gson;
|
|
|
import com.njuzr.eaibackend.config.DeepSeekConfig;
|
|
|
+import com.njuzr.eaibackend.config.ECloudEosConfig;
|
|
|
+import com.njuzr.eaibackend.exception.MyException;
|
|
|
import com.njuzr.eaibackend.po.AIEntry;
|
|
|
+import com.njuzr.eaibackend.service.DeepSeekService;
|
|
|
+import com.njuzr.eaibackend.service.impl.CourseServiceImpl;
|
|
|
+
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
-import org.apache.http.client.methods.HttpPost;
|
|
|
-import org.apache.http.entity.StringEntity;
|
|
|
-import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
-import org.apache.http.util.EntityUtils;
|
|
|
+import okhttp3.*;
|
|
|
+
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -25,49 +27,65 @@ import java.util.Map;
|
|
|
@Slf4j
|
|
|
public class DeepSeekService {
|
|
|
|
|
|
- private final CloseableHttpClient httpClient;
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
|
public DeepSeekService(
|
|
|
- CloseableHttpClient httpClient,
|
|
|
- ObjectMapper objectMapper) {
|
|
|
- this.httpClient = httpClient;
|
|
|
+
|
|
|
+ ObjectMapper objectMapper) {
|
|
|
this.objectMapper = objectMapper;
|
|
|
}
|
|
|
|
|
|
@Autowired
|
|
|
DeepSeekConfig deepSeekConfig;
|
|
|
|
|
|
+ private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
|
|
+
|
|
|
public String chatCompletion(List<AIEntry> message) throws IOException {
|
|
|
- HttpPost httpPost = new HttpPost(deepSeekConfig.getBaseUrl());
|
|
|
- // 设置请求头
|
|
|
- httpPost.setHeader("Content-Type", "application/json");
|
|
|
- httpPost.setHeader("Authorization", "Bearer " + deepSeekConfig.getApiKey());
|
|
|
- System.out.println(deepSeekConfig.getApiKey());
|
|
|
- // 构建请求体
|
|
|
+ OkHttpClient okHttpClient = deepSeekConfig.httpClient();
|
|
|
Map<String, Object> requestBody = new HashMap<>();
|
|
|
requestBody.put("model", deepSeekConfig.getModel());
|
|
|
requestBody.put("messages", message);
|
|
|
requestBody.put("temperature", deepSeekConfig.getTemperature());
|
|
|
+ requestBody.put("stream", false);
|
|
|
+ Gson gson = new Gson();
|
|
|
+ RequestBody body = RequestBody.create(JSON, gson.toJson(requestBody));
|
|
|
+ // 构建请求
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(deepSeekConfig.getBaseUrl())
|
|
|
+ .addHeader("Authorization", "Bearer " + deepSeekConfig.getApiKey())
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .post(body)
|
|
|
+ .build();
|
|
|
|
|
|
- StringEntity entity = new StringEntity(objectMapper.writeValueAsString(requestBody));
|
|
|
- System.out.println("请求体:" + entity.getContent().toString());
|
|
|
- httpPost.setEntity(entity);
|
|
|
+ // 发送请求
|
|
|
+ try (Response response = okHttpClient.newCall(request).execute()) {
|
|
|
+ if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
|
|
|
|
|
|
- // 执行请求
|
|
|
- try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
|
|
- String responseBody = EntityUtils.toString(response.getEntity());
|
|
|
+ // 解析并打印结果
|
|
|
+ String responseBody = response.body().string();
|
|
|
System.out.println(responseBody);
|
|
|
- Map<String, Object> responseMap = objectMapper.readValue(responseBody, Map.class);
|
|
|
-
|
|
|
- // 解析响应
|
|
|
- List<Map<String, Object>> choices = (List<Map<String, Object>>) responseMap.get("choices");
|
|
|
- if (choices != null && !choices.isEmpty()) {
|
|
|
- Map<String, Object> res = (Map<String, Object>) choices.get(0).get("message");
|
|
|
- return (String) res.get("content");
|
|
|
- }
|
|
|
- return "未获取到有效响应";
|
|
|
+ ResponseData data = gson.fromJson(responseBody, ResponseData.class);
|
|
|
+ System.out.println("\n回答内容:");
|
|
|
+ System.out.println(data.choices[0].message.content);
|
|
|
+ return data.choices[0].message.content;
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ throw new MyException(400, "获取回答失败" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 内部类用于解析返回 JSON
|
|
|
+ static class ResponseData {
|
|
|
+ Choice[] choices;
|
|
|
+
|
|
|
+ static class Choice {
|
|
|
+ Message message;
|
|
|
+ }
|
|
|
+
|
|
|
+ static class Message {
|
|
|
+ String content;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|