|
@@ -1,14 +1,18 @@
|
|
|
package com.zhentao.controller.AI;
|
|
|
|
|
|
+import com.mashape.unirest.http.HttpResponse;
|
|
|
+import com.mashape.unirest.http.Unirest;
|
|
|
import com.mashape.unirest.http.exceptions.UnirestException;
|
|
|
import com.zhentao.controller.dto.DeeseekRequest;
|
|
|
+import okhttp3.*;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
-import com.mashape.unirest.http.HttpResponse;
|
|
|
-import com.mashape.unirest.http.Unirest;
|
|
|
import com.google.gson.Gson;
|
|
|
+import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
@@ -18,13 +22,13 @@ public class Deekseep {
|
|
|
//智能Ai智能推荐功能
|
|
|
@RequestMapping("tall")
|
|
|
public String tallQuestion(String question) throws IOException, UnirestException {
|
|
|
-
|
|
|
+ System.err.println(question);
|
|
|
Unirest.setTimeouts(0, 0);
|
|
|
//DeeseekRequest: 自己的实体类名称
|
|
|
|
|
|
List<DeeseekRequest.Message> messages = new ArrayList<>();
|
|
|
//给deepSeek一个角色
|
|
|
- messages.add(DeeseekRequest.Message.builder().role("system").content("你是一个美食家").build());
|
|
|
+ messages.add(DeeseekRequest.Message.builder().role("system").content("你是一个酒店的智能客服").build());
|
|
|
|
|
|
// question:说你自己想说的话
|
|
|
messages.add(DeeseekRequest.Message.builder().role("user").content(question).build());
|
|
@@ -39,7 +43,74 @@ public class Deekseep {
|
|
|
.header("Authorization", "Bearer "+"sk-df51dab7323441998d41f18494098ddc")
|
|
|
.body(gson.toJson(requestBody))
|
|
|
.asString();
|
|
|
+ System.err.println(response.getBody());
|
|
|
return response.getBody();
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+// @RequestMapping("tall")
|
|
|
+// public SseEmitter tallQuestion(String question) throws IOException {
|
|
|
+// // 1. 创建 SSE Emitter(用于流式返回数据)
|
|
|
+// SseEmitter emitter = new SseEmitter(60_000L); // 设置超时时间(60秒)
|
|
|
+//
|
|
|
+// // 2. 构建 DeepSeek 请求
|
|
|
+// OkHttpClient client = new OkHttpClient();
|
|
|
+//
|
|
|
+// List<DeeseekRequest.Message> messages = new ArrayList<>();
|
|
|
+// messages.add(DeeseekRequest.Message.builder().role("system").content("你是一个酒店的智能客服").build());
|
|
|
+// messages.add(DeeseekRequest.Message.builder().role("user").content(question).build());
|
|
|
+//
|
|
|
+// DeeseekRequest requestBody = DeeseekRequest.builder()
|
|
|
+// .model("deepseek-chat")
|
|
|
+// .messages(messages)
|
|
|
+// .stream(true) // 关键:启用流式
|
|
|
+// .build();
|
|
|
+//
|
|
|
+// // 3. 发起异步请求
|
|
|
+// Request request = new Request.Builder()
|
|
|
+// .url("https://api.deepseek.com/chat/completions")
|
|
|
+// .post(RequestBody.create(gson.toJson(requestBody), MediaType.get("application/json")))
|
|
|
+// .header("Authorization", "Bearer "+"sk-df51dab7323441998d41f18494098ddc")
|
|
|
+// .header("Accept", "text/event-stream") // 必须声明接受流式数据
|
|
|
+// .header("Content-Type", "application/json") // 明确 Content-Type
|
|
|
+// .build();
|
|
|
+// System.err.println(gson.toJson(requestBody));
|
|
|
+// client.newCall(request).enqueue(new Callback() {
|
|
|
+// @Override
|
|
|
+// public void onFailure(Call call, IOException e) {
|
|
|
+// emitter.completeWithError(e); // 失败时返回错误
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onResponse(Call call, Response response) throws IOException {
|
|
|
+// try (ResponseBody body = response.body()) {
|
|
|
+// if (!response.isSuccessful()) {
|
|
|
+// emitter.completeWithError(new IOException("请求失败: " + response.code()));
|
|
|
+// return;
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 4. 流式读取 DeepSeek 的响应并发送给前端
|
|
|
+// try (BufferedReader reader = new BufferedReader(new InputStreamReader(body.byteStream()))) {
|
|
|
+// String line;
|
|
|
+// while ((line = reader.readLine()) != null) {
|
|
|
+// if (line.startsWith("data:")) {
|
|
|
+// String data = line.substring(5).trim();
|
|
|
+// System.err.println(data);
|
|
|
+// emitter.send(data); // 发送数据到前端
|
|
|
+// }
|
|
|
+// }
|
|
|
+// emitter.complete(); // 完成流
|
|
|
+// }
|
|
|
+// } catch (Exception e) {
|
|
|
+// emitter.completeWithError(e);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// });
|
|
|
+//
|
|
|
+// return emitter;
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|