package com.dsideal.Res.Test;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.jfinal.kit.PathKit;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.File;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;
public class CallDeepSeek {
private static final String API_KEY = "sk-44ae895eeb614aa1a9c6460579e322f1"; // 请替换为您的API KEY
private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
public static void callDeepSeekStream(String prompt, SSEListener listener) {
new Thread(() -> {
StringBuilder fullResponse = new StringBuilder();
try {
// 修改htmlPrompt变量,在提示词中明确要求输出格式
String htmlPrompt = prompt + "请严格按照以下mermaid配置输出SQL血缘关系图:\n" +
"1. 使用以下固定样式配置:\n" +
" graph TD\n" +
" classDef default fill:#f9f9f9,stroke:#333,stroke-width:1px\n" +
" classDef input fill:#D6EAF8,stroke:#333\n" +
" classDef output fill:#D5F5E3,stroke:#333\n" +
" linkStyle default stroke:#333,stroke-width:2px\n" +
"2. 按照以下HTML模板格式输出,不要生成html以外的其它文字:\n" +
"\n" +
"\n" +
"
\n" +
" SQL血缘关系图\n" +
" \n" +
" \n" +
"\n" +
"\n" +
" SQL血缘关系图
\n" +
" \n" +
" graph TD\n" +
" classDef default fill:#f9f9f9,stroke:#333,stroke-width:1px\n" +
" classDef input fill:#D6EAF8,stroke:#333\n" +
" classDef output fill:#D5F5E3,stroke:#333\n" +
" linkStyle default stroke:#333,stroke-width:2px\n" +
" [请在此处插入mermaid格式的血缘关系图]\n" +
"
\n" +
" \n" +
"\n" +
"\n";
JSONObject jsonPayload = new JSONObject();
jsonPayload.set("model", "deepseek-chat");
JSONObject message = new JSONObject();
message.set("role", "user");
message.set("content", htmlPrompt);
JSONArray messages = new JSONArray();
messages.add(message);
jsonPayload.set("messages", messages);
jsonPayload.set("stream", true);
HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1) // 明确指定HTTP版本
.build();
java.net.http.HttpRequest request = java.net.http.HttpRequest.newBuilder()
.uri(URI.create(API_URL))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + API_KEY)
.header("Accept", "text/event-stream") // 确保Accept头正确
.POST(java.net.http.HttpRequest.BodyPublishers.ofString(jsonPayload.toString(), StandardCharsets.UTF_8))
.build();
// 使用 BodyHandlers.ofLines() 来处理流式响应
CompletableFuture future = client.sendAsync(request, HttpResponse.BodyHandlers.ofLines())
.thenAccept(response -> {
if (response.statusCode() == 200) {
response.body().forEach(line -> {
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if (!data.equals("[DONE]")) {
try {
JSONObject jsonData = JSONUtil.parseObj(data);
if (jsonData.containsKey("choices")) {
String content = jsonData.getJSONArray("choices")
.getJSONObject(0)
.getJSONObject("delta")
.getStr("content", "");
if (content != null && !content.isEmpty()) {
fullResponse.append(content);
listener.onData(content); // 回调监听器
}
}
} catch (Exception e) {
System.err.println("解析SSE JSON数据错误: " + data + " \nError: " + e.getMessage());
}
}
}
});
// 流结束后的处理
String htmlContent = fullResponse.toString();
if (htmlContent.contains("") && htmlContent.contains("